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;
}
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);
}
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());
}
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())));
}
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);
}
Aggregations