Search in sources :

Example 6 with Discoverable

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

the class ServiceDiscoveredCodec method serialize.

@Override
public JsonElement serialize(ServiceDiscovered serviceDiscovered, Type typeOfSrc, JsonSerializationContext context) {
    JsonArray object = new JsonArray();
    for (Discoverable discoverable : serviceDiscovered) {
        JsonObject discoverableJson = new JsonObject();
        discoverableJson.addProperty("host", discoverable.getSocketAddress().getHostName());
        discoverableJson.addProperty("port", discoverable.getSocketAddress().getPort());
        object.add(discoverableJson);
    }
    return object;
}
Also used : JsonArray(com.google.gson.JsonArray) Discoverable(org.apache.twill.discovery.Discoverable) JsonObject(com.google.gson.JsonObject)

Example 7 with Discoverable

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

the class DiscoverableCodec method deserialize.

@Override
public Discoverable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String service = jsonObj.get("service").getAsString();
    String hostname = jsonObj.get("hostname").getAsString();
    int port = jsonObj.get("port").getAsInt();
    InetSocketAddress address = new InetSocketAddress(hostname, port);
    byte[] payload = context.deserialize(jsonObj.get("payload"), BYTE_ARRAY_TYPE);
    return new Discoverable(service, address, payload);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) InetSocketAddress(java.net.InetSocketAddress) JsonObject(com.google.gson.JsonObject)

Example 8 with Discoverable

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

the class StreamHttpService method startUp.

@Override
protected void startUp() throws Exception {
    LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getEntityName(), Constants.Logging.COMPONENT_NAME, Constants.Service.STREAMS));
    httpService.startAndWait();
    discoverable = ResolvingDiscoverable.of(new Discoverable(Constants.Service.STREAMS, httpService.getBindAddress()));
    cancellable = discoveryService.register(discoverable);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext)

Example 9 with Discoverable

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

the class RemoteSystemOperationsService method startUp.

@Override
protected void startUp() throws Exception {
    LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, Constants.Service.REMOTE_SYSTEM_OPERATION));
    LOG.info("Starting RemoteSystemOperationService...");
    httpService.startAndWait();
    cancellable = discoveryService.register(ResolvingDiscoverable.of(new Discoverable(Constants.Service.REMOTE_SYSTEM_OPERATION, httpService.getBindAddress())));
    LOG.info("RemoteSystemOperationService started successfully on {}", httpService.getBindAddress());
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext)

Example 10 with Discoverable

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

the class WebappProgramRunner method run.

@Override
public ProgramController run(Program program, ProgramOptions options) {
    try {
        ProgramType processorType = program.getType();
        Preconditions.checkNotNull(processorType, "Missing processor type");
        Preconditions.checkArgument(processorType == ProgramType.WEBAPP, "Only WEBAPP process type is supported");
        LOG.info("Initializing Webapp for app {} with jar {}", program.getApplicationId(), program.getJarLocation().getName());
        String serviceName = getServiceName(program.getId());
        Preconditions.checkNotNull(serviceName, "Cannot determine service name for program %s", program.getName());
        LOG.info("Got service name {}", serviceName);
        // Start netty server
        // TODO: add metrics reporting
        JarHttpHandler jarHttpHandler = webappHttpHandlerFactory.createHandler(program.getJarLocation());
        NettyHttpService.Builder builder = new CommonNettyHttpServiceBuilder(cConf, program.getId().toString());
        builder.addHttpHandlers(ImmutableSet.of(jarHttpHandler));
        builder.setUrlRewriter(new WebappURLRewriter(jarHttpHandler));
        builder.setHost(hostname.getCanonicalHostName());
        NettyHttpService httpService = builder.build();
        httpService.startAndWait();
        final InetSocketAddress address = httpService.getBindAddress();
        RunId runId = ProgramRunners.getRunId(options);
        // Register service, and the serving host names.
        final List<Cancellable> cancellables = Lists.newArrayList();
        LOG.info("Webapp {} running on address {} registering as {}", program.getApplicationId(), address, serviceName);
        cancellables.add(serviceAnnouncer.announce(serviceName, address.getPort()));
        for (String hname : getServingHostNames(Locations.newInputSupplier(program.getJarLocation()))) {
            final String sname = ProgramType.WEBAPP.name().toLowerCase() + "/" + hname;
            LOG.info("Webapp {} running on address {} registering as {}", program.getApplicationId(), address, sname);
            cancellables.add(discoveryService.register(ResolvingDiscoverable.of(new Discoverable(sname, address))));
        }
        return new WebappProgramController(program.getId(), runId, httpService, new Cancellable() {

            @Override
            public void cancel() {
                for (Cancellable cancellable : cancellables) {
                    cancellable.cancel();
                }
            }
        });
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) InetSocketAddress(java.net.InetSocketAddress) Cancellable(org.apache.twill.common.Cancellable) NettyHttpService(co.cask.http.NettyHttpService) ProgramType(co.cask.cdap.proto.ProgramType) RunId(org.apache.twill.api.RunId)

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