Search in sources :

Example 1 with EndpointStrategy

use of io.cdap.cdap.common.discovery.EndpointStrategy 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)

Example 2 with EndpointStrategy

use of io.cdap.cdap.common.discovery.EndpointStrategy 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 3 with EndpointStrategy

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

the class DefaultSecureStoreServiceTest method waitForService.

private static void waitForService(String service) {
    EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(service));
    Preconditions.checkNotNull(endpointStrategy.pick(5, TimeUnit.SECONDS), "%s service is not up after 5 seconds", service);
}
Also used : RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 4 with EndpointStrategy

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

the class AppFabricTestBase method getClientConfig.

private static ClientConfig getClientConfig(DiscoveryServiceClient discoveryClient, String service) {
    EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryClient.discover(service));
    Discoverable discoverable = endpointStrategy.pick(1, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverable);
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(discoverable.getSocketAddress().getHostName()).setPort(discoverable.getSocketAddress().getPort()).setSSLEnabled(URIScheme.HTTPS.isMatch(discoverable)).build();
    return ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connectionConfig).build();
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 5 with EndpointStrategy

use of io.cdap.cdap.common.discovery.EndpointStrategy 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)

Aggregations

EndpointStrategy (io.cdap.cdap.common.discovery.EndpointStrategy)14 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)13 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)6 Discoverable (org.apache.twill.discovery.Discoverable)5 Injector (com.google.inject.Injector)4 AbstractModule (com.google.inject.AbstractModule)3 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)3 ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)3 SConfiguration (io.cdap.cdap.common.conf.SConfiguration)3 ConfigModule (io.cdap.cdap.common.guice.ConfigModule)3 InMemoryDiscoveryModule (io.cdap.cdap.common.guice.InMemoryDiscoveryModule)3 DatasetService (io.cdap.cdap.data2.datafabric.dataset.service.DatasetService)3 DatasetOpExecutorService (io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService)3 AuthenticationContextModules (io.cdap.cdap.security.auth.context.AuthenticationContextModules)3 AuthorizationEnforcementModule (io.cdap.cdap.security.authorization.AuthorizationEnforcementModule)3 StructuredTableAdmin (io.cdap.cdap.spi.data.StructuredTableAdmin)3 TransactionManager (org.apache.tephra.TransactionManager)3 Service (com.google.common.util.concurrent.Service)2 DataSetServiceModules (io.cdap.cdap.data.runtime.DataSetServiceModules)2 DataSetsModules (io.cdap.cdap.data.runtime.DataSetsModules)2