Search in sources :

Example 6 with ReadableRaftState

use of org.neo4j.causalclustering.core.consensus.state.ReadableRaftState 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 7 with ReadableRaftState

use of org.neo4j.causalclustering.core.consensus.state.ReadableRaftState in project neo4j by neo4j.

the class AppendingTest method shouldPerformTruncation.

@Test
public void shouldPerformTruncation() throws Exception {
    // when
    // we have a log appended up to appendIndex, and committed somewhere before that
    long appendIndex = 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(appendIndex);
    ReadableRaftState state = mock(ReadableRaftState.class);
    when(state.entryLog()).thenReturn(logMock);
    when(state.commitIndex()).thenReturn(appendIndex - 3);
    // when
    // the leader asks to append after the commit index an entry that mismatches on term
    Appending.handleAppendEntriesRequest(state, outcome, new RaftMessages.AppendEntries.Request(aMember, localTermForAllEntries, appendIndex - 2, localTermForAllEntries, new RaftLogEntry[] { new RaftLogEntry(localTermForAllEntries + 1, ReplicatedInteger.valueOf(2)) }, appendIndex + 3), NullLog.getInstance());
    // then
    // we must produce a TruncateLogCommand at the earliest mismatching index
    verify(outcome, times(1)).addLogCommand(argThat(new LogCommandMatcher(appendIndex - 1)));
}
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 8 with ReadableRaftState

use of org.neo4j.causalclustering.core.consensus.state.ReadableRaftState in project neo4j by neo4j.

the class AppendingTest method shouldNotAllowTruncationBeforeCommit.

@Test
public void shouldNotAllowTruncationBeforeCommit() 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 - 2, 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) {
    // fine
    }
}
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 9 with ReadableRaftState

use of org.neo4j.causalclustering.core.consensus.state.ReadableRaftState in project neo4j by neo4j.

the class AppendingTest method shouldNotAttemptToTruncateAtIndexBeforeTheLogPrevIndex.

@Test
public void shouldNotAttemptToTruncateAtIndexBeforeTheLogPrevIndex() throws Exception {
    // given
    // a log with prevIndex and prevTerm set
    ReadableRaftLog logMock = mock(ReadableRaftLog.class);
    long prevIndex = 5;
    long prevTerm = 5;
    when(logMock.prevIndex()).thenReturn(prevIndex);
    when(logMock.readEntryTerm(prevIndex)).thenReturn(prevTerm);
    // and which also properly returns -1 as the term for an unknown entry, in this case prevIndex - 2
    when(logMock.readEntryTerm(prevIndex - 2)).thenReturn(-1L);
    // also, a state with a given commitIndex, obviously ahead of prevIndex
    long commitIndex = 10;
    ReadableRaftState state = mock(ReadableRaftState.class);
    when(state.entryLog()).thenReturn(logMock);
    when(state.commitIndex()).thenReturn(commitIndex);
    // which is also the append index
    when(logMock.appendIndex()).thenReturn(commitIndex);
    // when
    // an appendEntriesRequest arrives for appending entries before the prevIndex (for whatever reason)
    Outcome outcome = mock(Outcome.class);
    Appending.handleAppendEntriesRequest(state, outcome, new RaftMessages.AppendEntries.Request(aMember, prevTerm, prevIndex - 2, prevTerm, new RaftLogEntry[] { new RaftLogEntry(prevTerm, ReplicatedInteger.valueOf(2)) }, commitIndex + 3), NullLog.getInstance());
    // then
    // there should be no truncate commands. Actually, the whole thing should be a no op
    verify(outcome, times(0)).addLogCommand(any());
}
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 10 with ReadableRaftState

use of org.neo4j.causalclustering.core.consensus.state.ReadableRaftState in project neo4j by neo4j.

the class LeaderTest method leaderShouldIgnoreSuccessResponseThatIndicatesLaggingWhileLocalStateIndicatesFollowerIsCaughtUp.

@Test
public void leaderShouldIgnoreSuccessResponseThatIndicatesLaggingWhileLocalStateIndicatesFollowerIsCaughtUp() throws Exception {
    // given
    /*
         * A leader who
         * - has an append index of 100
         * - knows about instance 2
         * - assumes that instance 2 is fully caught up
         */
    Leader leader = new Leader();
    MemberId instance2 = member(2);
    int j = 100;
    FollowerState instance2State = createArtificialFollowerState(j);
    ReadableRaftState state = mock(ReadableRaftState.class);
    FollowerStates<MemberId> followerState = new FollowerStates<>();
    followerState = new FollowerStates<>(followerState, instance2, instance2State);
    ReadableRaftLog logMock = mock(ReadableRaftLog.class);
    when(logMock.appendIndex()).thenReturn(100L);
    //  with commit requests in this test
    when(state.commitIndex()).thenReturn(-1L);
    when(state.entryLog()).thenReturn(logMock);
    when(state.followerStates()).thenReturn(followerState);
    // both leader and follower are in the same term
    when(state.term()).thenReturn(4L);
    // when that leader is asked to handle a response from that follower that says that the follower is still
    // missing things
    RaftMessages.AppendEntries.Response response = appendEntriesResponse().success().matchIndex(80).term(4).from(instance2).build();
    Outcome outcome = leader.handle(response, state, mock(Log.class));
    // then the leader should not send anything, since this is a delayed, out of order response to a previous append
    // request
    assertTrue(outcome.getOutgoingMessages().isEmpty());
    // The follower state should not be touched
    FollowerStates<MemberId> updatedFollowerStates = outcome.getFollowerStates();
    assertEquals(100, updatedFollowerStates.get(instance2).getMatchIndex());
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Log(org.neo4j.logging.Log) ReadableRaftLog(org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) MemberId(org.neo4j.causalclustering.identity.MemberId) ReadableRaftLog(org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) FollowerStates(org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates) FollowerState(org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState) Test(org.junit.Test)

Aggregations

Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)10 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)10 Test (org.junit.Test)9 ReadableRaftLog (org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog)9 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)6 Log (org.neo4j.logging.Log)6 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)5 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)5 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)5 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)5 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)5 MemberId (org.neo4j.causalclustering.identity.MemberId)5 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)3 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 List (java.util.List)1 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)1 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)1 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)1 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)1