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);
}
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()));
}
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
}
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
}
}
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());
}
}
Aggregations