Example #1
0
        // TODO: rethink this test, it does too much
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void leaderShouldSpawnMismatchCommandOnFailure() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LeaderShouldSpawnMismatchCommandOnFailure()
        {
            // given

            /*
             * A leader who
             * - has an append index of 100
             * - knows about instance 2
             * - assumes that instance 2 is fully caught up
             */
            Leader        leader         = new Leader();
            MemberId      instance2      = member(2);
            FollowerState instance2State = CreateArtificialFollowerState(100);

            ReadableRaftState state = mock(typeof(ReadableRaftState));

            FollowerStates <MemberId> followerState = new FollowerStates <MemberId>();

            followerState = new FollowerStates <MemberId>(followerState, instance2, instance2State);

            RaftLog log = new InMemoryRaftLog();

            for (int i = 0; i <= 100; i++)
            {
                log.Append(new RaftLogEntry(0, valueOf(i)));
            }

            when(state.CommitIndex()).thenReturn(-1L);
            when(state.EntryLog()).thenReturn(log);
            when(state.FollowerStates()).thenReturn(followerState);
            when(state.Term()).thenReturn(4L);                 // both leader and follower are in the same term

            // when
            // that leader is asked to handle a response from that follower that says that the follower is still missing
            // things
            Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Response response = appendEntriesResponse().failure().appendIndex(0).matchIndex(-1).term(4).from(instance2).build();

            Outcome outcome = leader.Handle(response, state, mock(typeof(Log)));

            // then
            int mismatchCount = 0;

            foreach (ShipCommand shipCommand in outcome.ShipCommands)
            {
                if (shipCommand is ShipCommand.Mismatch)
                {
                    mismatchCount++;
                }
            }

            assertThat(mismatchCount, greaterThan(0));
        }
Example #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void leaderShouldRespondToSuccessResponseThatIndicatesLaggingFollowerWithJustWhatItsMissing() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LeaderShouldRespondToSuccessResponseThatIndicatesLaggingFollowerWithJustWhatItsMissing()
        {
            // given

            /*
             * A leader who
             * - has an append index of 100
             * - knows about instance 2
             * - assumes that instance 2 is at an index less than 100 -say 50
             */
            Leader        leader         = new Leader();
            MemberId      instance2      = member(2);
            FollowerState instance2State = CreateArtificialFollowerState(50);

            ReadableRaftState state = mock(typeof(ReadableRaftState));

            FollowerStates <MemberId> followerState = new FollowerStates <MemberId>();

            followerState = new FollowerStates <MemberId>(followerState, instance2, instance2State);

            ReadableRaftLog logMock = mock(typeof(ReadableRaftLog));

            when(logMock.AppendIndex()).thenReturn(100L);
            // with commit requests in this test

            when(state.CommitIndex()).thenReturn(-1L);
            when(state.EntryLog()).thenReturn(logMock);
            when(state.FollowerStates()).thenReturn(followerState);
            when(state.Term()).thenReturn(231L);                 // both leader and follower are in the same term

            // when that leader is asked to handle a response from that follower that says that the follower is still
            // missing things
            Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Response response = appendEntriesResponse().success().matchIndex(89).term(231).from(instance2).build();

            Outcome outcome = leader.Handle(response, state, mock(typeof(Log)));

            // then
            int matchCount = 0;

            foreach (ShipCommand shipCommand in outcome.ShipCommands)
            {
                if (shipCommand is ShipCommand.Match)
                {
                    matchCount++;
                }
            }

            assertThat(matchCount, greaterThan(0));
        }
Example #3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void leaderShouldIgnoreSuccessResponseThatIndicatesLaggingWhileLocalStateIndicatesFollowerIsCaughtUp() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LeaderShouldIgnoreSuccessResponseThatIndicatesLaggingWhileLocalStateIndicatesFollowerIsCaughtUp()
        {
            // given

            /*
             * A leader who
             * - has an append index of 100
             * - knows about instance 2
             * - assumes that instance 2 is fully caught up
             */
            Leader        leader         = new Leader();
            MemberId      instance2      = member(2);
            int           j              = 100;
            FollowerState instance2State = CreateArtificialFollowerState(j);

            ReadableRaftState state = mock(typeof(ReadableRaftState));

            FollowerStates <MemberId> followerState = new FollowerStates <MemberId>();

            followerState = new FollowerStates <MemberId>(followerState, instance2, instance2State);

            ReadableRaftLog logMock = mock(typeof(ReadableRaftLog));

            when(logMock.AppendIndex()).thenReturn(100L);
            //  with commit requests in this test

            when(state.CommitIndex()).thenReturn(-1L);
            when(state.EntryLog()).thenReturn(logMock);
            when(state.FollowerStates()).thenReturn(followerState);
            when(state.Term()).thenReturn(4L);                 // both leader and follower are in the same term

            // when that leader is asked to handle a response from that follower that says that the follower is still
            // missing things
            Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Response response = appendEntriesResponse().success().matchIndex(80).term(4).from(instance2).build();

            Outcome outcome = leader.Handle(response, state, mock(typeof(Log)));

            // then the leader should not send anything, since this is a delayed, out of order response to a previous append
            // request
            assertTrue(outcome.OutgoingMessages.Count == 0);
            // The follower state should not be touched
            FollowerStates <MemberId> updatedFollowerStates = outcome.FollowerStates;

            assertEquals(100, updatedFollowerStates.Get(instance2).MatchIndex);
        }
Example #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void leaderShouldNotRespondToSuccessResponseThatIndicatesUpToDateFollower() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LeaderShouldNotRespondToSuccessResponseThatIndicatesUpToDateFollower()
        {
            // given

            /*
             * A leader who
             * - has an append index of 100
             * - knows about instance 2
             * - assumes that instance 2 is at an index less than 100 -say 84
             */
            Leader        leader         = new Leader();
            MemberId      instance2      = member(2);
            FollowerState instance2State = CreateArtificialFollowerState(84);

            ReadableRaftState state = mock(typeof(ReadableRaftState));

            FollowerStates <MemberId> followerState = new FollowerStates <MemberId>();

            followerState = new FollowerStates <MemberId>(followerState, instance2, instance2State);

            ReadableRaftLog logMock = mock(typeof(ReadableRaftLog));

            when(logMock.AppendIndex()).thenReturn(100L);

            when(state.CommitIndex()).thenReturn(-1L);
            when(state.EntryLog()).thenReturn(logMock);
            when(state.FollowerStates()).thenReturn(followerState);
            when(state.Term()).thenReturn(4L);                 // both leader and follower are in the same term

            // when
            // that leader is asked to handle a response from that follower that says that the follower is up to date
            Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Response response = appendEntriesResponse().success().matchIndex(100).term(4).from(instance2).build();

            Outcome outcome = leader.Handle(response, state, mock(typeof(Log)));

            // then
            // The leader should not be trying to send any messages to that instance
            assertTrue(outcome.OutgoingMessages.Count == 0);
            // And the follower state should be updated
            FollowerStates <MemberId> updatedFollowerStates = outcome.FollowerStates;

            assertEquals(100, updatedFollowerStates.Get(instance2).MatchIndex);
        }
Example #5
0
        private void Defaults(Role currentRole, ReadableRaftState ctx)
        {
            _nextRole = currentRole;

            _term   = ctx.Term();
            _leader = ctx.Leader();

            _leaderCommit = ctx.LeaderCommit();

            _votedFor             = ctx.VotedFor();
            _renewElectionTimeout = false;
            _needsFreshSnapshot   = false;

            _isPreElection      = (currentRole == Role.FOLLOWER) && ctx.PreElection;
            _steppingDownInTerm = long?.empty();
            _preVotesForMe      = _isPreElection ? new HashSet <MemberId>(ctx.PreVotesForMe()) : emptySet();
            _votesForMe         = (currentRole == Role.CANDIDATE) ? new HashSet <MemberId>(ctx.VotesForMe()) : emptySet();
            _heartbeatResponses = (currentRole == Role.LEADER) ? new HashSet <MemberId>(ctx.HeartbeatResponses()) : emptySet();

            _lastLogIndexBeforeWeBecameLeader = (currentRole == Role.LEADER) ? ctx.LastLogIndexBeforeWeBecameLeader() : -1;
            _followerStates = (currentRole == Role.LEADER) ? ctx.FollowerStates() : new FollowerStates <MemberId>();

            _commitIndex = ctx.CommitIndex();
        }