Search in sources :

Example 1 with AppendLogEntry

use of org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry in project neo4j by neo4j.

the class LeaderTest method leaderShouldDecideToAppendToItsLogAndSendAppendEntriesMessageOnReceiptOfClientProposal.

@Test
public void leaderShouldDecideToAppendToItsLogAndSendAppendEntriesMessageOnReceiptOfClientProposal() throws Exception {
    // given
    RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).build();
    Leader leader = new Leader();
    RaftMessages.NewEntry.Request newEntryRequest = new RaftMessages.NewEntry.Request(member(9), CONTENT);
    // when
    Outcome outcome = leader.handle(newEntryRequest, state, log());
    //state.update( outcome );
    // then
    AppendLogEntry logCommand = (AppendLogEntry) single(outcome.getLogCommands());
    assertEquals(0, logCommand.index);
    assertEquals(0, logCommand.entry.term());
    ShipCommand.NewEntries shipCommand = (ShipCommand.NewEntries) single(outcome.getShipCommands());
    assertEquals(shipCommand, new ShipCommand.NewEntries(-1, -1, new RaftLogEntry[] { new RaftLogEntry(0, CONTENT) }));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 2 with AppendLogEntry

use of org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry in project neo4j by neo4j.

the class CandidateTest method shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm.

@Test
public void shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm() throws Exception {
    // given
    RaftState state = RaftStateBuilder.raftState().term(1).myself(myself).votingMembers(member1, member2).replicationMembers(member1, member2).build();
    // when
    Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(state.term()).from(member1).grant().build(), state, log());
    // then
    assertEquals(LEADER, outcome.getRole());
    assertEquals(true, outcome.electionTimeoutRenewed());
    assertThat(outcome.getLogCommands(), hasItem(new AppendLogEntry(0, new RaftLogEntry(state.term(), new NewLeaderBarrier()))));
    assertThat(outcome.getOutgoingMessages(), hasItems(new RaftMessages.Directed(member1, new RaftMessages.Heartbeat(myself, state.term(), -1, -1)), new RaftMessages.Directed(member2, new RaftMessages.Heartbeat(myself, state.term(), -1, -1))));
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 3 with AppendLogEntry

use of org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry in project neo4j by neo4j.

the class Appending method appendNewEntry.

static void appendNewEntry(ReadableRaftState ctx, Outcome outcome, ReplicatedContent content) throws IOException {
    long prevLogIndex = ctx.entryLog().appendIndex();
    long prevLogTerm = prevLogIndex == -1 ? -1 : prevLogIndex > ctx.lastLogIndexBeforeWeBecameLeader() ? ctx.term() : ctx.entryLog().readEntryTerm(prevLogIndex);
    RaftLogEntry newLogEntry = new RaftLogEntry(ctx.term(), content);
    outcome.addShipCommand(new ShipCommand.NewEntries(prevLogIndex, prevLogTerm, new RaftLogEntry[] { newLogEntry }));
    outcome.addLogCommand(new AppendLogEntry(prevLogIndex + 1, newLogEntry));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 4 with AppendLogEntry

use of org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry in project neo4j by neo4j.

the class RaftMembershipManagerTest method membershipManagerShouldRevertToOldMembershipSetAfterTruncationCausesLossOfAllAppendedMembershipSets.

@Test
public void membershipManagerShouldRevertToOldMembershipSetAfterTruncationCausesLossOfAllAppendedMembershipSets() throws Exception {
    // given
    final InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftMembershipManager membershipManager = lifeRule.add(raftMembershipManager(raftLog));
    // when
    List<RaftLogCommand> logCommands = asList(new AppendLogEntry(0, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 4))), new AppendLogEntry(1, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 5))), new TruncateLogCommand(1));
    for (RaftLogCommand logCommand : logCommands) {
        logCommand.applyTo(raftLog, log);
    }
    membershipManager.processLog(0, logCommands);
    // then
    assertEquals(new RaftTestGroup(1, 2, 3, 4).getMembers(), membershipManager.votingMembers());
    assertFalse(membershipManager.uncommittedMemberChangeInLog());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) TruncateLogCommand(org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftLogCommand(org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 5 with AppendLogEntry

use of org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry in project neo4j by neo4j.

the class RaftMembershipManagerTest method membershipManagerShouldRevertToEarlierAppendedMembershipSetAfterTruncationCausesLossOfLastAppened.

@Test
public void membershipManagerShouldRevertToEarlierAppendedMembershipSetAfterTruncationCausesLossOfLastAppened() throws Exception {
    // given
    final InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftMembershipManager membershipManager = lifeRule.add(raftMembershipManager(raftLog));
    // when
    List<RaftLogCommand> logCommands = asList(new AppendLogEntry(0, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 4))), new AppendLogEntry(1, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 5))), new AppendLogEntry(2, new RaftLogEntry(0, new RaftTestGroup(1, 2, 3, 6))), new TruncateLogCommand(2));
    for (RaftLogCommand logCommand : logCommands) {
        logCommand.applyTo(raftLog, log);
    }
    membershipManager.processLog(0, logCommands);
    // then
    assertEquals(new RaftTestGroup(1, 2, 3, 5).getMembers(), membershipManager.votingMembers());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) TruncateLogCommand(org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftLogCommand(org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)7 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)7 Test (org.junit.Test)6 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)4 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)3 RaftLogCommand (org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand)3 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)3 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)2 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)2 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)2 LinkedList (java.util.LinkedList)1 NewLeaderBarrier (org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)1 InFlightMap (org.neo4j.causalclustering.core.consensus.log.segmented.InFlightMap)1 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)1 TermState (org.neo4j.causalclustering.core.consensus.term.TermState)1 VoteState (org.neo4j.causalclustering.core.consensus.vote.VoteState)1