@Override public ServiceDiscovered discoverService(String serviceName) { return delegate.discoverService(serviceName); }
@Override public ServiceDiscovered discover(String service) { return twillController.discoverService(service); } }
@Override public ServiceDiscovered discoverService(String serviceName) { return delegate.discoverService(serviceName); }
@Override public List<InetSocketAddress> discover(String flowName, String service) { List<InetSocketAddress> endPoints = Lists.newArrayList(); try { Iterable<TwillController> controllers = lookupFlow(flowName); for (TwillController controller : controllers) { Iterable<Discoverable> iterable = controller.discoverService(service); sleepForZK(iterable); for (Discoverable discoverable : iterable) { endPoints.add(discoverable.getSocketAddress()); } } } catch (Exception e) { LOG.warn(e.getMessage(), e); } return endPoints; }
@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(); }
@Test public void testExtraOptions() throws InterruptedException, ExecutionException { // Start the testing app with jvm options at both global level as well as for the specific runnables. TwillController controller = getTwillRunner() .prepare(new JvmOptionsApplication()) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out))) .setJVMOptions("-Dservice.name=default") .setJVMOptions("r2", "-Dservice.name=r2") .start(); // For r1 and r3 will be using "default" as the service name. waitForSize(controller.discoverService("default"), 2, 120); // r2 will be use "r2" as the service name. waitForSize(controller.discoverService("r2"), 1, 120); controller.terminate().get(); }
@Test public void testCustomClassLoader() throws Exception { TwillController controller = getTwillRunner().prepare(new CustomClassLoaderRunnable()) .setClassLoader(CustomClassLoader.class.getName()) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .setJVMOptions("-Dservice.port=54321") .setJVMOptions(CustomClassLoaderRunnable.class.getSimpleName(), "-Dservice.name=custom") .start(); Assert.assertTrue(waitForSize(controller.discoverService("custom"), 1, 120)); controller.terminate().get(); } }
@Test public void testServiceDiscovery() throws InterruptedException, ExecutionException, TimeoutException { TwillRunner twillRunner = getTwillRunner(); TwillController controller = twillRunner .prepare(new ServiceApplication()) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .withApplicationArguments("echo") .start(); ServiceDiscovered discovered = controller.discoverService("discovered"); Assert.assertTrue(waitForSize(discovered, 1, 120)); controller.terminate().get(); }
Iterable<Discoverable> envEchoServices = controller.discoverService("envecho"); Assert.assertTrue(waitForSize(envEchoServices, 1, 120));
.start(); Iterable<Discoverable> discoverables = controller.discoverService("local"); Assert.assertTrue(waitForSize(discoverables, 1, 60));
controller.discoverService("log").watchChanges(new ServiceDiscovered.ChangeListener() { @Override public void onChange(ServiceDiscovered serviceDiscovered) {
/** * Test for requesting different container size in different order. * It specifically test for workaround for YARN-314. */ @Test public void testContainerSize() throws InterruptedException, TimeoutException, ExecutionException { TwillRunner runner = getTwillRunner(); TwillController controller = runner.prepare(new SleepApp()) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .start(); try { ServiceDiscovered discovered = controller.discoverService("sleep"); Assert.assertTrue(waitForSize(discovered, 2, 120)); } finally { controller.terminate().get(120, TimeUnit.SECONDS); } }
serviceDiscovered = controller.discoverService("DistributedTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 4, 60)); serviceDiscovered = controller.discoverService("DistributedTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 6, 60)); serviceDiscovered = controller.discoverService("DistributedTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 3, 60)); serviceDiscovered = controller.discoverService("DistributedTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 5, 60)); serviceDiscovered = controller.discoverService("DistributedTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 7, 60)); } finally {
Iterable<Discoverable> echoServices = controller.discoverService("echo"); Assert.assertTrue(waitForSize(echoServices, 2, 120));
.start(); Iterable<Discoverable> echoServices = controller.discoverService("echo"); Assert.assertTrue(waitForSize(echoServices, 1, 120)); echoServices = controllers.get(1).discoverService("echo"); Assert.assertTrue(waitForSize(echoServices, 2, 120)); echoServices = controllers.get(1).discoverService("echo"); Assert.assertTrue(waitForSize(echoServices, 1, 120));
ServiceDiscovered serviceDiscovered = controller.discoverService("DistributedTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 3, 60)); Assert.assertTrue(getProvisionedNodeManagerCount() >= 2); serviceDiscovered = controller.discoverService("DistributedTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 4, 60)); Assert.assertTrue(getProvisionedNodeManagerCount() >= 3); serviceDiscovered = controller.discoverService("DistributedTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 5, 60)); serviceDiscovered = controller.discoverService("DistributedTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 6, 60)); Assert.assertTrue(getProvisionedNodeManagerCount() >= 3);
ServiceDiscovered discovered = controller.discoverService("sleep"); Assert.assertTrue(waitForSize(discovered, 2, 120));
Iterable<Discoverable> echoServices = controller.discoverService("echo"); Assert.assertTrue(waitForSize(echoServices, 2, 120)); Assert.assertTrue(waitForSize(echoServices, 3, 120)); echoServices = controller.discoverService("echo2");
@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); }
/** * Test to verify placement policy without dynamically changing number of instances. */ @Test public void testPlacementPolicy() throws Exception { // Ignore test if it is running against older Hadoop versions which does not support blacklists. Assume.assumeTrue(YarnUtils.getHadoopVersion().equals(YarnUtils.HadoopVersions.HADOOP_22)); waitNodeManagerCount(0, 10, TimeUnit.SECONDS); TwillRunner runner = getTwillRunner(); TwillController controller = runner.prepare(new PlacementPolicyApplication()) .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) .withApplicationArguments("PlacementPolicyTest") .withArguments("hostRunnable", "host") .withArguments("hostRackRunnable", "hostRack") .withArguments("distributedRunnable", "distributed") .start(); try { // All runnables should get started. ServiceDiscovered serviceDiscovered = controller.discoverService("PlacementPolicyTest"); Assert.assertTrue(waitForSize(serviceDiscovered, 4, 80)); // DISTRIBUTED runnables should be provisioned on different nodes. Assert.assertTrue(getProvisionedNodeManagerCount() >= 2); } finally { controller.terminate().get(120, TimeUnit.SECONDS); } // Sleep a bit before exiting. TimeUnit.SECONDS.sleep(2); }