Search in sources :

Example 6 with NoOpMetricsCollectionService

use of co.cask.cdap.common.metrics.NoOpMetricsCollectionService 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 7 with NoOpMetricsCollectionService

use of co.cask.cdap.common.metrics.NoOpMetricsCollectionService 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)

Example 8 with NoOpMetricsCollectionService

use of co.cask.cdap.common.metrics.NoOpMetricsCollectionService in project cdap by caskdata.

the class HttpHandlerGeneratorTest method testContentConsumer.

@Test
public void testContentConsumer() 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);
        }
    });
    // Creates a Netty http server with 1K request buffer
    NettyHttpService service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(httpHandler)).setHttpChunkLimit(1024).build();
    service.startAndWait();
    try {
        InetSocketAddress bindAddress = service.getBindAddress();
        testUpload(outputDir, bindAddress, "");
        testUpload(outputDir, bindAddress, "-no-tx");
    } finally {
        service.stopAndWait();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) NettyHttpService(co.cask.http.NettyHttpService) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) File(java.io.File) Test(org.junit.Test)

Example 9 with NoOpMetricsCollectionService

use of co.cask.cdap.common.metrics.NoOpMetricsCollectionService in project cdap by caskdata.

the class UpgradeTool method createInjector.

@VisibleForTesting
Injector createInjector() throws Exception {
    return Guice.createInjector(new ConfigModule(cConf, hConf), new LocationRuntimeModule().getDistributedModules(), new ZKClientModule(), new DiscoveryRuntimeModule().getDistributedModules(), new MessagingClientModule(), Modules.override(new DataSetsModules().getDistributedModules()).with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(DatasetFramework.class).to(InMemoryDatasetFramework.class).in(Scopes.SINGLETON);
            // the DataSetsModules().getDistributedModules() binds to RemoteDatasetFramework so override that to
            // the same InMemoryDatasetFramework
            bind(DatasetFramework.class).annotatedWith(Names.named(DataSetsModules.BASE_DATASET_FRAMEWORK)).to(DatasetFramework.class);
            install(new FactoryModuleBuilder().implement(DatasetDefinitionRegistry.class, DefaultDatasetDefinitionRegistry.class).build(DatasetDefinitionRegistryFactory.class));
            // CDAP-5954 Upgrade tool does not need to record lineage and metadata changes for now.
            bind(LineageWriter.class).to(NoOpLineageWriter.class);
        }
    }), new ViewAdminModules().getDistributedModules(), new StreamAdminModules().getDistributedModules(), new NotificationFeedClientModule(), new TwillModule(), new ExploreClientModule(), new ProgramRunnerRuntimeModule().getDistributedModules(), new ServiceStoreModules().getDistributedModules(), new SystemDatasetRuntimeModule().getDistributedModules(), // don't need real notifications for upgrade, so use the in-memory implementations
    new NotificationServiceRuntimeModule().getInMemoryModules(), new KafkaClientModule(), new NamespaceStoreModule().getDistributedModules(), new AuthenticationContextModules().getMasterModule(), new AuthorizationModule(), new AuthorizationEnforcementModule().getMasterModule(), new SecureStoreModules().getDistributedModules(), new DataFabricModules(UpgradeTool.class.getName()).getDistributedModules(), new AppFabricServiceRuntimeModule().getDistributedModules(), new AbstractModule() {

        @Override
        protected void configure() {
            // the DataFabricDistributedModule needs MetricsCollectionService binding and since Upgrade tool does not do
            // anything with Metrics we just bind it to NoOpMetricsCollectionService
            bind(MetricsCollectionService.class).to(NoOpMetricsCollectionService.class).in(Scopes.SINGLETON);
            bind(MetricDatasetFactory.class).to(DefaultMetricDatasetFactory.class).in(Scopes.SINGLETON);
            bind(MetricStore.class).to(DefaultMetricStore.class);
        }

        @Provides
        @Singleton
        @Named("datasetInstanceManager")
        @SuppressWarnings("unused")
        public DatasetInstanceManager getDatasetInstanceManager(TransactionSystemClientService txClient, TransactionExecutorFactory txExecutorFactory, @Named("datasetMDS") DatasetFramework framework) {
            return new DatasetInstanceManager(txClient, txExecutorFactory, framework);
        }

        // This is needed because the LocalApplicationManager
        // expects a dsframework injection named datasetMDS
        @Provides
        @Singleton
        @Named("datasetMDS")
        @SuppressWarnings("unused")
        public DatasetFramework getInDsFramework(DatasetFramework dsFramework) {
            return dsFramework;
        }
    });
}
Also used : MessagingClientModule(co.cask.cdap.messaging.guice.MessagingClientModule) ConfigModule(co.cask.cdap.common.guice.ConfigModule) FactoryModuleBuilder(com.google.inject.assistedinject.FactoryModuleBuilder) NamespaceStoreModule(co.cask.cdap.store.guice.NamespaceStoreModule) NotificationServiceRuntimeModule(co.cask.cdap.notifications.guice.NotificationServiceRuntimeModule) ViewAdminModules(co.cask.cdap.data.view.ViewAdminModules) TransactionExecutorFactory(co.cask.cdap.data2.transaction.TransactionExecutorFactory) MetricDatasetFactory(co.cask.cdap.metrics.store.MetricDatasetFactory) DefaultMetricDatasetFactory(co.cask.cdap.metrics.store.DefaultMetricDatasetFactory) DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) InMemoryDatasetFramework(co.cask.cdap.data2.dataset2.InMemoryDatasetFramework) ZKClientModule(co.cask.cdap.common.guice.ZKClientModule) DatasetDefinitionRegistryFactory(co.cask.cdap.data2.dataset2.DatasetDefinitionRegistryFactory) KafkaClientModule(co.cask.cdap.common.guice.KafkaClientModule) TransactionSystemClientService(co.cask.cdap.data2.transaction.TransactionSystemClientService) SystemDatasetRuntimeModule(co.cask.cdap.data.runtime.SystemDatasetRuntimeModule) DiscoveryRuntimeModule(co.cask.cdap.common.guice.DiscoveryRuntimeModule) AuthorizationModule(co.cask.cdap.app.guice.AuthorizationModule) InMemoryDatasetFramework(co.cask.cdap.data2.dataset2.InMemoryDatasetFramework) Named(com.google.inject.name.Named) TwillModule(co.cask.cdap.app.guice.TwillModule) DatasetInstanceManager(co.cask.cdap.data2.datafabric.dataset.instance.DatasetInstanceManager) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) AuthenticationContextModules(co.cask.cdap.security.auth.context.AuthenticationContextModules) DataSetsModules(co.cask.cdap.data.runtime.DataSetsModules) SecureStoreModules(co.cask.cdap.security.guice.SecureStoreModules) LocationRuntimeModule(co.cask.cdap.common.guice.LocationRuntimeModule) DefaultMetricStore(co.cask.cdap.metrics.store.DefaultMetricStore) Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule) StreamAdminModules(co.cask.cdap.data.stream.StreamAdminModules) ProgramRunnerRuntimeModule(co.cask.cdap.app.guice.ProgramRunnerRuntimeModule) LineageWriter(co.cask.cdap.data2.metadata.writer.LineageWriter) NoOpLineageWriter(co.cask.cdap.data2.metadata.writer.NoOpLineageWriter) ExploreClientModule(co.cask.cdap.explore.guice.ExploreClientModule) Singleton(com.google.inject.Singleton) NotificationFeedClientModule(co.cask.cdap.notifications.feeds.client.NotificationFeedClientModule) DataFabricModules(co.cask.cdap.data.runtime.DataFabricModules) ServiceStoreModules(co.cask.cdap.app.guice.ServiceStoreModules) AuthorizationEnforcementModule(co.cask.cdap.security.authorization.AuthorizationEnforcementModule) AppFabricServiceRuntimeModule(co.cask.cdap.app.guice.AppFabricServiceRuntimeModule) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with NoOpMetricsCollectionService

use of co.cask.cdap.common.metrics.NoOpMetricsCollectionService in project cdap by caskdata.

the class MessagingHttpServiceTest method init.

@BeforeClass
public static void init() throws IOException {
    cConf = CConfiguration.create();
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().getAbsolutePath());
    cConf.setInt(Constants.MessagingSystem.HTTP_SERVER_CONSUME_CHUNK_SIZE, 128);
    // Set max life time to a high value so that dummy tx ids that we create in the tests still work
    cConf.setLong(TxConstants.Manager.CFG_TX_MAX_LIFETIME, 10000000000L);
    Injector injector = Guice.createInjector(new ConfigModule(cConf), new DiscoveryRuntimeModule().getInMemoryModules(), new MessagingServerRuntimeModule().getInMemoryModules(), new AbstractModule() {

        @Override
        protected void configure() {
            bind(MetricsCollectionService.class).toInstance(new NoOpMetricsCollectionService());
        }
    });
    httpService = injector.getInstance(MessagingHttpService.class);
    httpService.startAndWait();
    client = new ClientMessagingService(injector.getInstance(DiscoveryServiceClient.class));
}
Also used : Injector(com.google.inject.Injector) ConfigModule(co.cask.cdap.common.guice.ConfigModule) MessagingServerRuntimeModule(co.cask.cdap.messaging.guice.MessagingServerRuntimeModule) ClientMessagingService(co.cask.cdap.messaging.client.ClientMessagingService) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) DiscoveryRuntimeModule(co.cask.cdap.common.guice.DiscoveryRuntimeModule) AbstractModule(com.google.inject.AbstractModule) BeforeClass(org.junit.BeforeClass)

Aggregations

NoOpMetricsCollectionService (co.cask.cdap.common.metrics.NoOpMetricsCollectionService)14 Test (org.junit.Test)10 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)7 AppenderContext (co.cask.cdap.api.logging.AppenderContext)6 LocalAppenderContext (co.cask.cdap.logging.framework.LocalAppenderContext)6 TransactionSystemClient (org.apache.tephra.TransactionSystemClient)6 LocationFactory (org.apache.twill.filesystem.LocationFactory)6 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)5 HttpHandler (co.cask.http.HttpHandler)4 NettyHttpService (co.cask.http.NettyHttpService)4 InetSocketAddress (java.net.InetSocketAddress)4 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)3 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)3 ConfigModule (co.cask.cdap.common.guice.ConfigModule)3 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)3 FileMetaDataReader (co.cask.cdap.logging.meta.FileMetaDataReader)3 LogLocation (co.cask.cdap.logging.write.LogLocation)3 AbstractModule (com.google.inject.AbstractModule)3 IOException (java.io.IOException)3 HttpURLConnection (java.net.HttpURLConnection)3