Search in sources :

Example 1 with LeaderContext

use of org.neo4j.causalclustering.core.consensus.LeaderContext in project neo4j by neo4j.

the class RaftLogShipperTest method shouldPickUpAfterMissedBatch.

@Test
public void shouldPickUpAfterMissedBatch() throws Exception {
    //given
    raftLog.append(entry0);
    raftLog.append(entry1);
    raftLog.append(entry2);
    raftLog.append(entry3);
    startLogShipper();
    logShipper.onMatch(0, new LeaderContext(0, 0));
    // we are now in PIPELINE mode, because we matched and the entire last batch was sent out
    logShipper.onTimeout();
    // and now we should be in CATCHUP mode, awaiting a late response
    // the response to the batch never came, so on timeout we enter MISMATCH mode and send a single entry based on the latest we knowingly sent (entry3)
    logShipper.onTimeout();
    outbound.clear();
    // fake a match
    logShipper.onMatch(0, new LeaderContext(0, 0));
    assertThat(outbound.sentTo(follower), Matchers.hasRaftLogEntries(asList(entry1, entry2, entry3)));
}
Also used : LeaderContext(org.neo4j.causalclustering.core.consensus.LeaderContext) Test(org.junit.Test)

Example 2 with LeaderContext

use of org.neo4j.causalclustering.core.consensus.LeaderContext in project neo4j by neo4j.

the class RaftLogShipperTest method shouldResendLastSentEntryOnFirstMismatch.

@Test
public void shouldResendLastSentEntryOnFirstMismatch() throws Throwable {
    // given
    raftLog.append(entry0);
    startLogShipper();
    raftLog.append(entry1);
    raftLog.append(entry2);
    logShipper.onMatch(0, new LeaderContext(0, 0));
    logShipper.onNewEntries(0, 0, new RaftLogEntry[] { entry1 }, new LeaderContext(0, 0));
    logShipper.onNewEntries(1, 0, new RaftLogEntry[] { entry2 }, new LeaderContext(0, 0));
    // when
    outbound.clear();
    logShipper.onMismatch(1, new LeaderContext(0, 0));
    // then
    RaftMessages.AppendEntries.Request expected = new RaftMessages.AppendEntries.Request(leader, leaderTerm, 1, entry1.term(), RaftLogEntry.empty, leaderCommit);
    assertThat(outbound.sentTo(follower), hasItem(expected));
}
Also used : RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) LeaderContext(org.neo4j.causalclustering.core.consensus.LeaderContext) Test(org.junit.Test)

Example 3 with LeaderContext

use of org.neo4j.causalclustering.core.consensus.LeaderContext in project neo4j by neo4j.

the class RaftLogShipperTest method shouldSendAllEntriesAndCatchupCompletely.

@Test
public void shouldSendAllEntriesAndCatchupCompletely() throws Throwable {
    // given
    final int ENTRY_COUNT = catchupBatchSize * 10;
    Collection<RaftLogEntry> entries = new ArrayList<>();
    for (int i = 0; i < ENTRY_COUNT; i++) {
        entries.add(new RaftLogEntry(0, ReplicatedInteger.valueOf(i)));
    }
    for (RaftLogEntry entry : entries) {
        raftLog.append(entry);
    }
    // then
    startLogShipper();
    // back-tracking stage
    RaftMessages.AppendEntries.Request expected = new RaftMessages.AppendEntries.Request(leader, leaderTerm, 0, 0, RaftLogEntry.empty, leaderCommit);
    while (!outbound.sentTo(follower).contains(expected)) {
        logShipper.onMismatch(-1, new LeaderContext(0, 0));
    }
    // catchup stage
    long matchIndex;
    do {
        AppendEntries.Request last = (AppendEntries.Request) Iterables.last(outbound.sentTo(follower));
        matchIndex = last.prevLogIndex() + last.entries().length;
        outbound.clear();
        logShipper.onMatch(matchIndex, new LeaderContext(0, 0));
    } while (outbound.sentTo(follower).size() > 0);
    assertEquals(ENTRY_COUNT - 1, matchIndex);
}
Also used : ArrayList(java.util.ArrayList) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) LeaderContext(org.neo4j.causalclustering.core.consensus.LeaderContext) Test(org.junit.Test)

Example 4 with LeaderContext

use of org.neo4j.causalclustering.core.consensus.LeaderContext in project neo4j by neo4j.

the class RaftLogShipperTest method shouldSendMostRecentlyAvailableEntryIfPruningHappened.

@Test
public void shouldSendMostRecentlyAvailableEntryIfPruningHappened() throws IOException {
    //given
    raftLog.append(entry0);
    raftLog.append(entry1);
    raftLog.append(entry2);
    raftLog.append(entry3);
    startLogShipper();
    //when
    raftLog.prune(2);
    outbound.clear();
    logShipper.onMismatch(0, new LeaderContext(0, 0));
    //then
    RaftMessages.AppendEntries.Request expected = new RaftMessages.AppendEntries.Request(leader, leaderTerm, 2, entry2.term(), RaftLogEntry.empty, leaderCommit);
    assertThat(outbound.sentTo(follower), hasItem(expected));
}
Also used : RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) LeaderContext(org.neo4j.causalclustering.core.consensus.LeaderContext) Test(org.junit.Test)

Example 5 with LeaderContext

use of org.neo4j.causalclustering.core.consensus.LeaderContext in project neo4j by neo4j.

the class RaftLogShipperTest method shouldSendLogCompactionInfoToFollowerOnMatchIfEntryHasBeenPrunedAway.

@Test
public void shouldSendLogCompactionInfoToFollowerOnMatchIfEntryHasBeenPrunedAway() throws Exception {
    //given
    raftLog.append(entry0);
    raftLog.append(entry1);
    raftLog.append(entry2);
    raftLog.append(entry3);
    startLogShipper();
    //when
    outbound.clear();
    raftLog.prune(2);
    logShipper.onMatch(1, new LeaderContext(0, 0));
    //then
    assertTrue(outbound.hasAnyEntriesTo(follower));
    assertThat(outbound.sentTo(follower), hasMessage(new RaftMessages.LogCompactionInfo(leader, 0, 2)));
}
Also used : LeaderContext(org.neo4j.causalclustering.core.consensus.LeaderContext) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 LeaderContext (org.neo4j.causalclustering.core.consensus.LeaderContext)10 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)5 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)5 ArrayList (java.util.ArrayList)1 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)1