Search in sources :

Example 1 with RaftMachine

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]);
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Example 2 with RaftMachine

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]);
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Example 3 with RaftMachine

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);
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) ExposedRaftState(org.neo4j.causalclustering.core.consensus.state.ExposedRaftState) OnDemandJobScheduler(org.neo4j.test.OnDemandJobScheduler) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 4 with RaftMachine

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);
    }
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) CompletableFuture(java.util.concurrent.CompletableFuture) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with RaftMachine

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;
}
Also used : List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) TimeoutException(java.util.concurrent.TimeoutException) MemberId(org.neo4j.causalclustering.identity.MemberId) FilteringIterable(org.neo4j.helpers.collection.FilteringIterable) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) MemberId(org.neo4j.causalclustering.identity.MemberId)

Aggregations

RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)12 Test (org.junit.Test)10 ExposedRaftState (org.neo4j.causalclustering.core.consensus.state.ExposedRaftState)5 OnDemandJobScheduler (org.neo4j.test.OnDemandJobScheduler)5 TimeoutException (java.util.concurrent.TimeoutException)4 RaftMachineBuilder (org.neo4j.causalclustering.core.consensus.RaftMachineBuilder)3 MembershipEntry (org.neo4j.causalclustering.core.consensus.membership.MembershipEntry)3 ControlledRenewableTimeoutService (org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService)3 RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)3 FakeClock (org.neo4j.time.FakeClock)3 ArrayList (java.util.ArrayList)2 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)2 TestMessageBuilders.voteRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest)2 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)2 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)2 MemberId (org.neo4j.causalclustering.identity.MemberId)2 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)2 HashMap (java.util.HashMap)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1