private String createSession() { final ImmutableSession session = ImmutableSession.builder() .name(name) .ttl(Optional.fromNullable(ttl)) .build(); return consul.sessionClient().createSession(session).getId(); }
/** * Fill a builder with attribute values from the provided {@code SessionCreatedResponse} 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(SessionCreatedResponse instance) { Preconditions.checkNotNull(instance, "instance"); id(instance.getId()); return this; }
private String createSession(String serviceName) { final Session session = ImmutableSession.builder().name(serviceName).build(); return client.sessionClient().createSession(session).getId(); }
public SessionTask(Consul consul, String serviceName, String serviceId, Consumer<Throwable> fatalErrorConsumer) { serviceId = defaultIfNull(serviceId, serviceName); this.serviceId = serviceId; this.serviceName = requireNonNull(serviceName); this.consul = requireNonNull(consul); this.fatalErrorConsumer = requireNonNull(fatalErrorConsumer); try { final List<String> tags = singletonList("couchbase-elasticsearch-connector");// emptyList(); final Map<String, String> meta = singletonMap("uuid", serviceUuid); // todo catch exception, retry with backoff (wait for consul agent to start) final int sessionTtlSeconds = HEALTH_CHECK_INTERVAL_SECONDS * 2; consul.agentClient().register(0, sessionTtlSeconds, this.serviceName, this.serviceId, tags, meta); passHealthCheck(); this.sessionId = consul.sessionClient().createSession(ImmutableSession.builder() .name("couchbase:cbes:" + this.serviceId) .behavior("delete") .lockDelay("15s") .addChecks("service:" + this.serviceId) // consul client library names the health check "service:<serviceId>" .build() ).getId(); } catch (Throwable t) { fatalErrorConsumer.accept(t); // todo need to send to fatalErrorConsumer? throw t; } }
public void remove(String key) { // create session to avoid conflicts (not sure if that is safe enough) SessionClient sessionClient = consul.sessionClient(); String sessionName = "session_" + UUID.randomUUID().toString(); SessionCreatedResponse response = sessionClient .createSession(ImmutableSession.builder().name(sessionName).build()); String sessionId = response.getId(); kvClient = consul.keyValueClient(); String lockKey = "lock_" + key; kvClient.acquireLock(lockKey, sessionName, sessionId); Object object = lookupByName(key); if (object == null) { String msg = "Bean with key '" + key + "' did not exist in Consul Registry."; throw new NoSuchBeanException(msg); } kvClient.deleteKey(key); kvClient.deleteKey(object.getClass().getName() + "/" + key); kvClient.releaseLock(lockKey, sessionId); }
@Override protected void doStart() throws Exception { if (sessionId.get() == null) { client = configuration.createConsulClient(getCamelContext()); sessionClient = client.sessionClient(); keyValueClient = client.keyValueClient(); sessionId.set( sessionClient.createSession( ImmutableSession.builder() .name(getNamespace()) .ttl(configuration.getSessionTtl() + "s") .lockDelay(configuration.getSessionLockDelay() + "s") .build() ).getId() ); LOGGER.debug("Acquired session with id '{}'", sessionId.get()); boolean lock = acquireLock(); LOGGER.debug("Acquire lock on path '{}' with id '{}' result '{}'", path, sessionId.get(), lock); localMember.setMaster(lock); watcher.watch(); } }
public void put(String key, Object object) { // Substitute $ character in key key = key.replaceAll("\\$", "/"); // create session to avoid conflicts // (not sure if that is safe enough, again) SessionClient sessionClient = consul.sessionClient(); String sessionName = "session_" + UUID.randomUUID().toString(); SessionCreatedResponse response = sessionClient .createSession(ImmutableSession.builder().name(sessionName).build()); String sessionId = response.getId(); kvClient = consul.keyValueClient(); String lockKey = "lock_" + key; kvClient.acquireLock(lockKey, sessionName, sessionId); // Allow only unique keys, last one wins if (lookupByName(key) != null) { remove(key); } Object clone = ConsulRegistryUtils.clone((Serializable) object); byte[] serializedObject = ConsulRegistryUtils.serialize((Serializable) clone); // pre-encode due native encoding issues String value = ConsulRegistryUtils.encodeBase64(serializedObject); // store the actual class kvClient.putValue(key, value); // store just as a bookmark kvClient.putValue(object.getClass().getName().replaceAll("\\$", "/") + "/" + key, "1"); kvClient.releaseLock(lockKey, sessionId); }
.addChecks("service:" + this.serviceId) .build() ).getId();
.lockDelay(lockDelay + "s") .build() ).getId();