Beispiel #1
0
        private void SendProposal()
        {
            _acceptsReceived.Clear();
            _masterProposal = null;

            var master = GetBestMasterCandidate();

            if (master == null)
            {
                Log.Trace("ELECTIONS: (V={lastAttemptedView}) NO MASTER CANDIDATE WHEN TRYING TO SEND PROPOSAL.",
                          _lastAttemptedView);
                return;
            }

            _masterProposal = master;

            Log.Debug("ELECTIONS: (V={lastAttemptedView}) SENDING PROPOSAL CANDIDATE: {formatNodeInfo}, ME: {ownInfo}.",
                      _lastAttemptedView, FormatNodeInfo(master), FormatNodeInfo(GetOwnInfo()));

            var proposal = new ElectionMessage.Proposal(_nodeInfo.InstanceId, _nodeInfo.InternalHttp,
                                                        master.InstanceId, master.InternalHttp,
                                                        _lastInstalledView,
                                                        master.EpochNumber, master.EpochPosition, master.EpochId,
                                                        master.LastCommitPosition, master.WriterCheckpoint, master.ChaserCheckpoint);

            Handle(new ElectionMessage.Accept(_nodeInfo.InstanceId, _nodeInfo.InternalHttp,
                                              master.InstanceId, master.InternalHttp, _lastInstalledView));
            SendToAllExceptMe(proposal);
        }
Beispiel #2
0
        private void SendProposal()
        {
            _acceptsReceived.Clear();
            _leaderProposal = null;

            var leader = GetBestLeaderCandidate(_prepareOkReceived, _servers, _resigningLeaderInstanceId, _lastAttemptedView);

            if (leader == null)
            {
                Log.Information("ELECTIONS: (V={lastAttemptedView}) NO LEADER CANDIDATE WHEN TRYING TO SEND PROPOSAL.",
                                _lastAttemptedView);
                return;
            }

            _leaderProposal = leader;

            Log.Information("ELECTIONS: (V={lastAttemptedView}) SENDING PROPOSAL CANDIDATE: {formatNodeInfo}, ME: {ownInfo}.",
                            _lastAttemptedView, FormatNodeInfo(leader), FormatNodeInfo(GetOwnInfo()));

            var proposal = new ElectionMessage.Proposal(_memberInfo.InstanceId, _memberInfo.HttpEndPoint,
                                                        leader.InstanceId, leader.HttpEndPoint,
                                                        _lastInstalledView,
                                                        leader.EpochNumber, leader.EpochPosition, leader.EpochId, leader.EpochLeaderInstanceId,
                                                        leader.LastCommitPosition, leader.WriterCheckpoint, leader.ChaserCheckpoint, leader.NodePriority);

            Handle(new ElectionMessage.Accept(_memberInfo.InstanceId, _memberInfo.HttpEndPoint,
                                              leader.InstanceId, leader.HttpEndPoint, _lastInstalledView));
            SendToAllExceptMe(proposal);
        }
Beispiel #3
0
        public void Handle(ElectionMessage.Proposal message)
        {
            if (_state == ElectionsState.Shutdown)
            {
                return;
            }
            if (message.ServerId == _nodeInfo.InstanceId)
            {
                return;
            }
            if (_state != ElectionsState.NonLeader)
            {
                return;
            }
            if (message.View != _lastInstalledView)
            {
                return;
            }
            if (_servers.All(x => x.InstanceId != message.ServerId))
            {
                return;
            }
            if (_servers.All(x => x.InstanceId != message.MasterId))
            {
                return;
            }

            var candidate = new MasterCandidate(message.MasterId, message.MasterInternalHttp,
                                                message.EpochNumber, message.EpochPosition, message.EpochId,
                                                message.LastCommitPosition, message.WriterCheckpoint, message.ChaserCheckpoint, 0);

            if (!IsLegitimateMaster(message.View, message.ServerInternalHttp, message.ServerId, candidate))
            {
                return;
            }

            Log.Debug(
                "ELECTIONS: (V={lastAttemptedView}) PROPOSAL FROM [{serverInternalHttp},{serverId:B}] M={candidateInfo}. ME={ownInfo}.",
                _lastAttemptedView,
                message.ServerInternalHttp, message.ServerId, FormatNodeInfo(candidate), FormatNodeInfo(GetOwnInfo()));

            if (_masterProposal == null)
            {
                _masterProposal = candidate;
                _acceptsReceived.Clear();
            }

            if (_masterProposal.InstanceId == message.MasterId)
            {
                // NOTE: proposal from other server is also implicit Accept from that server
                Handle(new ElectionMessage.Accept(message.ServerId, message.ServerInternalHttp,
                                                  message.MasterId, message.MasterInternalHttp, message.View));
                var accept = new ElectionMessage.Accept(_nodeInfo.InstanceId, _nodeInfo.InternalHttp,
                                                        message.MasterId, message.MasterInternalHttp, message.View);
                Handle(accept);                 // implicitly sent accept to ourselves
                SendToAllExceptMe(accept);
            }
        }
        public void Send(ElectionMessage.Proposal message, IPEndPoint endPoint)
        {
            Ensure.NotNull(message, "message");
            Ensure.NotNull(endPoint, "endPoint");

            _client.Post(endPoint.ToHttpUrl("/elections/proposal"),
                         Codec.Json.To(new ElectionMessageDto.ProposalDto(message)),
                         Codec.Json.ContentType,
                         r => { /*ignore*/ },
                         e => { /*Log.ErrorException(e, "Error occured while writing request (elections/proposal)")*/ });
        }
 public void SendProposal(ElectionMessage.Proposal proposal, EndPoint destinationEndpoint, DateTime deadline)
 {
     SendProposalAsync(proposal.ServerId, proposal.ServerHttpEndPoint, proposal.LeaderId,
                       proposal.LeaderHttpEndPoint,
                       proposal.View, proposal.EpochNumber, proposal.EpochPosition, proposal.EpochId, proposal.EpochLeaderInstanceId,
                       proposal.LastCommitPosition, proposal.WriterCheckpoint, proposal.ChaserCheckpoint,
                       proposal.NodePriority,
                       deadline)
     .ContinueWith(r => {
         if (r.Exception != null)
         {
             Log.Information(r.Exception, "Proposal Send Failed to {Server}",
                             destinationEndpoint);
         }
     });
 }
Beispiel #6
0
        public void Handle(ElectionMessage.Proposal message)
        {
            if (_state == ElectionsState.Shutdown)
            {
                return;
            }
            if (message.ServerId == _memberInfo.InstanceId)
            {
                return;
            }
            if (_state != ElectionsState.Acceptor)
            {
                return;
            }
            if (message.View != _lastInstalledView)
            {
                return;
            }
            if (_servers.All(x => x.InstanceId != message.ServerId))
            {
                return;
            }
            if (_servers.All(x => x.InstanceId != message.LeaderId))
            {
                return;
            }

            var candidate = new LeaderCandidate(message.LeaderId, message.LeaderHttpEndPoint,
                                                message.EpochNumber, message.EpochPosition, message.EpochId, message.EpochLeaderInstanceId,
                                                message.LastCommitPosition, message.WriterCheckpoint, message.ChaserCheckpoint, message.NodePriority);

            var ownInfo = GetOwnInfo();

            if (!IsLegitimateLeader(message.View, message.ServerHttpEndPoint, message.ServerId,
                                    candidate, _servers, _lastElectedLeader, _memberInfo.InstanceId, ownInfo,
                                    _resigningLeaderInstanceId))
            {
                return;
            }

            Log.Information(
                "ELECTIONS: (V={lastAttemptedView}) PROPOSAL FROM [{serverHttpEndPoint},{serverId:B}] M={candidateInfo}. ME={ownInfo}, NodePriority={priority}",
                _lastAttemptedView,
                message.ServerHttpEndPoint, message.ServerId, FormatNodeInfo(candidate), FormatNodeInfo(GetOwnInfo()),
                message.NodePriority);

            if (_leaderProposal == null)
            {
                _leaderProposal = candidate;
                _acceptsReceived.Clear();
            }

            if (_leaderProposal.InstanceId == message.LeaderId)
            {
                // NOTE: proposal from other server is also implicit Accept from that server
                Handle(new ElectionMessage.Accept(message.ServerId, message.ServerHttpEndPoint,
                                                  message.LeaderId, message.LeaderHttpEndPoint, message.View));
                var accept = new ElectionMessage.Accept(_memberInfo.InstanceId, _memberInfo.HttpEndPoint,
                                                        message.LeaderId, message.LeaderHttpEndPoint, message.View);
                Handle(accept);                 // implicitly sent accept to ourselves
                SendToAllExceptMe(accept);
            }
        }