示例#1
0
//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)));
        }
示例#2
0
//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());
        }
示例#3
0
//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
            }
        }
示例#4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void commit(ClusterState clusterState, org.neo4j.causalclustering.identity.MemberId member, long commitIndex) throws java.io.IOException
        private void Commit(ClusterState clusterState, MemberId member, long commitIndex)
        {
            ComparableRaftState state   = clusterState.States[member];
            Outcome             outcome = new Outcome(clusterState.Roles[member], state);

            outcome.CommitIndex = commitIndex;
            state.Update(outcome);
        }
示例#5
0
//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)));
        }
示例#6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldStayAsCandidateOnReceivingDeniedVoteResponseWithCurrentTerm() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldStayAsCandidateOnReceivingDeniedVoteResponseWithCurrentTerm()
        {
            // given
            RaftState state = NewState();

            // when
            Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(state.Term()).from(_member1).deny().build(), state, Log());

            // then
            assertEquals(CANDIDATE, outcome.Role);
        }
示例#7
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are ignored unless the option to convert to C# 7.2 'in' parameters is selected:
//ORIGINAL LINE: public static RaftMessages_RaftMessage messageFor(org.neo4j.causalclustering.core.consensus.outcome.Outcome outcome, final org.neo4j.causalclustering.identity.MemberId member)
        public static RaftMessages_RaftMessage MessageFor(Outcome outcome, MemberId member)
        {
            System.Predicate <RaftMessages_Directed> selectMember = message => message.to() == member;
            try
            {
                return(Iterables.single(new FilteringIterable <>(outcome.OutgoingMessages, selectMember)).message());
            }
            catch (NoSuchElementException)
            {
                throw new AssertionError(format("Expected message for %s, but outcome only contains %s.", member, outcome.OutgoingMessages));
            }
        }
示例#8
0
//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));
        }
示例#9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldDeclinePreVoteFromSameTerm() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldDeclinePreVoteFromSameTerm()
        {
            // given
            RaftState raftState = raftState().myself(_myself).supportsPreVoting(true).build();

            // when
            Outcome outcome = CANDIDATE.handler.handle(preVoteRequest().candidate(_member1).from(_member1).term(raftState.Term()).build(), raftState, Log());

            // then
            assertThat(outcome.OutgoingMessages, hasItem(new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Directed(_member1, preVoteResponse().term(raftState.Term()).from(_myself).deny().build())));
            assertEquals(Role.Candidate, outcome.Role);
        }
示例#10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm()
        {
            // 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.Role);
            assertTrue(outcome.ElectionTimeoutRenewed());
            assertThat(outcome.LogCommands, hasItem(new AppendLogEntry(0, new RaftLogEntry(state.Term(), new NewLeaderBarrier()))));
            assertThat(outcome.OutgoingMessages, hasItems(new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Directed(_member1, new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Heartbeat(_myself, state.Term(), -1, -1)), new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Directed(_member2, new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Heartbeat(_myself, state.Term(), -1, -1))));
        }
示例#11
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRejectVoteResponseWithOldTerm() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRejectVoteResponseWithOldTerm()
        {
            // given
            RaftState state = NewState();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long voterTerm = state.term() - 1;
            long voterTerm = state.Term() - 1;

            // when
            Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(voterTerm).from(_member1).grant().build(), state, Log());

            // then
            assertEquals(CANDIDATE, outcome.Role);
        }
示例#12
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRejectVoteRequestFromPreviousTerm() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRejectVoteRequestFromPreviousTerm()
        {
            RaftState state = NewState();

            // when
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long candidateTerm = state.term() - 1;
            long candidateTerm = state.Term() - 1;

            Outcome outcome = Role.handler.handle(voteRequest().from(_member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, Log());

            // then
            assertFalse(((Org.Neo4j.causalclustering.core.consensus.RaftMessages_Vote_Response)messageFor(outcome, _member1)).voteGranted());
            assertEquals(Role, outcome.Role);
        }
示例#13
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldDenyForCandidateInLaterTermWhenPreVoteNotActive() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldDenyForCandidateInLaterTermWhenPreVoteNotActive()
        {
            // given
            RaftState state = NewState();

            // when
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long candidateTerm = state.term() + 1;
            long candidateTerm = state.Term() + 1;

            Outcome outcome = Role.handler.handle(preVoteRequest().from(_member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, Log());

            // then
            assertFalse(((Org.Neo4j.causalclustering.core.consensus.RaftMessages_PreVote_Response)messageFor(outcome, _member1)).voteGranted());
        }
示例#14
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldUpdateTermIfRequestIsFromLaterTerm() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldUpdateTermIfRequestIsFromLaterTerm()
        {
            // given
            RaftState state = NewState();

            // when
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long candidateTerm = state.term() + 1;
            long candidateTerm = state.Term() + 1;

            Outcome outcome = Role.handler.handle(preVoteRequest().from(_member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, Log());

            // then
            assertEquals(candidateTerm, outcome.Term);
        }
示例#15
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldBecomeFollowerIfReceivePreVoteRequestFromLaterTerm() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldBecomeFollowerIfReceivePreVoteRequestFromLaterTerm()
        {
            // given
            RaftState raftState = raftState().myself(_myself).supportsPreVoting(true).build();
            long      newTerm   = raftState.Term() + 1;

            // when
            Outcome outcome = CANDIDATE.handler.handle(preVoteRequest().candidate(_member1).from(_member1).term(newTerm).build(), raftState, Log());

            // then
            assertEquals(newTerm, outcome.Term);
            assertEquals(Role.Follower, outcome.Role);
            assertThat(outcome.VotesForMe, empty());

            assertThat(outcome.OutgoingMessages, hasItem(new Org.Neo4j.causalclustering.core.consensus.RaftMessages_Directed(_member1, preVoteResponse().term(newTerm).from(_myself).deny().build())));
        }
示例#16
0
//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)));
        }
示例#17
0
//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);
        }
示例#18
0
//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);
        }
示例#19
0
//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);
        }
示例#20
0
 internal static void HandlePruneRequest(Outcome outcome, Org.Neo4j.causalclustering.core.consensus.RaftMessages_PruneRequest pruneRequest)
 {
     outcome.AddLogCommand(new PruneLogCommand(pruneRequest.PruneIndex()));
 }