Search in sources :

Example 11 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by caskdata.

the class AppFabricServiceMainTest method testAppFabricService.

@Test
public void testAppFabricService() throws Exception {
    // Query the system services endpoint
    URL url = getRouterBaseURI().resolve("/v3/system/services").toURL();
    HttpResponse response = HttpRequests.execute(HttpRequest.get(url).build(), new DefaultHttpRequestConfig(false));
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    // Deploy an app
    LocationFactory locationFactory = new LocalLocationFactory(TEMP_FOLDER.newFolder());
    Location deploymentJar = AppJarHelper.createDeploymentJar(locationFactory, AllProgramsApp.class);
    URI baseURI = getRouterBaseURI().resolve("/v3/namespaces/default/");
    url = baseURI.resolve("apps").toURL();
    HttpRequestConfig requestConfig = new HttpRequestConfig(0, 0, false);
    response = HttpRequests.execute(HttpRequest.post(url).withBody((ContentProvider<? extends InputStream>) deploymentJar::getInputStream).addHeader("X-Archive-Name", AllProgramsApp.class.getSimpleName() + "-1.0-SNAPSHOT.jar").build(), requestConfig);
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    // Get the application
    url = baseURI.resolve("apps/" + AllProgramsApp.NAME).toURL();
    response = HttpRequests.execute(HttpRequest.get(url).build(), requestConfig);
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    ApplicationDetail appDetail = new Gson().fromJson(response.getResponseBodyAsString(), ApplicationDetail.class);
    // Do some basic validation only.
    Assert.assertEquals(AllProgramsApp.NAME, appDetail.getName());
    Assert.assertTrue(appDetail.getPrograms().stream().filter(r -> r.getType() == ProgramType.WORKFLOW).anyMatch(r -> AllProgramsApp.NoOpWorkflow.NAME.equals(r.getName())));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) Location(org.apache.twill.filesystem.Location) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) URL(java.net.URL) HttpResponse(io.cdap.common.http.HttpResponse) Test(org.junit.Test) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) LocationFactory(org.apache.twill.filesystem.LocationFactory) ProgramType(io.cdap.cdap.proto.ProgramType) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) AppJarHelper(io.cdap.cdap.common.test.AppJarHelper) Gson(com.google.gson.Gson) ContentProvider(io.cdap.common.ContentProvider) HttpRequests(io.cdap.common.http.HttpRequests) URI(java.net.URI) AllProgramsApp(io.cdap.cdap.AllProgramsApp) Assert(org.junit.Assert) HttpRequest(io.cdap.common.http.HttpRequest) InputStream(java.io.InputStream) ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) Gson(com.google.gson.Gson) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) AllProgramsApp(io.cdap.cdap.AllProgramsApp) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) URI(java.net.URI) URL(java.net.URL) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) LocationFactory(org.apache.twill.filesystem.LocationFactory) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 12 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by caskdata.

the class AppFabricTestBase method initializeAndStartServices.

protected static void initializeAndStartServices(CConfiguration cConf, Module overrides) throws Exception {
    injector = Guice.createInjector(Modules.override(new AppFabricTestModule(cConf, null)).with(overrides));
    int connectionTimeout = cConf.getInt(Constants.HTTP_CLIENT_CONNECTION_TIMEOUT_MS);
    int readTimeout = cConf.getInt(Constants.HTTP_CLIENT_READ_TIMEOUT_MS);
    httpRequestConfig = new HttpRequestConfig(connectionTimeout, readTimeout, false);
    messagingService = injector.getInstance(MessagingService.class);
    if (messagingService instanceof Service) {
        ((Service) messagingService).startAndWait();
    }
    txManager = injector.getInstance(TransactionManager.class);
    txManager.startAndWait();
    // Define all StructuredTable before starting any services that need StructuredTable
    StoreDefinition.createAllTables(injector.getInstance(StructuredTableAdmin.class));
    metadataStorage = injector.getInstance(MetadataStorage.class);
    metadataStorage.createIndex();
    dsOpService = injector.getInstance(DatasetOpExecutorService.class);
    dsOpService.startAndWait();
    datasetService = injector.getInstance(DatasetService.class);
    datasetService.startAndWait();
    appFabricServer = injector.getInstance(AppFabricServer.class);
    appFabricServer.startAndWait();
    DiscoveryServiceClient discoveryClient = injector.getInstance(DiscoveryServiceClient.class);
    appFabricEndpointStrategy = new RandomEndpointStrategy(() -> discoveryClient.discover(Constants.Service.APP_FABRIC_HTTP));
    txClient = injector.getInstance(TransactionSystemClient.class);
    metricsCollectionService = injector.getInstance(MetricsCollectionService.class);
    metricsCollectionService.startAndWait();
    serviceStore = injector.getInstance(ServiceStore.class);
    serviceStore.startAndWait();
    metadataService = injector.getInstance(MetadataService.class);
    metadataService.startAndWait();
    metadataSubscriberService = injector.getInstance(MetadataSubscriberService.class);
    metadataSubscriberService.startAndWait();
    logQueryService = injector.getInstance(LogQueryService.class);
    logQueryService.startAndWait();
    locationFactory = getInjector().getInstance(LocationFactory.class);
    datasetClient = new DatasetClient(getClientConfig(discoveryClient, Constants.Service.DATASET_MANAGER));
    remoteClientFactory = new RemoteClientFactory(discoveryClient, new DefaultInternalAuthenticator(new AuthenticationTestContext()));
    metadataClient = new MetadataClient(getClientConfig(discoveryClient, Constants.Service.METADATA_SERVICE));
    metadataServiceClient = new DefaultMetadataServiceClient(remoteClientFactory);
    metricStore = injector.getInstance(MetricStore.class);
    Scheduler programScheduler = injector.getInstance(Scheduler.class);
    // Wait for the scheduler to be functional.
    if (programScheduler instanceof CoreSchedulerService) {
        try {
            ((CoreSchedulerService) programScheduler).waitUntilFunctional(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    createNamespaces();
}
Also used : MetricStore(io.cdap.cdap.api.metrics.MetricStore) RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) StructuredTableAdmin(io.cdap.cdap.spi.data.StructuredTableAdmin) Scheduler(io.cdap.cdap.scheduler.Scheduler) AppFabricServer(io.cdap.cdap.internal.app.services.AppFabricServer) DatasetService(io.cdap.cdap.data2.datafabric.dataset.service.DatasetService) MetadataService(io.cdap.cdap.metadata.MetadataService) LogQueryService(io.cdap.cdap.logging.service.LogQueryService) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) MetadataClient(io.cdap.cdap.client.MetadataClient) DefaultMetadataServiceClient(io.cdap.cdap.data2.metadata.writer.DefaultMetadataServiceClient) ServiceStore(io.cdap.cdap.app.store.ServiceStore) CoreSchedulerService(io.cdap.cdap.scheduler.CoreSchedulerService) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) AuthenticationTestContext(io.cdap.cdap.security.auth.context.AuthenticationTestContext) MetadataSubscriberService(io.cdap.cdap.metadata.MetadataSubscriberService) DatasetOpExecutorService(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService) Service(com.google.common.util.concurrent.Service) MetadataService(io.cdap.cdap.metadata.MetadataService) MessagingService(io.cdap.cdap.messaging.MessagingService) DatasetService(io.cdap.cdap.data2.datafabric.dataset.service.DatasetService) LogQueryService(io.cdap.cdap.logging.service.LogQueryService) CoreSchedulerService(io.cdap.cdap.scheduler.CoreSchedulerService) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) NotFoundException(io.cdap.cdap.common.NotFoundException) UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) MessagingService(io.cdap.cdap.messaging.MessagingService) LocationFactory(org.apache.twill.filesystem.LocationFactory) DatasetClient(io.cdap.cdap.client.DatasetClient) DefaultInternalAuthenticator(io.cdap.cdap.common.internal.remote.DefaultInternalAuthenticator) TransactionManager(org.apache.tephra.TransactionManager) DatasetOpExecutorService(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService) MetadataSubscriberService(io.cdap.cdap.metadata.MetadataSubscriberService) MetadataStorage(io.cdap.cdap.spi.metadata.MetadataStorage) AppFabricTestModule(io.cdap.cdap.internal.guice.AppFabricTestModule) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 13 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by caskdata.

the class ArtifactCacheTest method testArtifactCache.

@Test
public void testArtifactCache() throws Exception {
    LocationFactory locationFactory = getInjector().getInstance(LocationFactory.class);
    ArtifactRepository artifactRepository = getInjector().getInstance(ArtifactRepository.class);
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "some-task", "1.0.0-SNAPSHOT");
    Location appJar = AppJarHelper.createDeploymentJar(locationFactory, TaskWorkerServiceTest.TestRunnableClass.class);
    File appJarFile = new File(tmpFolder.newFolder(), String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
    Locations.linkOrCopy(appJar, appJarFile);
    artifactRepository.addArtifact(artifactId, appJarFile);
    CConfiguration cConf = CConfiguration.create();
    String cacheDir = tmpFolder.toString();
    cConf.set(Constants.ArtifactCache.LOCAL_DATA_DIR, cacheDir);
    TetheringStore store = getInjector().getInstance(TetheringStore.class);
    ArtifactCache cache = new ArtifactCache(cConf);
    // Add a couple of tethered peers
    addPeer(store, "peer1");
    addPeer(store, "peer2");
    RemoteClientFactory factory = new RemoteClientFactory(new NoOpDiscoveryServiceClient(getEndPoint("").toString()), new NoOpInternalAuthenticator());
    HttpRequestConfig config = new DefaultHttpRequestConfig(true);
    RemoteClient remoteClient = factory.createRemoteClient("", config, Constants.Gateway.INTERNAL_API_VERSION_3);
    // Get artifact from first peer
    File peer1ArtifactPath = cache.getArtifact(artifactId.toEntityId(), "peer1", remoteClient);
    // Get the artifact again. The same path was returned
    Assert.assertEquals(peer1ArtifactPath, cache.getArtifact(artifactId.toEntityId(), "peer1", remoteClient));
    // Get artifact from another peer. It should be cached in a different path
    File peer2ArtifactPath = cache.getArtifact(artifactId.toEntityId(), "peer2", remoteClient);
    Assert.assertNotEquals(peer1ArtifactPath, peer2ArtifactPath);
    // Delete and recreate the artifact to update the last modified date
    artifactRepository.deleteArtifact(artifactId);
    // This sleep is needed to delay the file copy so that the lastModified time on the file is different
    Thread.sleep(1000);
    artifactRepository.addArtifact(artifactId, appJarFile);
    // Artifact should be cached in a different path
    File newPeer1ArtifactPath = cache.getArtifact(artifactId.toEntityId(), "peer1", remoteClient);
    Assert.assertNotEquals(peer1ArtifactPath, newPeer1ArtifactPath);
    // Run the artifact cleaner
    ArtifactLocalizerCleaner cleaner = new ArtifactLocalizerCleaner(Paths.get(cacheDir).resolve("peers"), 1);
    cleaner.run();
    // Older artifact should been deleted
    Assert.assertFalse(peer1ArtifactPath.exists());
    // Latest artifact should still be cached
    Assert.assertTrue(newPeer1ArtifactPath.exists());
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) NoOpInternalAuthenticator(io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) TaskWorkerServiceTest(io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) LocationFactory(org.apache.twill.filesystem.LocationFactory) ArtifactLocalizerCleaner(io.cdap.cdap.internal.app.worker.sidecar.ArtifactLocalizerCleaner) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) Id(io.cdap.cdap.common.id.Id) File(java.io.File) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test) TaskWorkerServiceTest(io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest)

Example 14 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by caskdata.

the class SpillableBodyConsumerTest method testPost.

private void testPost(String body, int bufferLimit) throws Exception {
    NettyHttpService httpService = NettyHttpService.builder("test").setHttpHandlers(new TestHandler(bufferLimit)).build();
    httpService.start();
    try {
        InetSocketAddress addr = httpService.getBindAddress();
        URL url = new URL(String.format("http://%s:%d/post", addr.getHostName(), addr.getPort()));
        HttpResponse response = HttpRequests.execute(io.cdap.common.http.HttpRequest.post(url).withBody(body).build(), new HttpRequestConfig(1000, 10000000));
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals(body, response.getResponseBodyAsString());
    } finally {
        httpService.stop();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NettyHttpService(io.cdap.http.NettyHttpService) HttpResponse(io.cdap.common.http.HttpResponse) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) URL(java.net.URL)

Example 15 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by caskdata.

the class SupportBundleTestBase method initializeAndStartServices.

protected static void initializeAndStartServices(CConfiguration cConf, Module overrides) throws Exception {
    injector = Guice.createInjector(Modules.override(new AppFabricTestModule(cConf, null)).with(overrides));
    int connectionTimeout = cConf.getInt(Constants.HTTP_CLIENT_CONNECTION_TIMEOUT_MS);
    int readTimeout = cConf.getInt(Constants.HTTP_CLIENT_READ_TIMEOUT_MS);
    httpRequestConfig = new HttpRequestConfig(connectionTimeout, readTimeout, false);
    messagingService = injector.getInstance(MessagingService.class);
    if (messagingService instanceof Service) {
        ((Service) messagingService).startAndWait();
    }
    txManager = injector.getInstance(TransactionManager.class);
    txManager.startAndWait();
    // Define all StructuredTable before starting any services that need StructuredTable
    StoreDefinition.createAllTables(injector.getInstance(StructuredTableAdmin.class));
    metadataStorage = injector.getInstance(MetadataStorage.class);
    metadataStorage.createIndex();
    dsOpService = injector.getInstance(DatasetOpExecutorService.class);
    dsOpService.startAndWait();
    datasetService = injector.getInstance(DatasetService.class);
    datasetService.startAndWait();
    appFabricServer = injector.getInstance(AppFabricServer.class);
    appFabricServer.startAndWait();
    DiscoveryServiceClient discoveryClient = injector.getInstance(DiscoveryServiceClient.class);
    appFabricEndpointStrategy = new RandomEndpointStrategy(() -> discoveryClient.discover(Constants.Service.APP_FABRIC_HTTP));
    metricsCollectionService = injector.getInstance(MetricsCollectionService.class);
    metricsCollectionService.startAndWait();
    serviceStore = injector.getInstance(ServiceStore.class);
    serviceStore.startAndWait();
    metadataService = injector.getInstance(MetadataService.class);
    metadataService.startAndWait();
    metadataSubscriberService = injector.getInstance(MetadataSubscriberService.class);
    metadataSubscriberService.startAndWait();
    logQueryService = injector.getInstance(LogQueryService.class);
    logQueryService.startAndWait();
    locationFactory = getInjector().getInstance(LocationFactory.class);
    datasetClient = new DatasetClient(getClientConfig(discoveryClient, Constants.Service.DATASET_MANAGER));
    remoteClientFactory = new RemoteClientFactory(discoveryClient, new DefaultInternalAuthenticator(new AuthenticationTestContext()));
    metadataClient = new MetadataClient(getClientConfig(discoveryClient, Constants.Service.METADATA_SERVICE));
    appFabricHealthCheckService = injector.getInstance(HealthCheckService.class);
    appFabricHealthCheckService.helper(Constants.AppFabricHealthCheck.APP_FABRIC_HEALTH_CHECK_SERVICE, cConf, Constants.Service.MASTER_SERVICES_BIND_ADDRESS);
    appFabricHealthCheckService.startAndWait();
    supportBundleInternalService = injector.getInstance(SupportBundleInternalService.class);
    supportBundleInternalService.startAndWait();
    Scheduler programScheduler = injector.getInstance(Scheduler.class);
    // Wait for the scheduler to be functional.
    if (programScheduler instanceof CoreSchedulerService) {
        try {
            ((CoreSchedulerService) programScheduler).waitUntilFunctional(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) StructuredTableAdmin(io.cdap.cdap.spi.data.StructuredTableAdmin) Scheduler(io.cdap.cdap.scheduler.Scheduler) AppFabricServer(io.cdap.cdap.internal.app.services.AppFabricServer) DatasetService(io.cdap.cdap.data2.datafabric.dataset.service.DatasetService) MetadataService(io.cdap.cdap.metadata.MetadataService) LogQueryService(io.cdap.cdap.logging.service.LogQueryService) MetadataClient(io.cdap.cdap.client.MetadataClient) ServiceStore(io.cdap.cdap.app.store.ServiceStore) CoreSchedulerService(io.cdap.cdap.scheduler.CoreSchedulerService) HealthCheckService(io.cdap.cdap.common.service.HealthCheckService) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) AuthenticationTestContext(io.cdap.cdap.security.auth.context.AuthenticationTestContext) MetadataSubscriberService(io.cdap.cdap.metadata.MetadataSubscriberService) DatasetOpExecutorService(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService) Service(com.google.common.util.concurrent.Service) MetadataService(io.cdap.cdap.metadata.MetadataService) HealthCheckService(io.cdap.cdap.common.service.HealthCheckService) MessagingService(io.cdap.cdap.messaging.MessagingService) SupportBundleInternalService(io.cdap.cdap.support.internal.app.services.SupportBundleInternalService) DatasetService(io.cdap.cdap.data2.datafabric.dataset.service.DatasetService) LogQueryService(io.cdap.cdap.logging.service.LogQueryService) CoreSchedulerService(io.cdap.cdap.scheduler.CoreSchedulerService) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) IOException(java.io.IOException) MessagingService(io.cdap.cdap.messaging.MessagingService) LocationFactory(org.apache.twill.filesystem.LocationFactory) DatasetClient(io.cdap.cdap.client.DatasetClient) SupportBundleInternalService(io.cdap.cdap.support.internal.app.services.SupportBundleInternalService) DefaultInternalAuthenticator(io.cdap.cdap.common.internal.remote.DefaultInternalAuthenticator) TransactionManager(org.apache.tephra.TransactionManager) DatasetOpExecutorService(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService) MetadataSubscriberService(io.cdap.cdap.metadata.MetadataSubscriberService) MetadataStorage(io.cdap.cdap.spi.metadata.MetadataStorage) AppFabricTestModule(io.cdap.cdap.internal.guice.AppFabricTestModule) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Aggregations

HttpRequestConfig (io.cdap.common.http.HttpRequestConfig)15 Test (org.junit.Test)8 RemoteClientFactory (io.cdap.cdap.common.internal.remote.RemoteClientFactory)7 HttpResponse (io.cdap.common.http.HttpResponse)7 URL (java.net.URL)7 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)5 RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)5 HttpURLConnection (java.net.HttpURLConnection)4 LocationFactory (org.apache.twill.filesystem.LocationFactory)4 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)3 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)3 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)3 Location (org.apache.twill.filesystem.Location)3 Service (com.google.common.util.concurrent.Service)2 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)2 ServiceStore (io.cdap.cdap.app.store.ServiceStore)2 DatasetClient (io.cdap.cdap.client.DatasetClient)2 MetadataClient (io.cdap.cdap.client.MetadataClient)2 DefaultInternalAuthenticator (io.cdap.cdap.common.internal.remote.DefaultInternalAuthenticator)2 DatasetService (io.cdap.cdap.data2.datafabric.dataset.service.DatasetService)2