private String createSession() { final ImmutableSession session = ImmutableSession.builder() .name(name) .ttl(Optional.fromNullable(ttl)) .build(); return consul.sessionClient().createSession(session).getId(); }
/** * Copy the current immutable object with elements that replace the content of {@link Session#getChecks() checks}. * @param elements The elements to set * @return A modified copy of {@code this} object */ public final ImmutableSession withChecks(String... elements) { ImmutableList<String> newValue = ImmutableList.copyOf(elements); return new ImmutableSession(this.lockDelay, this.name, this.node, newValue, this.behavior, this.ttl); }
/** * This instance is equal to all instances of {@code ImmutableSession} that have equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(@Nullable Object another) { if (this == another) return true; return another instanceof ImmutableSession && equalTo((ImmutableSession) another); }
/** * Copy the current immutable object with elements that replace the content of {@link Session#getChecks() checks}. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param elements An iterable of checks elements to set * @return A modified copy of {@code this} object */ public final ImmutableSession withChecks(Iterable<String> elements) { if (this.checks == elements) return this; ImmutableList<String> newValue = ImmutableList.copyOf(elements); return new ImmutableSession(this.lockDelay, this.name, this.node, newValue, this.behavior, this.ttl); }
/** * Creates an immutable copy of a {@link Session} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable Session instance */ public static ImmutableSession copyOf(Session instance) { if (instance instanceof ImmutableSession) { return (ImmutableSession) instance; } return ImmutableSession.builder() .from(instance) .build(); }
/** * Builds a new {@link ImmutableSession ImmutableSession}. * @return An immutable instance of Session * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableSession build() { return new ImmutableSession(lockDelay, name, node, checksBuilder.build(), behavior, ttl); } }
private String createSession(String serviceName) { final Session session = ImmutableSession.builder().name(serviceName).build(); return client.sessionClient().createSession(session).getId(); }
/** * Copy the current immutable object by setting an optional value for the {@link Session#getLockDelay() lockDelay} attribute. * An equality check is used on inner nullable value to prevent copying of the same value by returning {@code this}. * @param optional A value for lockDelay * @return A modified copy of {@code this} object */ public final ImmutableSession withLockDelay(Optional<String> optional) { @Nullable String value = optional.orElse(null); if (Objects.equals(this.lockDelay, value)) return this; return new ImmutableSession(value, this.name, this.node, this.checks, this.behavior, this.ttl); }
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; } }
/** * Copy the current immutable object by setting an optional value for the {@link Session#getName() name} attribute. * An equality check is used on inner nullable value to prevent copying of the same value by returning {@code this}. * @param optional A value for name * @return A modified copy of {@code this} object */ public final ImmutableSession withName(Optional<String> optional) { @Nullable String value = optional.orElse(null); if (Objects.equals(this.name, value)) return this; return new ImmutableSession(this.lockDelay, value, this.node, this.checks, this.behavior, this.ttl); }
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); }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link Session#getNode() node} attribute. * @param value The value for node * @return A modified copy of {@code this} object */ public final ImmutableSession withNode(String value) { @Nullable String newValue = Preconditions.checkNotNull(value, "node"); if (Objects.equals(this.node, newValue)) return this; return new ImmutableSession(this.lockDelay, this.name, newValue, this.checks, this.behavior, this.ttl); }
/** * @param json A JSON-bindable data structure * @return An immutable value type * @deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding infrastructure */ @Deprecated @JsonCreator static ImmutableSession fromJson(Json json) { ImmutableSession.Builder builder = ImmutableSession.builder(); if (json.lockDelay != null) { builder.lockDelay(json.lockDelay); } if (json.name != null) { builder.name(json.name); } if (json.node != null) { builder.node(json.node); } if (json.checks != null) { builder.addAllChecks(json.checks); } if (json.behavior != null) { builder.behavior(json.behavior); } if (json.ttl != null) { builder.ttl(json.ttl); } return builder.build(); }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link Session#getName() name} attribute. * @param value The value for name * @return A modified copy of {@code this} object */ public final ImmutableSession withName(String value) { @Nullable String newValue = Preconditions.checkNotNull(value, "name"); if (Objects.equals(this.name, newValue)) return this; return new ImmutableSession(this.lockDelay, newValue, this.node, this.checks, this.behavior, this.ttl); }
@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(); } }
/** * Copy the current immutable object by setting an optional value for the {@link Session#getNode() node} attribute. * An equality check is used on inner nullable value to prevent copying of the same value by returning {@code this}. * @param optional A value for node * @return A modified copy of {@code this} object */ public final ImmutableSession withNode(Optional<String> optional) { @Nullable String value = optional.orElse(null); if (Objects.equals(this.node, value)) return this; return new ImmutableSession(this.lockDelay, this.name, value, this.checks, this.behavior, this.ttl); }
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); }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link Session#getTtl() ttl} attribute. * @param value The value for ttl * @return A modified copy of {@code this} object */ public final ImmutableSession withTtl(String value) { @Nullable String newValue = Preconditions.checkNotNull(value, "ttl"); if (Objects.equals(this.ttl, newValue)) return this; return new ImmutableSession(this.lockDelay, this.name, this.node, this.checks, this.behavior, newValue); }
this.sessionId = consul.sessionClient().createSession(ImmutableSession.builder() .name("couchbase:cbes:" + this.serviceId) .behavior("delete")
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link Session#getLockDelay() lockDelay} attribute. * @param value The value for lockDelay * @return A modified copy of {@code this} object */ public final ImmutableSession withLockDelay(String value) { @Nullable String newValue = Preconditions.checkNotNull(value, "lockDelay"); if (Objects.equals(this.lockDelay, newValue)) return this; return new ImmutableSession(newValue, this.name, this.node, this.checks, this.behavior, this.ttl); }