Beispiel #1
0
        protected override VoteReply? VoteRequest2(Client client, VoteRequest request)
        {
            if (StepDown(request.Term))
                //TODO: push back in to sequencer
                return null;

            //return new VoteReply()
            //{
            //    From = _server.ID,
            //    Term = _server.PersistedStore.Term,
            //    Granted = false
            //};
            return null;
        }
Beispiel #2
0
        protected override VoteReply? VoteRequest2(Client client, VoteRequest request)
        {
            var _persistedState = _server.PersistedStore;
            if (_persistedState.Term < request.Term)
            {
                _persistedState.Term = request.Term;
                if (_heatbeatTimeout <= _server.Tick)
                    resetHeartbeat();
            }

            var ourLastLogTerm = _persistedState.GetLastTerm();
            var termCheck = _persistedState.Term == request.Term;
            var canVote = _persistedState.VotedFor == null || _persistedState.VotedFor == request.From;
            var ourLogIsBetter = _persistedState.LogIsBetter(request.LogLength, request.LastTerm);
            //var logTermFurther = request.LastTerm > ourLastLogTerm;
            //var logIndexLonger = request.LastTerm == ourLastLogTerm && request.LogLength >= _persistedState.Length;
            var granted = termCheck && canVote && !ourLogIsBetter;

            if (!termCheck)
                Console.WriteLine("{0}: Can not vote for {1} because term {2}, expected {3}", _server.Name, client.ID, request.Term, _persistedState.Term);

            if (!canVote)
                Console.WriteLine("{0}: Can not vote for {1} because I already voted for {2}", _server.Name, client.ID, _persistedState.VotedFor);

            if (ourLogIsBetter)
                Console.WriteLine("{0}: Can not vote for {1} because my log is more update to date", _server.Name, client.ID);

            if (granted)
            {
                Console.WriteLine("{0}: Voted for {1}", _server.Name, client.ID);
                _persistedState.VotedFor = client.ID;
                _leader = null;
                resetHeartbeat();
            }

            return new VoteReply()
            {
                From = _server.ID,
                Term = _server.PersistedStore.Term,
                Granted = granted
            };
        }
Beispiel #3
0
 public VoteReply? VoteRequest2(VoteRequest request)
 {
     var client = _server.GetClient(request.From);
     return VoteRequest2(client, request);
 }
Beispiel #4
0
 //protected abstract bool VoteRequest(Client client, VoteRequest request);
 protected virtual VoteReply? VoteRequest2(Client client, VoteRequest request)
 {
     return null;
 }
Beispiel #5
0
        //protected override bool VoteRequest(Client client, VoteRequest request)
        //{
        //    if (StepDown(request.Term))
        //        return false;

        //    client.SendVoteReply(false);
        //    return true;
        //}

        protected override VoteReply? VoteRequest2(Client client, VoteRequest request)
        {
            if (StepDown(request.Term))
                //TODO: push back in to sequencer
                return null;

            return null;
        }
Beispiel #6
0
        //private static WebHttpBinding CreateDefaultBinding()
        //{
        //    var binding = new WebHttpBinding();
        //    binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
        //    binding.TransferMode = TransferMode.Streamed;
        //    binding.MaxReceivedMessageSize = 1024 * 1024 * 25;
        //    binding.MaxBufferSize = MESSAGE_BUFFER_LENGTH;
        //    return binding;
        //}
        public void VoteRequest()
        {
            LogIndex lastIndex;
            var lastLogIndex = _server.PersistedStore.GetLastIndex(out lastIndex);

            var message = new VoteRequest()
            {
                From = _server.ID,
                Term = _server.PersistedStore.Term,
                LastTerm = lastIndex.Term,
                LogLength = _server.PersistedStore.Length
            };

            _currentMessage = _server.Transport.SendMessageAsync(this, message);
            _lastMessage = message;
            _rpcDue = _server.Tick + _server.PersistedStore.RPC_TIMEOUT;
        }
Beispiel #7
0
 public void SendRequest(Peer peer, VoteRequest request)
 {
     SendMessage(peer, request);
 }
Beispiel #8
0
        //protected override bool VoteRequest(Client client, VoteRequest request)
        //{
        //    LastMessage = request;
        //    LastClient = client;
        //    return true;
        //}

        protected override VoteReply? VoteRequest2(Client client, VoteRequest request)
        {
            LastMessage = request;
            LastClient = client;
            return null;
        }
Beispiel #9
0
        //protected void handleRequestStatus(IModel model, StatusRequest request)
        //{
        //    if (_persistedState.Term < request.Term)
        //        stepDown(model, request.Term);

        //    if (_state == ServerState.Follower && _persistedState.Term == request.Term)
        //    {
        //        var peer = _peers.First(x => x.ID == request.From);
        //        _electionAlarm = makeElectionAlarm(model);
        //        model.SendReply(peer, new StatusReply()
        //        {
        //            From = _id, 
        //            CommitIndex = _commitIndex,
        //            Term = _persistedState.Term
        //        });

        //        Console.WriteLine("{0}: Sent status {1} to {2}", _id, _commitIndex, peer.ID);
        //    }
        //}

        //protected void handleStatusReply(IModel model, StatusReply reply)
        //{
        //    if (_persistedState.Term < reply.Term)
        //        stepDown(model, reply.Term);

        //    if (_state == ServerState.Leader && _persistedState.Term == reply.Term)
        //    {
        //        var peer = _peers.First(x => x.ID == reply.From);
        //        peer.RpcDue = int.MaxValue;
        //        peer.MatchIndex = reply.CommitIndex;

        //        //Console.WriteLine("{0}: Peer {1} voted {2}", _id, peer.ID, peer.VotedGranted);
        //    }
        //}

        protected void handleRequestVote(IConsensus model, VoteRequest request)
        {
            if (_persistedState.Term < request.Term)
                stepDown(model, request.Term);

            //a leader would never request a peer vote
            System.Diagnostics.Debug.Assert(_state != ServerState.Adding && _state != ServerState.Removing);

            var peer = _peers.First(x => x.ID == request.From);
            var ourLastLogTerm = _persistedState.GetLastTerm();
            var termCheck = _persistedState.Term == request.Term;
            var canVote = _persistedState.VotedFor == null || _persistedState.VotedFor == request.From;
            var logTermFurther = request.LastTerm > ourLastLogTerm;
            var logIndexLonger = request.LastTerm == ourLastLogTerm && request.LogLength >= _persistedState.Length;
            var granted = termCheck && canVote && (logTermFurther || logIndexLonger);

            if (!termCheck)
                Console.WriteLine("{0}: Can not vote for {1} because term {2}, expected {3}", _id, peer.ID, request.Term, _persistedState.Term);

            if (!canVote)
                Console.WriteLine("{0}: Can not vote for {1} because I already voted for {2}", _id, peer.ID, _persistedState.VotedFor);

            if (!(logTermFurther || logIndexLonger))
                Console.WriteLine("{0}: Can not vote for {1} because my log is more update to date", _id, peer.ID);

            if (granted)
            {
                Console.WriteLine("{0}: Voted for {1}", _id, peer.ID);
                _persistedState.VotedFor = peer.ID;
                updateElectionAlarm(model);
            }

            model.SendReply(peer, new VoteReply() { From = _id, Term = _persistedState.Term, Granted = granted });

        }