Example #1
0
			public override void Reply(RequestVoteResponse resp)
			{
				Reply(resp.VoteGranted, resp);	
			}
Example #2
0
 public abstract void Reply(RequestVoteResponse resp);
		public override void Handle(RequestVoteResponse resp)
		{
            if (FromOurTopology(resp) == false)
            {
                _log.Info("Got a request vote response message outside my cluster topology (id: {0}), ignoring", resp.ClusterTopologyId);
                return;
            }
			
			long currentTerm = _wonTrialElection ? Engine.PersistentState.CurrentTerm : Engine.PersistentState.CurrentTerm + 1;
			if (resp.VoteTerm != currentTerm)
			{
				_log.Info("Got a vote for {2}election term {0} but current term is {1}, ignoring", resp.VoteTerm, currentTerm,
					_wonTrialElection ? " " : "trial ");
				return;
			}
			if (resp.CurrentTerm > currentTerm)
			{
				_log.Info("CandidateStateBehavior -> UpdateCurrentTerm called, there is a new leader, moving to follower state");
				Engine.UpdateCurrentTerm(resp.CurrentTerm, null);
				return;
			}

			if (resp.VoteGranted == false)
			{
				if (resp.TermIncreaseMightGetMyVote)
					_termIncreaseMightGetMyVote = true;
				_log.Info("Vote rejected from {0} trial: {1}", resp.From, resp.TrialOnly);
				return;
			}

			if (Engine.CurrentTopology.IsVoter(resp.From) == false) //precaution
			{
				_log.Info("Vote accepted from {0}, which isn't a voting node in our cluster", resp.From);
				return;
			}

			if (resp.TrialOnly && _wonTrialElection) // note that we can't get a vote for real election when we get a trail, because the terms would be different
			{
				_log.Info("Got a vote for trial only from {0} but we already won the trial election for this round, ignoring", resp.From);
				return;
			}

			_votesForMyLeadership.Add(resp.From);
			_log.Info("Adding to my votes: {0} (current votes: {1})", resp.From, string.Join(", ", _votesForMyLeadership));

			if (Engine.CurrentTopology.HasQuorum(_votesForMyLeadership) == false)
			{
				_log.Info("Not enough votes for leadership, votes = {0}", _votesForMyLeadership.Any() ? string.Join(", ", _votesForMyLeadership) : "empty");
				return;
			}

			if (_wonTrialElection == false)
			{
				_wonTrialElection = true;
				_log.Info("Won trial election with {0} votes from {1}, now running for real", _votesForMyLeadership.Count, string.Join(", ", _votesForMyLeadership));
				StartElection();
				return;
			}

			Engine.SetState(RaftEngineState.Leader);
			_log.Info("Selected as leader, term = {0}", resp.CurrentTerm);
		}
		public virtual void Handle(RequestVoteResponse resp)
		{
			//do nothing, irrelevant here
		}