Search in sources :

Example 1 with DistributedLock

use of com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock in project sofa-jraft by sofastack.

the class MemoryKVStoreTest method tryLockWith.

/**
 * Test method: {@link MemoryRawKVStore#tryLockWith(byte[], byte[], boolean, DistributedLock.Acquirer, KVStoreClosure)}
 */
@Test
public void tryLockWith() throws InterruptedException {
    byte[] lockKey = makeKey("lock_test");
    final DistributedLock<byte[]> lock = new LocalLock(lockKey, 3, TimeUnit.SECONDS, this.kvStore);
    assertNotNull(lock);
    assertTrue(lock.tryLock());
    assertTrue(lock.tryLock());
    assertTrue(lock.tryLock(3001, TimeUnit.MILLISECONDS));
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread(() -> {
        final DistributedLock<byte[]> lock2 = new LocalLock(lockKey, 3, TimeUnit.SECONDS, this.kvStore);
        try {
            assertTrue(!lock2.tryLock());
        } finally {
            latch.countDown();
        }
    }, "lock1-thread").start();
    latch.await();
    lock.unlock();
    assertTrue(lock.tryLock());
    lock.unlock();
}
Also used : DistributedLock(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) CountDownLatch(java.util.concurrent.CountDownLatch) LocalLock(com.alipay.sofa.jraft.rhea.storage.LocalLock) Test(org.junit.Test)

Example 2 with DistributedLock

use of com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock in project sofa-jraft by sofastack.

the class AbstractRheaKVStoreTest method distributedLockTest.

/**
 * Test method: {@link RheaKVStore#getDistributedLock(byte[], long, TimeUnit)}
 */
private void distributedLockTest(RheaKVStore store) throws InterruptedException {
    // regions: 1 -> [null, g), 2 -> [g, null)
    byte[] lockKey = makeKey("lock_test");
    checkRegion(store, lockKey, 2);
    final DistributedLock<byte[]> lock = store.getDistributedLock(lockKey, 3, TimeUnit.SECONDS);
    assertNotNull(lock);
    // can lock
    assertTrue(lock.tryLock());
    // reentrant lock
    assertTrue(lock.tryLock());
    final CountDownLatch latch1 = new CountDownLatch(1);
    Thread t = new Thread(() -> {
        final DistributedLock<byte[]> lock1 = store.getDistributedLock(lockKey, 3, TimeUnit.SECONDS);
        try {
            // fail to lock
            assertTrue(!lock1.tryLock());
        } finally {
            latch1.countDown();
        }
    }, "locker1-thread");
    t.setDaemon(true);
    t.start();
    latch1.await();
    final CountDownLatch latch2 = new CountDownLatch(1);
    t = new Thread(() -> {
        final DistributedLock<byte[]> lock2 = store.getDistributedLock(lockKey, 3, TimeUnit.SECONDS);
        try {
            // Waiting for the lock to expire and lock successfully
            assertTrue(lock2.tryLock(3100, TimeUnit.MILLISECONDS));
        } finally {
            lock2.unlock();
            latch2.countDown();
        }
    }, "locker2-thread");
    t.setDaemon(true);
    t.start();
    latch2.await();
    assertTrue(lock.tryLock());
    assertTrue(lock.tryLock());
    assertTrue(lock.tryLock());
    lock.unlock();
    final CountDownLatch latch3 = new CountDownLatch(1);
    t = new Thread(() -> {
        final DistributedLock<byte[]> lock3 = store.getDistributedLock(lockKey, 3, TimeUnit.SECONDS);
        try {
            // The previous lock was not released and the lock failed.
            assertTrue(!lock3.tryLock());
        } finally {
            latch3.countDown();
        }
    }, "locker3-thread");
    t.setDaemon(true);
    t.start();
    latch3.await();
    lock.unlock();
    lock.unlock();
    final CountDownLatch latch4 = new CountDownLatch(1);
    t = new Thread(() -> {
        final DistributedLock<byte[]> lock4 = store.getDistributedLock(lockKey, 3, TimeUnit.SECONDS);
        try {
            // lock success
            assertTrue(lock4.tryLock());
        } finally {
            lock4.unlock();
            latch4.countDown();
        }
    }, "locker4-thread");
    t.setDaemon(true);
    t.start();
    latch4.await();
    // lock with lease scheduler
    final ScheduledExecutorService watchdog = Executors.newScheduledThreadPool(1);
    final DistributedLock<byte[]> lockWithScheduler = store.getDistributedLock(lockKey, 3, TimeUnit.SECONDS, watchdog);
    lockWithScheduler.tryLock();
    Thread.sleep(5000);
    final CountDownLatch latch5 = new CountDownLatch(1);
    t = new Thread(() -> {
        final DistributedLock<byte[]> lock5 = store.getDistributedLock(lockKey, 3, TimeUnit.SECONDS);
        try {
            // Locking failed because lockWithScheduler is renewing
            assertTrue(!lock5.tryLock());
        } finally {
            latch5.countDown();
        }
    }, "locker5-thread");
    t.setDaemon(true);
    t.start();
    latch5.await();
    lockWithScheduler.unlock();
    final CountDownLatch latch6 = new CountDownLatch(1);
    t = new Thread(() -> {
        final DistributedLock<byte[]> lock6 = store.getDistributedLock(lockKey, 3, TimeUnit.SECONDS);
        try {
            // lock success
            assertTrue(lock6.tryLock());
        } finally {
            lock6.unlock();
            latch6.countDown();
        }
    }, "locker6-thread");
    t.setDaemon(true);
    t.start();
    latch6.await();
}
Also used : DistributedLock(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with DistributedLock

use of com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock in project sofa-jraft by sofastack.

the class RocksKVStoreTest method tryLockWith.

/**
 * Test method: {@link RocksRawKVStore#tryLockWith(byte[], byte[], boolean, DistributedLock.Acquirer, KVStoreClosure)}
 */
@Test
public void tryLockWith() throws InterruptedException {
    byte[] lockKey = makeKey("lock_test");
    final DistributedLock<byte[]> lock = new LocalLock(lockKey, 3, TimeUnit.SECONDS, this.kvStore);
    assertNotNull(lock);
    assertTrue(lock.tryLock());
    assertTrue(lock.tryLock());
    assertTrue(lock.tryLock(3001, TimeUnit.MILLISECONDS));
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread(() -> {
        final DistributedLock<byte[]> lock2 = new LocalLock(lockKey, 3, TimeUnit.SECONDS, this.kvStore);
        try {
            assertFalse(lock2.tryLock());
        } finally {
            latch.countDown();
        }
    }, "lock1-thread").start();
    latch.await();
    lock.unlock();
    assertTrue(lock.tryLock());
    lock.unlock();
}
Also used : DistributedLock(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) CountDownLatch(java.util.concurrent.CountDownLatch) LocalLock(com.alipay.sofa.jraft.rhea.storage.LocalLock) Test(org.junit.Test)

Aggregations

DistributedLock (com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 LocalLock (com.alipay.sofa.jraft.rhea.storage.LocalLock)2 Test (org.junit.Test)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1