Search in sources :

Example 16 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class TestBundleJarApp method testBundleJar.

@Test
public void testBundleJar() throws Exception {
    File helloWorldJar = new File(TestBundleJarApp.class.getClassLoader().getResource("helloworld.jar").toURI());
    ApplicationManager applicationManager = deployApplication(BundleJarApp.class, helloWorldJar);
    FlowManager flowManager = applicationManager.getFlowManager("SimpleFlow").start();
    StreamManager streamManager = getStreamManager("simpleInputStream");
    for (int i = 0; i < 5; i++) {
        streamManager.send("test" + i + ":" + i);
    }
    // Check the flowlet metrics
    RuntimeMetrics flowletMetrics = flowManager.getFlowletMetrics("simpleFlowlet");
    flowletMetrics.waitForProcessed(5, 5, TimeUnit.SECONDS);
    Assert.assertEquals(0L, flowletMetrics.getException());
    flowManager.stop();
    // Query the result
    ServiceManager serviceManager = applicationManager.getServiceManager("SimpleGetInput").start();
    // Verify the query result
    String queryResult = callServiceGet(serviceManager.getServiceURL(), "/get/test1");
    String expectedQueryResult = new Gson().toJson(ImmutableMap.of("test1", "1" + BundleJarApp.EXPECTED_LOAD_TEST_CLASSES_OUTPUT));
    Assert.assertEquals(expectedQueryResult, queryResult);
    serviceManager.stop();
    serviceManager = applicationManager.getServiceManager("PrintService").start();
    String helloWorldClassName = "hello.HelloWorld";
    String result = callServiceGet(serviceManager.getServiceURL(), "/load/" + helloWorldClassName);
    String expected = new Gson().toJson(ImmutableMap.of("Class.forName", helloWorldClassName));
    Assert.assertEquals(expected, result);
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) StreamManager(co.cask.cdap.test.StreamManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) ServiceManager(co.cask.cdap.test.ServiceManager) Gson(com.google.gson.Gson) File(java.io.File) Test(org.junit.Test)

Example 17 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class TestFrameworkTestRun method testAppWithServices.

@Category(SlowTests.class)
@Test
public void testAppWithServices() throws Exception {
    ApplicationManager applicationManager = deployApplication(AppWithServices.class);
    LOG.info("Deployed.");
    ServiceManager serviceManager = applicationManager.getServiceManager(AppWithServices.SERVICE_NAME).start();
    serviceManager.waitForStatus(true);
    LOG.info("Service Started");
    URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
    Assert.assertNotNull(serviceURL);
    // Call the ping endpoint
    URL url = new URL(serviceURL, "ping2");
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(200, response.getResponseCode());
    // Call the failure endpoint
    url = new URL(serviceURL, "failure");
    request = HttpRequest.get(url).build();
    response = HttpRequests.execute(request);
    Assert.assertEquals(500, response.getResponseCode());
    Assert.assertTrue(response.getResponseBodyAsString().contains("Exception"));
    // Call the verify ClassLoader endpoint
    url = new URL(serviceURL, "verifyClassLoader");
    request = HttpRequest.get(url).build();
    response = HttpRequests.execute(request);
    Assert.assertEquals(200, response.getResponseCode());
    RuntimeMetrics serviceMetrics = serviceManager.getMetrics();
    serviceMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
    Assert.assertEquals(3, serviceMetrics.getInput());
    Assert.assertEquals(2, serviceMetrics.getProcessed());
    Assert.assertEquals(1, serviceMetrics.getException());
    // in the AppWithServices the handlerName is same as the serviceName - "ServerService" handler
    RuntimeMetrics handlerMetrics = getMetricsManager().getServiceHandlerMetrics(NamespaceId.DEFAULT.getNamespace(), AppWithServices.APP_NAME, AppWithServices.SERVICE_NAME, AppWithServices.SERVICE_NAME);
    handlerMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
    Assert.assertEquals(3, handlerMetrics.getInput());
    Assert.assertEquals(2, handlerMetrics.getProcessed());
    Assert.assertEquals(1, handlerMetrics.getException());
    // we can verify metrics, by adding getServiceMetrics in MetricsManager and then disabling the system scope test in
    // TestMetricsCollectionService
    LOG.info("DatasetUpdateService Started");
    Map<String, String> args = ImmutableMap.of(AppWithServices.WRITE_VALUE_RUN_KEY, AppWithServices.DATASET_TEST_VALUE, AppWithServices.WRITE_VALUE_STOP_KEY, AppWithServices.DATASET_TEST_VALUE_STOP);
    ServiceManager datasetWorkerServiceManager = applicationManager.getServiceManager(AppWithServices.DATASET_WORKER_SERVICE_NAME).start(args);
    WorkerManager datasetWorker = applicationManager.getWorkerManager(AppWithServices.DATASET_UPDATE_WORKER).start(args);
    datasetWorkerServiceManager.waitForStatus(true);
    ServiceManager noopManager = applicationManager.getServiceManager("NoOpService").start();
    serviceManager.waitForStatus(true, 2, 1);
    String result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY);
    String decodedResult = new Gson().fromJson(result, String.class);
    Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE, decodedResult);
    handlerMetrics = getMetricsManager().getServiceHandlerMetrics(NamespaceId.DEFAULT.getNamespace(), AppWithServices.APP_NAME, "NoOpService", "NoOpHandler");
    handlerMetrics.waitForinput(1, 5, TimeUnit.SECONDS);
    Assert.assertEquals(1, handlerMetrics.getInput());
    Assert.assertEquals(1, handlerMetrics.getProcessed());
    Assert.assertEquals(0, handlerMetrics.getException());
    // Test that a service can discover another service
    String path = String.format("discover/%s/%s", AppWithServices.APP_NAME, AppWithServices.DATASET_WORKER_SERVICE_NAME);
    url = new URL(serviceURL, path);
    request = HttpRequest.get(url).build();
    response = HttpRequests.execute(request);
    Assert.assertEquals(200, response.getResponseCode());
    datasetWorker.stop();
    datasetWorkerServiceManager.stop();
    datasetWorkerServiceManager.waitForStatus(false);
    LOG.info("DatasetUpdateService Stopped");
    serviceManager.stop();
    serviceManager.waitForStatus(false);
    LOG.info("ServerService Stopped");
    result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP);
    decodedResult = new Gson().fromJson(result, String.class);
    Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP, decodedResult);
    result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP_2);
    decodedResult = new Gson().fromJson(result, String.class);
    Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP_2, decodedResult);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) WorkerManager(co.cask.cdap.test.WorkerManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) ServiceManager(co.cask.cdap.test.ServiceManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) HttpResponse(co.cask.common.http.HttpResponse) Gson(com.google.gson.Gson) URL(java.net.URL) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 18 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class SparkKMeansAppTest method test.

@Test
public void test() throws Exception {
    // Deploy the Application
    ApplicationManager appManager = deployApplication(SparkKMeansApp.class);
    // Start the Flow
    FlowManager flowManager = appManager.getFlowManager("PointsFlow").start();
    // Send a few points to the stream
    StreamManager streamManager = getStreamManager("pointsStream");
    streamManager.send("10.6 519.2 110.3");
    streamManager.send("10.6 518.1 110.1");
    streamManager.send("10.6 519.6 109.9");
    streamManager.send("10.6 517.9 108.9");
    streamManager.send("10.7 518 109.2");
    //  Wait for the events to be processed, or at most 5 seconds
    RuntimeMetrics metrics = flowManager.getFlowletMetrics("reader");
    metrics.waitForProcessed(3, 5, TimeUnit.SECONDS);
    // Start a Spark Program
    SparkManager sparkManager = appManager.getSparkManager("SparkKMeansProgram").start();
    sparkManager.waitForFinish(60, TimeUnit.SECONDS);
    flowManager.stop();
    // Start CentersService
    ServiceManager serviceManager = appManager.getServiceManager(SparkKMeansApp.CentersService.SERVICE_NAME).start();
    // Wait service startup
    serviceManager.waitForStatus(true);
    // Request data and verify it
    String response = requestService(new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), "centers/1"));
    String[] coordinates = response.split(",");
    Assert.assertTrue(coordinates.length == 3);
    for (String coordinate : coordinates) {
        double value = Double.parseDouble(coordinate);
        Assert.assertTrue(value > 0);
    }
    // Request data by incorrect index and verify response
    URL url = new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), "centers/10");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    try {
        Assert.assertEquals(HttpURLConnection.HTTP_NO_CONTENT, conn.getResponseCode());
    } finally {
        conn.disconnect();
    }
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) SparkManager(co.cask.cdap.test.SparkManager) HttpURLConnection(java.net.HttpURLConnection) StreamManager(co.cask.cdap.test.StreamManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) ServiceManager(co.cask.cdap.test.ServiceManager) URL(java.net.URL) Test(org.junit.Test)

Example 19 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class MetricsClient method getMetrics.

/**
   * Gets the {@link RuntimeMetrics} for a particular metrics context.
   *
   * @param tags the metrics tags
   * @param inputName the metrics key for input counter
   * @param processedName the metrics key for processed counter
   * @param exceptionName the metrics key for exception counter
   * @return the {@link RuntimeMetrics}
   */
private RuntimeMetrics getMetrics(final Map<String, String> tags, final String inputName, final String processedName, final String exceptionName) {
    return new RuntimeMetrics() {

        @Override
        public long getInput() {
            return getTotalCounter(tags, inputName);
        }

        @Override
        public long getProcessed() {
            return getTotalCounter(tags, processedName);
        }

        @Override
        public long getException() {
            return getTotalCounter(tags, exceptionName);
        }

        @Override
        public void waitForinput(long count, long timeout, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException {
            doWaitFor(inputName, count, timeout, timeoutUnit);
        }

        @Override
        public void waitForProcessed(long count, long timeout, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException {
            doWaitFor(processedName, count, timeout, timeoutUnit);
        }

        @Override
        public void waitForException(long count, long timeout, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException {
            doWaitFor(exceptionName, count, timeout, timeoutUnit);
        }

        @Override
        public void waitFor(String name, long count, long timeout, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException {
            doWaitFor(name, count, timeout, timeoutUnit);
        }

        private void doWaitFor(String name, long count, long timeout, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException {
            long value = getTotalCounter(tags, name);
            // Min sleep time is 10ms, max sleep time is 1 seconds
            long sleepMillis = Math.max(10, Math.min(timeoutUnit.toMillis(timeout) / 10, TimeUnit.SECONDS.toMillis(1)));
            Stopwatch stopwatch = new Stopwatch().start();
            while (value < count && stopwatch.elapsedTime(timeoutUnit) < timeout) {
                TimeUnit.MILLISECONDS.sleep(sleepMillis);
                value = getTotalCounter(tags, name);
            }
            if (value < count) {
                throw new TimeoutException("Time limit reached. Got '" + value + "' instead of '" + count + "'");
            }
        }

        @Override
        public String toString() {
            return String.format("%s; tags=%d, processed=%d, exception=%d", Joiner.on(",").withKeyValueSeparator(":").join(tags), getInput(), getProcessed(), getException());
        }
    };
}
Also used : RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) Stopwatch(com.google.common.base.Stopwatch) TimeUnit(java.util.concurrent.TimeUnit) TimeoutException(java.util.concurrent.TimeoutException)

Example 20 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class WebAnalyticsTest method testWebAnalytics.

@Test
public void testWebAnalytics() throws Exception {
    // Deploy the Application
    ApplicationManager appManager = deployApplication(WebAnalytics.class);
    // Start the Flow
    FlowManager flowManager = appManager.getFlowManager("WebAnalyticsFlow").start();
    // Send events to the Stream
    StreamManager streamManager = getStreamManager("log");
    BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/access.log"), "UTF-8"));
    int lines = 0;
    try {
        String line = reader.readLine();
        while (line != null) {
            streamManager.send(line);
            lines++;
            line = reader.readLine();
        }
    } finally {
        reader.close();
    }
    // Wait for the flow to process all data
    RuntimeMetrics flowletMetrics = flowManager.getFlowletMetrics("UniqueVisitor");
    flowletMetrics.waitForProcessed(lines, 10, TimeUnit.SECONDS);
    // Verify the unique count
    UniqueVisitCount uniqueVisitCount = this.<UniqueVisitCount>getDataset("UniqueVisitCount").get();
    Assert.assertEquals(3L, uniqueVisitCount.getCount("192.168.12.72"));
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) InputStreamReader(java.io.InputStreamReader) StreamManager(co.cask.cdap.test.StreamManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) BufferedReader(java.io.BufferedReader) Test(org.junit.Test)

Aggregations

RuntimeMetrics (co.cask.cdap.api.metrics.RuntimeMetrics)21 ApplicationManager (co.cask.cdap.test.ApplicationManager)17 FlowManager (co.cask.cdap.test.FlowManager)17 Test (org.junit.Test)16 ServiceManager (co.cask.cdap.test.ServiceManager)12 StreamManager (co.cask.cdap.test.StreamManager)12 URL (java.net.URL)10 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)6 Gson (com.google.gson.Gson)6 HttpURLConnection (java.net.HttpURLConnection)6 Category (org.junit.experimental.categories.Category)5 Get (co.cask.cdap.api.dataset.table.Get)2 Table (co.cask.cdap.api.dataset.table.Table)2 MapReduceManager (co.cask.cdap.test.MapReduceManager)2 SparkManager (co.cask.cdap.test.SparkManager)2 HttpRequest (co.cask.common.http.HttpRequest)2 HttpResponse (co.cask.common.http.HttpResponse)2 Map (java.util.Map)2 KeyValue (co.cask.cdap.api.dataset.lib.KeyValue)1 Row (co.cask.cdap.api.dataset.table.Row)1