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);
}
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());
}
}
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))));
}
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;
}
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);
}
Aggregations