Search in sources :

Example 1 with ShipCommand

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

the class LeaderTest method leaderShouldRespondToSuccessResponseThatIndicatesLaggingFollowerWithJustWhatItsMissing.

@Test
public void leaderShouldRespondToSuccessResponseThatIndicatesLaggingFollowerWithJustWhatItsMissing() throws Exception {
    // given
    /*
         * A leader who
         * - has an append index of 100
         * - knows about instance 2
         * - assumes that instance 2 is at an index less than 100 -say 50
         */
    Leader leader = new Leader();
    MemberId instance2 = member(2);
    FollowerState instance2State = createArtificialFollowerState(50);
    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(231L);
    // 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(89).term(231).from(instance2).build();
    Outcome outcome = leader.handle(response, state, mock(Log.class));
    // then
    int matchCount = 0;
    for (ShipCommand shipCommand : outcome.getShipCommands()) {
        if (shipCommand instanceof ShipCommand.Match) {
            matchCount++;
        }
    }
    assertThat(matchCount, greaterThan(0));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) 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)

Example 2 with ShipCommand

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

the class LeaderTest method leaderShouldDecideToAppendToItsLogAndSendAppendEntriesMessageOnReceiptOfClientProposal.

@Test
public void leaderShouldDecideToAppendToItsLogAndSendAppendEntriesMessageOnReceiptOfClientProposal() throws Exception {
    // given
    RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).build();
    Leader leader = new Leader();
    RaftMessages.NewEntry.Request newEntryRequest = new RaftMessages.NewEntry.Request(member(9), CONTENT);
    // when
    Outcome outcome = leader.handle(newEntryRequest, state, log());
    //state.update( outcome );
    // then
    AppendLogEntry logCommand = (AppendLogEntry) single(outcome.getLogCommands());
    assertEquals(0, logCommand.index);
    assertEquals(0, logCommand.entry.term());
    ShipCommand.NewEntries shipCommand = (ShipCommand.NewEntries) single(outcome.getShipCommands());
    assertEquals(shipCommand, new ShipCommand.NewEntries(-1, -1, new RaftLogEntry[] { new RaftLogEntry(0, CONTENT) }));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 3 with ShipCommand

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

the class LeaderTest method leaderShouldHandleBatch.

@Test
public void leaderShouldHandleBatch() throws Exception {
    // given
    RaftState state = raftState().votingMembers(asSet(myself, member1, member2)).build();
    Leader leader = new Leader();
    int BATCH_SIZE = 3;
    RaftMessages.NewEntry.BatchRequest batchRequest = new RaftMessages.NewEntry.BatchRequest(BATCH_SIZE);
    batchRequest.add(valueOf(0));
    batchRequest.add(valueOf(1));
    batchRequest.add(valueOf(2));
    // when
    Outcome outcome = leader.handle(batchRequest, state, log());
    // then
    BatchAppendLogEntries logCommand = (BatchAppendLogEntries) single(outcome.getLogCommands());
    assertEquals(0, logCommand.baseIndex);
    for (int i = 0; i < BATCH_SIZE; i++) {
        assertEquals(0, logCommand.entries[i].term());
        assertEquals(i, ((ReplicatedInteger) logCommand.entries[i].content()).get());
    }
    ShipCommand.NewEntries shipCommand = (ShipCommand.NewEntries) single(outcome.getShipCommands());
    assertEquals(shipCommand, new ShipCommand.NewEntries(-1, -1, new RaftLogEntry[] { new RaftLogEntry(0, valueOf(0)), new RaftLogEntry(0, valueOf(1)), new RaftLogEntry(0, valueOf(2)) }));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) Test(org.junit.Test)

Example 4 with ShipCommand

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

the class LeaderTest method leaderShouldSpawnMismatchCommandOnFailure.

// TODO: rethink this test, it does too much
@Test
public void leaderShouldSpawnMismatchCommandOnFailure() 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);
    FollowerState instance2State = createArtificialFollowerState(100);
    ReadableRaftState state = mock(ReadableRaftState.class);
    FollowerStates<MemberId> followerState = new FollowerStates<>();
    followerState = new FollowerStates<>(followerState, instance2, instance2State);
    RaftLog log = new InMemoryRaftLog();
    for (int i = 0; i <= 100; i++) {
        log.append(new RaftLogEntry(0, valueOf(i)));
    }
    when(state.commitIndex()).thenReturn(-1L);
    when(state.entryLog()).thenReturn(log);
    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().failure().appendIndex(0).matchIndex(-1).term(4).from(instance2).build();
    Outcome outcome = leader.handle(response, state, mock(Log.class));
    // then
    int mismatchCount = 0;
    for (ShipCommand shipCommand : outcome.getShipCommands()) {
        if (shipCommand instanceof ShipCommand.Mismatch) {
            mismatchCount++;
        }
    }
    assertThat(mismatchCount, greaterThan(0));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) 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) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) MemberId(org.neo4j.causalclustering.identity.MemberId) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) 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) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) ReadableRaftLog(org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)4 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)4 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)4 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)3 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)2 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)2 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)2 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)2 ReadableRaftLog (org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog)2 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)2 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)2 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)2 MemberId (org.neo4j.causalclustering.identity.MemberId)2 Log (org.neo4j.logging.Log)2 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)1 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)1