Search in sources :

Example 56 with RaftLogEntry

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

the class InFlightLogEntryReaderTest method shouldUseTheRaftLogWhenTheIndexIsNotPresent.

@Test
public void shouldUseTheRaftLogWhenTheIndexIsNotPresent() throws Exception {
    // given
    InFlightLogEntryReader reader = new InFlightLogEntryReader(raftLog, inFlightMap, clearCache);
    startingFromIndexReturnEntries(inFlightMap, logIndex, null);
    startingFromIndexReturnEntries(raftLog, logIndex, entry);
    // when
    RaftLogEntry raftLogEntry = reader.get(logIndex);
    // then
    assertEquals(entry, raftLogEntry);
    verify(inFlightMap).get(logIndex);
    verify(raftLog).getEntryCursor(logIndex);
    assertCacheIsUpdated(inFlightMap, logIndex);
    verifyNoMoreInteractions(inFlightMap);
    verifyNoMoreInteractions(raftLog);
}
Also used : RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 57 with RaftLogEntry

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

the class InFlightLogEntryReaderTest method startingFromIndexReturnEntries.

private void startingFromIndexReturnEntries(ReadableRaftLog raftLog, long startIndex, RaftLogEntry entry, RaftLogEntry... otherEntries) throws IOException {
    RaftLogCursor cursor = mock(RaftLogCursor.class);
    when(raftLog.getEntryCursor(startIndex)).thenReturn(cursor, (RaftLogCursor) null);
    Boolean[] bools = new Boolean[otherEntries.length + 1];
    Arrays.fill(bools, Boolean.TRUE);
    bools[otherEntries.length] = Boolean.FALSE;
    when(cursor.next()).thenReturn(true, bools);
    Long[] indexes = new Long[otherEntries.length + 1];
    for (int offset = 0; offset < indexes.length; offset++) {
        indexes[offset] = startIndex + 1 + offset;
    }
    indexes[otherEntries.length] = -1L;
    when(cursor.index()).thenReturn(startIndex, indexes);
    RaftLogEntry[] raftLogEntries = Arrays.copyOf(otherEntries, otherEntries.length + 1);
    when(cursor.get()).thenReturn(entry, raftLogEntries);
}
Also used : RaftLogCursor(org.neo4j.causalclustering.core.consensus.log.RaftLogCursor) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 58 with RaftLogEntry

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

the class AppendEntriesMessageFlowTest method setup.

@Before
public void setup() throws IOException {
    // given
    RaftLog raftLog = new InMemoryRaftLog();
    raftLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).raftLog(raftLog).outbound(outbound).build();
}
Also used : RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Before(org.junit.Before)

Example 59 with RaftLogEntry

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

the class AppendEntriesMessageFlowTest method shouldAcceptSequenceOfAppendEntries.

@Test
public void shouldAcceptSequenceOfAppendEntries() throws Exception {
    // when
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(0).prevLogIndex(0).prevLogTerm(0).logEntry(new RaftLogEntry(0, data(1))).leaderCommit(-1).build());
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(0).prevLogIndex(1).prevLogTerm(0).logEntry(new RaftLogEntry(0, data(2))).leaderCommit(-1).build());
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(0).prevLogIndex(2).prevLogTerm(0).logEntry(new RaftLogEntry(0, data(3))).leaderCommit(0).build());
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(1).prevLogIndex(3).prevLogTerm(0).logEntry(new RaftLogEntry(1, data(4))).leaderCommit(1).build());
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(1).prevLogIndex(4).prevLogTerm(1).logEntry(new RaftLogEntry(1, data(5))).leaderCommit(2).build());
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(1).prevLogIndex(5).prevLogTerm(1).logEntry(new RaftLogEntry(1, data(6))).leaderCommit(4).build());
    // then
    InOrder invocationOrder = inOrder(outbound);
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(0).appendIndex(1).matchIndex(1).success().build()));
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(0).appendIndex(2).matchIndex(2).success().build()));
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(0).appendIndex(3).matchIndex(3).success().build()));
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(1).appendIndex(4).matchIndex(4).success().build()));
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(1).appendIndex(5).matchIndex(5).success().build()));
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(1).appendIndex(6).matchIndex(6).success().build()));
}
Also used : InOrder(org.mockito.InOrder) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 60 with RaftLogEntry

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

the class AppendEntriesMessageFlowTest method shouldReturnFalseOnAppendRequestWhenPrevLogEntryNotMatched.

@Test
public void shouldReturnFalseOnAppendRequestWhenPrevLogEntryNotMatched() throws Exception {
    // when
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(0).prevLogIndex(0).prevLogTerm(1).logEntry(new RaftLogEntry(0, data)).build());
    // then
    verify(outbound).send(same(otherMember), eq(appendEntriesResponse().matchIndex(-1).appendIndex(0).from(myself).term(0).failure().build()));
}
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