Search in sources :

Example 66 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class RaftMachineTest method shouldBecomeLeaderInMajorityOf3.

@Test
public void shouldBecomeLeaderInMajorityOf3() throws Exception {
    // Given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    timeouts.invokeTimeout(ELECTION);
    assertThat(raft.isLeader(), is(false));
    // When
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    // Then
    assertThat(raft.isLeader(), is(true));
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Example 67 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class RaftMachineTest method shouldPersistAtSpecifiedLogIndex.

@Test
public void shouldPersistAtSpecifiedLogIndex() throws Exception {
    // given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).raftLog(raftLog).build();
    raftLog.append(new RaftLogEntry(0, new MemberIdSet(asSet(myself, member1, member2))));
    // when
    raft.handle(appendEntriesRequest().from(member1).prevLogIndex(0).prevLogTerm(0).leaderTerm(0).logEntry(new RaftLogEntry(0, data1)).build());
    // then
    assertEquals(1, raftLog.appendIndex());
    assertEquals(data1, readLogEntry(raftLog, 1).content());
}
Also used : FakeClock(org.neo4j.time.FakeClock) MemberIdSet(org.neo4j.causalclustering.core.consensus.membership.MemberIdSet) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 68 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class RaftMachineTest method newMembersShouldBeIncludedInHeartbeatMessages.

@Test
public void newMembersShouldBeIncludedInHeartbeatMessages() throws Exception {
    // Given
    DirectNetworking network = new DirectNetworking();
    final MemberId newMember = member(99);
    DirectNetworking.Inbound newMemberInbound = network.new Inbound(newMember);
    final OutboundMessageCollector messages = new OutboundMessageCollector();
    newMemberInbound.registerHandler(new Inbound.MessageHandler<RaftMessages.RaftMessage>() {

        @Override
        public void handle(RaftMessages.RaftMessage message) {
            messages.send(newMember, message);
        }
    });
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).outbound(messages).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    // We make ourselves the leader
    timeouts.invokeTimeout(ELECTION);
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    // When
    raft.setTargetMembershipSet(asSet(myself, member1, member2, newMember));
    network.processMessages();
    timeouts.invokeTimeout(RaftMachine.Timeouts.HEARTBEAT);
    network.processMessages();
    // Then
    assertEquals(RaftMessages.AppendEntries.Request.class, messages.sentTo(newMember).get(0).getClass());
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) FakeClock(org.neo4j.time.FakeClock) Inbound(org.neo4j.causalclustering.messaging.Inbound) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) MemberId(org.neo4j.causalclustering.identity.MemberId) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) Test(org.junit.Test)

Example 69 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class CatchupGoalTrackerTest method shouldNotFinishIfRoundsNotExhausted.

@Test
public void shouldNotFinishIfRoundsNotExhausted() throws Exception {
    FakeClock clock = Clocks.fakeClock();
    StubLog log = new StubLog();
    long appendIndex = 10;
    log.setAppendIndex(appendIndex);
    CatchupGoalTracker catchupGoalTracker = new CatchupGoalTracker(log, clock, ROUND_TIMEOUT, CATCHUP_TIMEOUT);
    for (int i = 0; i < CatchupGoalTracker.MAX_ROUNDS - 5; i++) {
        appendIndex += 10;
        log.setAppendIndex(appendIndex);
        clock.forward(ROUND_TIMEOUT + 1, TimeUnit.MILLISECONDS);
        catchupGoalTracker.updateProgress(new FollowerState().onSuccessResponse(appendIndex));
    }
    // then
    assertFalse(catchupGoalTracker.isGoalAchieved());
    assertFalse(catchupGoalTracker.isFinished());
}
Also used : FakeClock(org.neo4j.time.FakeClock) FollowerState(org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState) Test(org.junit.Test)

Example 70 with FakeClock

use of org.neo4j.time.FakeClock in project neo4j by neo4j.

the class CatchupGoalTrackerTest method shouldAchieveGoalIfWithinRoundTimeout.

@Test
public void shouldAchieveGoalIfWithinRoundTimeout() throws Exception {
    FakeClock clock = Clocks.fakeClock();
    StubLog log = new StubLog();
    log.setAppendIndex(10);
    CatchupGoalTracker catchupGoalTracker = new CatchupGoalTracker(log, clock, ROUND_TIMEOUT, CATCHUP_TIMEOUT);
    clock.forward(ROUND_TIMEOUT - 5, TimeUnit.MILLISECONDS);
    catchupGoalTracker.updateProgress(new FollowerState().onSuccessResponse(10));
    assertTrue(catchupGoalTracker.isGoalAchieved());
    assertTrue(catchupGoalTracker.isFinished());
}
Also used : FakeClock(org.neo4j.time.FakeClock) FollowerState(org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState) Test(org.junit.Test)

Aggregations

FakeClock (org.neo4j.time.FakeClock)111 Test (org.junit.Test)82 Test (org.junit.jupiter.api.Test)21 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)20 ControlledRenewableTimeoutService (org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService)17 MembershipEntry (org.neo4j.causalclustering.core.consensus.membership.MembershipEntry)16 RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)16 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)9 QueryLogger (org.neo4j.kernel.impl.query.QueryLoggerKernelExtension.QueryLogger)9 User (org.neo4j.kernel.impl.security.User)9 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)7 Channel (org.jboss.netty.channel.Channel)5 Path (java.nio.file.Path)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Response (javax.ws.rs.core.Response)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 InputStream (java.io.InputStream)3 ExecutionException (java.util.concurrent.ExecutionException)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)3