Search in sources :

Example 1 with InProcessLockProvider

use of org.apache.hudi.client.transaction.lock.InProcessLockProvider in project hudi by apache.

the class TestInProcessLockProvider method testLockAcquisition.

@Test
public void testLockAcquisition() {
    InProcessLockProvider inProcessLockProvider = new InProcessLockProvider(lockConfiguration, hadoopConfiguration);
    assertDoesNotThrow(() -> {
        inProcessLockProvider.lock();
    });
    assertDoesNotThrow(() -> {
        inProcessLockProvider.unlock();
    });
}
Also used : InProcessLockProvider(org.apache.hudi.client.transaction.lock.InProcessLockProvider) Test(org.junit.jupiter.api.Test)

Example 2 with InProcessLockProvider

use of org.apache.hudi.client.transaction.lock.InProcessLockProvider in project hudi by apache.

the class TestInProcessLockProvider method testTryLockAcquisitionWithTimeout.

@Test
public void testTryLockAcquisitionWithTimeout() {
    InProcessLockProvider inProcessLockProvider = new InProcessLockProvider(lockConfiguration, hadoopConfiguration);
    Assertions.assertTrue(inProcessLockProvider.tryLock(1, TimeUnit.MILLISECONDS));
    assertDoesNotThrow(() -> {
        inProcessLockProvider.unlock();
    });
}
Also used : InProcessLockProvider(org.apache.hudi.client.transaction.lock.InProcessLockProvider) Test(org.junit.jupiter.api.Test)

Example 3 with InProcessLockProvider

use of org.apache.hudi.client.transaction.lock.InProcessLockProvider in project hudi by apache.

the class TestInProcessLockProvider method testTryLockReAcquisitionBySameThread.

@Test
public void testTryLockReAcquisitionBySameThread() {
    InProcessLockProvider inProcessLockProvider = new InProcessLockProvider(lockConfiguration, hadoopConfiguration);
    Assertions.assertTrue(inProcessLockProvider.tryLock());
    assertThrows(HoodieLockException.class, () -> {
        inProcessLockProvider.tryLock(1, TimeUnit.MILLISECONDS);
    });
    assertDoesNotThrow(() -> {
        inProcessLockProvider.unlock();
    });
}
Also used : InProcessLockProvider(org.apache.hudi.client.transaction.lock.InProcessLockProvider) Test(org.junit.jupiter.api.Test)

Example 4 with InProcessLockProvider

use of org.apache.hudi.client.transaction.lock.InProcessLockProvider in project hudi by apache.

the class TestInProcessLockProvider method testUnlockWithoutLock.

@Test
public void testUnlockWithoutLock() {
    InProcessLockProvider inProcessLockProvider = new InProcessLockProvider(lockConfiguration, hadoopConfiguration);
    assertDoesNotThrow(() -> {
        inProcessLockProvider.unlock();
    });
}
Also used : InProcessLockProvider(org.apache.hudi.client.transaction.lock.InProcessLockProvider) Test(org.junit.jupiter.api.Test)

Example 5 with InProcessLockProvider

use of org.apache.hudi.client.transaction.lock.InProcessLockProvider in project hudi by apache.

the class TestInProcessLockProvider method testTryLockAcquisitionBeforeTimeOutFromTwoThreads.

@Test
public void testTryLockAcquisitionBeforeTimeOutFromTwoThreads() {
    final InProcessLockProvider inProcessLockProvider = new InProcessLockProvider(lockConfiguration, hadoopConfiguration);
    final int threadCount = 3;
    final long awaitMaxTimeoutMs = 2000L;
    final CountDownLatch latch = new CountDownLatch(threadCount);
    final AtomicBoolean writer1Completed = new AtomicBoolean(false);
    final AtomicBoolean writer2Completed = new AtomicBoolean(false);
    // Let writer1 get the lock first, then wait for others
    // to join the sync up point.
    Thread writer1 = new Thread(() -> {
        Assertions.assertTrue(inProcessLockProvider.tryLock());
        latch.countDown();
        try {
            latch.await(awaitMaxTimeoutMs, TimeUnit.MILLISECONDS);
            // Following sleep is to make sure writer2 attempts
            // to try lock and to get bocked on the lock which
            // this thread is currently holding.
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // 
        }
        assertDoesNotThrow(() -> {
            inProcessLockProvider.unlock();
        });
        writer1Completed.set(true);
    });
    writer1.start();
    // Writer2 will block on trying to acquire the lock
    // and will eventually get the lock before the timeout.
    Thread writer2 = new Thread(() -> {
        latch.countDown();
        Assertions.assertTrue(inProcessLockProvider.tryLock(awaitMaxTimeoutMs, TimeUnit.MILLISECONDS));
        assertDoesNotThrow(() -> {
            inProcessLockProvider.unlock();
        });
        writer2Completed.set(true);
    });
    writer2.start();
    // Let writer1 and writer2 wait at the sync up
    // point to make sure they run in parallel and
    // one get blocked by the other.
    latch.countDown();
    try {
        writer1.join();
        writer2.join();
    } catch (InterruptedException e) {
    // 
    }
    // Make sure both writers actually completed good
    Assertions.assertTrue(writer1Completed.get());
    Assertions.assertTrue(writer2Completed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InProcessLockProvider(org.apache.hudi.client.transaction.lock.InProcessLockProvider) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

InProcessLockProvider (org.apache.hudi.client.transaction.lock.InProcessLockProvider)11 Test (org.junit.jupiter.api.Test)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 CountDownLatch (java.util.concurrent.CountDownLatch)1