コード例 #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void update(org.neo4j.causalclustering.core.consensus.outcome.Outcome outcome) throws java.io.IOException
        public virtual void Update(Outcome outcome)
        {
            if (TermState().update(outcome.Term))
            {
                _termStorage.persistStoreData(TermState());
            }
            if (VoteState().update(outcome.VotedFor, outcome.Term))
            {
                _voteStorage.persistStoreData(VoteState());
            }

            LogIfLeaderChanged(outcome.Leader);
            _leader     = outcome.Leader;
            _leaderInfo = new LeaderInfo(outcome.Leader, outcome.Term);

            _leaderCommit       = outcome.LeaderCommit;
            _votesForMe         = outcome.VotesForMe;
            _preVotesForMe      = outcome.PreVotesForMe;
            _heartbeatResponses = outcome.HeartbeatResponses;
            _lastLogIndexBeforeWeBecameLeader = outcome.LastLogIndexBeforeWeBecameLeader;
            _followerStates = outcome.FollowerStates;
            _isPreElection  = outcome.PreElection;

            foreach (RaftLogCommand logCommand in outcome.LogCommands)
            {
                logCommand.ApplyTo(_entryLog, _log);
                logCommand.ApplyTo(_inFlightCache, _log);
            }
            _commitIndex = outcome.CommitIndex;
        }
コード例 #2
0
        // TODO: This method is inefficient.. we should not have to update this state by a complete
        // TODO: iteration each time. Instead it should be updated as a direct response to each
        // TODO: append response.
        public static long QuorumAppendIndex <MEMBER>(ISet <MEMBER> votingMembers, FollowerStates <MEMBER> states)
        {
            /*
             * Build up a map of tx id -> number of instances that have appended,
             * sorted by tx id.
             *
             * This allows us to then iterate backwards over the values in the map,
             * adding up a total count of how many have appended, until we reach a majority.
             * Once we do, the tx id at the current entry in the map will be the highest one
             * with a majority appended.
             */

            SortedDictionary <long, int> appendedCounts = new SortedDictionary <long, int>();

            foreach (MEMBER member in votingMembers)
            {
                long txId = states.Get(member).MatchIndex;
                appendedCounts.merge(txId, 1, (a, b) => a + b);
            }

            // Iterate over it until we find a majority
            int total = 0;

            foreach (KeyValuePair <long, int> entry in appendedCounts.descendingMap().entrySet())
            {
                total += entry.Value;
                if (MajorityIncludingSelfQuorum.IsQuorum(votingMembers.Count, total))
                {
                    return(entry.Key);
                }
            }

            // No majority for any appended entry
            return(-1);
        }
コード例 #3
0
ファイル: Follower.cs プロジェクト: ShantanuBhatia/PPTP2019
 void Start()
 {
     currentState         = FollowerStates.NOT_FOLLOWING;
     currentTalkTime      = 0f;
     pxs                  = GetComponent <ParallaxScrollSideways>();
     initialParallaxSpeed = pxs.speedCoefficient;
     gc = GameObject.Find("GameController").GetComponent <GameController>();
 }
コード例 #4
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));
        }
コード例 #5
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));
        }
コード例 #6
0
    void Start()
    {
        // 设置ID
        SetID(++EntityManager.lastEntityID);
        target     = GameObject.FindGameObjectWithTag(Tags.Leader);
        FollowerAC = this.gameObject.GetComponent <LocomotionFollower>();
        for (int i = 0; i < arraryOfVoices.Length; i++)
        {
            FollowerVoices.Add(arraryOfVoices[i].name, arraryOfVoices[i]);
        }
        voice = this.gameObject.GetComponent <AudioSource>();

        EntityManager.Instance().RegisterEntity(this);
        States          = this.gameObject.transform.FindChild("FollowerStates").GetComponent <FollowerStates>();
        m_pStateMachine = new StateMachine <Follower>(this);
        m_pStateMachine.SetCurrentState(States.IdleState);
    }
コード例 #7
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);
        }
コード例 #8
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void update(org.neo4j.causalclustering.core.consensus.outcome.Outcome outcome) throws java.io.IOException
        public virtual void Update(Outcome outcome)
        {
            TermConflict   = outcome.Term;
            _votedFor      = outcome.VotedFor;
            LeaderConflict = outcome.Leader;
            _votesForMe    = outcome.VotesForMe;
            _lastLogIndexBeforeWeBecameLeader = outcome.LastLogIndexBeforeWeBecameLeader;
            _followerStates = outcome.FollowerStates;
            _isPreElection  = outcome.PreElection;

            foreach (RaftLogCommand logCommand in outcome.LogCommands)
            {
                logCommand.ApplyTo(EntryLogConflict, _log);
                logCommand.ApplyTo(_inFlightCache, _log);
            }

            _commitIndex = outcome.CommitIndex;
        }
コード例 #9
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);
        }
コード例 #10
0
        public Outcome(Role nextRole, long term, MemberId leader, long leaderCommit, MemberId votedFor, ISet <MemberId> votesForMe, ISet <MemberId> preVotesForMe, long lastLogIndexBeforeWeBecameLeader, FollowerStates <MemberId> followerStates, bool renewElectionTimeout, ICollection <RaftLogCommand> logCommands, ICollection <Org.Neo4j.causalclustering.core.consensus.RaftMessages_Directed> outgoingMessages, ICollection <ShipCommand> shipCommands, long commitIndex, ISet <MemberId> heartbeatResponses, bool isPreElection)
        {
            this._nextRole      = nextRole;
            this._term          = term;
            this._leader        = leader;
            this._leaderCommit  = leaderCommit;
            this._votedFor      = votedFor;
            this._votesForMe    = new HashSet <MemberId>(votesForMe);
            this._preVotesForMe = new HashSet <MemberId>(preVotesForMe);
            this._lastLogIndexBeforeWeBecameLeader = lastLogIndexBeforeWeBecameLeader;
            this._followerStates       = followerStates;
            this._renewElectionTimeout = renewElectionTimeout;
            this._heartbeatResponses   = new HashSet <MemberId>(heartbeatResponses);

            this._logCommands.addAll(logCommands);
            this._outgoingMessages.addAll(outgoingMessages);
            this._shipCommands.addAll(shipCommands);
            this._commitIndex        = commitIndex;
            this._isPreElection      = isPreElection;
            this._steppingDownInTerm = long?.empty();
        }
コード例 #11
0
ファイル: Follower.cs プロジェクト: ShantanuBhatia/PPTP2019
    // Update is called once per frame
    void Update()
    {
        if (currentState == FollowerStates.NOT_FOLLOWING && Mathf.Abs(leader.transform.position.x - transform.position.x) < leaderConversationDist)
        {
            currentState = FollowerStates.CONVERSATION;
        }
        else if (currentState == FollowerStates.CONVERSATION && Mathf.Abs(leader.transform.position.x - transform.position.x) > leaderConversationDist)
        {
            currentState = FollowerStates.NOT_FOLLOWING;
        }

        if (currentState == FollowerStates.CONVERSATION)
        {
            currentTalkTime += Time.deltaTime;
            Debug.Log("Conversing...");
            if (currentTalkTime >= maxTalkTime)
            {
                Debug.Log("We're convinced, and we're going to follow you now");
                currentState = FollowerStates.FOLLOWING;
                TriggerFollowStart();
            }
        }
        else
        {
            currentTalkTime = 0f;
        }
        if (currentState == FollowerStates.FOLLOWING && Mathf.Abs(transform.position.x - targetX) < targetMargin)
        {
            TriggerFollowStop();
        }
        if (currentState == FollowerStates.REACHED_DEST)
        {
            transform.position   = endingPosition;
            transform.parent     = null;
            pxs.speedCoefficient = initialParallaxSpeed;
        }
    }
コード例 #12
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();
        }
コード例 #13
0
ファイル: Follower.cs プロジェクト: ShantanuBhatia/PPTP2019
 public void TriggerFollowStop()
 {
     currentState = FollowerStates.REACHED_DEST;
     leader.GetComponent <StanBehaviour>().CollectGroup();
 }
コード例 #14
0
ファイル: Follower.cs プロジェクト: ShantanuBhatia/PPTP2019
 public void TriggerFollowStart()
 {
     currentState = FollowerStates.FOLLOWING;
     transform.SetParent(leader.transform);
     pxs.speedCoefficient = 0f;
 }
コード例 #15
0
ファイル: Follower.cs プロジェクト: ShantanuBhatia/PPTP2019
 public void TriggerConversation()
 {
     currentState = FollowerStates.CONVERSATION;
 }
コード例 #16
0
 public override RaftMembershipStateMachineEventHandler OnFollowerStateChange(FollowerStates <MemberId> followerStates)
 {
     return(this);
 }
コード例 #17
0
 public virtual void ReplaceFollowerStates(FollowerStates <MemberId> followerStates)
 {
     this._followerStates = followerStates;
 }
コード例 #18
0
 public virtual void OnFollowerStateChange(FollowerStates <MemberId> followerStates)
 {
     _membershipChanger.onFollowerStateChange(followerStates);
 }
コード例 #19
0
            public override RaftMembershipStateMachineEventHandler OnFollowerStateChange(FollowerStates <MemberId> followerStates)
            {
                CatchupGoalTracker.updateProgress(followerStates.Get(outerInstance.catchingUpMember));

                if (CatchupGoalTracker.Finished)
                {
                    if (CatchupGoalTracker.GoalAchieved)
                    {
                        ISet <MemberId> updatedVotingMembers = new HashSet <MemberId>(outerInstance.membershipManager.VotingMembers());
                        updatedVotingMembers.Add(outerInstance.catchingUpMember);
                        outerInstance.membershipManager.DoConsensus(updatedVotingMembers);

                        MovingToConsensus = true;
                        return(new ConsensusInProgress(_outerInstance));
                    }
                    else
                    {
                        return(new Idle(_outerInstance));
                    }
                }
                return(this);
            }
コード例 #20
0
 internal virtual void OnFollowerStateChange(FollowerStates <MemberId> followerStates)
 {
     HandleState(State.onFollowerStateChange(followerStates));
 }