Search in sources :

Example 1 with NewLeaderBarrier

use of org.neo4j.causalclustering.core.consensus.NewLeaderBarrier 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 2 with NewLeaderBarrier

use of org.neo4j.causalclustering.core.consensus.NewLeaderBarrier in project neo4j by neo4j.

the class SegmentedRaftLogPartialEntryRecoveryTest method shouldNotAppendAtTheEndOfLogFileWithIncompleteEntries.

@Test
public void shouldNotAppendAtTheEndOfLogFileWithIncompleteEntries() throws Throwable {
    // Given
    // we use a RaftLog to create a raft log file and then we will chop some bits off the end
    SegmentedRaftLog raftLog = createRaftLog(100_000);
    raftLog.start();
    raftLog.append(new RaftLogEntry(4, new NewLeaderBarrier()));
    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);
    StoreChannel lastFile = fsRule.get().open(logFile, "rw");
    long currentSize = lastFile.size();
    lastFile.close();
    // When
    // We induce an incomplete entry at the end of the last file
    lastFile = fsRule.get().open(logFile, "rw");
    lastFile.truncate(currentSize - 1);
    lastFile.close();
    // We start the raft log again, on the previous log file with truncated last entry.
    raftLog = createRaftLog(100_000);
    //  Recovery will run here
    raftLog.start();
    // Append an entry
    raftLog.append(new RaftLogEntry(4, new NewLeaderBarrier()));
    // The log should report as containing only the last entry we've appended
    try (RaftLogCursor entryCursor = raftLog.getEntryCursor(0)) {
        // There should be exactly one entry, of type NewLeaderBarrier
        assertTrue(entryCursor.next());
        RaftLogEntry raftLogEntry = entryCursor.get();
        assertEquals(NewLeaderBarrier.class, raftLogEntry.content().getClass());
        assertFalse(entryCursor.next());
    }
}
Also used : RaftLogCursor(org.neo4j.causalclustering.core.consensus.log.RaftLogCursor) StoreChannel(org.neo4j.io.fs.StoreChannel) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) File(java.io.File) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 3 with NewLeaderBarrier

use of org.neo4j.causalclustering.core.consensus.NewLeaderBarrier in project neo4j by neo4j.

the class CandidateTest method shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm.

@Test
public void shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm() throws Exception {
    // given
    RaftState state = RaftStateBuilder.raftState().term(1).myself(myself).votingMembers(member1, member2).replicationMembers(member1, member2).build();
    // when
    Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(state.term()).from(member1).grant().build(), state, log());
    // then
    assertEquals(LEADER, outcome.getRole());
    assertEquals(true, outcome.electionTimeoutRenewed());
    assertThat(outcome.getLogCommands(), hasItem(new AppendLogEntry(0, new RaftLogEntry(state.term(), new NewLeaderBarrier()))));
    assertThat(outcome.getOutgoingMessages(), hasItems(new RaftMessages.Directed(member1, new RaftMessages.Heartbeat(myself, state.term(), -1, -1)), new RaftMessages.Directed(member2, new RaftMessages.Heartbeat(myself, state.term(), -1, -1))));
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 4 with NewLeaderBarrier

use of org.neo4j.causalclustering.core.consensus.NewLeaderBarrier in project neo4j by neo4j.

the class CoreReplicatedContentMarshal method unmarshal0.

@Override
public ReplicatedContent unmarshal0(ReadableChannel channel) throws IOException, EndOfStreamException {
    byte type = channel.get();
    final ReplicatedContent content;
    switch(type) {
        case TX_CONTENT_TYPE:
            content = ReplicatedTransactionSerializer.unmarshal(channel);
            break;
        case RAFT_MEMBER_SET_TYPE:
            content = MemberIdSetSerializer.unmarshal(channel);
            break;
        case ID_RANGE_REQUEST_TYPE:
            content = ReplicatedIdAllocationRequestSerializer.unmarshal(channel);
            break;
        case TOKEN_REQUEST_TYPE:
            content = ReplicatedTokenRequestSerializer.unmarshal(channel);
            break;
        case NEW_LEADER_BARRIER_TYPE:
            content = new NewLeaderBarrier();
            break;
        case LOCK_TOKEN_REQUEST:
            content = ReplicatedLockTokenSerializer.unmarshal(channel);
            break;
        case DISTRIBUTED_OPERATION:
            content = DistributedOperation.deserialize(channel);
            break;
        default:
            throw new IllegalArgumentException(String.format("Unknown content type 0x%x", type));
    }
    return content;
}
Also used : ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)

Example 5 with NewLeaderBarrier

use of org.neo4j.causalclustering.core.consensus.NewLeaderBarrier in project neo4j by neo4j.

the class SegmentedRaftLogPartialEntryRecoveryTest method incompleteHeaderOfLastOfMoreThanOneLogFilesShouldNotCauseFailure.

@Test
public void incompleteHeaderOfLastOfMoreThanOneLogFilesShouldNotCauseFailure() throws Throwable {
    // Given
    // we use a RaftLog to create two log files, in order to chop the header of the second
    SegmentedRaftLog raftLog = createRaftLog(1);
    raftLog.start();
    // will cause rotation
    raftLog.append(new RaftLogEntry(4, new NewLeaderBarrier()));
    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 of the second file and try to recover
    // Then
    // No exceptions should be thrown
    truncateAndRecover(logFile, 0);
}
Also used : NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) File(java.io.File) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

NewLeaderBarrier (org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)8 Test (org.junit.Test)7 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)7 File (java.io.File)3 InOrder (org.mockito.InOrder)2 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)1 RaftLogCursor (org.neo4j.causalclustering.core.consensus.log.RaftLogCursor)1 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)1 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)1 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)1 DistributedOperation (org.neo4j.causalclustering.core.replication.DistributedOperation)1 ReplicatedContent (org.neo4j.causalclustering.core.replication.ReplicatedContent)1 LocalOperationId (org.neo4j.causalclustering.core.replication.session.LocalOperationId)1 ReplicatedIdAllocationRequest (org.neo4j.causalclustering.core.state.machines.id.ReplicatedIdAllocationRequest)1 ReplicatedLockTokenRequest (org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenRequest)1 ReplicatedTokenRequest (org.neo4j.causalclustering.core.state.machines.token.ReplicatedTokenRequest)1 ReplicatedTransaction (org.neo4j.causalclustering.core.state.machines.tx.ReplicatedTransaction)1 MemberId (org.neo4j.causalclustering.identity.MemberId)1 StoreChannel (org.neo4j.io.fs.StoreChannel)1