use of org.neo4j.causalclustering.core.consensus.RaftMachine in project neo4j by neo4j.
the class RoleProcedureTest method shouldReturnLeader.
@Test
public void shouldReturnLeader() throws Exception {
// given
RaftMachine raft = mock(RaftMachine.class);
when(raft.isLeader()).thenReturn(true);
RoleProcedure proc = new CoreRoleProcedure(raft);
// when
RawIterator<Object[], ProcedureException> result = proc.apply(null, null);
// then
assertEquals(Role.LEADER.name(), single(result)[0]);
}
use of org.neo4j.causalclustering.core.consensus.RaftMachine in project neo4j by neo4j.
the class RoleProcedureTest method shouldReturnFollower.
@Test
public void shouldReturnFollower() throws Exception {
// given
RaftMachine raft = mock(RaftMachine.class);
when(raft.isLeader()).thenReturn(false);
RoleProcedure proc = new CoreRoleProcedure(raft);
// when
RawIterator<Object[], ProcedureException> result = proc.apply(null, null);
// then
assertEquals(Role.FOLLOWER.name(), single(result)[0]);
}
use of org.neo4j.causalclustering.core.consensus.RaftMachine in project neo4j by neo4j.
the class MembershipWaiterTest method shouldWaitUntilLeaderCommitIsAvailable.
@Test
public void shouldWaitUntilLeaderCommitIsAvailable() throws Exception {
OnDemandJobScheduler jobScheduler = new OnDemandJobScheduler();
MembershipWaiter waiter = new MembershipWaiter(member(0), jobScheduler, () -> dbHealth, 500, NullLogProvider.getInstance());
InMemoryRaftLog raftLog = new InMemoryRaftLog();
raftLog.append(new RaftLogEntry(0, valueOf(0)));
ExposedRaftState raftState = RaftStateBuilder.raftState().votingMembers(member(0)).leaderCommit(0).entryLog(raftLog).commitIndex(0L).build().copy();
RaftMachine raft = mock(RaftMachine.class);
when(raft.state()).thenReturn(raftState);
CompletableFuture<Boolean> future = waiter.waitUntilCaughtUpMember(raft);
jobScheduler.runJob();
future.get(1, TimeUnit.SECONDS);
}
use of org.neo4j.causalclustering.core.consensus.RaftMachine in project neo4j by neo4j.
the class ElectionUtil method waitForLeaderAgreement.
public static MemberId waitForLeaderAgreement(Iterable<RaftMachine> validRafts, long maxTimeMillis) throws InterruptedException, TimeoutException {
long viewCount = Iterables.count(validRafts);
Map<MemberId, MemberId> leaderViews = new HashMap<>();
CompletableFuture<MemberId> futureAgreedLeader = new CompletableFuture<>();
Collection<Runnable> destructors = new ArrayList<>();
for (RaftMachine raft : validRafts) {
destructors.add(leaderViewUpdatingListener(raft, validRafts, leaderViews, viewCount, futureAgreedLeader));
}
try {
try {
return futureAgreedLeader.get(maxTimeMillis, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
} finally {
destructors.forEach(Runnable::run);
}
}
use of org.neo4j.causalclustering.core.consensus.RaftMachine in project neo4j by neo4j.
the class DisconnectLeaderScenario method oneIteration.
private long oneIteration(long leaderStabilityMaxTimeMillis) throws InterruptedException, TimeoutException {
List<RaftMachine> rafts = fixture.rafts.stream().map(Fixture.RaftFixture::raftMachine).collect(toList());
MemberId oldLeader = ElectionUtil.waitForLeaderAgreement(rafts, leaderStabilityMaxTimeMillis);
long startTime = System.currentTimeMillis();
fixture.net.disconnect(oldLeader);
MemberId newLeader = ElectionUtil.waitForLeaderAgreement(new FilteringIterable<>(rafts, raft -> !raft.identity().equals(oldLeader)), leaderStabilityMaxTimeMillis);
// this should be guaranteed by the waitForLeaderAgreement call
assert !newLeader.equals(oldLeader);
return System.currentTimeMillis() - startTime;
}
Aggregations