Search in sources :

Example 26 with Outcome

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

the class AppendEntriesRequestTest method shouldTruncateOnReceiptOfConflictingEntry.

@Test
public void shouldTruncateOnReceiptOfConflictingEntry() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftState state = raftState().myself(myself).term(5).entryLog(raftLog).build();
    long leaderTerm = state.term() + leaderTermDifference;
    raftLog.append(new RaftLogEntry(state.term() - 1, content()));
    raftLog.append(new RaftLogEntry(state.term() - 1, content()));
    // when
    long previousIndex = raftLog.appendIndex() - 1;
    Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(previousIndex).prevLogTerm(raftLog.readEntryTerm(previousIndex)).logEntry(new RaftLogEntry(leaderTerm, content())).build(), state, log());
    // then
    assertTrue(((Response) messageFor(outcome, leader)).success());
    assertThat(outcome.getLogCommands(), hasItem(new TruncateLogCommand(1)));
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) TruncateLogCommand(org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 27 with Outcome

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

the class AppendEntriesRequestTest method shouldAcceptInitialEntryAfterBootstrap.

@Test
public void shouldAcceptInitialEntryAfterBootstrap() throws Exception {
    RaftLog raftLog = bootstrappedLog();
    RaftState state = raftState().entryLog(raftLog).myself(myself).build();
    long leaderTerm = state.term() + leaderTermDifference;
    RaftLogEntry logEntry = new RaftLogEntry(leaderTerm, content());
    // when
    Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(0).prevLogTerm(0).logEntry(logEntry).build(), state, log());
    // then
    assertTrue(((Response) messageFor(outcome, leader)).success());
    assertThat(outcome.getLogCommands(), hasItem(new BatchAppendLogEntries(1, 0, new RaftLogEntry[] { logEntry })));
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 28 with Outcome

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

the class AppendingTest method shouldNotAllowTruncationAtCommit.

@Test
public void shouldNotAllowTruncationAtCommit() throws Exception {
    // given
    long commitIndex = 5;
    long localTermForAllEntries = 1L;
    Outcome outcome = mock(Outcome.class);
    ReadableRaftLog logMock = mock(ReadableRaftLog.class);
    // for simplicity, all entries are at term 1
    when(logMock.readEntryTerm(anyLong())).thenReturn(localTermForAllEntries);
    when(logMock.appendIndex()).thenReturn(commitIndex);
    ReadableRaftState state = mock(ReadableRaftState.class);
    when(state.entryLog()).thenReturn(logMock);
    when(state.commitIndex()).thenReturn(commitIndex);
    // when - then
    try {
        Appending.handleAppendEntriesRequest(state, outcome, new RaftMessages.AppendEntries.Request(aMember, localTermForAllEntries, commitIndex - 1, localTermForAllEntries, new RaftLogEntry[] { new RaftLogEntry(localTermForAllEntries + 1, ReplicatedInteger.valueOf(2)) }, commitIndex + 3), NullLog.getInstance());
        fail("Appending should not allow truncation at or before the commit index");
    } catch (IllegalStateException expected) {
    // ok
    }
}
Also used : ReadableRaftLog(org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 29 with Outcome

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

the class RaftStateBuilder method build.

public RaftState build() throws IOException {
    StateStorage<TermState> termStore = new InMemoryStateStorage<>(new TermState());
    StateStorage<VoteState> voteStore = new InMemoryStateStorage<>(new VoteState());
    StubMembership membership = new StubMembership(votingMembers, replicationMembers);
    RaftState state = new RaftState(myself, termStore, membership, entryLog, voteStore, new InFlightMap<>(), NullLogProvider.getInstance());
    Collection<RaftMessages.Directed> noMessages = Collections.emptyList();
    List<RaftLogCommand> noLogCommands = Collections.emptyList();
    state.update(new Outcome(null, term, leader, leaderCommit, votedFor, votesForMe, lastLogIndexBeforeWeBecameLeader, followerStates, false, noLogCommands, noMessages, emptySet(), commitIndex, emptySet()));
    return state;
}
Also used : RaftLogCommand(org.neo4j.causalclustering.core.consensus.outcome.RaftLogCommand) InMemoryStateStorage(org.neo4j.causalclustering.core.state.storage.InMemoryStateStorage) VoteState(org.neo4j.causalclustering.core.consensus.vote.VoteState) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) TermState(org.neo4j.causalclustering.core.consensus.term.TermState)

Example 30 with Outcome

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

the class Appending method appendNewEntries.

static void appendNewEntries(ReadableRaftState ctx, Outcome outcome, List<ReplicatedContent> contents) throws IOException {
    long prevLogIndex = ctx.entryLog().appendIndex();
    long prevLogTerm = prevLogIndex == -1 ? -1 : prevLogIndex > ctx.lastLogIndexBeforeWeBecameLeader() ? ctx.term() : ctx.entryLog().readEntryTerm(prevLogIndex);
    RaftLogEntry[] raftLogEntries = contents.stream().map(content -> new RaftLogEntry(ctx.term(), content)).toArray(RaftLogEntry[]::new);
    outcome.addShipCommand(new ShipCommand.NewEntries(prevLogIndex, prevLogTerm, raftLogEntries));
    outcome.addLogCommand(new BatchAppendLogEntries(prevLogIndex + 1, 0, raftLogEntries));
}
Also used : Log(org.neo4j.logging.Log) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) IOException(java.io.IOException) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) String.format(java.lang.String.format) ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) List(java.util.List) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) TruncateLogCommand(org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand) ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Aggregations

Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)58 Test (org.junit.Test)53 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)42 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)27 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)24 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)19 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)15 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)11 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)11 ReadableRaftLog (org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog)9 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)6 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)6 Log (org.neo4j.logging.Log)6 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)5 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)5 MemberId (org.neo4j.causalclustering.identity.MemberId)5 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)4 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)4 ReplicatedString (org.neo4j.causalclustering.core.consensus.ReplicatedString)3 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)3