@VisibleForTesting ImmutableMap<K, V> convertToMap(final ConsulResponse<List<V>> response) { if (response == null || response.getResponse() == null || response.getResponse().isEmpty()) { return ImmutableMap.of(); } final ImmutableMap.Builder<K, V> builder = ImmutableMap.builder(); final Set<K> keySet = new HashSet<>(); for (final V v : response.getResponse()) { final K key = keyConversion.apply(v); if (key != null) { if (!keySet.contains(key)) { builder.put(key, v); } else { LOGGER.warn("Duplicate service encountered. May differ by tags. Try using more specific tags? " + key.toString()); } } keySet.add(key); } return builder.build(); }
private static Function<ConsulResponse<Value>, String> valueAsString() { return value -> value.getResponse().getValueAsString(UTF_8).orElse(""); }
@Override public List<ServiceDefinition> getServices(String name) { List<CatalogService> services = client.get().catalogClient() .getService(name, queryOptions) .getResponse(); List<ServiceHealth> healths = client.get().healthClient() .getAllServiceInstances(name, queryOptions) .getResponse(); return services.stream() .map(service -> newService(name, service, healths)) .collect(Collectors.toList()); }
@GET @Timed @Path("/consul/{service}") public List<ServiceHealth> getHealthyServiceInstances(@PathParam("service") String service) { return consul.healthClient().getHealthyServiceInstances(service).getResponse(); }
@Override public void onComplete(ConsulResponse<ResponseBody> consulResponse) { // Note that response.body() and response.body().byteStream() should be closed. // see: https://square.github.io/okhttp/3.x/okhttp/okhttp3/ResponseBody.html try (ResponseBody responseBody = consulResponse.getResponse()) { try (InputStream inputStream = responseBody.byteStream()) { Files.copy(inputStream, destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING); callback.onResponse(consulResponse.getIndex()); } } catch (IOException e) { callback.onFailure(e); } }
@Override public Collection<ServiceHealth> discover(final Consul consul) { return consul.healthClient().getHealthyServiceInstances(service).getResponse(); } }
protected <T> void processConsulResponse(Message message, ConsulResponse<T> response) throws Exception { message.setHeader(ConsulConstants.CONSUL_INDEX, response.getIndex()); message.setHeader(ConsulConstants.CONSUL_LAST_CONTACT, response.getLastContact()); message.setHeader(ConsulConstants.CONSUL_KNOWN_LEADER, response.isKnownLeader()); setBodyAndResult(message, response.getResponse()); }
/** * Lists events for the Consul agent. * * GET /v1/event/list?name={name} * * @param name Event name to filter. * @param queryOptions The query options to use. * @return A {@link com.orbitz.consul.model.ConsulResponse} object containing * a list of {@link com.orbitz.consul.model.event.Event} objects. */ public EventResponse listEvents(String name, QueryOptions queryOptions) { Map<String, String> query = StringUtils.isNotEmpty(name) ? ImmutableMap.of("name", name) : Collections.emptyMap(); ConsulResponse<List<Event>> response = http.extractConsulResponse(api.listEvents(query)); return ImmutableEventResponse.of(response.getResponse(), response.getIndex()); }
public synchronized Membership updateGroupMembership() throws ClusterMembershipException { if (panic) { throw new ClusterMembershipException("Already permanently failed a health check."); } final List<ServiceHealth> healthyServices = consul.healthClient().getHealthyServiceInstances(serviceName).getResponse(); final List<String> serviceKeys = healthyServices.stream() .map(ConsulCoordinator::serviceKey) .sorted() .collect(toList()); System.out.println(); serviceKeys.forEach(System.out::println); int i = 1; for (String key : serviceKeys) { if (key.endsWith(serviceUuid)) { return Membership.of(i, serviceKeys.size()); } i++; } throw new ClusterMembershipException("We're not in the list of healthy services." + " Was another connector worker started on this node without specifying a unique service ID?"); }
public static Map<String, String> getKeyValue(String route) { Consul consul = consulInstance(); KeyValueClient keyValueClient = consul.keyValueClient(); Map<String, String> keyValues = new HashMap<>(); ConsulResponse<List<Value>> consulResponseWithValues = keyValueClient.getConsulResponseWithValues(route); if(consulResponseWithValues.getResponse() != null) { for (Value value: consulResponseWithValues.getResponse()) { String key = value.getKey(); String v = ""; if(value.getValueAsString().isPresent()) { v = value.getValueAsString().get(); } LOG.info("Load key: " + route + " value: " + v); keyValues.put(key, v); } } return keyValues; }
private void bind() throws InterruptedException, EndpointAlreadyInUseException { while (!closed()) { LOGGER.info("Attempting to binding to RPC endpoint document {}", endpointKey); final boolean acquired = kv.acquireLock(endpointKey, "{}", sessionId); if (acquired) { LOGGER.info("Successfully bound to RPC endpoint document {}", endpointKey); return; } final Optional<ConsulResponse<Value>> existing = kv.getConsulResponseWithValue(endpointKey); if (existing.isPresent()) { // somebody else has acquired the lock final String lockSession = existing.get().getResponse().getSession().orElse(null); throw new EndpointAlreadyInUseException( "Failed to lock RPC endpoint document " + endpointKey + " ; already locked by session " + lockSession); } LOGGER.info("Endpoint lock acquisition failed; will retry after delay."); SECONDS.sleep(1); } } }
@Override public void onComplete(ConsulResponse<Optional<Value>> consulResponse) { if (isStarting() || isStarted()) { Optional<Value> value = consulResponse.getResponse(); if (value.isPresent()) { Optional<String> sid = value.get().getSession(); if (!sid.isPresent()) { // If the key is not held by any session, try acquire a // lock (become leader) boolean lock = acquireLock(); LOGGER.debug("Try to acquire lock on path '{}' with id '{}', result '{}'", path, sessionId.get(), lock); localMember.setMaster(lock); } else { boolean master = sid.get().equals(sessionId.get()); if (!master) { LOGGER.debug("Path {} is held by session {}, local session is {}", path, sid.get(), sessionId.get()); } localMember.setMaster(sid.get().equals(sessionId.get())); } } index.set(consulResponse.getIndex()); watch(); } }
@Override public void onComplete(ConsulResponse<Optional<Value>> consulResponse) { if (isRunAllowed()) { Optional<Value> value = consulResponse.getResponse(); if (value.isPresent()) { Optional<String> sid = value.get().getSession(); if (sid.isPresent() && ObjectHelper.isNotEmpty(sid.get())) { // If the key is not held by any session, try acquire a // lock (become leader) LOGGER.debug("Try to take leadership ..."); setLeader(keyValueClient.acquireLock(servicePath, sessionId)); } else if (!sessionId.equals(sid) && leader.get()) { // Looks like I've lost leadership setLeader(false); } } index.set(consulResponse.getIndex()); run(); } }
@Override public void onComplete(ConsulResponse<List<Event>> response) { callback.onComplete(ImmutableEventResponse.of(response.getResponse(), response.getIndex())); }
public static List<String> getActivePEServicesEndPoints() { LOG.info("Load active PE services endpoints"); Consul consul = consulInstance(); HealthClient healthClient = consul.healthClient(); List<String> endpoints = new LinkedList<>(); List<ServiceHealth> nodes = healthClient.getHealthyServiceInstances(PE_SERVICE_NAME).getResponse(); for (ServiceHealth node: nodes) { String endpoint = node.getService().getAddress() + ":" + node.getService().getPort(); LOG.info("Active PE endpoint:" + endpoint); endpoints.add(endpoint); } return endpoints; }
public static void atomicUpdate(KeyValueClient kv, ConsulResponse<Value> initialResponse, Function<String, String> mutator) throws IOException { final Value v = initialResponse.getResponse(); final String key = v.getKey(); LOGGER.debug("Updating key {}", key); final String oldValue = v.getValueAsString(UTF_8).orElse(missingDocumentValue); final String newValue = mutator.apply(oldValue); if (Objects.equals(newValue, oldValue)) { return; } final long index = initialResponse.getIndex().longValue(); final PutOptions options = ImmutablePutOptions.builder().cas(index).build(); boolean success = kv.putValue(key, newValue, 0, options, UTF_8); if (!success) { LOGGER.info("Failed to put new document (optimistic locking failure?); reloading and retrying"); atomicUpdate(kv, key, mutator); } }
private static void atomicUpdate(KeyValueClient kv, String key, Function<String, String> mutator) throws IOException { while (true) { final ConsulResponse<Value> r = kv.getConsulResponseWithValue(key).orElse(null); if (r == null) { // Don't automatically create the document, because it might need to be associated with another node's session. // For example, an RPC endpoint doc is updated by both client and server, but is tied to the server session. throw new IOException("Can't update non-existent document: " + key); } final BigInteger index = r.getIndex(); final String oldValue = r.getResponse().getValueAsString(UTF_8).orElse(missingDocumentValue); final String newValue = mutator.apply(oldValue); if (Objects.equals(newValue, oldValue)) { return; } final PutOptions options = ImmutablePutOptions.builder().cas(index.longValue()).build(); boolean success = kv.putValue(key, newValue, 0, options, UTF_8); if (success) { return; } // todo truncated exponential backoff, please! Die if timeout! //MILLISECONDS.sleep(100); } }
@Override public void onComplete(ConsulResponse<List<Value>> consulResponse) { callback.onComplete( new ConsulResponse<>(getSingleValue(consulResponse.getResponse()), consulResponse.getLastContact(), consulResponse.isKnownLeader(), consulResponse.getIndex())); }
@Override public void onComplete(ConsulResponse<T> consulResponse) { if (isRunAllowed()) { onResponse(consulResponse.getResponse()); setIndex(consulResponse.getIndex()); watch(); } }
@Override public List<IServiceNode> getNodesForFunction(IFunctionIdentifier identifier) throws SiteWhereException { if (getConsulClient() != null) { HealthClient healthClient = getConsulClient().healthClient(); List<ServiceHealth> matches = healthClient.getHealthyServiceInstances(identifier.getShortName()) .getResponse(); List<IServiceNode> nodes = new ArrayList<>(); for (ServiceHealth match : matches) { String host = match.getService().getAddress(); ServiceNode node = new ServiceNode(); node.setAddress(host); nodes.add(node); } return nodes; } else { List<IServiceNode> nodes = new ArrayList<>(); return nodes; } }