示例#1
0
        private Task <Log> SendAppendEntriesInParallel(string log, bool heartBeat)
        {
            if (!heartBeat)
            {
                LocalLog.Add(Term, log);
            }

            var tcs          = new TaskCompletionSource <Log>();
            var successCount = 0;

            Peers.ForEach(peer => Task.Run(async() =>
            {
                AppendEntriesResult result;
                Debug.WriteLine("{0}: sending append entries to {1}", NodeInfo(), peer.Id);
                result = await AppendAllTheEntries(heartBeat, peer);
                Debug.WriteLine("{0}: Got a result {1}", NodeInfo(), peer.Id);
                return(result);
            })
                          .ContinueWith(async t =>
            {
                Debug.WriteLine("{0}: ContinueingWith {1}", NodeInfo(), t.Result.Success);
                if (t.Result.Success)
                {
                    Interlocked.Increment(ref successCount);
                }

                if (successCount >= PeerAgreementsNeededForConcensus())
                {
                    await CommitToServerLog(log);
                    tcs.SetResult(ServerLog);
                }
            }));

            return(tcs.Task);
        }
示例#2
0
        private async Task <LogResult> SendAppendEntries(string log, bool heartBeat)
        {
            if (!heartBeat)
            {
                LocalLog.Add(Term, log);
            }
            var results = new List <AppendEntriesResult>();

            Peers.ForEach(async peer =>
            {
                Debug.WriteLine("{0}: sending append entries to {1}", NodeInfo(), peer.Id);
                var result = (await AppendAllTheEntries(heartBeat, peer)
                              .ToObservable()
                              .Timeout(TimeSpan.FromSeconds(5))
                              .Catch <AppendEntriesResult, TimeoutException>(e => Observable.Return(new AppendEntriesResult(0, false))));

                if (result.CurrentTerm > Term)
                {
                    StepDown(result.CurrentTerm, peer.Id);
                }

                results.Add(result);
                Debug.WriteLine("{0}: Got a result {1}", NodeInfo(), peer.Id);
            });

            if (results.Count(r => r.Success) >= PeerAgreementsNeededForConcensus())
            {
                await CommitToServerLog(log);
            }

            return(new LogCommited
            {
                ServerLog = ServerLog
            });
        }
示例#3
0
        public virtual async Task <AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
        {
            Debug.WriteLine("{0}: GOT APPEND ENTRIES CALL {1}: leader commit {2} : entries: {3}", NodeInfo(), request.Id, request.CommitIndex, String.Concat(request.Entries.Select(e => e.Log + " ")));
            HeartBeat(request.Id, request.Term);

            if (request.Entries == null || !request.Entries.Any()) //heartbeat
            {
                if (request.CommitIndex > ServerLog.LastIndex)
                {
                    Debug.WriteLine("{0}: HeartBeat Adding To Server Log up to index {1} leaderCommitIndex {2}", NodeInfo(), request.PreviousLogIndex, request.CommitIndex);
                    LocalLog.Entries.Where(e => e.Index > ServerLog.LastIndex && e.Index <= request.CommitIndex).ForEach(e => ServerLog.Add(e.Term, e.Log));
                }

                return(new AppendEntriesResult(Term, true));
            }

            if (Term < request.Term) //servers term is behind. we need to ensure we are in follower state and reset who we have voted for
            {
                StepDown(request.Term, request.Id);
                return(new AppendEntriesResult(Term, false));
            }

            if (request.Term < Term)
            {
                return(new AppendEntriesResult(Term, false));
            }

            if (request.PreviousLogIndex == 0 || (request.PreviousLogIndex <= ServerLog.LastIndex &&
                                                  ServerLog.LastTerm == request.PreviousLogTerm))
            {
                Debug.WriteLine("{0}: huh");
            }

            //reply false if log doesnt contain an entry at prevLogIndex whose term matches prevlogTerm
            if (!LocalLog.Entries.Any(e => e.Index == request.PreviousLogIndex && e.Term == request.PreviousLogTerm)) //not to sure about this line yet
            {
                return(new AppendEntriesResult(Term, false));
            }

            if (LocalLog.Entries.Any(e => e.Index == request.PreviousLogIndex && e.Term != request.PreviousLogTerm))
            {
                //delete the entry and all that follow it
                LocalLog.RemoveEntryAndThoseAfterIt(request.PreviousLogIndex);
            }

            if (request.Entries.Max(e => e.Index) > LocalLog.LastIndex)
            {
                Debug.WriteLine("{0}: Adding To Local Log up to index {1} leaderCommitIndex {2}", NodeInfo(), request.Entries.Max(e => e.Index), request.CommitIndex);
                request.Entries.Where(e => e.Index > LocalLog.LastIndex).ForEach(e => LocalLog.Add(e.Term, e.Log));
            }
            if (request.CommitIndex > ServerLog.LastIndex)
            {
                Debug.WriteLine("{0}: Adding To Server Log up to index {1} leaderCommitIndex {2}", NodeInfo(), request.PreviousLogIndex, request.CommitIndex);
                request.Entries.Where(e => e.Index > ServerLog.LastIndex && e.Index <= request.CommitIndex).ForEach(e => ServerLog.Add(e.Term, e.Log));
            }

            if (request.CommitIndex > CommitIndex)
            {
                CommitIndex = Math.Min(request.CommitIndex, LocalLog.LastIndex);
            }

            return(new AppendEntriesResult(Term, true));
        }