Search in sources :

Example 36 with Discoverable

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

the class ResourceAssignmentTypeAdapter method serialize.

@Override
public JsonElement serialize(ResourceAssignment src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("name", src.getName());
    src.getAssignments().entries();
    JsonArray assignments = new JsonArray();
    for (Map.Entry<Discoverable, PartitionReplica> entry : src.getAssignments().entries()) {
        JsonArray entryJson = new JsonArray();
        entryJson.add(context.serialize(entry.getKey(), Discoverable.class));
        entryJson.add(context.serialize(entry.getValue()));
        assignments.add(entryJson);
    }
    json.add("assignments", assignments);
    return json;
}
Also used : JsonArray(com.google.gson.JsonArray) Discoverable(org.apache.twill.discovery.Discoverable) JsonObject(com.google.gson.JsonObject) Map(java.util.Map)

Example 37 with Discoverable

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

the class ResourceAssignmentTypeAdapter method deserialize.

@Override
public ResourceAssignment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Expect a json object, got " + json);
    }
    JsonObject jsonObj = json.getAsJsonObject();
    String name = jsonObj.get("name").getAsString();
    Multimap<Discoverable, PartitionReplica> assignments = TreeMultimap.create(DiscoverableComparator.COMPARATOR, PartitionReplica.COMPARATOR);
    JsonArray assignmentsJson = context.deserialize(jsonObj.get("assignments"), JsonArray.class);
    for (JsonElement element : assignmentsJson) {
        if (!element.isJsonArray()) {
            throw new JsonParseException("Expect a json array, got " + element);
        }
        JsonArray entryJson = element.getAsJsonArray();
        if (entryJson.size() != 2) {
            throw new JsonParseException("Expect json array of size = 2, got " + entryJson.size());
        }
        Discoverable key = context.deserialize(entryJson.get(0), Discoverable.class);
        PartitionReplica value = context.deserialize(entryJson.get(1), PartitionReplica.class);
        assignments.put(key, value);
    }
    return new ResourceAssignment(name, assignments);
}
Also used : JsonArray(com.google.gson.JsonArray) Discoverable(org.apache.twill.discovery.Discoverable) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 38 with Discoverable

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

the class MessagingHttpService method startUp.

@Override
protected void startUp() throws Exception {
    httpService = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.MESSAGING_SERVICE).setHost(cConf.get(Constants.MessagingSystem.HTTP_SERVER_BIND_ADDRESS)).setHandlerHooks(ImmutableList.of(new MetricsReporterHook(metricsCollectionService, Constants.Service.MESSAGING_SERVICE))).setWorkerThreadPoolSize(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_WORKER_THREADS)).setExecThreadPoolSize(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_EXECUTOR_THREADS)).setHttpChunkLimit(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_MAX_REQUEST_SIZE_MB) * 1024 * 1024).setExceptionHandler(new HttpExceptionHandler() {

        @Override
        public void handle(Throwable t, HttpRequest request, HttpResponder responder) {
            // TODO: CDAP-7688. Override the handling to return 400 on IllegalArgumentException
            if (t instanceof IllegalArgumentException) {
                logWithTrace(request, t);
                responder.sendString(HttpResponseStatus.BAD_REQUEST, t.getMessage());
            } else {
                super.handle(t, request, responder);
            }
        }

        private void logWithTrace(HttpRequest request, Throwable t) {
            LOG.trace("Error in handling request={} {} for user={}:", request.getMethod().getName(), request.getUri(), Objects.firstNonNull(SecurityRequestContext.getUserId(), "<null>"), t);
        }
    }).addHttpHandlers(handlers).build();
    httpService.startAndWait();
    cancelDiscovery = discoveryService.register(new Discoverable(Constants.Service.MESSAGING_SERVICE, httpService.getBindAddress()));
    LOG.info("Messaging HTTP server started on {}", httpService.getBindAddress());
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) HttpResponder(co.cask.http.HttpResponder) Discoverable(org.apache.twill.discovery.Discoverable) MetricsReporterHook(co.cask.cdap.common.metrics.MetricsReporterHook) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) HttpExceptionHandler(co.cask.cdap.common.HttpExceptionHandler)

Example 39 with Discoverable

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

the class LogSaverStatusService method startUp.

@Override
protected void startUp() throws Exception {
    LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(Id.Namespace.SYSTEM.getId(), Constants.Logging.COMPONENT_NAME, Constants.Service.LOGSAVER));
    httpService.startAndWait();
    cancellable = discoveryService.register(ResolvingDiscoverable.of(new Discoverable(Constants.Service.LOGSAVER, 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 40 with Discoverable

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

the class UnitTestManager method getQueryClient.

@Override
public Connection getQueryClient(NamespaceId namespace) throws Exception {
    // this makes sure the Explore JDBC driver is loaded
    Class.forName(ExploreDriver.class.getName());
    Discoverable discoverable = new StickyEndpointStrategy(discoveryClient.discover(Constants.Service.EXPLORE_HTTP_USER_SERVICE)).pick();
    if (null == discoverable) {
        throw new IOException("Explore service could not be discovered.");
    }
    InetSocketAddress address = discoverable.getSocketAddress();
    String host = address.getHostName();
    int port = address.getPort();
    String connectString = String.format("%s%s:%d?namespace=%s", Constants.Explore.Jdbc.URL_PREFIX, host, port, namespace.getNamespace());
    return DriverManager.getConnection(connectString);
}
Also used : ExploreDriver(co.cask.cdap.explore.jdbc.ExploreDriver) Discoverable(org.apache.twill.discovery.Discoverable) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) StickyEndpointStrategy(co.cask.cdap.common.discovery.StickyEndpointStrategy)

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