Search in sources :

Example 11 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService in project neo4j by neo4j.

the class RaftMachineTest method leaderShouldSendHeartBeatsOnHeartbeatTimeout.

@Test
public void leaderShouldSendHeartBeatsOnHeartbeatTimeout() throws Exception {
    // Given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    OutboundMessageCollector messages = new OutboundMessageCollector();
    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();
    timeouts.invokeTimeout(ELECTION);
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    // When
    timeouts.invokeTimeout(RaftMachine.Timeouts.HEARTBEAT);
    // Then
    assertTrue(last(messages.sentTo(member1)) instanceof RaftMessages.Heartbeat);
    assertTrue(last(messages.sentTo(member2)) instanceof RaftMessages.Heartbeat);
}
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 12 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService in project neo4j by neo4j.

the class RaftMachineTest method shouldRequestVotesOnElectionTimeout.

@Test
public void shouldRequestVotesOnElectionTimeout() throws Exception {
    // Given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    OutboundMessageCollector messages = new OutboundMessageCollector();
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).outbound(messages).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    // When
    timeouts.invokeTimeout(ELECTION);
    // Then
    assertThat(messages.sentTo(myself).size(), equalTo(0));
    assertThat(messages.sentTo(member1).size(), equalTo(1));
    assertThat(messages.sentTo(member1).get(0), instanceOf(RaftMessages.Vote.Request.class));
    assertThat(messages.sentTo(member2).size(), equalTo(1));
    assertThat(messages.sentTo(member2).get(0), instanceOf(RaftMessages.Vote.Request.class));
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) TestMessageBuilders.voteRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest) TestMessageBuilders.appendEntriesRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.appendEntriesRequest) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Example 13 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService 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 14 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService 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 15 with ControlledRenewableTimeoutService

use of org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService 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)

Aggregations

Test (org.junit.Test)18 ControlledRenewableTimeoutService (org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService)18 MembershipEntry (org.neo4j.causalclustering.core.consensus.membership.MembershipEntry)17 RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)17 FakeClock (org.neo4j.time.FakeClock)17 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)3 RaftMachineBuilder (org.neo4j.causalclustering.core.consensus.RaftMachineBuilder)3 TestMessageBuilders.voteRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest)3 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)2 TestMessageBuilders.appendEntriesRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.appendEntriesRequest)1 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)1 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)1 MemberIdSet (org.neo4j.causalclustering.core.consensus.membership.MemberIdSet)1 MemberId (org.neo4j.causalclustering.identity.MemberId)1 Inbound (org.neo4j.causalclustering.messaging.Inbound)1 Monitors (org.neo4j.kernel.monitoring.Monitors)1