//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldNotAllowTruncationAtCommit() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldNotAllowTruncationAtCommit() { // given long commitIndex = 5; long localTermForAllEntries = 1L; Outcome outcome = mock(typeof(Outcome)); ReadableRaftLog logMock = mock(typeof(ReadableRaftLog)); when(logMock.ReadEntryTerm(anyLong())).thenReturn(localTermForAllEntries); // for simplicity, all entries are at term 1 when(logMock.AppendIndex()).thenReturn(commitIndex); ReadableRaftState state = mock(typeof(ReadableRaftState)); when(state.EntryLog()).thenReturn(logMock); when(state.CommitIndex()).thenReturn(commitIndex); // when - then try { Appending.HandleAppendEntriesRequest(state, outcome, new Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request(_aMember, localTermForAllEntries, commitIndex - 1, localTermForAllEntries, new RaftLogEntry[] { new RaftLogEntry(localTermForAllEntries + 1, ReplicatedInteger.valueOf(2)) }, commitIndex + 3), NullLog.Instance); fail("Appending should not allow truncation at or before the commit index"); } catch (System.InvalidOperationException) { // ok } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldNotAttemptToTruncateAtIndexBeforeTheLogPrevIndex() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldNotAttemptToTruncateAtIndexBeforeTheLogPrevIndex() { // given // a log with prevIndex and prevTerm set ReadableRaftLog logMock = mock(typeof(ReadableRaftLog)); long prevIndex = 5; long prevTerm = 5; when(logMock.PrevIndex()).thenReturn(prevIndex); when(logMock.ReadEntryTerm(prevIndex)).thenReturn(prevTerm); // and which also properly returns -1 as the term for an unknown entry, in this case prevIndex - 2 when(logMock.ReadEntryTerm(prevIndex - 2)).thenReturn(-1L); // also, a state with a given commitIndex, obviously ahead of prevIndex long commitIndex = 10; ReadableRaftState state = mock(typeof(ReadableRaftState)); when(state.EntryLog()).thenReturn(logMock); when(state.CommitIndex()).thenReturn(commitIndex); // which is also the append index when(logMock.AppendIndex()).thenReturn(commitIndex); // when // an appendEntriesRequest arrives for appending entries before the prevIndex (for whatever reason) Outcome outcome = mock(typeof(Outcome)); Appending.HandleAppendEntriesRequest(state, outcome, new Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request(_aMember, prevTerm, prevIndex - 2, prevTerm, new RaftLogEntry[] { new RaftLogEntry(prevTerm, ReplicatedInteger.valueOf(2)) }, commitIndex + 3), NullLog.Instance); // then // there should be no truncate commands. Actually, the whole thing should be a no op verify(outcome, never()).addLogCommand(any()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldPerformTruncation() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldPerformTruncation() { // when // we have a log appended up to appendIndex, and committed somewhere before that long appendIndex = 5; long localTermForAllEntries = 1L; Outcome outcome = mock(typeof(Outcome)); ReadableRaftLog logMock = mock(typeof(ReadableRaftLog)); when(logMock.ReadEntryTerm(anyLong())).thenReturn(localTermForAllEntries); // for simplicity, all entries are at term 1 when(logMock.AppendIndex()).thenReturn(appendIndex); ReadableRaftState state = mock(typeof(ReadableRaftState)); when(state.EntryLog()).thenReturn(logMock); when(state.CommitIndex()).thenReturn(appendIndex - 3); // when // the leader asks to append after the commit index an entry that mismatches on term Appending.HandleAppendEntriesRequest(state, outcome, new Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request(_aMember, localTermForAllEntries, appendIndex - 2, localTermForAllEntries, new RaftLogEntry[] { new RaftLogEntry(localTermForAllEntries + 1, ReplicatedInteger.valueOf(2)) }, appendIndex + 3), NullLog.Instance); // then // we must produce a TruncateLogCommand at the earliest mismatching index verify(outcome, times(1)).addLogCommand(argThat(new LogCommandMatcher(appendIndex - 1))); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private static void respondToPreVoteRequest(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState state, org.neo4j.causalclustering.core.consensus.outcome.Outcome outcome, org.neo4j.causalclustering.core.consensus.RaftMessages_PreVote_Request voteRequest, org.neo4j.function.ThrowingBooleanSupplier<java.io.IOException> willVoteFor) throws java.io.IOException private static void RespondToPreVoteRequest(ReadableRaftState state, Outcome outcome, Org.Neo4j.causalclustering.core.consensus.RaftMessages_PreVote_Request voteRequest, ThrowingBooleanSupplier <IOException> willVoteFor) { if (voteRequest.Term() > state.Term()) { outcome.NextTerm = voteRequest.Term(); } outcome.AddOutgoingMessage(new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Directed(voteRequest.From(), new Org.Neo4j.causalclustering.core.consensus.RaftMessages_PreVote_Response(state.Myself(), outcome.Term, willVoteFor.AsBoolean))); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private static boolean shouldVoteFor(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState state, org.neo4j.causalclustering.core.consensus.outcome.Outcome outcome, org.neo4j.causalclustering.core.consensus.RaftMessages_AnyVote_Request voteRequest, boolean committedToVotingForAnother, org.neo4j.logging.Log log) throws java.io.IOException private static bool ShouldVoteFor(ReadableRaftState state, Outcome outcome, Org.Neo4j.causalclustering.core.consensus.RaftMessages_AnyVote_Request voteRequest, bool committedToVotingForAnother, Log log) { long requestTerm = voteRequest.Term(); MemberId candidate = voteRequest.Candidate(); long requestLastLogTerm = voteRequest.LastLogTerm(); long requestLastLogIndex = voteRequest.LastLogIndex(); long contextTerm = outcome.Term; long contextLastAppended = state.EntryLog().appendIndex(); long contextLastLogTerm = state.EntryLog().readEntryTerm(contextLastAppended); return(ShouldVoteFor(candidate, contextTerm, requestTerm, contextLastLogTerm, requestLastLogTerm, contextLastAppended, requestLastLogIndex, committedToVotingForAnother, log)); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: static void handleVoteRequest(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState state, org.neo4j.causalclustering.core.consensus.outcome.Outcome outcome, org.neo4j.causalclustering.core.consensus.RaftMessages_Vote_Request voteRequest, org.neo4j.logging.Log log) throws java.io.IOException internal static void HandleVoteRequest(ReadableRaftState state, Outcome outcome, Org.Neo4j.causalclustering.core.consensus.RaftMessages_Vote_Request voteRequest, Log log) { if (voteRequest.Term() > state.Term()) { outcome.NextTerm = voteRequest.Term(); outcome.VotedFor = null; } bool votedForAnother = outcome.VotedFor != null && !outcome.VotedFor.Equals(voteRequest.Candidate()); bool willVoteForCandidate = ShouldVoteFor(state, outcome, voteRequest, votedForAnother, log); if (willVoteForCandidate) { outcome.VotedFor = voteRequest.From(); outcome.RenewElectionTimeout(); } outcome.AddOutgoingMessage(new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Directed(voteRequest.From(), new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Vote_Response(state.Myself(), outcome.Term, willVoteForCandidate))); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: static void beat(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState state, org.neo4j.causalclustering.core.consensus.outcome.Outcome outcome, org.neo4j.causalclustering.core.consensus.RaftMessages_Heartbeat request, org.neo4j.logging.Log log) throws java.io.IOException internal static void Beat(ReadableRaftState state, Outcome outcome, Org.Neo4j.causalclustering.core.consensus.RaftMessages_Heartbeat request, Log log) { if (request.LeaderTerm() < state.Term()) { return; } outcome.PreElection = false; outcome.NextTerm = request.LeaderTerm(); outcome.Leader = request.From(); outcome.LeaderCommit = request.CommitIndex(); outcome.AddOutgoingMessage(new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Directed(request.From(), new Org.Neo4j.causalclustering.core.consensus.RaftMessages_HeartbeatResponse(state.Myself()))); if (!Follower.LogHistoryMatches(state, request.CommitIndex(), request.CommitIndexTerm())) { return; } Follower.CommitToLogOnUpdate(state, request.CommitIndex(), request.CommitIndex(), outcome); }
internal Handler(ReadableRaftState ctx, Log log) { this.Ctx = ctx; this.Log = log; this.Outcome = new Outcome(CANDIDATE, ctx); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: public org.neo4j.causalclustering.core.consensus.outcome.Outcome handle(org.neo4j.causalclustering.core.consensus.RaftMessages_RaftMessage message, org.neo4j.causalclustering.core.consensus.state.ReadableRaftState ctx, org.neo4j.logging.Log log) throws java.io.IOException public override Outcome Handle(Org.Neo4j.causalclustering.core.consensus.RaftMessages_RaftMessage message, ReadableRaftState ctx, Log log) { return(message.Dispatch(new Handler(ctx, log))); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: static void declinePreVoteRequest(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState state, org.neo4j.causalclustering.core.consensus.outcome.Outcome outcome, org.neo4j.causalclustering.core.consensus.RaftMessages_PreVote_Request voteRequest) throws java.io.IOException internal static void DeclinePreVoteRequest(ReadableRaftState state, Outcome outcome, Org.Neo4j.causalclustering.core.consensus.RaftMessages_PreVote_Request voteRequest) { RespondToPreVoteRequest(state, outcome, voteRequest, () => false); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: static void handlePreVoteRequest(org.neo4j.causalclustering.core.consensus.state.ReadableRaftState state, org.neo4j.causalclustering.core.consensus.outcome.Outcome outcome, org.neo4j.causalclustering.core.consensus.RaftMessages_PreVote_Request voteRequest, org.neo4j.logging.Log log) throws java.io.IOException internal static void HandlePreVoteRequest(ReadableRaftState state, Outcome outcome, Org.Neo4j.causalclustering.core.consensus.RaftMessages_PreVote_Request voteRequest, Log log) { ThrowingBooleanSupplier <IOException> willVoteForCandidate = () => ShouldVoteFor(state, outcome, voteRequest, false, log); RespondToPreVoteRequest(state, outcome, voteRequest, willVoteForCandidate); }