本文整理汇总了Java中org.apache.twill.discovery.Discoverable类的典型用法代码示例。如果您正苦于以下问题:Java Discoverable类的具体用法?Java Discoverable怎么用?Java Discoverable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Discoverable类属于org.apache.twill.discovery包,在下文中一共展示了Discoverable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getInstances
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
private Set<Integer> getInstances(Iterable<Discoverable> discoverables) throws IOException {
Set<Integer> instances = Sets.newHashSet();
for (Discoverable discoverable : discoverables) {
InetSocketAddress socketAddress = discoverable.getSocketAddress();
try (Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort())) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
String msg = "Failure";
writer.println(msg);
String line = reader.readLine();
Assert.assertTrue(line.endsWith(msg));
instances.add(Integer.parseInt(line.substring(0, line.length() - msg.length())));
}
}
return instances;
}
示例2: getInstances
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
private Set<Integer> getInstances(Iterable<Discoverable> discoverables) throws IOException {
Set<Integer> instances = Sets.newHashSet();
for (Discoverable discoverable : discoverables) {
InetSocketAddress socketAddress = discoverable.getSocketAddress();
Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort());
try {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
String msg = "Failure";
writer.println(msg);
String line = reader.readLine();
Assert.assertTrue(line.endsWith(msg));
instances.add(Integer.parseInt(line.substring(0, line.length() - msg.length())));
} finally {
socket.close();
}
}
return instances;
}
示例3: testEnv
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Test
public void testEnv() throws Exception {
TwillRunner runner = getTwillRunner();
TwillController controller = runner.prepare(new EchoApp())
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.withApplicationArguments("echo")
.withArguments("echo1", "echo1")
.withArguments("echo2", "echo2")
.withEnv(ImmutableMap.of("GREETING", "Hello"))
.withEnv("echo2", ImmutableMap.of("GREETING", "Hello2"))
.start();
// Service echo1 should returns "Hello" as greeting, echo2 should returns "Hello2"
Map<String, String> runnableGreetings = ImmutableMap.of("echo1", "Hello", "echo2", "Hello2");
for (Map.Entry<String, String> entry : runnableGreetings.entrySet()) {
Discoverable discoverable = getDiscoverable(controller.discoverService(entry.getKey()), 60, TimeUnit.SECONDS);
try (
Socket socket = new Socket(discoverable.getSocketAddress().getAddress(),
discoverable.getSocketAddress().getPort())
) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
writer.println("GREETING");
Assert.assertEquals(entry.getValue(), reader.readLine());
}
}
controller.terminate().get();
}
示例4: testFailureRestart
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Test
public void testFailureRestart() throws Exception {
TwillRunner runner = getTwillRunner();
ResourceSpecification resource = ResourceSpecification.Builder.with()
.setVirtualCores(1)
.setMemory(512, ResourceSpecification.SizeUnit.MEGA)
.setInstances(2)
.build();
TwillController controller = runner.prepare(new FailureRunnable(), resource)
.withApplicationArguments("failure")
.withArguments(FailureRunnable.class.getSimpleName(), "failure2")
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.start();
Iterable<Discoverable> discoverables = controller.discoverService("failure");
Assert.assertTrue(waitForSize(discoverables, 2, 120));
// Make sure we see the right instance IDs
Assert.assertEquals(Sets.newHashSet(0, 1), getInstances(discoverables));
// Kill server with instanceId = 0
controller.sendCommand(FailureRunnable.class.getSimpleName(), Command.Builder.of("kill0").build());
// Make sure the runnable is killed.
Assert.assertTrue(waitForSize(discoverables, 1, 120));
// Wait for the restart
Assert.assertTrue(waitForSize(discoverables, 2, 120));
// Make sure we see the right instance IDs
Assert.assertEquals(Sets.newHashSet(0, 1), getInstances(discoverables));
controller.terminate().get(120, TimeUnit.SECONDS);
}
示例5: providesDiscoveryService
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Provides
@Singleton
private DiscoveryService providesDiscoveryService(final ZKClientService zkClient,
final ZKDiscoveryService delegate) {
return new DiscoveryService() {
@Override
public Cancellable register(Discoverable discoverable) {
if (!zkClient.isRunning()) {
zkClient.startAndWait();
}
return delegate.register(discoverable);
}
};
}
示例6: pick
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Override
public Discoverable pick() {
// Reservoir sampling
Discoverable result = null;
Iterator<Discoverable> itor = endpoints.iterator();
Random random = new Random();
int count = 0;
while (itor.hasNext()) {
Discoverable next = itor.next();
if (random.nextInt(++count) == 0) {
result = next;
}
}
return result;
}
示例7: testLocalFile
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Test
public void testLocalFile() throws Exception {
String header = Files.readFirstLine(new File(getClass().getClassLoader().getResource("header.txt").toURI()),
Charsets.UTF_8);
TwillRunner runner = YarnTestUtils.getTwillRunner();
TwillController controller = runner.prepare(new LocalFileApplication())
.addJVMOptions(" -verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails")
.withApplicationArguments("local")
.withArguments("LocalFileSocketServer", "local2")
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.start();
Iterable<Discoverable> discoverables = controller.discoverService("local");
Assert.assertTrue(YarnTestUtils.waitForSize(discoverables, 1, 60));
InetSocketAddress socketAddress = discoverables.iterator().next().getSocketAddress();
Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort());
try {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
String msg = "Local file test";
writer.println(msg);
Assert.assertEquals(header, reader.readLine());
Assert.assertEquals(msg, reader.readLine());
} finally {
socket.close();
}
controller.stopAndWait();
Assert.assertTrue(YarnTestUtils.waitForSize(discoverables, 0, 60));
TimeUnit.SECONDS.sleep(2);
}
示例8: testFailureRestart
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Test
public void testFailureRestart() throws Exception {
TwillRunner runner = YarnTestUtils.getTwillRunner();
ResourceSpecification resource = ResourceSpecification.Builder.with()
.setVirtualCores(1)
.setMemory(512, ResourceSpecification.SizeUnit.MEGA)
.setInstances(2)
.build();
TwillController controller = runner.prepare(new FailureRunnable(), resource)
.withApplicationArguments("failure")
.withArguments(FailureRunnable.class.getSimpleName(), "failure2")
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.start();
Iterable<Discoverable> discoverables = controller.discoverService("failure");
Assert.assertTrue(YarnTestUtils.waitForSize(discoverables, 2, 120));
// Make sure we see the right instance IDs
Assert.assertEquals(Sets.newHashSet(0, 1), getInstances(discoverables));
// Kill server with instanceId = 0
controller.sendCommand(FailureRunnable.class.getSimpleName(), Command.Builder.of("kill0").build());
// Make sure the runnable is killed.
Assert.assertTrue(YarnTestUtils.waitForSize(discoverables, 1, 120));
// Wait for the restart
Assert.assertTrue(YarnTestUtils.waitForSize(discoverables, 2, 120));
// Make sure we see the right instance IDs
Assert.assertEquals(Sets.newHashSet(0, 1), getInstances(discoverables));
controller.stopAndWait();
}
示例9: announce
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Override
public Cancellable announce(final String serviceName, final int port) {
return discoveryService.register(new Discoverable() {
@Override
public String getName() {
return serviceName;
}
@Override
public InetSocketAddress getSocketAddress() {
return new InetSocketAddress(getHost(), port);
}
});
}
示例10: testZKCleanup
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Test
public void testZKCleanup() throws Exception {
final ZKClientService zkClient = ZKClientService.Builder.of(getZKConnectionString() + "/twill").build();
zkClient.startAndWait();
try {
TwillRunner runner = getTwillRunner();
// Start an application and stop it.
TwillController controller = runner.prepare(new EchoServer())
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.withApplicationArguments("echo")
.withArguments("EchoServer", "echo2")
.start();
Iterable<Discoverable> echoServices = controller.discoverService("echo");
Assert.assertTrue(waitForSize(echoServices, 1, 120));
controller.terminate().get();
// Verify the ZK node gets cleanup
waitFor(null, new Callable<Stat>() {
@Override
public Stat call() throws Exception {
return zkClient.exists("/EchoServer").get();
}
}, 10000, 100, TimeUnit.MILLISECONDS);
// Start two instances of the application and stop one of it
List<TwillController> controllers = new ArrayList<>();
for (int i = 0; i < 2; i++) {
controllers.add(runner.prepare(new EchoServer())
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.withApplicationArguments("echo")
.withArguments("EchoServer", "echo2")
.start());
}
// There should be two instances up and running.
echoServices = controllers.get(1).discoverService("echo");
Assert.assertTrue(waitForSize(echoServices, 2, 120));
// Stop one instance of the app
controllers.get(0).terminate().get();
// Verify the ZK node should still be there
Assert.assertNotNull(zkClient.exists("/EchoServer").get());
// We should still be able to do discovery, which depends on the ZK node.
echoServices = controllers.get(1).discoverService("echo");
Assert.assertTrue(waitForSize(echoServices, 1, 120));
// Stop second instance of the app
controllers.get(1).terminate().get();
// Verify the ZK node gets cleanup
waitFor(null, new Callable<Stat>() {
@Override
public Stat call() throws Exception {
return zkClient.exists("/EchoServer").get();
}
}, 10000, 100, TimeUnit.MILLISECONDS);
} finally {
zkClient.stopAndWait();
}
}
示例11: testRunnablesGetAllowedResourcesInEnv
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Test
public void testRunnablesGetAllowedResourcesInEnv() throws InterruptedException, IOException,
TimeoutException, ExecutionException {
TwillRunner runner = getTwillRunner();
ResourceSpecification resourceSpec = ResourceSpecification.Builder.with()
.setVirtualCores(1)
.setMemory(2048, ResourceSpecification.SizeUnit.MEGA)
.setInstances(1)
.build();
TwillController controller = runner.prepare(new EnvironmentEchoServer(), resourceSpec)
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.withApplicationArguments("envecho")
.withArguments("EnvironmentEchoServer", "echo2")
.start();
final CountDownLatch running = new CountDownLatch(1);
controller.onRunning(new Runnable() {
@Override
public void run() {
running.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(running.await(120, TimeUnit.SECONDS));
Iterable<Discoverable> envEchoServices = controller.discoverService("envecho");
Assert.assertTrue(waitForSize(envEchoServices, 1, 120));
// TODO: check virtual cores once yarn adds the ability
Map<String, String> expectedValues = Maps.newHashMap();
expectedValues.put(EnvKeys.YARN_CONTAINER_MEMORY_MB, "2048");
expectedValues.put(EnvKeys.TWILL_INSTANCE_COUNT, "1");
// check environment of the runnable.
Discoverable discoverable = envEchoServices.iterator().next();
for (Map.Entry<String, String> expected : expectedValues.entrySet()) {
try (
Socket socket = new Socket(discoverable.getSocketAddress().getHostName(),
discoverable.getSocketAddress().getPort())
) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
writer.println(expected.getKey());
Assert.assertEquals(expected.getValue(), reader.readLine());
}
}
controller.terminate().get(120, TimeUnit.SECONDS);
// Sleep a bit before exiting.
TimeUnit.SECONDS.sleep(2);
}
示例12: testResourceReportWithFailingContainers
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Test
public void testResourceReportWithFailingContainers() throws InterruptedException, IOException,
TimeoutException, ExecutionException {
TwillRunner runner = getTwillRunner();
ResourceSpecification resourceSpec = ResourceSpecification.Builder.with()
.setVirtualCores(1)
.setMemory(256, ResourceSpecification.SizeUnit.MEGA)
.setInstances(2)
.build();
TwillController controller = runner.prepare(new BuggyServer(), resourceSpec)
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.withApplicationArguments("echo")
.withArguments("BuggyServer", "echo2")
.start();
final CountDownLatch running = new CountDownLatch(1);
controller.onRunning(new Runnable() {
@Override
public void run() {
running.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(running.await(120, TimeUnit.SECONDS));
Iterable<Discoverable> echoServices = controller.discoverService("echo");
Assert.assertTrue(waitForSize(echoServices, 2, 120));
// check that we have 2 runnables.
ResourceReport report = getResourceReport(controller, 10000);
Assert.assertEquals(2, report.getRunnableResources("BuggyServer").size());
// cause a divide by 0 in one server
Discoverable discoverable = echoServices.iterator().next();
try (
Socket socket = new Socket(discoverable.getSocketAddress().getAddress(),
discoverable.getSocketAddress().getPort())
) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
writer.println("0");
}
// takes some time for app master to find out the container completed...
int count = 0;
while (count < 100) {
report = getResourceReport(controller, 10000);
// check that we have 1 runnable, not 2.
if (report.getRunnableResources("BuggyServer").size() == 1) {
break;
}
LOG.info("Wait for BuggyServer to have 1 instance left. Trial {}.", count);
count++;
TimeUnit.SECONDS.sleep(1);
}
Assert.assertTrue("Still has 2 contains running after 100 seconds", count < 100);
controller.terminate().get(100, TimeUnit.SECONDS);
// Sleep a bit before exiting.
TimeUnit.SECONDS.sleep(2);
}
示例13: testLocalFile
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Test
public void testLocalFile() throws Exception {
// Generate a header and a footer files.
File headerFile = tmpFolder.newFile("header.txt");
File footerFile = tmpFolder.newFile("footer.txt");
String headerMsg = "Header Message";
String footerMsg = "Footer Message";
Files.write(headerMsg, headerFile, StandardCharsets.UTF_8);
Files.write(footerMsg, footerFile, StandardCharsets.UTF_8);
TwillRunner runner = getTwillRunner();
TwillController controller = runner.prepare(new LocalFileApplication(headerFile))
.addJVMOptions(" -verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails")
.withApplicationArguments("local")
.withArguments("LocalFileSocketServer", "local2")
.withResources(footerFile.toURI())
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.start();
Iterable<Discoverable> discoverables = controller.discoverService("local");
Assert.assertTrue(waitForSize(discoverables, 1, 60));
InetSocketAddress socketAddress = discoverables.iterator().next().getSocketAddress();
try (Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort())) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
String msg = "Local file test";
writer.println(msg);
Assert.assertEquals(headerMsg, reader.readLine());
Assert.assertEquals(msg, reader.readLine());
Assert.assertEquals(footerMsg, reader.readLine());
}
controller.terminate().get(120, TimeUnit.SECONDS);
Assert.assertTrue(waitForSize(discoverables, 0, 60));
TimeUnit.SECONDS.sleep(2);
}
示例14: announce
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
@Override
public Cancellable announce(final String serviceName, final int port, final byte[] payload) {
return discoveryService.register(new Discoverable(serviceName, new InetSocketAddress(getHost(), port), payload));
}
示例15: doRegister
import org.apache.twill.discovery.Discoverable; //导入依赖的package包/类
protected void doRegister() {
cancelDiscovery = discoveryService.register(new Discoverable(serviceName, getAddress()));
}