Search in sources :

Example 1 with BatchAppendLogEntries

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

the class Appending method appendNewEntries.

static void appendNewEntries(ReadableRaftState ctx, Outcome outcome, List<ReplicatedContent> contents) throws IOException {
    long prevLogIndex = ctx.entryLog().appendIndex();
    long prevLogTerm = prevLogIndex == -1 ? -1 : prevLogIndex > ctx.lastLogIndexBeforeWeBecameLeader() ? ctx.term() : ctx.entryLog().readEntryTerm(prevLogIndex);
    RaftLogEntry[] raftLogEntries = contents.stream().map(content -> new RaftLogEntry(ctx.term(), content)).toArray(RaftLogEntry[]::new);
    outcome.addShipCommand(new ShipCommand.NewEntries(prevLogIndex, prevLogTerm, raftLogEntries));
    outcome.addLogCommand(new BatchAppendLogEntries(prevLogIndex + 1, 0, raftLogEntries));
}
Also used : Log(org.neo4j.logging.Log) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) IOException(java.io.IOException) ReadableRaftState(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState) String.format(java.lang.String.format) ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) List(java.util.List) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) TruncateLogCommand(org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand) ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 2 with BatchAppendLogEntries

use of org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries 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 3 with BatchAppendLogEntries

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

the class AppendEntriesRequestTest method shouldAcceptInitialEntriesAfterBootstrap.

@Test
public void shouldAcceptInitialEntriesAfterBootstrap() throws Exception {
    RaftLog raftLog = bootstrappedLog();
    RaftState state = raftState().entryLog(raftLog).myself(myself).build();
    long leaderTerm = state.term() + leaderTermDifference;
    RaftLogEntry logEntry1 = new RaftLogEntry(leaderTerm, content());
    RaftLogEntry logEntry2 = new RaftLogEntry(leaderTerm, content());
    // when
    Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(0).prevLogTerm(0).logEntry(logEntry1).logEntry(logEntry2).build(), state, log());
    // then
    assertTrue(((Response) messageFor(outcome, leader)).success());
    assertThat(outcome.getLogCommands(), hasItem(new BatchAppendLogEntries(1, 0, new RaftLogEntry[] { logEntry1, logEntry2 })));
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 4 with BatchAppendLogEntries

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

the class AppendEntriesRequestTest method shouldAcceptInitialEntryAfterBootstrap.

@Test
public void shouldAcceptInitialEntryAfterBootstrap() throws Exception {
    RaftLog raftLog = bootstrappedLog();
    RaftState state = raftState().entryLog(raftLog).myself(myself).build();
    long leaderTerm = state.term() + leaderTermDifference;
    RaftLogEntry logEntry = new RaftLogEntry(leaderTerm, content());
    // when
    Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(0).prevLogTerm(0).logEntry(logEntry).build(), state, log());
    // then
    assertTrue(((Response) messageFor(outcome, leader)).success());
    assertThat(outcome.getLogCommands(), hasItem(new BatchAppendLogEntries(1, 0, new RaftLogEntry[] { logEntry })));
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 5 with BatchAppendLogEntries

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

the class AppendEntriesRequestTest method shouldAppendNewEntryAndCommitPreviouslyAppendedEntry.

@Test
public void shouldAppendNewEntryAndCommitPreviouslyAppendedEntry() throws Exception {
    // given
    InMemoryRaftLog raftLog = new InMemoryRaftLog();
    RaftState state = raftState().entryLog(raftLog).myself(myself).build();
    long leaderTerm = state.term() + leaderTermDifference;
    RaftLogEntry previouslyAppendedEntry = new RaftLogEntry(leaderTerm, content());
    raftLog.append(previouslyAppendedEntry);
    RaftLogEntry newLogEntry = new RaftLogEntry(leaderTerm, content());
    // when
    Outcome outcome = role.handler.handle(appendEntriesRequest().from(leader).leaderTerm(leaderTerm).prevLogIndex(raftLog.appendIndex()).prevLogTerm(leaderTerm).logEntry(newLogEntry).leaderCommit(0).build(), state, log());
    // then
    assertTrue(((Response) messageFor(outcome, leader)).success());
    assertThat(outcome.getCommitIndex(), Matchers.equalTo(0L));
    assertThat(outcome.getLogCommands(), hasItem(new BatchAppendLogEntries(1, 0, new RaftLogEntry[] { newLogEntry })));
}
Also used : InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) BatchAppendLogEntries(org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)6 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)5 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)5 Test (org.junit.Test)4 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)4 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)3 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)3 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)2 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)2 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)2 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)2 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 List (java.util.List)1 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)1 ReplicatedContent (org.neo4j.causalclustering.core.replication.ReplicatedContent)1 Log (org.neo4j.logging.Log)1