Search in sources :

Example 1 with PrimitiveState

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);
}
Also used : AtomicLock(io.atomix.core.lock.AtomicLock) AbstractAsyncPrimitive(io.atomix.primitive.AbstractAsyncPrimitive) ProxyClient(io.atomix.primitive.proxy.ProxyClient) PrimitiveState(io.atomix.primitive.PrimitiveState) CompletableFuture(java.util.concurrent.CompletableFuture) Maps(com.google.common.collect.Maps) AsyncAtomicLock(io.atomix.core.lock.AsyncAtomicLock) PrimitiveRegistry(io.atomix.primitive.PrimitiveRegistry) Consumer(java.util.function.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Optional(java.util.Optional) Scheduled(io.atomix.utils.concurrent.Scheduled) PrimitiveException(io.atomix.primitive.PrimitiveException) Version(io.atomix.utils.time.Version) Optional(java.util.Optional) PrimitiveState(io.atomix.primitive.PrimitiveState)

Example 2 with PrimitiveState

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());
}
Also used : ProxyClient(io.atomix.primitive.proxy.ProxyClient) PrimitiveState(io.atomix.primitive.PrimitiveState) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) Maps(com.google.common.collect.Maps) PartitionId(io.atomix.primitive.partition.PartitionId) Consumer(java.util.function.Consumer) OptionalLong(java.util.OptionalLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Scheduled(io.atomix.utils.concurrent.Scheduled) PrimitiveException(io.atomix.primitive.PrimitiveException) PrimitiveState(io.atomix.primitive.PrimitiveState)

Aggregations

Maps (com.google.common.collect.Maps)2 PrimitiveException (io.atomix.primitive.PrimitiveException)2 PrimitiveState (io.atomix.primitive.PrimitiveState)2 ProxyClient (io.atomix.primitive.proxy.ProxyClient)2 Scheduled (io.atomix.utils.concurrent.Scheduled)2 Duration (java.time.Duration)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Consumer (java.util.function.Consumer)2 AsyncAtomicLock (io.atomix.core.lock.AsyncAtomicLock)1 AtomicLock (io.atomix.core.lock.AtomicLock)1 AbstractAsyncPrimitive (io.atomix.primitive.AbstractAsyncPrimitive)1 PrimitiveRegistry (io.atomix.primitive.PrimitiveRegistry)1 PartitionId (io.atomix.primitive.partition.PartitionId)1 Version (io.atomix.utils.time.Version)1 Optional (java.util.Optional)1 OptionalLong (java.util.OptionalLong)1 Supplier (java.util.function.Supplier)1