Search in sources :

Example 51 with Discoverable

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

the class TransactionHttpService method startUp.

@Override
protected void startUp() throws Exception {
    LOG.info("Starting Transaction HTTP Service...");
    httpService.start();
    // Register the service
    cancelDiscovery = discoveryService.register(ResolvingDiscoverable.of(new Discoverable(Constants.Service.TRANSACTION_HTTP, httpService.getBindAddress())));
    LOG.info("Transaction HTTP started successfully on {}", httpService.getBindAddress());
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable)

Example 52 with Discoverable

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

the class ThriftHelper method getThriftProtocol.

/**
 * generic method to discover a thrift service and start up the
 * thrift transport and protocol layer.
 */
public static TProtocol getThriftProtocol(String serviceName, EndpointStrategy endpointStrategy) throws ServerException {
    Discoverable endpoint = endpointStrategy.pick();
    if (endpoint == null) {
        String message = String.format("Service '%s' is not registered in discovery service.", serviceName);
        LOG.error(message);
        throw new ServerException(message);
    }
    TTransport transport = new TFramedTransport(new TSocket(endpoint.getSocketAddress().getHostName(), endpoint.getSocketAddress().getPort()));
    try {
        transport.open();
    } catch (TTransportException e) {
        String message = String.format("Unable to connect to thrift service %s at %s. Reason: %s", serviceName, endpoint.getSocketAddress(), e.getMessage());
        LOG.error(message);
        throw new ServerException(message, e);
    }
    // now try to connect the thrift client
    return new TBinaryProtocol(transport);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ServerException(co.cask.cdap.common.service.ServerException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransportException(org.apache.thrift.transport.TTransportException) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 53 with Discoverable

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

the class TransactionServiceManager method isServiceAvailable.

@Override
public boolean isServiceAvailable() {
    try {
        ServiceDiscovered discovered = discoveryServiceClient.discover(serviceName);
        Discoverable discoverable = new RandomEndpointStrategy(discovered).pick(discoveryTimeout, TimeUnit.SECONDS);
        if (discoverable == null) {
            return false;
        }
        return txClient.status().equals(Constants.Monitor.STATUS_OK);
    } catch (IllegalArgumentException e) {
        return false;
    } catch (Exception e) {
        LOG.warn("Unable to ping {} : Reason {} ", serviceName, e.getMessage());
        return false;
    }
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ServiceDiscovered(org.apache.twill.discovery.ServiceDiscovered) RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy)

Example 54 with Discoverable

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

the class AppFabricServer method startHttpService.

private Cancellable startHttpService(final NettyHttpService httpService) throws Exception {
    httpService.start();
    String announceAddress = cConf.get(Constants.Service.MASTER_SERVICES_ANNOUNCE_ADDRESS, httpService.getBindAddress().getHostName());
    int announcePort = cConf.getInt(Constants.AppFabric.SERVER_ANNOUNCE_PORT, httpService.getBindAddress().getPort());
    final InetSocketAddress socketAddress = new InetSocketAddress(announceAddress, announcePort);
    LOG.info("AppFabric HTTP Service announced at {}", socketAddress);
    // Tag the discoverable's payload to mark it as supporting ssl.
    byte[] sslPayload = sslEnabled ? Constants.Security.SSL_URI_SCHEME.getBytes() : Bytes.EMPTY_BYTE_ARRAY;
    // TODO accept a list of services, and start them here
    // When it is running, register it with service discovery
    final List<Cancellable> cancellables = new ArrayList<>();
    for (final String serviceName : servicesNames) {
        cancellables.add(discoveryService.register(ResolvingDiscoverable.of(new Discoverable(serviceName, socketAddress, sslPayload))));
    }
    return new Cancellable() {

        @Override
        public void cancel() {
            LOG.debug("Stopping AppFabric HTTP service.");
            for (Cancellable cancellable : cancellables) {
                if (cancellable != null) {
                    cancellable.cancel();
                }
            }
            try {
                httpService.stop();
            } catch (Exception e) {
                LOG.warn("Exception raised when stopping AppFabric HTTP service", e);
            }
            LOG.info("AppFabric HTTP service stopped.");
        }
    };
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) InetSocketAddress(java.net.InetSocketAddress) Cancellable(org.apache.twill.common.Cancellable) ArrayList(java.util.ArrayList)

Aggregations

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