Search in sources :

Example 1 with Discoverable

use of com.continuuity.weave.discovery.Discoverable 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 Discoverable

use of com.continuuity.weave.discovery.Discoverable in project weave by continuuity.

the class FailureRestartTestRun method getInstances.

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;
}
Also used : Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) LineReader(com.google.common.io.LineReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter)

Example 3 with Discoverable

use of com.continuuity.weave.discovery.Discoverable 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 4 with Discoverable

use of com.continuuity.weave.discovery.Discoverable in project weave by continuuity.

the class EchoServerTestRun method testEchoServer.

@Test
public void testEchoServer() throws InterruptedException, ExecutionException, IOException, URISyntaxException, TimeoutException {
    WeaveRunner runner = YarnTestSuite.getWeaveRunner();
    WeaveController 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(YarnTestSuite.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(YarnTestSuite.waitForSize(echoServices, 3, 60));
    echoServices = controller.discoverService("echo2");
    // Decrease number of instances
    controller.changeInstances("EchoServer", 1);
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 1, 60));
    // Increase number of instances again
    controller.changeInstances("EchoServer", 2);
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 2, 60));
    // Make sure still only one app is running
    Iterable<WeaveRunner.LiveInfo> apps = runner.lookupLive();
    Assert.assertTrue(YarnTestSuite.waitForSize(apps, 1, 60));
    // Creates a new runner service to check it can regain control over running app.
    WeaveRunnerService runnerService = YarnTestSuite.createWeaveRunnerService();
    runnerService.startAndWait();
    try {
        Iterable<WeaveController> controllers = runnerService.lookup("EchoServer");
        Assert.assertTrue(YarnTestSuite.waitForSize(controllers, 1, 60));
        for (WeaveController c : controllers) {
            LOG.info("Stopping application: " + c.getRunId());
            c.stop().get(30, TimeUnit.SECONDS);
        }
        Assert.assertTrue(YarnTestSuite.waitForSize(apps, 0, 60));
    } finally {
        runnerService.stopAndWait();
    }
    // Sleep a bit before exiting.
    TimeUnit.SECONDS.sleep(2);
}
Also used : WeaveRunner(com.continuuity.weave.api.WeaveRunner) Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) LineReader(com.google.common.io.LineReader) PrinterLogHandler(com.continuuity.weave.api.logging.PrinterLogHandler) WeaveController(com.continuuity.weave.api.WeaveController) OutputStreamWriter(java.io.OutputStreamWriter) WeaveRunnerService(com.continuuity.weave.api.WeaveRunnerService) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 5 with Discoverable

use of com.continuuity.weave.discovery.Discoverable in project weave by continuuity.

the class ResourceReportTestRun method testResourceReportWithFailingContainers.

@Test
public void testResourceReportWithFailingContainers() throws InterruptedException, IOException, TimeoutException, ExecutionException {
    WeaveRunner runner = YarnTestSuite.getWeaveRunner();
    ResourceSpecification resourceSpec = ResourceSpecification.Builder.with().setVirtualCores(1).setMemory(128, ResourceSpecification.SizeUnit.MEGA).setInstances(2).build();
    WeaveController 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.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(YarnTestSuite.waitForSize(echoServices, 2, 60));
    // check that we have 2 runnables.
    ResourceReport report = controller.getResourceReport();
    Assert.assertEquals(2, report.getRunnableResources("BuggyServer").size());
    // cause a divide by 0 in one server
    Discoverable discoverable = echoServices.iterator().next();
    Socket socket = new Socket(discoverable.getSocketAddress().getAddress(), discoverable.getSocketAddress().getPort());
    try {
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
        writer.println("0");
    } finally {
        socket.close();
    }
    // takes some time for app master to find out the container completed...
    TimeUnit.SECONDS.sleep(5);
    // check that we have 1 runnable, not 2.
    report = controller.getResourceReport();
    Assert.assertEquals(1, report.getRunnableResources("BuggyServer").size());
    controller.stop().get(30, TimeUnit.SECONDS);
    // Sleep a bit before exiting.
    TimeUnit.SECONDS.sleep(2);
}
Also used : WeaveRunner(com.continuuity.weave.api.WeaveRunner) Discoverable(com.continuuity.weave.discovery.Discoverable) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) ResourceSpecification(com.continuuity.weave.api.ResourceSpecification) CountDownLatch(java.util.concurrent.CountDownLatch) PrinterLogHandler(com.continuuity.weave.api.logging.PrinterLogHandler) WeaveController(com.continuuity.weave.api.WeaveController) ResourceReport(com.continuuity.weave.api.ResourceReport) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

Discoverable (com.continuuity.weave.discovery.Discoverable)7 PrintWriter (java.io.PrintWriter)7 WeaveController (com.continuuity.weave.api.WeaveController)6 WeaveRunner (com.continuuity.weave.api.WeaveRunner)6 PrinterLogHandler (com.continuuity.weave.api.logging.PrinterLogHandler)6 Test (org.junit.Test)6 OutputStreamWriter (java.io.OutputStreamWriter)5 Socket (java.net.Socket)5 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)4 LineReader (com.google.common.io.LineReader)4 InputStreamReader (java.io.InputStreamReader)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ResourceSpecification (com.continuuity.weave.api.ResourceSpecification)3 ResourceReport (com.continuuity.weave.api.ResourceReport)2 InetSocketAddress (java.net.InetSocketAddress)2 WeaveRunResources (com.continuuity.weave.api.WeaveRunResources)1 WeaveRunnerService (com.continuuity.weave.api.WeaveRunnerService)1 File (java.io.File)1 Collection (java.util.Collection)1 Map (java.util.Map)1