Example #1
0
        public void Handle(ElectionMessage.Accept message)
        {
            if (_state == ElectionsState.Shutdown)
            {
                return;
            }
            if (message.View != _lastInstalledView)
            {
                return;
            }
            if (_masterProposal == null)
            {
                return;
            }
            if (_masterProposal.InstanceId != message.MasterId)
            {
                return;
            }

            Log.Debug("ELECTIONS: (V={0}) ACCEPT FROM [{1},{2:B}] M=[{3},{4:B}]).", message.View,
                      message.ServerInternalHttp, message.ServerId, message.MasterInternalHttp, message.MasterId);

            if (_acceptsReceived.Add(message.ServerId) && _acceptsReceived.Count == _clusterSize / 2 + 1)
            {
                var master = _servers.FirstOrDefault(x => x.InstanceId == _masterProposal.InstanceId);
                if (master != null)
                {
                    _master = _masterProposal.InstanceId;
                    Log.Info("ELECTIONS: (V={0}) DONE. ELECTED MASTER = {1}. ME={2}.", message.View,
                             FormatNodeInfo(_masterProposal), FormatNodeInfo(GetOwnInfo()));
                    _lastElectedMaster = _master;
                    _publisher.Publish(new ElectionMessage.ElectionsDone(message.View, master));
                }
            }
        }
Example #2
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.Accept message, IPEndPoint endPoint)
        {
            Ensure.NotNull(message, "message");
            Ensure.NotNull(endPoint, "endPoint");

            _client.Post(endPoint.ToHttpUrl("/elections/accept"),
                         Codec.Json.To(new ElectionMessageDto.AcceptDto(message)),
                         Codec.Json.ContentType,
                         r => { /*ignore*/ },
                         e => { /*Log.ErrorException(e, "Error occured while writing request (elections/accept)")*/ });
        }
 public void SendAccept(ElectionMessage.Accept accept, EndPoint destinationEndpoint, DateTime deadline)
 {
     SendAcceptAsync(accept.ServerId, accept.ServerHttpEndPoint, accept.LeaderId, accept.LeaderHttpEndPoint,
                     accept.View, deadline)
     .ContinueWith(r => {
         if (r.Exception != null)
         {
             Log.Information(r.Exception, "Accept Send Failed to {Server}", destinationEndpoint);
         }
     });
 }
Example #5
0
        public void Handle(ElectionMessage.Accept message)
        {
            if (_state == ElectionsState.Shutdown)
            {
                return;
            }
            if (message.View != _lastInstalledView)
            {
                return;
            }
            if (_leaderProposal == null)
            {
                return;
            }
            if (_leaderProposal.InstanceId != message.LeaderId)
            {
                return;
            }

            Log.Information(
                "ELECTIONS: (V={view}) ACCEPT FROM [{serverHttpEndPoint},{serverId:B}] M=[{leaderHttpEndPoint},{leaderId:B}]).",
                message.View,
                message.ServerHttpEndPoint, message.ServerId, message.LeaderHttpEndPoint, message.LeaderId);

            if (_acceptsReceived.Add(message.ServerId) && _acceptsReceived.Count == _clusterSize / 2 + 1)
            {
                var leader = _servers.FirstOrDefault(x => x.InstanceId == _leaderProposal.InstanceId);
                if (leader != null)
                {
                    _leader = _leaderProposal.InstanceId;
                    Log.Information("ELECTIONS: (V={view}) DONE. ELECTED LEADER = {leaderInfo}. ME={ownInfo}.", message.View,
                                    FormatNodeInfo(_leaderProposal), FormatNodeInfo(GetOwnInfo()));
                    _lastElectedLeader         = _leader;
                    _resigningLeaderInstanceId = null;
                    _publisher.Publish(new ElectionMessage.ElectionsDone(message.View, leader));
                }
            }
        }
Example #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);
            }
        }