public static List<String> listKeys(KeyValueClient kv, String keyPrefix) { try { return kv.getKeys(keyPrefix); } catch (ConsulException e) { if (e.getCode() == HTTP_NOT_FOUND) { return new ArrayList<>(0); } throw e; } }
/** * Pings the Consul Agent. */ public void ping() { try { retrofit2.Response<Void> response = api.ping().execute(); if (!response.isSuccessful()) { throw new ConsulException(String.format("Error pinging Consul: %s", response.message())); } } catch (Exception ex) { throw new ConsulException("Error connecting to Consul", ex); } }
public ConsulException(int code, Response<?> response) { super(String.format("Consul request failed with status [%s]: %s", code, message(response))); this.code = code; }
/** * Retrieves a {@link com.orbitz.consul.model.kv.Value} for a specific key * from the key/value store. * * GET /v1/kv/{key} * * @param key The key to retrieve. * @param queryOptions The query options. * @return An {@link Optional} containing the value or {@link Optional#empty()()} */ public Optional<Value> getValue(String key, QueryOptions queryOptions) { try { return getSingleValue(http.extract(api.getValue(trimLeadingSlash(key), queryOptions.toQuery()), NOT_FOUND_404)); } catch (ConsulException ignored) { if(ignored.getCode() != NOT_FOUND_404) { throw ignored; } } return Optional.empty(); }
strategy.markRequestFailed(nextRequest.get()); throw new ConsulException("Unable to successfully determine a viable host for communication."); throw new ConsulException("Consul failover strategy has determined that there are no viable hosts remaining.");
/** * Returns a {@link ConsulResponse<Value>} for a specific key from the kv store. * Contains the consul response headers along with the configuration value. * * GET /v1/kv/{key} * * @param key The key to retrieve. * @param queryOptions The query options. * @return An {@link Optional} containing the ConsulResponse or {@link Optional#empty()} */ public Optional<ConsulResponse<Value>> getConsulResponseWithValue(String key, QueryOptions queryOptions) { try { ConsulResponse<List<Value>> consulResponse = http.extractConsulResponse(api.getValue(trimLeadingSlash(key), queryOptions.toQuery()), NOT_FOUND_404); Optional<Value> consulValue = getSingleValue(consulResponse.getResponse()); if (consulValue.isPresent()) { ConsulResponse<Value> result = new ConsulResponse<>(consulValue.get(), consulResponse.getLastContact(), consulResponse.isKnownLeader(), consulResponse.getIndex()); return Optional.of(result); } } catch (ConsulException ignored) { if (ignored.getCode() != NOT_FOUND_404) { throw ignored; } } return Optional.empty(); }
/** * Performs a Consul transaction. * * PUT /v1/tx * * @deprecated Replaced by {@link #performTransaction(TransactionOptions, Operation...)} * * @param consistency The consistency to use for the transaction. * @param operations A list of KV operations. * @return A {@link ConsulResponse} containing results and potential errors. */ @Deprecated public ConsulResponse<TxResponse> performTransaction(ConsistencyMode consistency, Operation... operations) { Map<String, Object> query = consistency == ConsistencyMode.DEFAULT ? ImmutableMap.of() : ImmutableMap.of(consistency.toParam().get(), "true"); try { return http.extractConsulResponse(api.performTransaction(RequestBody.create(MediaType.parse("application/json"), Jackson.MAPPER.writeValueAsString(kv(operations))), query)); } catch (JsonProcessingException e) { throw new ConsulException(e); } }
/** * Returns {@code true} if the given session owned the lock, otherwise {@code false}. */ public static boolean unlockAndDelete(KeyValueClient kv, String key, String sessionId) { try { kv.performTransaction( ImmutableOperation.builder(Verb.UNLOCK).key(key).session(sessionId).build(), ImmutableOperation.builder(Verb.DELETE).key(key).build()); return true; } catch (ConsulException e) { if (e.getCode() == HTTP_CONFLICT) { return false; // didn't own lock; no worries } throw e; } }
private <T> Response<T> executeCall(Call<T> call) { try { return call.execute(); } catch (IOException e) { eventHandler.httpRequestFailure(call.request(), e); throw new ConsulException(e); } }
/** * Performs a Consul transaction. * * PUT /v1/tx * * @param transactionOptions transaction options (e.g. dc, consistency). * @param operations A list of KV operations. * @return A {@link ConsulResponse} containing results and potential errors. */ public ConsulResponse<TxResponse> performTransaction(TransactionOptions transactionOptions, Operation... operations) { Map<String, Object> query = transactionOptions.toQuery(); try { return http.extractConsulResponse(api.performTransaction(RequestBody.create(MediaType.parse("application/json"), Jackson.MAPPER.writeValueAsString(kv(operations))), query)); } catch (JsonProcessingException e) { throw new ConsulException(e); } }
onFailure(new ConsulException("Consul cluster has no elected leader"));
private <T> void ensureResponseSuccessful(Call<T> call, Response<T> response, Integer... okCodes) { if(isSuccessful(response, okCodes)) { eventHandler.httpRequestSuccess(call.request()); } else { ConsulException exception = new ConsulException(response.code(), response); eventHandler.httpRequestInvalid(call.request(), exception); throw exception; } }