Search in sources :

Example 16 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class ContinuousJobTest method shouldTerminateOnStop.

@Test
public void shouldTerminateOnStop() throws Exception {
    // given: this task is gonna take >20 ms total
    Semaphore semaphore = new Semaphore(-20);
    Runnable task = () -> {
        // 1 ms
        LockSupport.parkNanos(1_000_000);
        semaphore.release();
    };
    Neo4jJobScheduler scheduler = new Neo4jJobScheduler();
    ContinuousJob continuousJob = new ContinuousJob(scheduler, jobGroup, task, NullLogProvider.getInstance());
    // when
    long startTime = System.currentTimeMillis();
    try (Lifespan ignored = new Lifespan(scheduler, continuousJob)) {
        semaphore.acquireUninterruptibly();
    }
    long runningTime = System.currentTimeMillis() - startTime;
    // then
    assertThat(runningTime, lessThan(DEFAULT_TIMEOUT_MS));
    //noinspection StatementWithEmptyBody
    while (semaphore.tryAcquire()) {
    // consume all outstanding permits
    }
    // no more permits should be granted
    semaphore.tryAcquire(10, MILLISECONDS);
}
Also used : Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) Semaphore(java.util.concurrent.Semaphore) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Example 17 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class ContinuousJobTest method shouldRunJobContinuously.

@Test
public void shouldRunJobContinuously() throws Throwable {
    // given
    CountDownLatch latch = new CountDownLatch(10);
    Runnable task = latch::countDown;
    Neo4jJobScheduler scheduler = new Neo4jJobScheduler();
    ContinuousJob continuousJob = new ContinuousJob(scheduler, jobGroup, task, NullLogProvider.getInstance());
    // when
    try (Lifespan ignored = new Lifespan(scheduler, continuousJob)) {
        //then
        assertTrue(latch.await(DEFAULT_TIMEOUT_MS, MILLISECONDS));
    }
}
Also used : Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Example 18 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class TransactionLogCatchUpWriterTest method simulateStoreCopy.

private org.neo4j.kernel.impl.store.StoreId simulateStoreCopy() throws IOException {
    // create an empty store
    org.neo4j.kernel.impl.store.StoreId storeId;
    NeoStoreDataSource ds = dsRule.getDataSource(storeDir, fs, pageCache, emptyMap());
    try (Lifespan ignored = new Lifespan(ds)) {
        storeId = ds.getStoreId();
    }
    // we don't have log files after a store copy
    PhysicalLogFiles logFiles = new PhysicalLogFiles(storeDir, fsRule.get());
    logFiles.accept((file, version) -> file.delete());
    return storeId;
}
Also used : NeoStoreDataSource(org.neo4j.kernel.NeoStoreDataSource) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) PhysicalLogFiles(org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles)

Example 19 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class SegmentedRaftLogRotationTest method shouldBeAbleToRecoverToLatestStateAfterRotation.

@Test
public void shouldBeAbleToRecoverToLatestStateAfterRotation() throws Throwable {
    // Given
    int term = 0;
    long indexToRestoreTo;
    try (Lifespan lifespan = new Lifespan()) {
        SegmentedRaftLog log = lifespan.add(createRaftLog(ROTATE_AT_SIZE_IN_BYTES));
        log.append(new RaftLogEntry(term, replicatedStringOfBytes(ROTATE_AT_SIZE_IN_BYTES - 40)));
        indexToRestoreTo = log.append(new RaftLogEntry(term, ReplicatedInteger.valueOf(1)));
    }
    // When
    SegmentedRaftLog log = life.add(createRaftLog(ROTATE_AT_SIZE_IN_BYTES));
    // Then
    assertEquals(indexToRestoreTo, log.appendIndex());
    assertEquals(term, log.readEntryTerm(indexToRestoreTo));
}
Also used : Lifespan(org.neo4j.kernel.lifecycle.Lifespan) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 20 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class ReplicatedLockTokenStateMachineTest method shouldPersistAndRecoverState.

@Test
public void shouldPersistAndRecoverState() throws Exception {
    // given
    EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
    fsa.mkdir(testDir.directory());
    StateMarshal<ReplicatedLockTokenState> marshal = new ReplicatedLockTokenState.Marshal(new MemberId.Marshal());
    MemberId memberA = member(0);
    MemberId memberB = member(1);
    int candidateId;
    DurableStateStorage<ReplicatedLockTokenState> storage = new DurableStateStorage<>(fsa, testDir.directory(), "state", marshal, 100, NullLogProvider.getInstance());
    try (Lifespan lifespan = new Lifespan(storage)) {
        ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(storage);
        // when
        candidateId = 0;
        stateMachine.applyCommand(new ReplicatedLockTokenRequest(memberA, candidateId), 0, r -> {
        });
        candidateId = 1;
        stateMachine.applyCommand(new ReplicatedLockTokenRequest(memberB, candidateId), 1, r -> {
        });
        stateMachine.flush();
        fsa.crash();
    }
    // then
    DurableStateStorage<ReplicatedLockTokenState> storage2 = new DurableStateStorage<>(fsa, testDir.directory(), "state", marshal, 100, NullLogProvider.getInstance());
    try (Lifespan lifespan = new Lifespan(storage2)) {
        ReplicatedLockTokenState initialState = storage2.getInitialState();
        assertEquals(memberB, initialState.get().owner());
        assertEquals(candidateId, initialState.get().id());
    }
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) StateMarshal(org.neo4j.causalclustering.core.state.storage.StateMarshal) DurableStateStorage(org.neo4j.causalclustering.core.state.storage.DurableStateStorage) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Aggregations

Lifespan (org.neo4j.kernel.lifecycle.Lifespan)32 Test (org.junit.Test)22 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)8 Transaction (org.neo4j.graphdb.Transaction)6 File (java.io.File)5 Node (org.neo4j.graphdb.Node)4 DurableStateStorage (org.neo4j.causalclustering.core.state.storage.DurableStateStorage)3 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)3 StoreChannel (org.neo4j.io.fs.StoreChannel)3 CountsOracle (org.neo4j.kernel.impl.store.CountsOracle)3 PhysicalLogFiles (org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles)3 ReadOnlyTransactionIdStore (org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore)3 TransactionIdStore (org.neo4j.kernel.impl.transaction.log.TransactionIdStore)3 Monitors (org.neo4j.kernel.monitoring.Monitors)3 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Future (java.util.concurrent.Future)2 StateMarshal (org.neo4j.causalclustering.core.state.storage.StateMarshal)2 MemberId (org.neo4j.causalclustering.identity.MemberId)2 PageCache (org.neo4j.io.pagecache.PageCache)2