Ejemplo n.º 1
0
 public void Send(JsonOperationContext context, RequestVote rv)
 {
     Send(context, new DynamicJsonValue
     {
         ["Type"] = nameof(RequestVote),
         [nameof(RequestVote.Term)]             = rv.Term,
         [nameof(RequestVote.Source)]           = rv.Source,
         [nameof(RequestVote.LastLogTerm)]      = rv.LastLogTerm,
         [nameof(RequestVote.LastLogIndex)]     = rv.LastLogIndex,
         [nameof(RequestVote.IsTrialElection)]  = rv.IsTrialElection,
         [nameof(RequestVote.IsForcedElection)] = rv.IsForcedElection
     });
 }
Ejemplo n.º 2
0
 public void Send(JsonOperationContext context, RequestVote rv)
 {
     if (_log.IsInfoEnabled)
     {
         _log.Info(
             $"{rv.Source} requests vote in {rv.Term}, trial: {rv.IsTrialElection}, forced: {rv.IsForcedElection}, result: {rv.ElectionResult} with: ({rv.LastLogIndex} / {rv.LastLogTerm}).");
     }
     Send(context, new DynamicJsonValue
     {
         ["Type"] = nameof(RequestVote),
         [nameof(RequestVote.Term)]             = rv.Term,
         [nameof(RequestVote.Source)]           = rv.Source,
         [nameof(RequestVote.LastLogTerm)]      = rv.LastLogTerm,
         [nameof(RequestVote.LastLogIndex)]     = rv.LastLogIndex,
         [nameof(RequestVote.IsTrialElection)]  = rv.IsTrialElection,
         [nameof(RequestVote.IsForcedElection)] = rv.IsForcedElection,
         [nameof(RequestVote.ElectionResult)]   = rv.ElectionResult,
         [nameof(RequestVote.SendingThread)]    = Thread.CurrentThread.ManagedThreadId
     });
 }
Ejemplo n.º 3
0
        private HandleVoteResult ShouldGrantVote(TransactionOperationContext context, long lastIndex, RequestVote rv, long lastTerm)
        {
            var result = new HandleVoteResult();
            var lastEntryUnderWriteLock = _engine.GetLastEntryIndex(context);

            if (lastEntryUnderWriteLock != lastIndex)
            {
                result.DeclineVote   = true;
                result.DeclineReason = "Log was changed";
                return(result);
            }

            var(whoGotMyVoteIn, votedTerm) = _engine.GetWhoGotMyVoteIn(context, rv.Term);
            result.VotedTerm = votedTerm;

            if (whoGotMyVoteIn != null && whoGotMyVoteIn != rv.Source)
            {
                result.DeclineVote   = true;
                result.DeclineReason = $"Already voted in {rv.LastLogTerm}, for {whoGotMyVoteIn}";
                return(result);
            }
            if (votedTerm >= rv.Term)
            {
                result.DeclineVote   = true;
                result.DeclineReason = $"Already voted in {rv.LastLogTerm}, for another node in higher term: {votedTerm}";
                return(result);
            }
            if (lastTerm == rv.LastLogTerm && lastIndex > rv.LastLogIndex)
            {
                result.DeclineVote   = true;
                result.DeclineReason = $"Vote declined because my log {lastIndex} is more up to date than yours {rv.LastLogIndex}";
                return(result);
            }

            return(result);
        }