Search in sources :

Example 31 with RaftLogEntry

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

the class FollowerTest method shouldTruncateIfTermDoesNotMatch.

@Test
public void shouldTruncateIfTermDoesNotMatch() throws Exception {
    // given
    RaftLog entryLog = new InMemoryRaftLog();
    entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    int term = 1;
    RaftState state = raftState().myself(myself).entryLog(entryLog).term(term).build();
    Follower follower = new Follower();
    state.update(follower.handle(new AppendEntries.Request(member1, 1, 0, 0, new RaftLogEntry[] { new RaftLogEntry(2, ContentGenerator.content()) }, 0), state, log()));
    RaftLogEntry[] entries = { new RaftLogEntry(1, new ReplicatedString("commit this!")) };
    Outcome outcome = follower.handle(new AppendEntries.Request(member1, 1, 0, 0, entries, 0), state, log());
    state.update(outcome);
    // then
    assertEquals(1, state.entryLog().appendIndex());
    assertEquals(1, state.entryLog().readEntryTerm(1));
}
Also used : RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) TestMessageBuilders.appendEntriesRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.appendEntriesRequest) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) ReplicatedString(org.neo4j.causalclustering.core.consensus.ReplicatedString) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) Test(org.junit.Test)

Example 32 with RaftLogEntry

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

the class CommandApplicationProcessTest method shouldIncreaseLastAppliedForStateMachineCommands.

@Test
public void shouldIncreaseLastAppliedForStateMachineCommands() throws Exception {
    // given
    applicationProcess.start();
    // when
    raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    applicationProcess.notifyCommitted(2);
    applier.sync(false);
    // then
    assertEquals(2, applicationProcess.lastApplied());
}
Also used : RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 33 with RaftLogEntry

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

the class CommandApplicationProcessTest method shouldFailWhenCacheAndLogMiss.

@Test
public void shouldFailWhenCacheAndLogMiss() throws Throwable {
    //When an entry is not in the log, we must fail.
    applicationProcess.start();
    inFlightMap.put(0L, new RaftLogEntry(0, operation(nullTx)));
    raftLog.append(new RaftLogEntry(0, operation(nullTx)));
    raftLog.append(new RaftLogEntry(1, operation(nullTx)));
    //when
    applicationProcess.notifyCommitted(2);
    applier.sync(false);
    //then
    assertFalse(dbHealth.isHealthy());
}
Also used : RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 34 with RaftLogEntry

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

the class CommandApplicationProcessTest method outOfOrderDuplicatesShouldBeIgnoredButStillIncreaseCommandIndex.

@Test
public void outOfOrderDuplicatesShouldBeIgnoredButStillIncreaseCommandIndex() throws Exception {
    // given
    applicationProcess.start();
    // when
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(tx((byte) 100), globalSession, new LocalOperationId(0, 0))));
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(tx((byte) 101), globalSession, new LocalOperationId(0, 1))));
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(tx((byte) 102), globalSession, new LocalOperationId(0, 2))));
    // duplicate of tx 101
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(tx((byte) 101), globalSession, new LocalOperationId(0, 1))));
    // duplicate of tx 100
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(tx((byte) 100), globalSession, new LocalOperationId(0, 0))));
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(tx((byte) 103), globalSession, new LocalOperationId(0, 3))));
    raftLog.append(new RaftLogEntry(0, new DistributedOperation(tx((byte) 104), globalSession, new LocalOperationId(0, 4))));
    applicationProcess.notifyCommitted(6);
    applier.sync(false);
    InOrder inOrder = inOrder(coreStateMachines, commandDispatcher);
    // then
    inOrder.verify(coreStateMachines).commandDispatcher();
    inOrder.verify(commandDispatcher).dispatch(eq(tx((byte) 100)), eq(0L), anyCallback());
    inOrder.verify(commandDispatcher).dispatch(eq(tx((byte) 101)), eq(1L), anyCallback());
    inOrder.verify(commandDispatcher).dispatch(eq(tx((byte) 102)), eq(2L), anyCallback());
    // duplicate of tx 101 not dispatched, at index 3
    // duplicate of tx 100 not dispatched, at index 4
    inOrder.verify(commandDispatcher).dispatch(eq(tx((byte) 103)), eq(5L), anyCallback());
    inOrder.verify(commandDispatcher).dispatch(eq(tx((byte) 104)), eq(6L), anyCallback());
    inOrder.verify(commandDispatcher).close();
    verifyNoMoreInteractions(commandDispatcher);
}
Also used : InOrder(org.mockito.InOrder) LocalOperationId(org.neo4j.causalclustering.core.replication.session.LocalOperationId) DistributedOperation(org.neo4j.causalclustering.core.replication.DistributedOperation) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 35 with RaftLogEntry

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

the class AppendEntriesMessageFlowTest method shouldReturnFalseIfLogHistoryDoesNotMatch.

@Test
public void shouldReturnFalseIfLogHistoryDoesNotMatch() throws Exception {
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(0).prevLogIndex(0).prevLogTerm(0).logEntry(new RaftLogEntry(0, data(1))).build());
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(0).prevLogIndex(1).prevLogTerm(0).logEntry(new RaftLogEntry(0, data(2))).build());
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(0).prevLogIndex(2).prevLogTerm(0).logEntry(new RaftLogEntry(0, data(3))).build());
    // when
    raft.handle(appendEntriesRequest().from(otherMember).leaderTerm(2).prevLogIndex(3).prevLogTerm(1).logEntry(new RaftLogEntry(2, data(4))).build());
    // then
    InOrder invocationOrder = inOrder(outbound);
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(0).matchIndex(1).appendIndex(1).success().build()));
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(0).matchIndex(2).appendIndex(2).success().build()));
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(0).matchIndex(3).appendIndex(3).success().build()));
    invocationOrder.verify(outbound, times(1)).send(same(otherMember), eq(appendEntriesResponse().from(myself).term(2).matchIndex(-1).appendIndex(3).failure().build()));
}
Also used : InOrder(org.mockito.InOrder) 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