public boolean acquireLock(long maxWait, TimeUnit unit) { KeyValueClient kv = consul.keyValueClient(); sessionId = createSession(); Optional<Value> value = kv.getValue(path); if (kv.acquireLock(path, sessionId)) { return true; } BigInteger index = BigInteger.valueOf(value.get().getModifyIndex()); kv.getValue(path, QueryOptions.blockMinutes((int) unit.toMinutes(maxWait), index).build()); if (!kv.acquireLock(path, sessionId)) { destroySession(); return false; } else { return true; } }
/** * Retrieves a single node for a given datacenter with {@link com.orbitz.consul.option.QueryOptions}. * <p/> * GET /v1/catalog/node/{node}?dc={datacenter} * * @param queryOptions The Query Options to use. * @return A list of matching {@link com.orbitz.consul.model.catalog.CatalogService} objects. */ public ConsulResponse<CatalogNode> getNode(String node, QueryOptions queryOptions) { return http.extractConsulResponse(api.getNode(node, queryOptions.toQuery(), queryOptions.getTag(), queryOptions.getNodeMeta())); }
protected static QueryOptions watchParams(final BigInteger index, final int blockSeconds, QueryOptions queryOptions) { checkArgument(!queryOptions.getIndex().isPresent() && !queryOptions.getWait().isPresent(), "Index and wait cannot be overridden"); ImmutableQueryOptions.Builder builder = ImmutableQueryOptions.builder() .from(watchDefaultParams(index, blockSeconds)) .token(queryOptions.getToken()) .consistencyMode(queryOptions.getConsistencyMode()) .near(queryOptions.getNear()) .datacenter(queryOptions.getDatacenter()); for (String tag : queryOptions.getTag()) { builder.addTag(tag); } return builder.build(); }
@Override public Map<String, Object> toQuery() { Map<String, Object> result = new HashMap<>(); switch (getConsistencyMode()) { case CONSISTENT: result.put("consistent", ""); break; case STALE: result.put("stale", ""); break; } if (isBlocking()) { optionallyAdd(result, "wait", getWait()); optionallyAdd(result, "index", getIndex()); } optionallyAdd(result, "token", getToken()); optionallyAdd(result, "near", getNear()); optionallyAdd(result, "dc", getDatacenter()); return result; } }
private static QueryOptions watchDefaultParams(final BigInteger index, final int blockSeconds) { if (index == null) { return QueryOptions.BLANK; } else { return QueryOptions.blockSeconds(blockSeconds, index).build(); } }
@Value.Check void validate() { if (isBlocking()) { checkArgument(getIndex().isPresent(), "If wait is specified, index must also be specified"); } }
public static ImmutableQueryOptions.Builder blockSeconds(int seconds, BigInteger index) { return blockBuilder("s", seconds, index); }
@Value.Derived public List<String> getNodeMetaQuery() { return getNodeMeta() == null ? Collections.emptyList() : ImmutableList.copyOf(getNodeMeta()); }
@Value.Derived public boolean hasToken() { return getToken().isPresent(); }
/** * Retrieves all services for a given datacenter. * <p/> * GET /v1/catalog/services?dc={datacenter} * * @param queryOptions The Query Options to use. * @return A {@link com.orbitz.consul.model.ConsulResponse} containing a map of service name to list of tags. */ public ConsulResponse<Map<String, List<String>>> getServices(QueryOptions queryOptions) { return http.extractConsulResponse(api.getServices(queryOptions.toQuery(), queryOptions.getTag(), queryOptions.getNodeMeta())); }
Optional<String> waitOptional = instance.getWait(); if (waitOptional.isPresent()) { wait(waitOptional); Optional<String> tokenOptional = instance.getToken(); if (tokenOptional.isPresent()) { token(tokenOptional); Optional<BigInteger> indexOptional = instance.getIndex(); if (indexOptional.isPresent()) { index(indexOptional); Optional<String> nearOptional = instance.getNear(); if (nearOptional.isPresent()) { near(nearOptional); Optional<String> datacenterOptional = instance.getDatacenter(); if (datacenterOptional.isPresent()) { datacenter(datacenterOptional); addAllNodeMeta(instance.getNodeMeta()); addAllTag(instance.getTag()); consistencyMode(instance.getConsistencyMode()); return this;
protected QueryOptions queryOptions() { return QueryOptions.blockSeconds(configuration.getBlockSeconds(), index.get()).build(); }
public static ImmutableQueryOptions.Builder blockMinutes(int minutes, BigInteger index) { return blockBuilder("m", minutes, index); }
/** * Asynchronously retrieves the single node for a given datacenter with {@link com.orbitz.consul.option.QueryOptions}. * <p/> * GET /v1/catalog/node/{node}?dc={datacenter} * * @param queryOptions The Query Options to use. * @param callback Callback implemented by callee to handle results. */ public void getNode(String node, QueryOptions queryOptions, ConsulResponseCallback<CatalogNode> callback) { http.extractConsulResponse(api.getNode(node, queryOptions.toQuery(), queryOptions.getTag(), queryOptions.getNodeMeta()), callback); }
@Override public void run() { client.listEvents( key, QueryOptions.blockSeconds(configuration.getBlockSeconds(), index.get()).build(), EventWatcher.this ); } }, configuration.getBlockSeconds(), TimeUnit.SECONDS);
/** * Retrieves all nodes for a given datacenter with {@link com.orbitz.consul.option.QueryOptions}. * <p/> * GET /v1/catalog/nodes?dc={datacenter} * * @param queryOptions The Query Options to use. * @return A {@link com.orbitz.consul.model.ConsulResponse} containing a list of * {@link com.orbitz.consul.model.health.Node} objects. */ public ConsulResponse<List<Node>> getNodes(QueryOptions queryOptions) { return http.extractConsulResponse(api.getNodes(queryOptions.toQuery(), queryOptions.getTag(), queryOptions.getNodeMeta())); }
@Override public void run() { if (isRunAllowed()) { // Refresh session sessionClient.renewSession(sessionId); keyValueClient.getValue( servicePath, QueryOptions.blockSeconds(ttl / 3, index.get()).build(), this ); } } }
/** * Asynchronously retrieves the services for a given datacenter. * <p/> * GET /v1/catalog/services?dc={datacenter} * * @param queryOptions The Query Options to use. * @param callback Callback implemented by callee to handle results. * @return A {@link com.orbitz.consul.model.ConsulResponse} containing a map of service name to list of tags. */ public void getServices(QueryOptions queryOptions, ConsulResponseCallback<Map<String, List<String>>> callback) { http.extractConsulResponse(api.getServices(queryOptions.toQuery(), queryOptions.getTag(), queryOptions.getNodeMeta()), callback); }
public void watch() { if (sessionId.get() == null) { return; } if (isStarting() || isStarted()) { // Watch for changes keyValueClient.getValue( path, QueryOptions.blockSeconds(configuration.getSessionRefreshInterval(), index.get()).build(), this ); if (sessionId.get() != null) { // Refresh session sessionClient.renewSession(sessionId.get()); } } } }