Search in sources :

Example 6 with RaftLogEntry

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

the class SegmentedRaftLog method append.

@Override
public synchronized long append(RaftLogEntry... entries) throws IOException {
    ensureOk();
    try {
        for (RaftLogEntry entry : entries) {
            state.appendIndex++;
            state.terms.append(state.appendIndex, entry.term());
            state.segments.last().write(state.appendIndex, entry);
        }
        state.segments.last().flush();
    } catch (Throwable e) {
        needsRecovery = true;
        throw e;
    }
    if (state.segments.last().position() >= rotateAtSize) {
        rotateSegment(state.appendIndex, state.appendIndex, state.terms.latest());
    }
    return state.appendIndex;
}
Also used : RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 7 with RaftLogEntry

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

the class SegmentedRaftLog method readEntryTerm.

@Override
public long readEntryTerm(long logIndex) throws IOException {
    if (logIndex > state.appendIndex) {
        return -1;
    }
    long term = state.terms.get(logIndex);
    if (term == -1 && logIndex >= state.prevIndex) {
        RaftLogEntry entry = readLogEntry(logIndex);
        term = (entry != null) ? entry.term() : -1;
    }
    return term;
}
Also used : RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 8 with RaftLogEntry

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

the class InFlightLogEntryReader method get.

public RaftLogEntry get(long logIndex) throws IOException {
    RaftLogEntry entry = null;
    if (useInFlightMap) {
        entry = inFlightMap.get(logIndex);
    }
    if (entry == null) {
        useInFlightMap = false;
        entry = getUsingCursor(logIndex);
    }
    if (removeReadIndexFromMap) {
        inFlightMap.remove(logIndex);
    }
    return entry;
}
Also used : RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 9 with RaftLogEntry

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

the class SegmentedRaftLogPartialEntryRecoveryTest method incompleteEntriesAtTheEndShouldNotCauseFailures.

@Test
public void incompleteEntriesAtTheEndShouldNotCauseFailures() throws Throwable {
    // Given
    // we use a RaftLog to create a raft log file and then we will start chopping bits off from the end
    SegmentedRaftLog raftLog = createRaftLog(100_000);
    raftLog.start();
    // Add a bunch of entries, preferably one of each available kind.
    raftLog.append(new RaftLogEntry(4, new NewLeaderBarrier()));
    raftLog.append(new RaftLogEntry(4, new ReplicatedIdAllocationRequest(new MemberId(UUID.randomUUID()), IdType.RELATIONSHIP, 1, 1024)));
    raftLog.append(new RaftLogEntry(4, new ReplicatedIdAllocationRequest(new MemberId(UUID.randomUUID()), IdType.RELATIONSHIP, 1025, 1024)));
    raftLog.append(new RaftLogEntry(4, new ReplicatedLockTokenRequest(new MemberId(UUID.randomUUID()), 1)));
    raftLog.append(new RaftLogEntry(4, new NewLeaderBarrier()));
    raftLog.append(new RaftLogEntry(5, new ReplicatedTokenRequest(TokenType.LABEL, "labelToken", new byte[] { 1, 2, 3 })));
    raftLog.append(new RaftLogEntry(5, new ReplicatedTransaction(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })));
    raftLog.stop();
    // We use a temporary RecoveryProtocol to get the file to chop
    RecoveryProtocol recovery = createRecoveryProtocol();
    State recoveryState = recovery.run();
    String logFilename = recoveryState.segments.last().getFilename();
    File logFile = new File(logDirectory, logFilename);
    // When
    // We remove any number of bytes from the end (up to but not including the header) and try to recover
    // Then
    // No exceptions should be thrown
    truncateAndRecover(logFile, SegmentHeader.SIZE);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) ReplicatedLockTokenRequest(org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenRequest) ReplicatedTokenRequest(org.neo4j.causalclustering.core.state.machines.token.ReplicatedTokenRequest) ReplicatedIdAllocationRequest(org.neo4j.causalclustering.core.state.machines.id.ReplicatedIdAllocationRequest) ReplicatedTransaction(org.neo4j.causalclustering.core.state.machines.tx.ReplicatedTransaction) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) File(java.io.File) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 10 with RaftLogEntry

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

the class SegmentedRaftLogRotationTest method shouldRotateOnAppendWhenRotateSizeIsReached.

@Test
public void shouldRotateOnAppendWhenRotateSizeIsReached() throws Exception {
    // When
    SegmentedRaftLog log = life.add(createRaftLog(ROTATE_AT_SIZE_IN_BYTES));
    log.append(new RaftLogEntry(0, replicatedStringOfBytes(ROTATE_AT_SIZE_IN_BYTES)));
    // Then
    File[] files = fileSystemRule.get().listFiles(testDirectory.directory());
    assertEquals(2, files.length);
}
Also used : File(java.io.File) 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