Search in sources :

Example 1 with RaftLogEntry

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

the class RaftMessageEncodingDecodingTest method shouldSerializeAppendRequestWithMultipleEntries.

@Test
public void shouldSerializeAppendRequestWithMultipleEntries() throws Exception {
    MemberId sender = new MemberId(UUID.randomUUID());
    RaftMessages.AppendEntries.Request request = new AppendEntriesRequestBuilder().from(sender).leaderCommit(2).leaderTerm(4).logEntry(new RaftLogEntry(1, ReplicatedInteger.valueOf(2))).logEntry(new RaftLogEntry(1, ReplicatedInteger.valueOf(3))).logEntry(new RaftLogEntry(1, ReplicatedInteger.valueOf(4))).build();
    serializeReadBackAndVerifyMessage(request);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) AppendEntriesRequestBuilder(org.neo4j.causalclustering.core.consensus.roles.AppendEntriesRequestBuilder) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 2 with RaftLogEntry

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

the class RaftMembershipManager method append.

@Override
public void append(long baseIndex, RaftLogEntry... entries) throws IOException {
    for (RaftLogEntry entry : entries) {
        if (entry.content() instanceof RaftGroup) {
            RaftGroup<MemberId> raftGroup = (RaftGroup<MemberId>) entry.content();
            if (state.uncommittedMemberChangeInLog()) {
                log.warn("Appending with uncommitted membership change in log");
            }
            if (state.append(baseIndex, new HashSet<>(raftGroup.getMembers()))) {
                log.info("Appending new member set %s", state);
                storage.persistStoreData(state);
                updateMemberSets();
            } else {
                log.warn("Appending member set was ignored. Current state: %s, Appended set: %s, Log index: %d%n", state, raftGroup, baseIndex);
            }
        }
        baseIndex++;
    }
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 3 with RaftLogEntry

use of org.neo4j.causalclustering.core.consensus.log.RaftLogEntry 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 4 with RaftLogEntry

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

the class Appending method appendNewEntry.

static void appendNewEntry(ReadableRaftState ctx, Outcome outcome, ReplicatedContent content) throws IOException {
    long prevLogIndex = ctx.entryLog().appendIndex();
    long prevLogTerm = prevLogIndex == -1 ? -1 : prevLogIndex > ctx.lastLogIndexBeforeWeBecameLeader() ? ctx.term() : ctx.entryLog().readEntryTerm(prevLogIndex);
    RaftLogEntry newLogEntry = new RaftLogEntry(ctx.term(), content);
    outcome.addShipCommand(new ShipCommand.NewEntries(prevLogIndex, prevLogTerm, new RaftLogEntry[] { newLogEntry }));
    outcome.addLogCommand(new AppendLogEntry(prevLogIndex + 1, newLogEntry));
}
Also used : ShipCommand(org.neo4j.causalclustering.core.consensus.outcome.ShipCommand) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 5 with RaftLogEntry

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

the class RaftLogShipper method sendRange.

private void sendRange(long startIndex, long endIndex, LeaderContext leaderContext) {
    if (startIndex > endIndex) {
        return;
    }
    lastSentIndex = endIndex;
    try {
        int batchSize = (int) (endIndex - startIndex + 1);
        RaftLogEntry[] entries = new RaftLogEntry[batchSize];
        long prevLogIndex = startIndex - 1;
        long prevLogTerm = raftLog.readEntryTerm(prevLogIndex);
        if (prevLogTerm > leaderContext.term) {
            log.warn("%s aborting send. Not leader anymore? %s, prevLogTerm=%d", statusAsString(), leaderContext, prevLogTerm);
            return;
        }
        boolean entryMissing = false;
        try (InFlightLogEntryReader logEntrySupplier = new InFlightLogEntryReader(raftLog, inFlightMap, false)) {
            for (int offset = 0; offset < batchSize; offset++) {
                entries[offset] = logEntrySupplier.get(startIndex + offset);
                if (entries[offset] == null) {
                    entryMissing = true;
                    break;
                }
                if (entries[offset].term() > leaderContext.term) {
                    log.warn("%s aborting send. Not leader anymore? %s, entryTerm=%d", statusAsString(), leaderContext, entries[offset].term());
                    return;
                }
            }
        }
        if (entryMissing || doesNotExistInLog(prevLogIndex, prevLogTerm)) {
            if (raftLog.prevIndex() >= prevLogIndex) {
                sendLogCompactionInfo(leaderContext);
            } else {
                log.error("%s: Could not send compaction info and entries were missing, but log is not behind.", statusAsString());
            }
        } else {
            RaftMessages.AppendEntries.Request appendRequest = new RaftMessages.AppendEntries.Request(leader, leaderContext.term, prevLogIndex, prevLogTerm, entries, leaderContext.commitIndex);
            outbound.send(follower, appendRequest);
        }
    } catch (IOException e) {
        log.warn(statusAsString() + " exception during batch send", e);
    }
}
Also used : InFlightLogEntryReader(org.neo4j.causalclustering.core.state.InFlightLogEntryReader) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) IOException(java.io.IOException) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

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