private List<URI> getAllNodes(URI server) { Request request = prepareGet().setUri(uriBuilderFrom(server).replacePath("/v1/service/presto").build()).build(); JsonResponseHandler<ServiceDescriptorsRepresentation> responseHandler = createJsonResponseHandler(jsonCodec(ServiceDescriptorsRepresentation.class)); ServiceDescriptorsRepresentation serviceDescriptors = httpClient.execute(request, responseHandler); ImmutableList.Builder<URI> addresses = ImmutableList.builder(); for (ServiceDescriptor serviceDescriptor : serviceDescriptors.getServiceDescriptors()) { String httpUri = serviceDescriptor.getProperties().get("http"); if (httpUri != null) { addresses.add(URI.create(httpUri)); } } return addresses.build(); } }
if (!environment.equals(serviceDescriptorsRepresentation.getEnvironment())) { logServerError("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment()); List<ServiceDescriptor> descriptors = newArrayList(serviceDescriptorsRepresentation.getServiceDescriptors()); Collections.shuffle(descriptors); serviceDescriptors.set(ImmutableList.copyOf(descriptors));
@Inject public ServiceInventoryResource(NodeInfo nodeInfo) { environment = nodeInfo.getEnvironment(); descriptor.set(new ServiceDescriptorsRepresentation(environment, ImmutableList.<ServiceDescriptor>of())); }
@PUT @Consumes(MediaType.APPLICATION_JSON) public Response setServiceInventory(ServiceDescriptorsRepresentation descriptor) { if (!environment.equals(descriptor.getEnvironment())) { return Response.status(Status.BAD_REQUEST).entity(format("Expected environment to be %s, but was %s", environment, descriptor.getEnvironment())).build(); } this.descriptor.set(descriptor); return Response.ok().build(); } }
if (!environment.equals(serviceDescriptorsRepresentation.getEnvironment())) { logServerError("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment()); List<ServiceDescriptor> descriptors = new ArrayList<>(serviceDescriptorsRepresentation.getServiceDescriptors()); Collections.shuffle(descriptors); serviceDescriptors.set(ImmutableList.copyOf(descriptors));
@GET @Produces(MediaType.APPLICATION_JSON) public Response getServiceInventory() { return Response.ok(new ServiceDescriptorsRepresentation(environment, serviceInventory.getServiceInventory(coordinator.getAllSlotStatus()))).build(); } }
if (!environment.equals(serviceDescriptorsRepresentation.getEnvironment())) { logServerError("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment()); List<ServiceDescriptor> descriptors = newArrayList(serviceDescriptorsRepresentation.getServiceDescriptors()); Collections.shuffle(descriptors); serviceDescriptors.set(ImmutableList.copyOf(descriptors));
private List<URI> getAllNodes(URI server) { Request request = prepareGet().setUri(uriBuilderFrom(server).replacePath("/v1/service/presto").build()).build(); JsonResponseHandler<ServiceDescriptorsRepresentation> responseHandler = createJsonResponseHandler(jsonCodec(ServiceDescriptorsRepresentation.class)); ServiceDescriptorsRepresentation serviceDescriptors = httpClient.execute(request, responseHandler); ImmutableList.Builder<URI> addresses = ImmutableList.builder(); for (ServiceDescriptor serviceDescriptor : serviceDescriptors.getServiceDescriptors()) { String httpUri = serviceDescriptor.getProperties().get("http"); if (httpUri != null) { addresses.add(URI.create(httpUri)); } } return addresses.build(); } }
private void updateServiceInventory() { List<ServiceDescriptor> inventory = serviceInventory.getServiceInventory(coordinator.getAllSlotStatus()); ServiceDescriptorsRepresentation serviceDescriptors = new ServiceDescriptorsRepresentation(environment, inventory); File serviceInventoryFile = new File(localDirectory, "service-inventory.json"); try { Files.write(SERVICE_DESCRIPTORS_CODEC.toJson(serviceDescriptors), serviceInventoryFile, Charsets.UTF_8); } catch (IOException e) { System.out.println("Unable to write " + serviceInventoryFile); } } }
@Override public ServiceDescriptors handle(Request request, Response response) { Duration maxAge = extractMaxAge(response); String eTag = response.getHeader(HttpHeaders.ETAG); if (NOT_MODIFIED.code() == response.getStatusCode() && serviceDescriptors != null) { return new ServiceDescriptors(serviceDescriptors, maxAge, eTag); } if (OK.code() != response.getStatusCode()) { throw new DiscoveryException(format("Lookup of %s failed with status code %s", type, response.getStatusCode())); } String json; try { json = CharStreams.toString(new InputStreamReader(response.getInputStream(), Charsets.UTF_8)); } catch (IOException e) { throw new DiscoveryException(format("Lookup of %s failed", type), e); } ServiceDescriptorsRepresentation serviceDescriptorsRepresentation = serviceDescriptorsCodec.fromJson(json); if (!environment.equals(serviceDescriptorsRepresentation.getEnvironment())) { throw new DiscoveryException(format("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment())); } return new ServiceDescriptors( type, pool, serviceDescriptorsRepresentation.getServiceDescriptors(), maxAge, eTag); } });
private void updateServiceInventory() { List<ServiceDescriptor> inventory = serviceInventory.getServiceInventory(coordinator.getAllSlotStatus()); ServiceDescriptorsRepresentation serviceDescriptors = new ServiceDescriptorsRepresentation(environment, inventory); File serviceInventoryFile = new File(localDirectory, "service-inventory.json"); try { Files.write(SERVICE_DESCRIPTORS_CODEC.toJson(serviceDescriptors), serviceInventoryFile, Charsets.UTF_8); } catch (IOException e) { System.out.println("Unable to write " + serviceInventoryFile); } } }
@Override public ServiceDescriptors handle(Request request, Response response) { Duration maxAge = extractMaxAge(response); String eTag = response.getHeader(HttpHeaders.ETAG); if (NOT_MODIFIED.code() == response.getStatusCode() && serviceDescriptors != null) { return new ServiceDescriptors(serviceDescriptors, maxAge, eTag); } if (OK.code() != response.getStatusCode()) { throw new DiscoveryException(format("Lookup of %s failed with status code %s", type, response.getStatusCode())); } byte[] json; try { json = ByteStreams.toByteArray(response.getInputStream()); } catch (IOException e) { throw new DiscoveryException(format("Lookup of %s failed", type), e); } ServiceDescriptorsRepresentation serviceDescriptorsRepresentation = serviceDescriptorsCodec.fromJson(json); if (!environment.equals(serviceDescriptorsRepresentation.getEnvironment())) { throw new DiscoveryException(format("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment())); } return new ServiceDescriptors( type, pool, serviceDescriptorsRepresentation.getServiceDescriptors(), maxAge, eTag); } });
.setUri(uriBuilderFrom(internalUri).replacePath("/v1/serviceInventory").build()) .setHeader(CONTENT_TYPE, APPLICATION_JSON) .setBodyGenerator(jsonBodyGenerator(serviceDescriptorsCodec, new ServiceDescriptorsRepresentation(environment, serviceInventory))) .build();
@Override public ServiceDescriptors handle(Request request, Response response) { Duration maxAge = extractMaxAge(response); String eTag = response.getHeader(HttpHeaders.ETAG); if (NOT_MODIFIED.code() == response.getStatusCode() && serviceDescriptors != null) { return new ServiceDescriptors(serviceDescriptors, maxAge, eTag); } if (OK.code() != response.getStatusCode()) { throw new DiscoveryException(format("Lookup of %s failed with status code %s", type, response.getStatusCode())); } byte[] json; try { json = ByteStreams.toByteArray(response.getInputStream()); } catch (IOException e) { throw new DiscoveryException(format("Lookup of %s failed", type), e); } ServiceDescriptorsRepresentation serviceDescriptorsRepresentation = serviceDescriptorsCodec.fromJson(json); if (!environment.equals(serviceDescriptorsRepresentation.getEnvironment())) { throw new DiscoveryException(format("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment())); } return new ServiceDescriptors( type, pool, serviceDescriptorsRepresentation.getServiceDescriptors(), maxAge, eTag); } });