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 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(); } }
List<ServiceDescriptor> descriptors = newArrayList(serviceDescriptorsRepresentation.getServiceDescriptors()); Collections.shuffle(descriptors); serviceDescriptors.set(ImmutableList.copyOf(descriptors));
List<ServiceDescriptor> descriptors = new ArrayList<>(serviceDescriptorsRepresentation.getServiceDescriptors()); Collections.shuffle(descriptors); serviceDescriptors.set(ImmutableList.copyOf(descriptors));
List<ServiceDescriptor> descriptors = newArrayList(serviceDescriptorsRepresentation.getServiceDescriptors()); Collections.shuffle(descriptors); serviceDescriptors.set(ImmutableList.copyOf(descriptors));
@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); } });
@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); } });
@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); } });