コード例 #1
0
        public void should_select_valid_best_leader_candidate(TestCase tc)
        {
            var epochId    = Guid.NewGuid();
            var members    = new MemberInfo[3];
            var prepareOks = new Dictionary <Guid, ElectionMessage.PrepareOk>();

            Func <int, long> writerCheckpoint = i => tc.WriterCheckpoints[i];
            Func <int, long> chaserCheckpoint = i => tc.ChaserCheckpoints[i];
            Func <int, int>  nodePriority     = i => tc.NodePriorities[i];
            Func <int, int>  epochNumber      = i => tc.EpochNumbers[i];

            for (int index = 0; index < 3; index++)
            {
                members[index] = CreateMemberInfo(index, epochId, writerCheckpoint,
                                                  chaserCheckpoint, nodePriority, epochNumber);
            }

            var clusterInfo = new ClusterInfo(members);
            Func <int, Guid> previousLeaderId = i => !tc.LastElectedLeader[i].HasValue ? Guid.Empty : IdForNode(tc.LastElectedLeader[i].Value);

            for (int index = 0; index < 3; index++)
            {
                var prepareOk = CreatePrepareOk(index, epochId, writerCheckpoint, chaserCheckpoint,
                                                nodePriority, epochNumber, previousLeaderId, clusterInfo);
                prepareOks.Add(prepareOk.ServerId, prepareOk);
            }

            //var lastElectedLeader = tc.LastElectedLeader.HasValue
            //	? (Guid?)IdForNode(tc.LastElectedLeader.Value)
            //	: null;
            var resigningLeadership = tc.ResigningLeader.HasValue
                                ? (Guid?)IdForNode(tc.ResigningLeader.Value)
                                : null;
            var mc = SUT.GetBestLeaderCandidate(prepareOks, members, resigningLeadership, 0);

            Assert.AreEqual(IdForNode(tc.ExpectedLeaderCandidateNode), mc.InstanceId, $"Expected node {tc.ExpectedLeaderCandidateNode} got node {Array.FindIndex(members, 0, m => m.InstanceId == mc.InstanceId) + 1 }");

            var ownInfo = CreateLeaderCandidate(1, epochId, writerCheckpoint, chaserCheckpoint,
                                                nodePriority, epochNumber, previousLeaderId);

            var isLegit = SUT.IsLegitimateLeader(1, EndpointForNode(tc.ProposingNode),
                                                 IdForNode(tc.ProposingNode), mc, members, null, members[0].InstanceId,
                                                 ownInfo, resigningLeadership);

            Assert.True(isLegit);
        }
コード例 #2
0
        public void should_select_valid_best_leader_candidate(TestCase tc)
        {
            var epochId    = Guid.NewGuid();
            var members    = new MemberInfo[3];
            var prepareOks = new Dictionary <Guid, ElectionMessage.PrepareOk>();
            Func <int, long> lastCommitPosition = i => tc.CommitPositions[i];
            Func <int, long> writerCheckpoint   = i => tc.WriterCheckpoints[i];
            Func <int, long> chaserCheckpoint   = i => tc.ChaserCheckpoints[i];
            Func <int, int>  nodePriority       = i => tc.NodePriorities[i];

            for (int index = 0; index < 3; index++)
            {
                members[index] = CreateMemberInfo(index, epochId, lastCommitPosition, writerCheckpoint,
                                                  chaserCheckpoint, nodePriority);
                var prepareOk = CreatePrepareOk(index, epochId, lastCommitPosition, writerCheckpoint, chaserCheckpoint,
                                                nodePriority);
                prepareOks.Add(prepareOk.ServerId, prepareOk);
            }

            var lastElectedLeader = tc.LastElectedLeader.HasValue
                                ? (Guid?)IdForNode(tc.LastElectedLeader.Value)
                                : null;
            var resigningLeadership = tc.ResigningLeader.HasValue
                                ? (Guid?)IdForNode(tc.ResigningLeader.Value)
                                : null;
            var mc = SUT.GetBestLeaderCandidate(prepareOks, members, lastElectedLeader, resigningLeadership);

            Assert.AreEqual(IdForNode(tc.ExpectedLeaderCandidateNode), mc.InstanceId);

            var ownInfo = CreateLeaderCandidate(1, epochId, lastCommitPosition, writerCheckpoint, chaserCheckpoint,
                                                nodePriority);

            var localNode = FromMember(0, members);

            var isLegit = SUT.IsLegitimateLeader(1, EndpointForNode(tc.ProposingNode),
                                                 IdForNode(tc.ProposingNode), mc, members, null, localNode,
                                                 ownInfo, resigningLeadership);

            Assert.True(isLegit);
        }