示例#1
0
 private async Task CommitToServerLog(string log)
 {
     if (log == null)
     {
         return;
     }
     Debug.WriteLine("{0}: Commiting to server log {1}", NodeInfo(), log);
     ServerLog.Add(Term, log);
     CommitIndex = ServerLog.LastIndex;
     await SendHeartBeat();
 }
        public static void Add(ServerLogItem logItem)
        {
            if (IgnoreSenderEvents.Exists((type) => type.FullName == logItem.SenderType))
            {
                return;
            }
            var eventArgs = new ServerLogArgs(logItem);

            LogPreAdded?.Invoke(eventArgs);
            if (eventArgs.Discard)
            {
                return;
            }
            lock (lockObject)
                ServerLog.Add(logItem);
            LogAdded?.Invoke(logItem);
        }
示例#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));
        }
示例#4
0
 protected static void AmendLog(string msg)
 {
     ServerLog.Add(msg);
     LastLog = msg;
 }