Esempio n. 1
0
        internal ElectionVote CastVote(Election election, Membership existingMembership, HeartbeatInfo info)
        {
            if (election == null || election.ElectionId == null)
            {
                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsWarnEnabled)
                {
                    LoggerManager.Instance.ShardLogger.Warn("ElectionManager.CastVote()", "Invalid election.");
                }
                return(null);
            }
            ElectionVote vote = new ElectionVote();

            vote.RequestingNode      = election.ElectionId.RequestingNode;
            vote.Sourcenode          = new ServerNode();
            vote.Sourcenode.Name     = _context.LocalAddress.IpAddress.ToString();
            vote.Sourcenode.Priority = (int)LocalServerPriority;
            if (existingMembership != null && (election.ElectionType == ElectionType.GeneralElections && existingMembership.Primary == null) || election.ElectionType == ElectionType.TakeoverElections)
            {
                if (election.RequestingServerInfo.LastOperationId > LocalServerInfo().LastOperationId)
                {
                    vote.NodeVote = ElectionVote.Vote.yes;
                }
                else if (election.RequestingServerInfo.LastOperationId == LocalServerInfo().LastOperationId)
                {
                    if (election.ElectionId.RequestingNode.Priority <= (int)LocalServerPriority)
                    {
                        vote.NodeVote = ElectionVote.Vote.yes;
                    }
                    else
                    {
                        ShardConfiguration sConfig = null;
                        if (_clusterConfigMgr != null)
                        {
                            sConfig = _clusterConfigMgr.GetShardConfiguration(_context.LocalShardName);
                        }
                        int configuredNodes = 0;
                        if (sConfig != null || sConfig.Servers != null)
                        {
                            configuredNodes = sConfig.Servers.Nodes.Count;
                        }
                        if (info != null && info.CSStatus == ConnectivityStatus.Connected && (configuredNodes != null && existingMembership.Servers.Count >= configuredNodes / 2))
                        {
                            vote.NodeVote = ElectionVote.Vote.no;
                        }
                        else
                        {
                            vote.NodeVote = ElectionVote.Vote.yes;
                        }
                    }
                }
                else
                {
                    vote.NodeVote = ElectionVote.Vote.no;
                }
            }
            else
            {
                vote.NodeVote = ElectionVote.Vote.no;
            }

            return(vote);
        }
Esempio n. 2
0
        internal Configuration.Services.ElectionResult ConductElection(ElectionId electionId, Address[] activeNodes, Activity activity)
        {
            Address[] votingNodes = null;
            if (activeNodes == null)
            {
                throw new ArgumentNullException("Active nodes are null");
            }
            Election election = null;

            if (votingNodes == null)
            {
                votingNodes = new Address[activeNodes.Length];
            }
            try
            {
                ElectionType electionType = ElectionType.None;
                if (activity == Activity.GeneralElectionsTriggered)
                {
                    electionType = ElectionType.GeneralElections;
                }
                else if (activity == Activity.TakeoverElectionsTriggered)
                {
                    electionType = ElectionType.TakeoverElections;
                }

                election    = new Election(electionId, electionType);
                votingNodes = activeNodes;
                election.RequestingServerInfo = LocalServerInfo();

                if (election.StartElection(votingNodes))
                {
                    ResponseCollection <object> response = (ResponseCollection <object>)MulticastRequest(election);

                    if (response != null)
                    {
                        foreach (var server in _shard.ActiveChannelsList)
                        {
                            IClusterResponse <object> serverResponse = response.GetResponse(server);
                            if (serverResponse.IsSuccessfull)
                            {
                                if (serverResponse.Value != null)
                                {
                                    ElectionVote vote = serverResponse.Value as ElectionVote;
                                    if (vote != null)
                                    {
                                        election.AddVote(vote);
                                    }
                                }
                            }
                        }
                    }
                    return(election.GetElectionResult());
                }
            }
            catch (Exception e)
            {
                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                {
                    LoggerManager.Instance.ShardLogger.Error("ElectionManager.ConductElection()", e.ToString());
                }
            }
            return(null);
        }