Search in sources :

Example 1 with WeaveController

use of com.continuuity.weave.api.WeaveController in project weave by continuuity.

the class FailureRestartTestRun method testFailureRestart.

@Test
public void testFailureRestart() throws Exception {
    WeaveRunner runner = YarnTestSuite.getWeaveRunner();
    ResourceSpecification resource = ResourceSpecification.Builder.with().setVirtualCores(1).setMemory(512, ResourceSpecification.SizeUnit.MEGA).setInstances(2).build();
    WeaveController 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(YarnTestSuite.waitForSize(discoverables, 2, 60));
    // 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());
    // Do a shot sleep, make sure the runnable is killed.
    TimeUnit.SECONDS.sleep(5);
    Assert.assertTrue(YarnTestSuite.waitForSize(discoverables, 2, 60));
    // Make sure we see the right instance IDs
    Assert.assertEquals(Sets.newHashSet(0, 1), getInstances(discoverables));
    controller.stopAndWait();
}
Also used : WeaveRunner(com.continuuity.weave.api.WeaveRunner) Discoverable(com.continuuity.weave.discovery.Discoverable) PrinterLogHandler(com.continuuity.weave.api.logging.PrinterLogHandler) WeaveController(com.continuuity.weave.api.WeaveController) ResourceSpecification(com.continuuity.weave.api.ResourceSpecification) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 2 with WeaveController

use of com.continuuity.weave.api.WeaveController in project weave by continuuity.

the class LocalFileTestRun method testLocalFile.

@Test
public void testLocalFile() throws Exception {
    String header = Files.readFirstLine(new File(getClass().getClassLoader().getResource("header.txt").toURI()), Charsets.UTF_8);
    WeaveRunner runner = YarnTestSuite.getWeaveRunner();
    if (runner instanceof YarnWeaveRunnerService) {
        ((YarnWeaveRunnerService) runner).setJVMOptions("-verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails");
    }
    WeaveController controller = runner.prepare(new LocalFileApplication()).withApplicationArguments("local").withArguments("LocalFileSocketServer", "local2").addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))).start();
    if (runner instanceof YarnWeaveRunnerService) {
        ((YarnWeaveRunnerService) runner).setJVMOptions("");
    }
    Iterable<Discoverable> discoverables = controller.discoverService("local");
    Assert.assertTrue(YarnTestSuite.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(YarnTestSuite.waitForSize(discoverables, 0, 60));
    TimeUnit.SECONDS.sleep(2);
}
Also used : WeaveRunner(com.continuuity.weave.api.WeaveRunner) Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) LineReader(com.google.common.io.LineReader) PrinterLogHandler(com.continuuity.weave.api.logging.PrinterLogHandler) WeaveController(com.continuuity.weave.api.WeaveController) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 3 with WeaveController

use of com.continuuity.weave.api.WeaveController in project weave by continuuity.

the class ControllerTest method testController.

@Test
public void testController() throws ExecutionException, InterruptedException, TimeoutException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
    zkServer.startAndWait();
    LOG.info("ZKServer: " + zkServer.getConnectionStr());
    try {
        RunId runId = RunIds.generate();
        ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        zkClientService.startAndWait();
        Service service = createService(zkClientService, runId);
        service.startAndWait();
        WeaveController controller = getController(zkClientService, runId);
        controller.sendCommand(Command.Builder.of("test").build()).get(2, TimeUnit.SECONDS);
        controller.stop().get(2, TimeUnit.SECONDS);
        Assert.assertEquals(ServiceController.State.TERMINATED, controller.state());
        final CountDownLatch terminateLatch = new CountDownLatch(1);
        service.addListener(new ServiceListenerAdapter() {

            @Override
            public void terminated(Service.State from) {
                terminateLatch.countDown();
            }
        }, Threads.SAME_THREAD_EXECUTOR);
        Assert.assertTrue(service.state() == Service.State.TERMINATED || terminateLatch.await(2, TimeUnit.SECONDS));
        zkClientService.stopAndWait();
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) WeaveController(com.continuuity.weave.api.WeaveController) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) RunId(com.continuuity.weave.api.RunId) CountDownLatch(java.util.concurrent.CountDownLatch) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 4 with WeaveController

use of com.continuuity.weave.api.WeaveController in project weave by continuuity.

the class ControllerTest method testControllerBefore.

// Test controller created before service starts.
@Test
public void testControllerBefore() throws InterruptedException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
    zkServer.startAndWait();
    LOG.info("ZKServer: " + zkServer.getConnectionStr());
    try {
        RunId runId = RunIds.generate();
        ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        zkClientService.startAndWait();
        final CountDownLatch runLatch = new CountDownLatch(1);
        final CountDownLatch stopLatch = new CountDownLatch(1);
        WeaveController controller = getController(zkClientService, runId);
        controller.addListener(new ServiceListenerAdapter() {

            @Override
            public void running() {
                runLatch.countDown();
            }

            @Override
            public void terminated(Service.State from) {
                stopLatch.countDown();
            }
        }, Threads.SAME_THREAD_EXECUTOR);
        Service service = createService(zkClientService, runId);
        service.start();
        Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));
        Assert.assertFalse(stopLatch.await(2, TimeUnit.SECONDS));
        service.stop();
        Assert.assertTrue(stopLatch.await(2, TimeUnit.SECONDS));
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) WeaveController(com.continuuity.weave.api.WeaveController) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) RunId(com.continuuity.weave.api.RunId) CountDownLatch(java.util.concurrent.CountDownLatch) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Example 5 with WeaveController

use of com.continuuity.weave.api.WeaveController in project weave by continuuity.

the class ControllerTest method testControllerListener.

// Test controller listener receive first state change without state transition from service
@Test
public void testControllerListener() throws InterruptedException {
    InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
    zkServer.startAndWait();
    LOG.info("ZKServer: " + zkServer.getConnectionStr());
    try {
        RunId runId = RunIds.generate();
        ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
        zkClientService.startAndWait();
        Service service = createService(zkClientService, runId);
        service.startAndWait();
        final CountDownLatch runLatch = new CountDownLatch(1);
        WeaveController controller = getController(zkClientService, runId);
        controller.addListener(new ServiceListenerAdapter() {

            @Override
            public void running() {
                runLatch.countDown();
            }
        }, Threads.SAME_THREAD_EXECUTOR);
        Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));
        service.stopAndWait();
        zkClientService.stopAndWait();
    } finally {
        zkServer.stopAndWait();
    }
}
Also used : ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ZKClientService(com.continuuity.weave.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) WeaveController(com.continuuity.weave.api.WeaveController) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) RunId(com.continuuity.weave.api.RunId) CountDownLatch(java.util.concurrent.CountDownLatch) InMemoryZKServer(com.continuuity.weave.internal.zookeeper.InMemoryZKServer) Test(org.junit.Test)

Aggregations

WeaveController (com.continuuity.weave.api.WeaveController)13 Test (org.junit.Test)12 WeaveRunner (com.continuuity.weave.api.WeaveRunner)9 PrinterLogHandler (com.continuuity.weave.api.logging.PrinterLogHandler)9 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)9 PrintWriter (java.io.PrintWriter)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 Discoverable (com.continuuity.weave.discovery.Discoverable)6 Service (com.google.common.util.concurrent.Service)5 OutputStreamWriter (java.io.OutputStreamWriter)4 Socket (java.net.Socket)4 ResourceSpecification (com.continuuity.weave.api.ResourceSpecification)3 RunId (com.continuuity.weave.api.RunId)3 InMemoryZKServer (com.continuuity.weave.internal.zookeeper.InMemoryZKServer)3 ZKClientService (com.continuuity.weave.zookeeper.ZKClientService)3 LineReader (com.google.common.io.LineReader)3 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)3 InputStreamReader (java.io.InputStreamReader)3 ResourceReport (com.continuuity.weave.api.ResourceReport)2 WeaveRunResources (com.continuuity.weave.api.WeaveRunResources)1