Search in sources :

Example 6 with RandomEndpointStrategy

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

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)

Example 7 with RandomEndpointStrategy

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

the class LogsServiceMainTest method doGet.

private HttpResponse doGet(String path, DiscoveryServiceClient discoveryServiceClient, String serviceName) throws IOException {
    Discoverable discoverable = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(serviceName)).pick(10, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverable);
    URL url = URIScheme.createURI(discoverable, path).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 8 with RandomEndpointStrategy

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

the class ProgramLifecycleHttpHandler method getServiceAvailability.

/**
 * Return the availability (i.e. discoverable registration) status of a service.
 */
@GET
@Path("/apps/{app-name}/versions/{app-version}/{service-type}/{program-name}/available")
public void getServiceAvailability(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("app-version") String appVersion, @PathParam("service-type") String serviceType, @PathParam("program-name") String programName) throws Exception {
    // Currently we only support services and sparks as the service-type
    ProgramType programType = getProgramType(serviceType);
    if (!ServiceDiscoverable.getUserServiceTypes().contains(programType)) {
        throw new BadRequestException("Only service or spark is support for service availability check");
    }
    ProgramId programId = new ProgramId(new ApplicationId(namespaceId, appName, appVersion), programType, programName);
    ProgramStatus status = lifecycleService.getProgramStatus(programId);
    if (status == ProgramStatus.STOPPED) {
        throw new ServiceUnavailableException(programId.toString(), "Service is stopped. Please start it.");
    }
    // Construct discoverable name and return 200 OK if discoverable is present. If not return 503.
    String discoverableName = ServiceDiscoverable.getName(programId);
    // TODO: CDAP-12959 - Should use the UserServiceEndpointStrategy and discover based on the version
    // and have appVersion nullable for the non versioned endpoint
    EndpointStrategy strategy = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(discoverableName));
    if (strategy.pick(300L, TimeUnit.MILLISECONDS) == null) {
        LOG.trace("Discoverable endpoint {} not found", discoverableName);
        throw new ServiceUnavailableException(programId.toString(), "Service is running but not accepting requests at this time.");
    }
    responder.sendString(HttpResponseStatus.OK, "Service is available to accept requests.");
}
Also used : RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) BadRequestException(io.cdap.cdap.common.BadRequestException) ProgramType(io.cdap.cdap.proto.ProgramType) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) ProgramId(io.cdap.cdap.proto.id.ProgramId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) BatchProgramStatus(io.cdap.cdap.proto.BatchProgramStatus) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 9 with RandomEndpointStrategy

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

the class AppFabricServerTest method testSSL.

@Test
public void testSSL() throws IOException {
    CConfiguration cConf = CConfiguration.create();
    cConf.setBoolean(Constants.Security.SSL.INTERNAL_ENABLED, true);
    SConfiguration sConf = SConfiguration.create();
    final Injector injector = AppFabricTestHelper.getInjector(cConf, sConf);
    try {
        final DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
        AppFabricServer appFabricServer = injector.getInstance(AppFabricServer.class);
        appFabricServer.startAndWait();
        Assert.assertTrue(appFabricServer.isRunning());
        Supplier<EndpointStrategy> endpointStrategySupplier = Suppliers.memoize(() -> new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.APP_FABRIC_HTTP)))::get;
        Discoverable discoverable = endpointStrategySupplier.get().pick(3, TimeUnit.SECONDS);
        Assert.assertNotNull(discoverable);
        Assert.assertTrue(URIScheme.HTTPS.isMatch(discoverable));
        InetSocketAddress addr = discoverable.getSocketAddress();
        // Since the server uses a self signed certificate we need a client that trusts all certificates
        SSLSocket socket = (SSLSocket) LDAPLoginModule.TrustAllSSLSocketFactory.getDefault().createSocket(addr.getHostName(), addr.getPort());
        // in millis
        socket.setSoTimeout(5000);
        // Would throw exception if the server does not support ssl.
        // "javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"
        socket.startHandshake();
        appFabricServer.stopAndWait();
    } finally {
        AppFabricTestHelper.shutdown();
    }
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) Injector(com.google.inject.Injector) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) SConfiguration(io.cdap.cdap.common.conf.SConfiguration) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Test(org.junit.Test)

Example 10 with RandomEndpointStrategy

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

the class AppFabricServerTest method startStopServer.

@Test
public void startStopServer() throws Exception {
    Injector injector = AppFabricTestHelper.getInjector();
    try {
        AppFabricServer server = injector.getInstance(AppFabricServer.class);
        DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
        Service.State state = server.startAndWait();
        Assert.assertSame(state, Service.State.RUNNING);
        final EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.APP_FABRIC_HTTP));
        Assert.assertNotNull(endpointStrategy.pick(5, TimeUnit.SECONDS));
        state = server.stopAndWait();
        Assert.assertSame(state, Service.State.TERMINATED);
        Tasks.waitFor(true, () -> endpointStrategy.pick() == null, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    } finally {
        AppFabricTestHelper.shutdown();
    }
}
Also used : DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) Injector(com.google.inject.Injector) Service(com.google.common.util.concurrent.Service) 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