/** * Builds a new {@link ImmutablePutOptions ImmutablePutOptions}. * @return An immutable instance of PutOptions * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutablePutOptions build() { return new ImmutablePutOptions(cas, acquire, release, dc, token); } }
/** * This instance is equal to all instances of {@code ImmutablePutOptions} 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 ImmutablePutOptions && equalTo((ImmutablePutOptions) another); }
/** * Creates an immutable copy of a {@link PutOptions} 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 PutOptions instance */ public static ImmutablePutOptions copyOf(PutOptions instance) { if (instance instanceof ImmutablePutOptions) { return (ImmutablePutOptions) instance; } return ImmutablePutOptions.builder() .from(instance) .build(); }
/** * Releases the lock for a given service and session. * * GET /v1/kv/{key}?release={sessionId} * * @param key identifying the service. * @param sessionId * * @return {@link SessionInfo}. */ public boolean releaseLock(final String key, final String sessionId) { return putValue(key, "", 0, ImmutablePutOptions.builder().release(sessionId).build()); }
/** * Aquire a lock for a given key. * * PUT /v1/kv/{key}?acquire={session} * * @param key The key to acquire the lock. * @param session The session to acquire lock. * @param value key value (usually - application specific info about the lock requester) * @return true if the lock is acquired successfully, false otherwise. */ public boolean acquireLock(final String key, final String value, final String session) { return putValue(key, value, 0, ImmutablePutOptions.builder().acquire(session).build()); }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link PutOptions#getRelease() release} attribute. * @param value The value for release * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withRelease(String value) { @Nullable String newValue = Preconditions.checkNotNull(value, "release"); if (Objects.equals(this.release, newValue)) return this; return new ImmutablePutOptions(this.cas, this.acquire, newValue, this.dc, this.token); }
public static void atomicUpdate(KeyValueClient kv, ConsulResponse<Value> initialResponse, Function<String, String> mutator) throws IOException { final Value v = initialResponse.getResponse(); final String key = v.getKey(); LOGGER.debug("Updating key {}", key); final String oldValue = v.getValueAsString(UTF_8).orElse(missingDocumentValue); final String newValue = mutator.apply(oldValue); if (Objects.equals(newValue, oldValue)) { return; } final long index = initialResponse.getIndex().longValue(); final PutOptions options = ImmutablePutOptions.builder().cas(index).build(); boolean success = kv.putValue(key, newValue, 0, options, UTF_8); if (!success) { LOGGER.info("Failed to put new document (optimistic locking failure?); reloading and retrying"); atomicUpdate(kv, key, mutator); } }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link PutOptions#getCas() cas} attribute. * @param value The value for cas * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withCas(long value) { @Nullable Long newValue = value; if (Objects.equals(this.cas, newValue)) return this; return new ImmutablePutOptions(newValue, this.acquire, this.release, this.dc, this.token); }
private static void atomicUpdate(KeyValueClient kv, String key, Function<String, String> mutator) throws IOException { while (true) { final ConsulResponse<Value> r = kv.getConsulResponseWithValue(key).orElse(null); if (r == null) { // Don't automatically create the document, because it might need to be associated with another node's session. // For example, an RPC endpoint doc is updated by both client and server, but is tied to the server session. throw new IOException("Can't update non-existent document: " + key); } final BigInteger index = r.getIndex(); final String oldValue = r.getResponse().getValueAsString(UTF_8).orElse(missingDocumentValue); final String newValue = mutator.apply(oldValue); if (Objects.equals(newValue, oldValue)) { return; } final PutOptions options = ImmutablePutOptions.builder().cas(index.longValue()).build(); boolean success = kv.putValue(key, newValue, 0, options, UTF_8); if (success) { return; } // todo truncated exponential backoff, please! Die if timeout! //MILLISECONDS.sleep(100); } }
/** * Copy the current immutable object by setting an optional value for the {@link PutOptions#getToken() token} 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 token * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withToken(Optional<String> optional) { @Nullable String value = optional.orElse(null); if (Objects.equals(this.token, value)) return this; return new ImmutablePutOptions(this.cas, this.acquire, this.release, this.dc, value); }
private void createControlDocumentIfDoesNotExist() { final String defaultControlDoc = "{\"paused\":false}"; consul.keyValueClient().putValue(controlKey(), defaultControlDoc, 0, ImmutablePutOptions.builder().cas(0).build(), UTF_8); }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link PutOptions#getDc() dc} attribute. * @param value The value for dc * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withDc(String value) { @Nullable String newValue = Preconditions.checkNotNull(value, "dc"); if (Objects.equals(this.dc, newValue)) return this; return new ImmutablePutOptions(this.cas, this.acquire, this.release, newValue, this.token); }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link PutOptions#getToken() token} attribute. * @param value The value for token * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withToken(String value) { @Nullable String newValue = Preconditions.checkNotNull(value, "token"); if (Objects.equals(this.token, newValue)) return this; return new ImmutablePutOptions(this.cas, this.acquire, this.release, this.dc, newValue); }
/** * Copy the current immutable object by setting a <i>present</i> value for the optional {@link PutOptions#getAcquire() acquire} attribute. * @param value The value for acquire * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withAcquire(String value) { @Nullable String newValue = Preconditions.checkNotNull(value, "acquire"); if (Objects.equals(this.acquire, newValue)) return this; return new ImmutablePutOptions(this.cas, newValue, this.release, this.dc, this.token); }
/** * Copy the current immutable object by setting an optional value for the {@link PutOptions#getCas() cas} 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 cas * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withCas(Optional<Long> optional) { @Nullable Long value = optional.orElse(null); if (Objects.equals(this.cas, value)) return this; return new ImmutablePutOptions(value, this.acquire, this.release, this.dc, this.token); }
/** * Copy the current immutable object by setting an optional value for the {@link PutOptions#getAcquire() acquire} 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 acquire * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withAcquire(Optional<String> optional) { @Nullable String value = optional.orElse(null); if (Objects.equals(this.acquire, value)) return this; return new ImmutablePutOptions(this.cas, value, this.release, this.dc, this.token); }
/** * Copy the current immutable object by setting an optional value for the {@link PutOptions#getRelease() release} 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 release * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withRelease(Optional<String> optional) { @Nullable String value = optional.orElse(null); if (Objects.equals(this.release, value)) return this; return new ImmutablePutOptions(this.cas, this.acquire, value, this.dc, this.token); }
/** * Copy the current immutable object by setting an optional value for the {@link PutOptions#getDc() dc} 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 dc * @return A modified copy of {@code this} object */ public final ImmutablePutOptions withDc(Optional<String> optional) { @Nullable String value = optional.orElse(null); if (Objects.equals(this.dc, value)) return this; return new ImmutablePutOptions(this.cas, this.acquire, this.release, value, this.token); }