Search in sources :

Example 21 with Discoverable

use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.

the class AbstractDistributedMasterServiceManager method isServiceAvailable.

@Override
public boolean isServiceAvailable() {
    String url = null;
    try {
        Iterable<Discoverable> discoverables = this.discoveryServiceClient.discover(getDiscoverableName());
        for (Discoverable discoverable : discoverables) {
            String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME;
            url = String.format("%s%s:%d/ping", scheme, discoverable.getSocketAddress().getHostName(), discoverable.getSocketAddress().getPort());
            //Ping the discovered service to check its status.
            if (checkGetStatus(url).equals(HttpResponseStatus.OK)) {
                return true;
            }
        }
        return false;
    } catch (IllegalArgumentException e) {
        return false;
    } catch (Exception e) {
        LOG.warn("Unable to ping {} at {} : Reason : {}", serviceName, url, e.getMessage());
        return false;
    }
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) SocketTimeoutException(java.net.SocketTimeoutException)

Example 22 with Discoverable

use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.

the class ResourceBalancerService method createDiscoverable.

private Discoverable createDiscoverable(final String serviceName) {
    InetSocketAddress address;
    // NOTE: at this moment we are not using port anywhere
    int port = Networks.getRandomPort();
    try {
        address = new InetSocketAddress(InetAddress.getLocalHost(), port);
    } catch (UnknownHostException e) {
        address = new InetSocketAddress(port);
    }
    return new Discoverable(serviceName, address);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress)

Example 23 with Discoverable

use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.

the class MetadataService method startUp.

@Override
protected void startUp() throws Exception {
    LOG.info("Starting Metadata Service");
    metadataUpgrader.createOrUpgradeIfNecessary();
    httpService = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.METADATA_SERVICE).addHttpHandlers(handlers).setHandlerHooks(ImmutableList.of(new MetricsReporterHook(metricsCollectionService, Constants.Service.METADATA_SERVICE))).setHost(cConf.get(Constants.Metadata.SERVICE_BIND_ADDRESS)).setPort(cConf.getInt(Constants.Metadata.SERVICE_BIND_PORT)).setWorkerThreadPoolSize(cConf.getInt(Constants.Metadata.SERVICE_WORKER_THREADS)).setExecThreadPoolSize(cConf.getInt(Constants.Metadata.SERVICE_EXEC_THREADS)).setConnectionBacklog(20000).build();
    httpService.addListener(new ServiceListenerAdapter() {

        private Cancellable cancellable;

        @Override
        public void running() {
            final InetSocketAddress socketAddress = httpService.getBindAddress();
            LOG.info("Metadata service running at {}", socketAddress);
            cancellable = discoveryService.register(ResolvingDiscoverable.of(new Discoverable(Constants.Service.METADATA_SERVICE, socketAddress)));
        }

        @Override
        public void terminated(State from) {
            LOG.info("Metadata HTTP service stopped");
            cancellable.cancel();
        }

        @Override
        public void failed(State from, Throwable failure) {
            LOG.info("Metadata HTTP service stopped with failure.", failure);
            cancellable.cancel();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    httpService.startAndWait();
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) MetricsReporterHook(co.cask.cdap.common.metrics.MetricsReporterHook) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) Cancellable(org.apache.twill.common.Cancellable) InetSocketAddress(java.net.InetSocketAddress) ServiceListenerAdapter(org.apache.twill.internal.ServiceListenerAdapter)

Example 24 with Discoverable

use of org.apache.twill.discovery.Discoverable in project cdap by caskdata.

the class ArtifactHttpHandlerTest method setup.

@BeforeClass
public static void setup() throws IOException {
    artifactRepository = getInjector().getInstance(ArtifactRepository.class);
    DiscoveryServiceClient discoveryClient = getInjector().getInstance(DiscoveryServiceClient.class);
    ServiceDiscovered metadataHttpDiscovered = discoveryClient.discover(Constants.Service.METADATA_SERVICE);
    EndpointStrategy endpointStrategy = new RandomEndpointStrategy(metadataHttpDiscovered);
    Discoverable discoverable = endpointStrategy.pick(1, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverable);
    String host = "127.0.0.1";
    int port = discoverable.getSocketAddress().getPort();
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(host).setPort(port).build();
    clientConfig = ClientConfig.builder().setConnectionConfig(connectionConfig).build();
    metadataClient = new MetadataClient(clientConfig);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) MetadataClient(co.cask.cdap.client.MetadataClient) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) EndpointStrategy(co.cask.cdap.common.discovery.EndpointStrategy) RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy) ArtifactRepository(co.cask.cdap.internal.app.runtime.artifact.ArtifactRepository) ServiceDiscovered(org.apache.twill.discovery.ServiceDiscovered) ConnectionConfig(co.cask.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy) BeforeClass(org.junit.BeforeClass)

Example 25 with Discoverable

use of org.apache.twill.discovery.Discoverable 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);
    int port = discoverable.getSocketAddress().getPort();
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(hostname).setPort(port).build();
    return ClientConfig.builder().setConnectionConfig(connectionConfig).build();
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(co.cask.cdap.common.discovery.EndpointStrategy) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) ConnectionConfig(co.cask.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy)

Aggregations

Discoverable (org.apache.twill.discovery.Discoverable)50 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)17 InetSocketAddress (java.net.InetSocketAddress)14 Test (org.junit.Test)14 RandomEndpointStrategy (co.cask.cdap.common.discovery.RandomEndpointStrategy)10 ServiceLoggingContext (co.cask.cdap.common.logging.ServiceLoggingContext)8 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)7 EndpointStrategy (co.cask.cdap.common.discovery.EndpointStrategy)6 Cancellable (org.apache.twill.common.Cancellable)6 IOException (java.io.IOException)5 CommonNettyHttpServiceBuilder (co.cask.cdap.common.http.CommonNettyHttpServiceBuilder)4 JsonObject (com.google.gson.JsonObject)4 ArrayList (java.util.ArrayList)4 ProgramDescriptor (co.cask.cdap.app.program.ProgramDescriptor)3 ProgramController (co.cask.cdap.app.runtime.ProgramController)3 ServiceDiscoverable (co.cask.cdap.common.service.ServiceDiscoverable)3 ApplicationWithPrograms (co.cask.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms)3 BasicArguments (co.cask.cdap.internal.app.runtime.BasicArguments)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 ProgramId (co.cask.cdap.proto.id.ProgramId)3