Search in sources :

Example 16 with RandomEndpointStrategy

use of io.cdap.cdap.common.discovery.RandomEndpointStrategy in project cdap by caskdata.

the class SupportBundleHttpHandlerTest method requestExportBundle.

/**
 * Requests zip and download support bundle.
 *
 * @param bundleId String
 * @return HTTPResponse
 * @throws IOException if failed to request bundle download
 */
private HttpResponse requestExportBundle(String bundleId) throws IOException {
    DiscoveryServiceClient discoveryServiceClient = getInjector().getInstance(DiscoveryServiceClient.class);
    Discoverable discoverable = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.SUPPORT_BUNDLE_SERVICE)).pick(5, TimeUnit.SECONDS);
    Assert.assertNotNull("No service for support bundle", discoverable);
    String path = String.format("%s/support/bundles/%s", Constants.Gateway.API_VERSION_3, bundleId);
    HttpRequest request = HttpRequest.post(URIScheme.createURI(discoverable, path).toURL()).build();
    return HttpRequests.execute(request, new DefaultHttpRequestConfig(false));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 17 with RandomEndpointStrategy

use of io.cdap.cdap.common.discovery.RandomEndpointStrategy in project cdap by cdapio.

the class LogHttpHandlerTest method doGet.

/**
 * Performs a get call on the given path from the log query server.
 */
private HttpResponse doGet(String path) throws IOException {
    Discoverable discoverable = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.LOG_QUERY)).pick(10, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverable);
    // Path is literal, hence replacing the "%" with "%%" for formatter
    URL url = URIScheme.createURI(discoverable, path.replace("%", "%%")).toURL();
    return HttpRequests.execute(HttpRequest.get(url).build(), new DefaultHttpRequestConfig(false));
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) URL(java.net.URL) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 18 with RandomEndpointStrategy

use of io.cdap.cdap.common.discovery.RandomEndpointStrategy in project cdap by cdapio.

the class MetricsSuiteTestBase method startMetricsService.

public static Injector startMetricsService(CConfiguration conf) throws Exception {
    Injector injector = Guice.createInjector(Modules.override(new ConfigModule(conf), RemoteAuthenticatorModules.getNoOpModule(), new NonCustomLocationUnitTestModule(), new InMemoryDiscoveryModule(), new MetricsHandlerModule(), new MetricsClientRuntimeModule().getInMemoryModules(), new DataFabricModules().getInMemoryModules(), new DataSetsModules().getStandaloneModules(), new DataSetServiceModules().getInMemoryModules(), new ExploreClientModule(), new NamespaceAdminTestModule(), new AuthorizationTestModule(), new AuthorizationEnforcementModule().getInMemoryModules(), new AuthenticationContextModules().getMasterModule()).with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Store.class).to(DefaultStore.class);
            bind(UGIProvider.class).to(UnsupportedUGIProvider.class);
            bind(OwnerAdmin.class).to(NoOpOwnerAdmin.class);
            // TODO (CDAP-14677): find a better way to inject metadata publisher
            bind(MetadataServiceClient.class).to(NoOpMetadataServiceClient.class);
        }
    }));
    transactionManager = injector.getInstance(TransactionManager.class);
    transactionManager.startAndWait();
    StoreDefinition.createAllTables(injector.getInstance(StructuredTableAdmin.class));
    dsOpService = injector.getInstance(DatasetOpExecutorService.class);
    dsOpService.startAndWait();
    datasetService = injector.getInstance(DatasetService.class);
    datasetService.startAndWait();
    metrics = injector.getInstance(MetricsQueryService.class);
    metrics.startAndWait();
    collectionService = injector.getInstance(MetricsCollectionService.class);
    collectionService.startAndWait();
    // initialize the dataset instantiator
    DiscoveryServiceClient discoveryClient = injector.getInstance(DiscoveryServiceClient.class);
    EndpointStrategy metricsEndPoints = new RandomEndpointStrategy(() -> discoveryClient.discover(Constants.Service.METRICS));
    discoverable = metricsEndPoints.pick(1L, TimeUnit.SECONDS);
    Assert.assertNotNull("Could not discover metrics service", discoverable);
    return injector;
}
Also used : InMemoryDiscoveryModule(io.cdap.cdap.common.guice.InMemoryDiscoveryModule) DataSetServiceModules(io.cdap.cdap.data.runtime.DataSetServiceModules) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) NamespaceAdminTestModule(io.cdap.cdap.common.guice.NamespaceAdminTestModule) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) StructuredTableAdmin(io.cdap.cdap.spi.data.StructuredTableAdmin) UGIProvider(io.cdap.cdap.security.impersonation.UGIProvider) UnsupportedUGIProvider(io.cdap.cdap.security.impersonation.UnsupportedUGIProvider) MetricStore(io.cdap.cdap.api.metrics.MetricStore) Store(io.cdap.cdap.app.store.Store) DefaultStore(io.cdap.cdap.internal.app.store.DefaultStore) DatasetService(io.cdap.cdap.data2.datafabric.dataset.service.DatasetService) MetricsClientRuntimeModule(io.cdap.cdap.metrics.guice.MetricsClientRuntimeModule) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) Injector(com.google.inject.Injector) MetricsQueryService(io.cdap.cdap.metrics.query.MetricsQueryService) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) AuthenticationContextModules(io.cdap.cdap.security.auth.context.AuthenticationContextModules) DataSetsModules(io.cdap.cdap.data.runtime.DataSetsModules) NoOpOwnerAdmin(io.cdap.cdap.security.impersonation.NoOpOwnerAdmin) OwnerAdmin(io.cdap.cdap.security.impersonation.OwnerAdmin) NonCustomLocationUnitTestModule(io.cdap.cdap.common.guice.NonCustomLocationUnitTestModule) AuthorizationTestModule(io.cdap.cdap.security.authorization.AuthorizationTestModule) AbstractModule(com.google.inject.AbstractModule) MetadataServiceClient(io.cdap.cdap.data2.metadata.writer.MetadataServiceClient) NoOpMetadataServiceClient(io.cdap.cdap.data2.metadata.writer.NoOpMetadataServiceClient) MetricsHandlerModule(io.cdap.cdap.metrics.guice.MetricsHandlerModule) ExploreClientModule(io.cdap.cdap.explore.guice.ExploreClientModule) TransactionManager(org.apache.tephra.TransactionManager) DatasetOpExecutorService(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService) DataFabricModules(io.cdap.cdap.data.runtime.DataFabricModules) AuthorizationEnforcementModule(io.cdap.cdap.security.authorization.AuthorizationEnforcementModule) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 19 with RandomEndpointStrategy

use of io.cdap.cdap.common.discovery.RandomEndpointStrategy in project cdap by cdapio.

the class NettyRouterTestBase method testConnectionClose2.

@Test
public void testConnectionClose2() throws Exception {
    // turn off retry on POST request
    String oldValue = System.getProperty("sun.net.http.retryPost");
    System.setProperty("sun.net.http.retryPost", "false");
    try {
        URL url = new URL(resolveURI("/v1/sleep"));
        // Disable the server 2 from discovery
        defaultServer2.cancelRegistration();
        HttpURLConnection urlConn = openURL(url);
        urlConn.setDoOutput(true);
        urlConn.setRequestMethod("POST");
        // Sleep for 50 ms
        urlConn.getOutputStream().write("50".getBytes(StandardCharsets.UTF_8));
        Assert.assertEquals(200, urlConn.getResponseCode());
        urlConn.getInputStream().close();
        urlConn.disconnect();
        // Now disable server1 and enable server2 from discovery
        defaultServer1.cancelRegistration();
        defaultServer2.registerServer();
        // Make sure the discovery change is in effect
        Assert.assertNotNull(new RandomEndpointStrategy(() -> ((DiscoveryServiceClient) discoveryService).discover(APP_FABRIC_SERVICE)).pick(5, TimeUnit.SECONDS));
        // Make a call to sleep for couple seconds
        urlConn = openURL(url);
        urlConn.setDoOutput(true);
        urlConn.setRequestMethod("POST");
        urlConn.getOutputStream().write("3000".getBytes(StandardCharsets.UTF_8));
        // Wait for the result asynchronously, while at the same time shutdown server1.
        // Shutting down server1 shouldn't affect the connection.
        CompletableFuture<Integer> result = new CompletableFuture<>();
        HttpURLConnection finalUrlConn = urlConn;
        Thread t = new Thread(() -> {
            try {
                result.complete(finalUrlConn.getResponseCode());
            } catch (Exception e) {
                result.completeExceptionally(e);
            } finally {
                try {
                    finalUrlConn.getInputStream().close();
                    finalUrlConn.disconnect();
                } catch (IOException e) {
                    LOG.error("Exception when closing url connection", e);
                }
            }
        });
        t.start();
        defaultServer1.stopAndWait();
        Assert.assertEquals(200, result.get().intValue());
        Assert.assertEquals(1, defaultServer1.getNumRequests());
        Assert.assertEquals(1, defaultServer2.getNumRequests());
    } finally {
        if (oldValue == null) {
            System.clearProperty("sun.net.http.retryPost");
        } else {
            System.setProperty("sun.net.http.retryPost", oldValue);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Test(org.junit.Test)

Example 20 with RandomEndpointStrategy

use of io.cdap.cdap.common.discovery.RandomEndpointStrategy in project cdap by cdapio.

the class AuthenticationServiceMainTest method testBasicAuthenticationEnabled.

@Test
public void testBasicAuthenticationEnabled() throws IOException {
    HttpResponse response = HttpRequests.execute(HttpRequest.get(getAuthenticationBaseURI().toURL()).build(), new HttpRequestConfig(0, 0, false));
    Assert.assertEquals("basic realm=\"null\"", response.getHeaders().get("WWW-Authenticate").stream().findFirst().orElse(null));
    Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, response.getResponseCode());
    Injector injector = getServiceMainInstance(AuthenticationServiceMain.class).getInjector();
    DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
    Discoverable authenticationEndpoint = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Service.EXTERNAL_AUTHENTICATION)).pick(5, TimeUnit.SECONDS);
    Assert.assertNotNull(authenticationEndpoint);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) Injector(com.google.inject.Injector) HttpResponse(io.cdap.common.http.HttpResponse) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Test(org.junit.Test)

Aggregations

RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)66 Discoverable (org.apache.twill.discovery.Discoverable)38 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)38 EndpointStrategy (io.cdap.cdap.common.discovery.EndpointStrategy)26 Test (org.junit.Test)22 Injector (com.google.inject.Injector)20 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)12 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)12 DatasetService (io.cdap.cdap.data2.datafabric.dataset.service.DatasetService)12 StructuredTableAdmin (io.cdap.cdap.spi.data.StructuredTableAdmin)12 InetSocketAddress (java.net.InetSocketAddress)12 TransactionManager (org.apache.tephra.TransactionManager)12 ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)10 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)10 DatasetOpExecutorService (io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService)10 URL (java.net.URL)10 Service (com.google.common.util.concurrent.Service)8 AbstractModule (com.google.inject.AbstractModule)8 ConfigModule (io.cdap.cdap.common.guice.ConfigModule)8 InMemoryDiscoveryModule (io.cdap.cdap.common.guice.InMemoryDiscoveryModule)8