use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.
the class RaftMachineTest method shouldVoteFalseForCandidateInOldTerm.
@Test
public void shouldVoteFalseForCandidateInOldTerm() 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
raft.handle(voteRequest().from(member1).term(-1).candidate(member1).lastLogIndex(0).lastLogTerm(-1).build());
// Then
assertThat(messages.sentTo(member1).size(), equalTo(1));
assertThat(messages.sentTo(member1), hasItem(voteResponse().from(myself).term(0).deny().build()));
}
use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.
the class RaftMachineTest method shouldNotBecomeLeaderWhenVotingOnItself.
@Test
public void shouldNotBecomeLeaderWhenVotingOnItself() 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);
// When
raft.handle(voteResponse().from(myself).term(1).grant().build());
// Then
assertThat(raft.isLeader(), is(false));
}
use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.
the class CatchUpTest method newMemberWithNoLogShouldCatchUpFromPeers.
@Test
public void newMemberWithNoLogShouldCatchUpFromPeers() throws Throwable {
DirectNetworking net = new DirectNetworking();
// given
final MemberId leaderId = member(0);
final MemberId sleepyId = member(2);
final MemberId[] awakeMembers = { leaderId, member(1) };
final MemberId[] allMembers = { leaderId, member(1), sleepyId };
RaftTestFixture fixture = new RaftTestFixture(net, 3, allMembers);
fixture.bootstrap(allMembers);
fixture.members().withId(leaderId).raftInstance().installCoreState(new RaftCoreState(new MembershipEntry(0, new HashSet<>(Arrays.asList(allMembers)))));
fixture.members().withId(leaderId).timeoutService().invokeTimeout(RaftMachine.Timeouts.ELECTION);
net.processMessages();
final MemberId leader = fixture.members().withId(leaderId).member();
net.disconnect(sleepyId);
// when
fixture.members().withId(leaderId).raftInstance().handle(new Request(leader, valueOf(10)));
fixture.members().withId(leaderId).raftInstance().handle(new Request(leader, valueOf(20)));
fixture.members().withId(leaderId).raftInstance().handle(new Request(leader, valueOf(30)));
fixture.members().withId(leaderId).raftInstance().handle(new Request(leader, valueOf(40)));
net.processMessages();
// then
for (MemberId awakeMember : awakeMembers) {
assertThat(integerValues(fixture.members().withId(awakeMember).raftLog()), hasItems(10, 20, 30, 40));
}
assertThat(integerValues(fixture.members().withId(sleepyId).raftLog()), empty());
// when
net.reconnect(sleepyId);
// TODO: This needs an injectable/controllable timeout service for the log shipper.
Thread.sleep(500);
net.processMessages();
// then
assertThat(integerValues(fixture.members().withId(sleepyId).raftLog()), hasItems(10, 20, 30, 40));
}
use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.
the class RaftMachineTest method shouldAppendNewLeaderBarrierAfterBecomingLeader.
@Test
public void shouldAppendNewLeaderBarrierAfterBecomingLeader() throws Exception {
// Given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
OutboundMessageCollector messages = new OutboundMessageCollector();
InMemoryRaftLog raftLog = new InMemoryRaftLog();
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).clock(fakeClock).outbound(messages).raftLog(raftLog).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
raft.startTimers();
// When
timeouts.invokeTimeout(ELECTION);
raft.handle(voteResponse().from(member1).term(1).grant().build());
// Then
assertEquals(new NewLeaderBarrier(), readLogEntry(raftLog, raftLog.appendIndex()).content());
}
use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.
the class RaftMachineTest method shouldThrowExceptionIfReceivesClientRequestWithNoLeaderElected.
@Test
public void shouldThrowExceptionIfReceivesClientRequestWithNoLeaderElected() 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();
try {
// When
// There is no leader
raft.getLeader();
fail("Should have thrown exception");
}// Then
catch (NoLeaderFoundException e) {
// expected
}
}
Aggregations