Search in sources :

Example 1 with Version

use of io.atomix.utils.time.Version in project atomix by atomix.

the class DistributedLockTest method testReleaseOnClose.

/**
 * Tests releasing a lock when the client's session is closed.
 */
@Test
public void testReleaseOnClose() throws Throwable {
    AsyncDistributedLock lock1 = atomix().lockBuilder("test-lock-on-close", protocol()).build().async();
    AsyncDistributedLock lock2 = atomix().lockBuilder("test-lock-on-close", protocol()).build().async();
    lock1.lock().join();
    CompletableFuture<Version> future = lock2.lock();
    lock1.close();
    future.join();
}
Also used : Version(io.atomix.utils.time.Version) AsyncDistributedLock(io.atomix.core.lock.AsyncDistributedLock) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Example 2 with Version

use of io.atomix.utils.time.Version 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 3 with Version

use of io.atomix.utils.time.Version in project atomix by atomix.

the class DistributedLockProxy method handleLocked.

private void handleLocked(LockEvent event) {
    CompletableFuture<Version> future = futures.remove(event.id());
    if (future != null) {
        this.lock = event.id();
        future.complete(new Version(event.version()));
    }
}
Also used : Version(io.atomix.utils.time.Version)

Example 4 with Version

use of io.atomix.utils.time.Version in project atomix by atomix.

the class AtomicSemaphoreTest method testQueue.

@Test(timeout = 10000)
public void testQueue() throws Exception {
    Atomix atomix = atomix();
    AtomicSemaphore semaphore = atomix.atomicSemaphoreBuilder("test-semaphore-queue").withProtocol(protocol()).withInitialCapacity(10).build();
    CompletableFuture<Version> future20 = semaphore.async().acquire(20);
    CompletableFuture<Version> future11 = semaphore.async().acquire(11);
    semaphore.increasePermits(1);
    assertNotNull(future11);
    assertFalse(future20.isDone());
    assertEquals(0, semaphore.availablePermits());
    CompletableFuture<Version> future21 = semaphore.async().acquire(21);
    CompletableFuture<Version> future5 = semaphore.async().acquire(5);
    // wakeup 5 and 20
    semaphore.release(25);
    future5.get(10, TimeUnit.SECONDS);
    future20.get(10, TimeUnit.SECONDS);
    assertFalse(future21.isDone());
}
Also used : Atomix(io.atomix.core.Atomix) Version(io.atomix.utils.time.Version) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Example 5 with Version

use of io.atomix.utils.time.Version in project atomix by atomix.

the class AtomicLockTest method testLockUnlock.

/**
 * Tests locking and unlocking a lock.
 */
@Test
public void testLockUnlock() throws Throwable {
    AtomicLock lock = atomix().atomicLockBuilder("test-lock-unlock").withProtocol(protocol()).build();
    Version version = lock.lock();
    assertTrue(lock.isLocked());
    assertTrue(lock.isLocked(version));
    assertFalse(lock.isLocked(new Version(version.value() + 1)));
    lock.unlock();
    assertFalse(lock.isLocked());
    assertFalse(lock.isLocked(version));
}
Also used : Version(io.atomix.utils.time.Version) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Aggregations

Version (io.atomix.utils.time.Version)5 AbstractPrimitiveTest (io.atomix.core.AbstractPrimitiveTest)3 Test (org.junit.Test)3 Maps (com.google.common.collect.Maps)1 Atomix (io.atomix.core.Atomix)1 AsyncAtomicLock (io.atomix.core.lock.AsyncAtomicLock)1 AsyncDistributedLock (io.atomix.core.lock.AsyncDistributedLock)1 AtomicLock (io.atomix.core.lock.AtomicLock)1 AbstractAsyncPrimitive (io.atomix.primitive.AbstractAsyncPrimitive)1 PrimitiveException (io.atomix.primitive.PrimitiveException)1 PrimitiveRegistry (io.atomix.primitive.PrimitiveRegistry)1 PrimitiveState (io.atomix.primitive.PrimitiveState)1 ProxyClient (io.atomix.primitive.proxy.ProxyClient)1 Scheduled (io.atomix.utils.concurrent.Scheduled)1 Duration (java.time.Duration)1 Map (java.util.Map)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Consumer (java.util.function.Consumer)1