Package org.apache.twill.api

Examples of org.apache.twill.api.TwillController


  private static final Logger LOG = LoggerFactory.getLogger(SessionExpireTestRun.class);

  @Test
  public void testAppSessionExpire() throws InterruptedException, ExecutionException {
    TwillRunner runner = YarnTestUtils.getTwillRunner();
    TwillController controller = runner.prepare(new SleepRunnable(600))
                                       .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
                                       .start();

    final CountDownLatch runLatch = new CountDownLatch(1);
    controller.addListener(new ServiceListenerAdapter() {
      @Override
      public void running() {
        runLatch.countDown();
      }
    }, Threads.SAME_THREAD_EXECUTOR);

    // Wait for application running
    Assert.assertTrue(runLatch.await(60, TimeUnit.SECONDS));

    // Find the app master ZK session and expire it two times, 10 seconds apart.
    for (int i = 0; i < 2; i++) {
      Assert.assertTrue(expireAppMasterZKSession(controller, 10, TimeUnit.SECONDS));

      ListenableFuture<Service.State> completion = Services.getCompletionFuture(controller);
      try {
        completion.get(10, TimeUnit.SECONDS);
        Assert.fail("Unexpected application termination.");
      } catch (TimeoutException e) {
        // OK, expected.
      }
    }

    controller.stopAndWait();
  }
View Full Code Here


public class ContainerSizeTestRun extends BaseYarnTest {

  @Test
  public void testContainerSize() throws InterruptedException {
    TwillRunner runner = YarnTestUtils.getTwillRunner();
    TwillController controller = runner.prepare(new SleepApp())
      .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
      .start();

    try {
      ServiceDiscovered discovered = controller.discoverService("sleep");
      Assert.assertTrue(YarnTestUtils.waitForSize(discovered, 2, 60));
    } finally {
      controller.stopAndWait();
    }
  }
View Full Code Here

  @Test
  public void testDebugPortOneRunnable() throws Exception {
    YarnTwillRunnerService runner = (YarnTwillRunnerService) YarnTestUtils.getTwillRunner();
    runner.startAndWait();

    TwillController controller = runner.prepare(new DummyApplication())
                                       .enableDebugging("r1")
                                       .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out)))
                                       .start();
    final CountDownLatch running = new CountDownLatch(1);
    controller.addListener(new ServiceListenerAdapter() {
      @Override
      public void running() {
        running.countDown();
      }
    }, Threads.SAME_THREAD_EXECUTOR);

    Assert.assertTrue(running.await(30, TimeUnit.SECONDS));
    Assert.assertTrue(waitForDebugPort(controller, "r1", 30));
    controller.stop().get(30, TimeUnit.SECONDS);
    // Sleep a bit before exiting.
    TimeUnit.SECONDS.sleep(2);
  }
View Full Code Here

  @Test
  public void testDebugPortAllRunnables() throws Exception {
    YarnTwillRunnerService runner = (YarnTwillRunnerService) YarnTestUtils.getTwillRunner();
    runner.startAndWait();

    TwillController controller = runner.prepare(new DummyApplication())
                                       .enableDebugging()
                                       .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out)))
                                       .start();
    final CountDownLatch running = new CountDownLatch(1);
    controller.addListener(new ServiceListenerAdapter() {
      @Override
      public void running() {
        running.countDown();
      }
    }, Threads.SAME_THREAD_EXECUTOR);

    Assert.assertTrue(running.await(30, TimeUnit.SECONDS));
    Assert.assertTrue(waitForDebugPort(controller, "r1", 30));
    Assert.assertTrue(waitForDebugPort(controller, "r2", 30));
    controller.stop().get(30, TimeUnit.SECONDS);
    // Sleep a bit before exiting.
    TimeUnit.SECONDS.sleep(2);
  }
View Full Code Here

public final class ServiceDiscoveryTestRun extends BaseYarnTest {

  @Test
  public void testServiceDiscovery() throws InterruptedException, ExecutionException, TimeoutException {
    TwillRunner twillRunner = YarnTestUtils.getTwillRunner();
    TwillController controller = twillRunner
      .prepare(new ServiceApplication())
      .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
      .withArguments("r1", "12345")
      .withArguments("r2", "45678")
      .start();

    ListenableFuture<Service.State> completion = Services.getCompletionFuture(controller);
    try {
      completion.get(60, TimeUnit.SECONDS);
    } finally {
      controller.stopAndWait();
    }
  }
View Full Code Here

    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);
  }
View Full Code Here

  @Test
  public void testEchoServer() throws InterruptedException, ExecutionException, IOException,
    URISyntaxException, TimeoutException {
    TwillRunner runner = YarnTestUtils.getTwillRunner();

    TwillController controller = runner.prepare(new EchoServer(),
                                                ResourceSpecification.Builder.with()
                                                         .setVirtualCores(1)
                                                         .setMemory(1, ResourceSpecification.SizeUnit.GIGA)
                                                         .setInstances(2)
                                                         .build())
                                        .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
                                        .withApplicationArguments("echo")
                                        .withArguments("EchoServer", "echo2")
                                        .start();

    final CountDownLatch running = new CountDownLatch(1);
    controller.addListener(new ServiceListenerAdapter() {
      @Override
      public void running() {
        running.countDown();
      }
    }, Threads.SAME_THREAD_EXECUTOR);

    Assert.assertTrue(running.await(30, TimeUnit.SECONDS));

    Iterable<Discoverable> echoServices = controller.discoverService("echo");
    Assert.assertTrue(YarnTestUtils.waitForSize(echoServices, 2, 60));

    for (Discoverable discoverable : echoServices) {
      String msg = "Hello: " + discoverable.getSocketAddress();

      Socket socket = new Socket(discoverable.getSocketAddress().getAddress(),
                                 discoverable.getSocketAddress().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));

        writer.println(msg);
        Assert.assertEquals(msg, reader.readLine());
      } finally {
        socket.close();
      }
    }

    // Increase number of instances
    controller.changeInstances("EchoServer", 3);
    Assert.assertTrue(YarnTestUtils.waitForSize(echoServices, 3, 60));

    echoServices = controller.discoverService("echo2");

    // Decrease number of instances
    controller.changeInstances("EchoServer", 1);
    Assert.assertTrue(YarnTestUtils.waitForSize(echoServices, 1, 60));

    // Increase number of instances again
    controller.changeInstances("EchoServer", 2);
    Assert.assertTrue(YarnTestUtils.waitForSize(echoServices, 2, 60));

    // Make sure still only one app is running
    Iterable<TwillRunner.LiveInfo> apps = runner.lookupLive();
    Assert.assertTrue(YarnTestUtils.waitForSize(apps, 1, 60));
View Full Code Here

        }
      }
    };

    TwillRunner runner = YarnTestUtils.getTwillRunner();
    TwillController controller = runner.prepare(new LogRunnable())
                                       .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
                                       .addLogHandler(logHandler)
                                       .start();

    Services.getCompletionFuture(controller).get();
View Full Code Here

  @Override
  public void addLogHandler(String flowName, PrintStream out) {
    try {
      Iterable<TwillController> iterable = lookupFlow(flowName);
      if (iterable.iterator().hasNext()) {
        TwillController controller = iterable.iterator().next();
        controller.addLogHandler(new PrinterLogHandler(new PrintWriter(out, true)));
      }
    } catch (Exception e) {
      LOG.warn(e.getMessage(), e);
    }
  }
View Full Code Here

      twillController = iterator.next();

      if (iterator.hasNext()) {
        LOG.warn("Found more than one instance of {} running. Stopping the others...", serviceName);
        for (; iterator.hasNext(); ) {
          TwillController controller = iterator.next();
          LOG.warn("Stopping one extra instance of {}", serviceName);
          controller.stopAndWait();
        }
        LOG.warn("Done stopping extra instances of {}", serviceName);
      }
    } else {
      LOG.info("Starting {} application", serviceName);
View Full Code Here

TOP

Related Classes of org.apache.twill.api.TwillController

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.