Exemple #1
0
 public override async Task <AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
 {
     if (SleepOnAppendEntries)
     {
         await Task.Delay(Timeout.Infinite);
     }
     return(await base.AppendEntries(request));
 }
Exemple #2
0
 public override async Task<AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
 {
     if (SleepOnAppendEntries)
     {
         await Task.Delay(Timeout.Infinite);
     }
     return await base.AppendEntries(request);
 }
Exemple #3
0
            public async Task<AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
            {
                Debug.WriteLine("{0}: GOT APPEND ENTRIES CALL {1}: leader commit {2} : entries: {3}", node.NodeInfo(), request.Id, request.CommitIndex, String.Concat(request.Entries.Select(e => e.Log + " ")));
                if (request.Term <= node.Term)
                {
                    return new AppendEntriesResult(node.Term, false);
                }

                node.CurrentLeader = request.Id;
                node.requestHandler = new Follower(node);
                node.Term = request.Term;
                node.lastHeartBeat = node.settings.Scheduler.Now;
                return await node.requestHandler.AppendEntries(request);
            }
Exemple #4
0
            public async Task <AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
            {
                Debug.WriteLine("{0}: GOT APPEND ENTRIES CALL {1}: leader commit {2} : entries: {3}", node.NodeInfo(), request.Id, request.CommitIndex, String.Concat(request.Entries.Select(e => e.Log + " ")));
                if (request.Term <= node.Term)
                {
                    return(new AppendEntriesResult(node.Term, false));
                }

                node.CurrentLeader  = request.Id;
                node.requestHandler = new Follower(node);
                node.Term           = request.Term;
                node.lastHeartBeat  = node.settings.Scheduler.Now;
                return(await node.requestHandler.AppendEntries(request));
            }
Exemple #5
0
            public async Task <AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
            {
                Debug.WriteLine("{0}: GOT APPEND ENTRIES CALL {1}: leader commit {2} : entries: {3}", node.NodeInfo(), request.Id, request.CommitIndex, String.Concat(request.Entries.Select(e => e.Log + " ")));

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

                node.CurrentLeader = request.Id;
                node.Term          = request.Term;
                node.lastHeartBeat = node.settings.Scheduler.Now;

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

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

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

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

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

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

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

                return(new AppendEntriesResult(node.Term, true));
            }
Exemple #6
0
 public async Task <AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
 {
     return(await appendEntries(request));
 }
Exemple #7
0
            public async Task<AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
            {
                Debug.WriteLine("{0}: GOT APPEND ENTRIES CALL {1}: leader commit {2} : entries: {3}", node.NodeInfo(), request.Id, request.CommitIndex, String.Concat(request.Entries.Select(e => e.Log + " ")));

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

                node.CurrentLeader = request.Id;
                node.Term = request.Term;
                node.lastHeartBeat = node.settings.Scheduler.Now;

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

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

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

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

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

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

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

                return new AppendEntriesResult(node.Term, true);
            }
Exemple #8
0
 public virtual async Task <AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
 {
     return(await requestHandler.AppendEntries(request));
 }
Exemple #9
0
 public async Task<AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
 {
     return await appendEntries(request);
 }
Exemple #10
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));
        }
Exemple #11
0
 public virtual async Task<AppendEntriesResult> AppendEntries(AppendEntriesRequest request)
 {
     return await requestHandler.AppendEntries(request);
 }