Search in sources :

Example 1 with SynchronizedSessionRunner

use of com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner in project fdb-record-layer by FoundationDB.

the class SynchronizedSessionTest method secondSessionShouldTakeLockIfTheFirstOneExpiresAndTheFirstOneCannotContinue.

@Test
public void secondSessionShouldTakeLockIfTheFirstOneExpiresAndTheFirstOneCannotContinue() throws Exception {
    try (SynchronizedSessionRunner session1Runner = newRunnerStartSession(lockSubspace1)) {
        checkActive(session1Runner);
        waitLongEnough();
        // Able to create another session and the new session is active.
        try (SynchronizedSessionRunner session2Runner = newRunnerStartSession(lockSubspace1)) {
            checkActive(session2Runner);
        }
        // Should not be able to use session1, neither by existing runner nor by new runner.
        assertFailedContinueSession(session1Runner);
        assertFailedJoinSession(lockSubspace1, session1Runner.getSessionId());
        session1Runner.endSession();
    }
}
Also used : SynchronizedSessionRunner(com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner) Test(org.junit.jupiter.api.Test)

Example 2 with SynchronizedSessionRunner

use of com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner in project fdb-record-layer by FoundationDB.

the class SynchronizedSessionTest method sessionsOnDifferentLocksShouldNotInterfere.

// Sessions on distinct locks.
@Test
public void sessionsOnDifferentLocksShouldNotInterfere() {
    try (SynchronizedSessionRunner sessionOnLock1 = newRunnerStartSession(lockSubspace1)) {
        sessionOnLock1.run(context -> {
            try (SynchronizedSessionRunner sessionOnLock2 = newRunnerStartSession(lockSubspace2)) {
                checkActive(sessionOnLock2);
                sessionOnLock2.endSession();
            }
            return null;
        });
        sessionOnLock1.endSession();
    }
}
Also used : SynchronizedSessionRunner(com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner) Test(org.junit.jupiter.api.Test)

Example 3 with SynchronizedSessionRunner

use of com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner in project fdb-record-layer by FoundationDB.

the class SynchronizedSessionTest method testRenewLeaseContinuously.

private void testRenewLeaseContinuously(boolean reuseRunner) throws Exception {
    try (SynchronizedSessionRunner session1Runner0 = database.newRunner().startSynchronizedSession(lockSubspace1, 1_000)) {
        UUID session1 = session1Runner0.getSessionId();
        AtomicBoolean session1Stopped = new AtomicBoolean(false);
        Thread longSession = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SynchronizedSessionRunner runner = reuseRunner ? session1Runner0 : database.newRunner().joinSynchronizedSession(lockSubspace1, session1, 1_000);
                checkActive(runner);
            }
            session1Stopped.set(true);
        });
        Thread tryStartSession = new Thread(() -> {
            while (!session1Stopped.get()) {
                assertFailedStartSession(lockSubspace1);
                try {
                    Thread.sleep(random.nextInt(500));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        AtomicBoolean threadsHaveExceptions = new AtomicBoolean(false);
        logExceptionsInThreads(threadsHaveExceptions);
        longSession.start();
        tryStartSession.start();
        tryStartSession.join();
        assertTrue(!threadsHaveExceptions.get());
        session1Runner0.endSession();
    }
}
Also used : SynchronizedSessionRunner(com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UUID(java.util.UUID)

Example 4 with SynchronizedSessionRunner

use of com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner in project fdb-record-layer by FoundationDB.

the class SynchronizedSessionTest method assertFailedContinueSession.

private void assertFailedContinueSession(SynchronizedSessionRunner synchronizedSessionRunner) {
    SynchronizedSessionLockedException exception = assertThrows(SynchronizedSessionLockedException.class, () -> synchronizedSessionRunner.run(c -> null));
    assertEquals("Failed to continue the session", exception.getMessage());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Logger(org.slf4j.Logger) SynchronizedSessionRunner(com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner) Tags(com.apple.test.Tags) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) UUID(java.util.UUID) Subspace(com.apple.foundationdb.subspace.Subspace) Test(org.junit.jupiter.api.Test) KeySpacePath(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpacePath) AtomicLong(java.util.concurrent.atomic.AtomicLong) SynchronizedSessionLockedException(com.apple.foundationdb.synchronizedsession.SynchronizedSessionLockedException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SynchronizedSessionLockedException(com.apple.foundationdb.synchronizedsession.SynchronizedSessionLockedException)

Example 5 with SynchronizedSessionRunner

use of com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner in project fdb-record-layer by FoundationDB.

the class SynchronizedSessionTest method clearSession.

@Test
public void clearSession() {
    try (SynchronizedSessionRunner session1Runner = newRunnerStartSession(lockSubspace1)) {
        checkActive(session1Runner);
        session1Runner.endSession();
        // Runners of the current session should not be able to work (neither existing runner nor newly created runner).
        assertFailedContinueSession(session1Runner);
        assertFailedJoinSession(lockSubspace1, session1Runner.getSessionId());
        // The new session should be able to be created and used right away.
        try (SynchronizedSessionRunner session2Runner = newRunnerStartSession(lockSubspace1)) {
            checkActive(session2Runner);
            // This call should no nothing because Session 1 has ended.
            session1Runner.endSession();
            // Make sure manually ending Session 1 does not clear the current lock.
            checkActive(session2Runner);
            // Use session1Runner to end eny active session (i.e. Session 2 here) even Session 1 has ended.
            session1Runner.endAnySession();
            // Check Session 2 is ended.
            assertFailedContinueSession(session2Runner);
            assertFailedJoinSession(lockSubspace1, session2Runner.getSessionId());
        }
    }
}
Also used : SynchronizedSessionRunner(com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner) Test(org.junit.jupiter.api.Test)

Aggregations

SynchronizedSessionRunner (com.apple.foundationdb.record.provider.foundationdb.synchronizedsession.SynchronizedSessionRunner)8 Test (org.junit.jupiter.api.Test)7 UUID (java.util.UUID)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)1 KeySpacePath (com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpacePath)1 Subspace (com.apple.foundationdb.subspace.Subspace)1 SynchronizedSessionLockedException (com.apple.foundationdb.synchronizedsession.SynchronizedSessionLockedException)1 Tags (com.apple.test.Tags)1 Random (java.util.Random)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Tag (org.junit.jupiter.api.Tag)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1