public void releaseLock() { try { KeyValueClient kv = consul.keyValueClient(); Optional<Value> value = kv.getValue(path); if (value.isPresent()) { Optional<String> session = value.get().getSession(); if (session.isPresent()) { kv.releaseLock(path, session.get()); } } } finally { destroySession(); } } }
public boolean releaseLockForService(final String serviceName) { final String key = getServiceKey(serviceName); KeyValueClient kv = client.keyValueClient(); Optional<Value> value = kv.getValue(key); if(value.isPresent() && value.get().getSession().isPresent()) { return kv.releaseLock(key, value.get().getSession().get()); } else { return true; } }
public Optional<String> getLeaderInfoForService(final String serviceName) { String key = getServiceKey(serviceName); Optional<Value> value = client.keyValueClient().getValue(key); return value.flatMap(val -> { if(val.getSession().isPresent()) { return val.getValueAsString(); } return Optional.empty(); }); }
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(); } }
protected void onValue(Value value) { final Exchange exchange = endpoint.createExchange(); final Message message = exchange.getIn(); message.setHeader(ConsulConstants.CONSUL_KEY, value.getKey()); message.setHeader(ConsulConstants.CONSUL_RESULT, true); message.setHeader(ConsulConstants.CONSUL_FLAGS, value.getFlags()); message.setHeader(ConsulConstants.CONSUL_CREATE_INDEX, value.getCreateIndex()); message.setHeader(ConsulConstants.CONSUL_LOCK_INDEX, value.getLockIndex()); message.setHeader(ConsulConstants.CONSUL_MODIFY_INDEX, value.getModifyIndex()); if (value.getSession().isPresent()) { message.setHeader(ConsulConstants.CONSUL_SESSION, value.getSession().get()); } message.setBody(configuration.isValueAsString() ? value.getValueAsString().orElse(null) : value.getValue().orElse(null)); try { getProcessor().process(exchange); } catch (Exception e) { getExceptionHandler().handleException("Error processing exchange", exchange, e); } }
/** * Fill a builder with attribute values from the provided {@code Value} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(Value instance) { Preconditions.checkNotNull(instance, "instance"); createIndex(instance.getCreateIndex()); modifyIndex(instance.getModifyIndex()); lockIndex(instance.getLockIndex()); key(instance.getKey()); flags(instance.getFlags()); Optional<String> valueOptional = instance.getValue(); if (valueOptional.isPresent()) { value(valueOptional); } Optional<String> sessionOptional = instance.getSession(); if (sessionOptional.isPresent()) { session(sessionOptional); } return this; }