-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #355 from mesos/enhancement/334-UseJars
Enhancement/334 use jars
- Loading branch information
Showing
8 changed files
with
285 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
scheduler/src/main/java/org/apache/mesos/elasticsearch/scheduler/SimpleFileServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.apache.mesos.elasticsearch.scheduler; | ||
|
||
import com.sun.net.httpserver.Headers; | ||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpServer; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.log4j.Logger; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
|
||
/** | ||
* Simple file server for distributing jars and zips across the cluster | ||
*/ | ||
public class SimpleFileServer implements Runnable { | ||
private static final Logger LOGGER = Logger.getLogger(SimpleFileServer.class); | ||
public static final String ES_EXECUTOR_JAR = "elasticsearch-mesos-executor.jar"; | ||
private HttpServer server; | ||
|
||
private static void writeClassPathResource(HttpExchange t, String classPathResource) throws IOException { | ||
InputStream in = SimpleFileServer.class.getClassLoader().getResourceAsStream(classPathResource); | ||
|
||
// Must send headers before body. | ||
t.sendResponseHeaders(200, 0); | ||
OutputStream os = t.getResponseBody(); | ||
IOUtils.copy(in, os); | ||
os.flush(); | ||
|
||
os.close(); | ||
in.close(); | ||
} | ||
|
||
public void serve() throws IOException { | ||
server = HttpServer.create(new InetSocketAddress(0), 0); // Pick a random available port | ||
server.createContext("/info", new InfoHandler()); | ||
server.createContext("/get", new GetHandler()); | ||
server.setExecutor(null); // creates a default executor | ||
server.start(); | ||
} | ||
|
||
public InetSocketAddress getAddress() throws UnknownHostException { | ||
if (server != null) { | ||
return new InetSocketAddress(InetAddress.getLocalHost().getHostName(), server.getAddress().getPort()); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
this.serve(); | ||
LOGGER.info("Running Executor JAR file server on: " + this.getAddress().getHostName() + ":" + this.getAddress().getPort()); | ||
} catch (IOException e) { | ||
LOGGER.error("Elasticsearch file server stopped", e); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
static class InfoHandler implements HttpHandler { | ||
public void handle(HttpExchange t) throws IOException { | ||
String response = "Use /get to download the executor jar"; | ||
t.sendResponseHeaders(200, response.length()); | ||
|
||
OutputStream os = t.getResponseBody(); | ||
IOUtils.write(response, os); | ||
os.close(); | ||
} | ||
} | ||
|
||
static class GetHandler implements HttpHandler { | ||
public void handle(HttpExchange t) throws IOException { | ||
|
||
Headers h = t.getResponseHeaders(); | ||
h.add("Content-Type", "application/octet-stream"); | ||
|
||
writeClassPathResource(t, ES_EXECUTOR_JAR); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
scheduler/src/test/java/org/apache/mesos/elasticsearch/scheduler/ConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.apache.mesos.elasticsearch.scheduler; | ||
|
||
import org.apache.mesos.elasticsearch.common.cli.ZookeeperCLIParameter; | ||
import org.junit.Test; | ||
|
||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
/** | ||
* Tests | ||
**/ | ||
public class ConfigurationTest { | ||
@Test | ||
public void shouldReturnValidServerPath() throws UnknownHostException { | ||
Configuration configuration = new Configuration(ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, "aa"); | ||
String localhost = "localhost"; | ||
int port = 1234; | ||
configuration.setFrameworkFileServerAddress(new InetSocketAddress(localhost, port)); | ||
assertEquals("http://" + localhost + ":" + port, configuration.getFrameworkFileServerAddress()); | ||
} | ||
|
||
@Test | ||
public void shouldNotHaveDefaultInetAddressToStringMethod() throws UnknownHostException { | ||
Configuration configuration = new Configuration(ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, "aa"); | ||
int port = 1234; | ||
configuration.setFrameworkFileServerAddress(new InetSocketAddress(InetAddress.getLocalHost().getHostName(), port)); | ||
assertFalse(configuration.getFrameworkFileServerAddress().replace("http://", "").contains("/")); | ||
} | ||
|
||
@Test | ||
public void shouldProvideJavaHomeWithEndSlashAndWithoutJava() { | ||
Configuration configuration = new Configuration(ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, "aa", Configuration.JAVA_HOME, "/usr/bin/java"); | ||
assertEquals("/usr/bin/", configuration.getJavaHome()); | ||
configuration = new Configuration(ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, "aa", Configuration.JAVA_HOME, "/usr/bin/"); | ||
assertEquals("/usr/bin/", configuration.getJavaHome()); | ||
configuration = new Configuration(ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, "aa", Configuration.JAVA_HOME, "/usr/bin"); | ||
assertEquals("/usr/bin/", configuration.getJavaHome()); | ||
} | ||
} |
Oops, something went wrong.