Search in sources :

Example 71 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class LeaderTest method leaderShouldCommitOnMajorityResponse.

@Test
public void leaderShouldCommitOnMajorityResponse() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, new ReplicatedString("lalalala")));
    RaftState state = raftState().votingMembers(member1, member2).term(0).lastLogIndexBeforeWeBecameLeader(-1).leader(myself).leaderCommit(-1).entryLog(raftLog).messagesSentToFollower(member1, raftLog.appendIndex() + 1).messagesSentToFollower(member2, raftLog.appendIndex() + 1).build();
    Leader leader = new Leader();
    // when a single instance responds (plus self == 2 out of 3 instances)
    Outcome outcome = leader.handle(new RaftMessages.AppendEntries.Response(member1, 0, true, 0, 0), state, log());
    // then
    assertEquals(0L, outcome.getCommitIndex());
    assertEquals(0L, outcome.getLeaderCommit());
}
Also used : ReplicatedString(org.neo4j.causalclustering.core.consensus.ReplicatedString) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 72 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class LeaderTest method leaderShouldCommitAllPreviouslyAppendedEntriesWhenCommittingLaterEntryInSameTerm.

// TODO move this someplace else, since log no longer holds the commit
@Test
public void leaderShouldCommitAllPreviouslyAppendedEntriesWhenCommittingLaterEntryInSameTerm() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, new ReplicatedString("first!")));
    raftLog.append(new RaftLogEntry(0, new ReplicatedString("second")));
    raftLog.append(new RaftLogEntry(0, new ReplicatedString("third")));
    RaftState state = raftState().votingMembers(myself, member1, member2).term(0).entryLog(raftLog).messagesSentToFollower(member1, raftLog.appendIndex() + 1).messagesSentToFollower(member2, raftLog.appendIndex() + 1).build();
    Leader leader = new Leader();
    // when
    Outcome outcome = leader.handle(new AppendEntries.Response(member1, 0, true, 2, 2), state, log());
    state.update(outcome);
    // then
    assertEquals(2, state.commitIndex());
}
Also used : ReplicatedString(org.neo4j.causalclustering.core.consensus.ReplicatedString) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 73 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class FollowerTest method followerLearningAboutHigherCommitCausesValuesTobeAppliedToItsLog.

// TODO move this to outcome tests
@Test
public void followerLearningAboutHigherCommitCausesValuesTobeAppliedToItsLog() throws Exception {
    // given
    RaftLog entryLog = new InMemoryRaftLog();
    entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    RaftState state = raftState().myself(myself).entryLog(entryLog).build();
    Follower follower = new Follower();
    appendSomeEntriesToLog(state, follower, 3, 0, 1);
    // when receiving AppEntries with high leader commit (4)
    Outcome outcome = follower.handle(new AppendEntries.Request(myself, 0, 3, 0, new RaftLogEntry[] { new RaftLogEntry(0, ContentGenerator.content()) }, 4), state, log());
    state.update(outcome);
    // then
    assertEquals(4, state.commitIndex());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) 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 74 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class FollowerTest method shouldUpdateCommitIndexIfNecessary.

@Test
public void shouldUpdateCommitIndexIfNecessary() throws Exception {
    //  If leaderCommit > commitIndex, set commitIndex = min( leaderCommit, index of last new entry )
    // given
    RaftLog entryLog = new InMemoryRaftLog();
    entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    RaftState state = raftState().myself(myself).entryLog(entryLog).build();
    Follower follower = new Follower();
    int localAppendIndex = 3;
    int localCommitIndex = localAppendIndex - 1;
    int term = 0;
    // append index is 0 based
    appendSomeEntriesToLog(state, follower, localAppendIndex, term, 1);
    // the next when-then simply verifies that the test is setup properly, with commit and append index as expected
    // when
    Outcome raftTestMemberOutcome = new Outcome(FOLLOWER, state);
    raftTestMemberOutcome.setCommitIndex(localCommitIndex);
    state.update(raftTestMemberOutcome);
    // then
    assertEquals(localAppendIndex, state.entryLog().appendIndex());
    assertEquals(localCommitIndex, state.commitIndex());
    // when
    // an append req comes in with leader commit index > localAppendIndex but localCommitIndex < localAppendIndex
    Outcome outcome = follower.handle(appendEntriesRequest().leaderTerm(term).prevLogIndex(3).prevLogTerm(term).leaderCommit(localCommitIndex + 4).build(), state, log());
    state.update(outcome);
    // then
    // The local commit index must be brought as far along as possible
    assertEquals(3, state.commitIndex());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) 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 75 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry in project neo4j by neo4j.

the class CommandApplicationProcessTest method shouldPeriodicallyFlushState.

// TODO: Test recovery, see CoreState#start().
@Test
public void shouldPeriodicallyFlushState() throws Throwable {
    // given
    applicationProcess.start();
    int interactions = flushEvery * 5;
    for (int i = 0; i < interactions; i++) {
        raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    }
    // when
    applicationProcess.notifyCommitted(interactions);
    applier.sync(false);
    // then
    verify(coreStateMachines, times(interactions / batchSize)).flush();
    assertEquals(interactions - (interactions % batchSize) - 1, (long) lastFlushedStorage.getInitialState());
}
Also used : RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)87 Test (org.junit.Test)69 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)27 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)25 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)20 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)12 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)10 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)10 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)8 NewLeaderBarrier (org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)7 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)7 MemberId (org.neo4j.causalclustering.identity.MemberId)7 InOrder (org.mockito.InOrder)6 RaftLogCursor (org.neo4j.causalclustering.core.consensus.log.RaftLogCursor)6 ReadableRaftLog (org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog)5 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)5 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)5 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)5 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)5 File (java.io.File)4