Search in sources :

Example 11 with MetricsContext

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

the class DefaultServiceConfigurer method createHttpHandler.

private <T extends HttpServiceHandler> HttpHandler createHttpHandler(T handler) {
    MetricsContext noOpsMetricsContext = new NoOpMetricsCollectionService().getContext(new HashMap<String, String>());
    HttpHandlerFactory factory = new HttpHandlerFactory("", noOpsMetricsContext);
    @SuppressWarnings("unchecked") TypeToken<T> type = (TypeToken<T>) TypeToken.of(handler.getClass());
    return factory.createHttpHandler(type, new VerificationDelegateContext<>(handler));
}
Also used : HttpHandlerFactory(co.cask.cdap.internal.app.runtime.service.http.HttpHandlerFactory) TypeToken(com.google.common.reflect.TypeToken) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService)

Example 12 with MetricsContext

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

the class MetricsHandlerTestRun method setupMetrics.

private static void setupMetrics() throws Exception {
    // Adding metrics for app "WordCount1" in namespace "myspace", "WCount1" in "yourspace"
    MetricsContext collector = collectionService.getContext(getFlowletContext("myspace", "WordCount1", "WordCounter", "run1", "splitter"));
    collector.increment("reads", 1);
    collector.increment("writes", 1);
    collector = collectionService.getContext(getFlowletContext("yourspace", "WCount1", "WordCounter", "run1", "splitter"));
    collector.increment("reads", 1);
    collector = collectionService.getContext(getFlowletContext("yourspace", "WCount1", "WCounter", "run1", "splitter"));
    emitTs = System.currentTimeMillis();
    // we want to emit in two different seconds
    // todo : figure out why we need this
    TimeUnit.SECONDS.sleep(1);
    collector.increment("reads", 1);
    TimeUnit.MILLISECONDS.sleep(2000);
    collector.increment("reads", 2);
    collector = collectionService.getContext(getFlowletContext("yourspace", "WCount1", "WCounter", "run1", "counter"));
    collector.increment("reads", 1);
    collector = collectionService.getContext(getMapReduceTaskContext("yourspace", "WCount1", "ClassicWordCount", MapReduceMetrics.TaskType.Mapper, "run1", "task1"));
    collector.increment("reads", 1);
    collector = collectionService.getContext(getMapReduceTaskContext("yourspace", "WCount1", "ClassicWordCount", MapReduceMetrics.TaskType.Reducer, "run1", "task2"));
    collector.increment("reads", 1);
    collector = collectionService.getContext(getFlowletContext("myspace", "WordCount1", "WordCounter", "run1", "splitter"));
    collector.increment("reads", 1);
    collector.increment("writes", 1);
    collector = collectionService.getContext(getFlowletContext("myspace", "WordCount1", "WordCounter", "run1", "collector"));
    collector.increment("aa", 1);
    collector.increment("zz", 1);
    collector.increment("ab", 1);
    collector = collectionService.getContext(getWorkerContext("yourspace", "WCount1", "WorkerWordCount", "run1", "task1"));
    collector.increment("workerreads", 5);
    collector.increment("workerwrites", 6);
    collector = collectionService.getContext(getWorkerContext("yourspace", "WCount1", "WorkerWordCount", "run2", "task1"));
    collector.increment("workerreads", 5);
    collector.increment("workerwrites", 6);
    // also: user metrics
    Metrics userMetrics = new ProgramUserMetrics(collectionService.getContext(getFlowletContext("myspace", "WordCount1", "WordCounter", "run1", "splitter")));
    userMetrics.count("reads", 1);
    userMetrics.count("writes", 2);
    collector = collectionService.getContext(new HashMap<String, String>());
    collector.increment("resources.total.storage", 10);
    // need a better way to do this
    TimeUnit.SECONDS.sleep(2);
}
Also used : MapReduceMetrics(co.cask.cdap.app.metrics.MapReduceMetrics) ProgramUserMetrics(co.cask.cdap.app.metrics.ProgramUserMetrics) Metrics(co.cask.cdap.api.metrics.Metrics) HashMap(java.util.HashMap) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) ProgramUserMetrics(co.cask.cdap.app.metrics.ProgramUserMetrics)

Example 13 with MetricsContext

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

the class MapReduceMetricsWriter method reportMapTaskMetrics.

private void reportMapTaskMetrics(TaskReport taskReport) {
    Counters counters = taskReport.getTaskCounters();
    MetricsContext metricsContext = mapTaskMetricsCollectors.getUnchecked(taskReport.getTaskId());
    metricsContext.gauge(MapReduceMetrics.METRIC_TASK_INPUT_RECORDS, getTaskCounter(counters, TaskCounter.MAP_INPUT_RECORDS));
    metricsContext.gauge(MapReduceMetrics.METRIC_TASK_OUTPUT_RECORDS, getTaskCounter(counters, TaskCounter.MAP_OUTPUT_RECORDS));
    metricsContext.gauge(MapReduceMetrics.METRIC_TASK_BYTES, getTaskCounter(counters, TaskCounter.MAP_OUTPUT_BYTES));
    metricsContext.gauge(MapReduceMetrics.METRIC_TASK_COMPLETION, (long) (taskReport.getProgress() * 100));
}
Also used : MetricsContext(co.cask.cdap.api.metrics.MetricsContext) Counters(org.apache.hadoop.mapreduce.Counters)

Example 14 with MetricsContext

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

the class HttpHandlerGeneratorTest method testContentProducer.

@Test
public void testContentProducer() throws Exception {
    MetricsContext noOpsMetricsContext = new NoOpMetricsCollectionService().getContext(new HashMap<String, String>());
    HttpHandlerFactory factory = new HttpHandlerFactory("/content", noOpsMetricsContext);
    // Create the file upload handler and starts a netty server with it
    final File outputDir = TEMP_FOLDER.newFolder();
    HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() {

        @Override
        protected FileHandler createHandler() {
            return new FileHandler(outputDir);
        }
    });
    NettyHttpService service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(httpHandler)).build();
    service.startAndWait();
    try {
        // Generate a 100K file
        File file = TEMP_FOLDER.newFile();
        Files.write(Strings.repeat("0123456789", 10240).getBytes(Charsets.UTF_8), file);
        InetSocketAddress bindAddress = service.getBindAddress();
        // Upload the generated file
        URL uploadURL = new URL(String.format("http://%s:%d/content/upload/test.txt", bindAddress.getHostName(), bindAddress.getPort()));
        HttpURLConnection urlConn = (HttpURLConnection) uploadURL.openConnection();
        try {
            urlConn.setDoOutput(true);
            urlConn.setRequestMethod("PUT");
            Files.copy(file, urlConn.getOutputStream());
            Assert.assertEquals(200, urlConn.getResponseCode());
        } finally {
            urlConn.disconnect();
        }
        // Download the file
        File downloadFile = TEMP_FOLDER.newFile();
        urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        try {
            ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
        } finally {
            urlConn.disconnect();
        }
        // Compare if the file content are the same
        Assert.assertTrue(Files.equal(file, downloadFile));
        // Download a file that doesn't exist
        urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test2.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        try {
            Assert.assertEquals(500, urlConn.getResponseCode());
        } finally {
            urlConn.disconnect();
        }
        // Upload the file to the POST endpoint. The endpoint should response with the same file content
        downloadFile = TEMP_FOLDER.newFile();
        urlConn = (HttpURLConnection) uploadURL.openConnection();
        try {
            urlConn.setDoOutput(true);
            urlConn.setRequestMethod("POST");
            Files.copy(file, urlConn.getOutputStream());
            ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
            Assert.assertEquals(200, urlConn.getResponseCode());
            Assert.assertTrue(Files.equal(file, downloadFile));
        } finally {
            urlConn.disconnect();
        }
    } finally {
        service.stopAndWait();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) NettyHttpService(co.cask.http.NettyHttpService) File(java.io.File) Test(org.junit.Test)

Example 15 with MetricsContext

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

the class HttpHandlerGeneratorTest method testHttpHandlerGenerator.

@Test
public void testHttpHandlerGenerator() throws Exception {
    MetricsContext noOpsMetricsContext = new NoOpMetricsCollectionService().getContext(new HashMap<String, String>());
    HttpHandlerFactory factory = new HttpHandlerFactory("/prefix", noOpsMetricsContext);
    HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(MyHttpHandler.class), new AbstractDelegatorContext<MyHttpHandler>() {

        @Override
        protected MyHttpHandler createHandler() {
            return new MyHttpHandler();
        }
    });
    HttpHandler httpHandlerWithoutAnnotation = factory.createHttpHandler(TypeToken.of(NoAnnotationHandler.class), new AbstractDelegatorContext<NoAnnotationHandler>() {

        @Override
        protected NoAnnotationHandler createHandler() {
            return new NoAnnotationHandler();
        }
    });
    NettyHttpService service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(httpHandler, httpHandlerWithoutAnnotation)).build();
    service.startAndWait();
    try {
        InetSocketAddress bindAddress = service.getBindAddress();
        // Make a GET call
        URLConnection urlConn = new URL(String.format("http://%s:%d/prefix/p2/handle", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        urlConn.setReadTimeout(2000);
        Assert.assertEquals("Hello World", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
        // Make a POST call
        urlConn = new URL(String.format("http://%s:%d/prefix/p2/echo/test", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        urlConn.setReadTimeout(2000);
        urlConn.setDoOutput(true);
        ByteStreams.copy(ByteStreams.newInputStreamSupplier("Hello".getBytes(Charsets.UTF_8)), urlConn.getOutputStream());
        Assert.assertEquals("Hello test", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
        // Ensure that even though the handler did not have a class-level annotation, we still prefix the path that it
        // handles by "/prefix"
        urlConn = new URL(String.format("http://%s:%d/prefix/ping", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        urlConn.setReadTimeout(2000);
        Assert.assertEquals("OK", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
    } finally {
        service.stopAndWait();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL) NettyHttpService(co.cask.http.NettyHttpService) Test(org.junit.Test)

Aggregations

MetricsContext (co.cask.cdap.api.metrics.MetricsContext)20 NoOpMetricsCollectionService (co.cask.cdap.common.metrics.NoOpMetricsCollectionService)5 Test (org.junit.Test)5 HttpHandler (co.cask.http.HttpHandler)4 NettyHttpService (co.cask.http.NettyHttpService)4 InetSocketAddress (java.net.InetSocketAddress)4 Map (java.util.Map)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)2 QueueSpecification (co.cask.cdap.app.queue.QueueSpecification)2 QueueName (co.cask.cdap.common.queue.QueueName)2 TableId (co.cask.cdap.data2.util.TableId)2 DatasetSpecificationSummary (co.cask.cdap.proto.DatasetSpecificationSummary)2 DatasetId (co.cask.cdap.proto.id.DatasetId)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 TypeToken (com.google.common.reflect.TypeToken)2 File (java.io.File)2 HashMap (java.util.HashMap)2