use of io.atomix.primitive.PrimitiveState in project atomix by atomix.
the class AtomicLockProxy method tryLock.
@Override
public CompletableFuture<Optional<Version>> tryLock() {
// If the proxy is currently disconnected from the cluster, we can just fail the lock attempt here.
PrimitiveState state = getProxyClient().getPartition(name()).getState();
if (state != PrimitiveState.CONNECTED) {
return CompletableFuture.completedFuture(Optional.empty());
}
// Create and register a new attempt and invoke the LOCK operation on teh replicated state machine with
// a 0 timeout. The timeout will cause the state machine to immediately reject the request if the lock is
// already owned by another process.
LockAttempt attempt = new LockAttempt();
getProxyClient().acceptBy(name(), service -> service.lock(attempt.id(), 0)).whenComplete((result, error) -> {
if (error != null) {
attempt.completeExceptionally(error);
}
});
return attempt.thenApply(Optional::ofNullable);
}
use of io.atomix.primitive.PrimitiveState in project atomix by atomix.
the class KeyLock method tryLock.
/**
* Attempts to lock the key.
*
* @return a future to be completed once the lock attempt is complete
*/
CompletableFuture<OptionalLong> tryLock() {
// If the proxy is currently disconnected from the cluster, we can just fail the lock attempt here.
PrimitiveState state = client.getPartition(partitionId).getState();
if (state != PrimitiveState.CONNECTED) {
return CompletableFuture.completedFuture(OptionalLong.empty());
}
// Create and register a new attempt and invoke the LOCK operation on teh replicated state machine with
// a 0 timeout. The timeout will cause the state machine to immediately reject the request if the lock is
// already owned by another process.
LockFuture future = new LockFuture();
client.acceptOn(partitionId, service -> service.lock(key, future.id(), 0)).whenComplete((result, error) -> {
if (error != null) {
future.completeExceptionally(error);
}
});
return future.thenApply(v -> v != null ? OptionalLong.of(v) : OptionalLong.empty());
}
Aggregations