Search in sources :

Example 76 with MemberId

use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.

the class CoreBootstrapperTest method shouldSetAllCoreState.

@Test
public void shouldSetAllCoreState() throws Exception {
    // given
    int nodeCount = 100;
    FileSystemAbstraction fileSystem = fileSystemRule.get();
    File classicNeo4jStore = RestoreClusterUtils.createClassicNeo4jStore(testDirectory.directory(), fileSystem, nodeCount, Standard.LATEST_NAME);
    PageCache pageCache = pageCacheRule.getPageCache(fileSystem);
    CoreBootstrapper bootstrapper = new CoreBootstrapper(classicNeo4jStore, pageCache, fileSystem, Config.defaults(), NullLogProvider.getInstance());
    // when
    Set<MemberId> membership = asSet(randomMember(), randomMember(), randomMember());
    CoreSnapshot snapshot = bootstrapper.bootstrap(membership);
    // then
    assertEquals(nodeCount, ((IdAllocationState) snapshot.get(CoreStateType.ID_ALLOCATION)).firstUnallocated(IdType.NODE));
    /* Bootstrapped state is created in RAFT land at index -1 and term -1. */
    assertEquals(0, snapshot.prevIndex());
    assertEquals(0, snapshot.prevTerm());
    /* Lock is initially not taken. */
    assertEquals(new ReplicatedLockTokenState(), snapshot.get(CoreStateType.LOCK_TOKEN));
    /* Raft has the bootstrapped set of members initially. */
    assertEquals(membership, ((RaftCoreState) snapshot.get(CoreStateType.RAFT_CORE_STATE)).committed().members());
    /* The session state is initially empty. */
    assertEquals(new GlobalSessionTrackerState(), snapshot.get(CoreStateType.SESSION_TRACKER));
    LastCommittedIndexFinder lastCommittedIndexFinder = new LastCommittedIndexFinder(new ReadOnlyTransactionIdStore(pageCache, classicNeo4jStore), new ReadOnlyTransactionStore(pageCache, fileSystem, classicNeo4jStore, new Monitors()), NullLogProvider.getInstance());
    long lastCommittedIndex = lastCommittedIndexFinder.getLastCommittedIndex();
    assertEquals(-1, lastCommittedIndex);
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) LastCommittedIndexFinder(org.neo4j.causalclustering.core.state.machines.tx.LastCommittedIndexFinder) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore) GlobalSessionTrackerState(org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState) MemberId(org.neo4j.causalclustering.identity.MemberId) CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) ReadOnlyTransactionIdStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) Monitors(org.neo4j.kernel.monitoring.Monitors) ReplicatedLockTokenState(org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 77 with MemberId

use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.

the class DumpClusterStateTest method createStates.

private void createStates() throws IOException {
    SimpleStorage<MemberId> memberIdStorage = new SimpleFileStorage<>(fsa.get(), clusterStateDirectory.get(), CORE_MEMBER_ID_NAME, new MemberId.Marshal(), NullLogProvider.getInstance());
    memberIdStorage.writeState(new MemberId(UUID.randomUUID()));
    createDurableState(LAST_FLUSHED_NAME, new LongIndexMarshal());
    createDurableState(LOCK_TOKEN_NAME, new ReplicatedLockTokenState.Marshal(new MemberId.Marshal()));
    createDurableState(ID_ALLOCATION_NAME, new IdAllocationState.Marshal());
    createDurableState(SESSION_TRACKER_NAME, new GlobalSessionTrackerState.Marshal(new MemberId.Marshal()));
    /* raft state */
    createDurableState(RAFT_MEMBERSHIP_NAME, new RaftMembershipState.Marshal());
    createDurableState(RAFT_TERM_NAME, new TermState.Marshal());
    createDurableState(RAFT_VOTE_NAME, new VoteState.Marshal(new MemberId.Marshal()));
}
Also used : IdAllocationState(org.neo4j.causalclustering.core.state.machines.id.IdAllocationState) GlobalSessionTrackerState(org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState) MemberId(org.neo4j.causalclustering.identity.MemberId) SimpleFileStorage(org.neo4j.causalclustering.core.state.storage.SimpleFileStorage) VoteState(org.neo4j.causalclustering.core.consensus.vote.VoteState) ReplicatedLockTokenState(org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState) RaftMembershipState(org.neo4j.causalclustering.core.consensus.membership.RaftMembershipState) StateMarshal(org.neo4j.causalclustering.core.state.storage.StateMarshal) TermState(org.neo4j.causalclustering.core.consensus.term.TermState)

Example 78 with MemberId

use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.

the class LeaderOnlyLockManagerTest method shouldIssueLocksOnLeader.

@Test
public void shouldIssueLocksOnLeader() throws Exception {
    // given
    MemberId me = member(0);
    ReplicatedLockTokenStateMachine replicatedLockStateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage(new ReplicatedLockTokenState()));
    DirectReplicator replicator = new DirectReplicator(replicatedLockStateMachine);
    LeaderLocator leaderLocator = mock(LeaderLocator.class);
    when(leaderLocator.getLeader()).thenReturn(me);
    Locks locks = mock(Locks.class);
    when(locks.newClient()).thenReturn(mock(Locks.Client.class));
    LeaderOnlyLockManager lockManager = new LeaderOnlyLockManager(me, replicator, leaderLocator, locks, replicatedLockStateMachine);
    // when
    lockManager.newClient().acquireExclusive(LockTracer.NONE, ResourceTypes.NODE, 0L);
// then
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) InMemoryStateStorage(org.neo4j.causalclustering.core.state.storage.InMemoryStateStorage) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) DirectReplicator(org.neo4j.causalclustering.core.replication.DirectReplicator) Locks(org.neo4j.kernel.impl.locking.Locks) Test(org.junit.Test)

Example 79 with MemberId

use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.

the class LeaderOnlyLockManagerTest method shouldNotIssueLocksOnNonLeader.

@Test
public void shouldNotIssueLocksOnNonLeader() throws Exception {
    // given
    MemberId me = member(0);
    MemberId leader = member(1);
    ReplicatedLockTokenStateMachine replicatedLockStateMachine = new ReplicatedLockTokenStateMachine(new InMemoryStateStorage(new ReplicatedLockTokenState()));
    DirectReplicator replicator = new DirectReplicator(replicatedLockStateMachine);
    LeaderLocator leaderLocator = mock(LeaderLocator.class);
    when(leaderLocator.getLeader()).thenReturn(leader);
    Locks locks = mock(Locks.class);
    when(locks.newClient()).thenReturn(mock(Locks.Client.class));
    LeaderOnlyLockManager lockManager = new LeaderOnlyLockManager(me, replicator, leaderLocator, locks, replicatedLockStateMachine);
    // when
    Locks.Client lockClient = lockManager.newClient();
    try {
        lockClient.acquireExclusive(LockTracer.NONE, ResourceTypes.NODE, 0L);
        fail("Should have thrown exception");
    } catch (AcquireLockTimeoutException e) {
    // expected
    }
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) InMemoryStateStorage(org.neo4j.causalclustering.core.state.storage.InMemoryStateStorage) AcquireLockTimeoutException(org.neo4j.storageengine.api.lock.AcquireLockTimeoutException) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) DirectReplicator(org.neo4j.causalclustering.core.replication.DirectReplicator) Locks(org.neo4j.kernel.impl.locking.Locks) Test(org.junit.Test)

Example 80 with MemberId

use of org.neo4j.causalclustering.identity.MemberId 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

MemberId (org.neo4j.causalclustering.identity.MemberId)114 Test (org.junit.Test)83 HashMap (java.util.HashMap)26 CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)16 CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)15 LeaderLocator (org.neo4j.causalclustering.core.consensus.LeaderLocator)13 ReadReplicaTopology (org.neo4j.causalclustering.discovery.ReadReplicaTopology)12 DirectNetworking (org.neo4j.causalclustering.core.consensus.DirectNetworking)10 RaftTestFixture (org.neo4j.causalclustering.core.consensus.RaftTestFixture)10 CoreTopologyService (org.neo4j.causalclustering.discovery.CoreTopologyService)10 Log (org.neo4j.logging.Log)10 ClusterId (org.neo4j.causalclustering.identity.ClusterId)9 ArrayList (java.util.ArrayList)8 UUID (java.util.UUID)8 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)8 TopologyService (org.neo4j.causalclustering.discovery.TopologyService)7 ByteBuf (io.netty.buffer.ByteBuf)6 File (java.io.File)6 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)6 HashSet (java.util.HashSet)5