public StateLogEntrySuggestion GetNextStateLogEntrySuggestion(StateLogEntryRequest req)
        {
            StateLogEntrySuggestion le = new StateLogEntrySuggestion()
            {
                LeaderTerm = this.stateMachine.NodeTerm
            };
            ulong         prevId   = 0;
            ulong         prevTerm = 0;
            StateLogEntry entry    = null;

            if (req.StateLogEntryId == 0)
            {
                //send first record to sync
                var   key = GetKey(0, 0);
                Slice value;
                var   iter = db.NewIterator(ReadOptions.Default);
                iter.Seek(key);
                iter.Next();
                while (iter.Valid())
                {
                    entry = StateLogEntry.BiserDecode(value.ToArray());
                    if (entry.IsCommitted)
                    {
                        break;
                    }
                    iter.Next();
                }
            }
            else
            {
                var   key = GetKey(req.StateLogEntryTerm, req.StateLogEntryId);
                Slice value;
                var   iter = db.NewIterator(ReadOptions.Default);
                iter.Seek(key);
                var lastOne = iter.Value();
                iter.Next();
                while (iter.Valid())
                {
                    entry = StateLogEntry.BiserDecode(value.ToArray());
                    if (entry.Index > req.StateLogEntryId)
                    {
                        var oldEntry = StateLogEntry.BiserDecode(lastOne.ToArray());
                        prevId   = oldEntry.Index;
                        prevTerm = oldEntry.Term;
                    }
                }
            }
            if (entry != null)
            {
                le.StateLogEntry           = entry;
                entry.PreviousStateLogId   = prevId;
                entry.PreviousStateLogTerm = prevTerm;
                le.IsCommitted             = entry.IsCommitted;
                return(le);
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// +
        /// Can be null.
        /// Must be called inside of operation lock.
        /// </summary>
        /// <param name="logEntryId"></param>
        /// <param name="LeaderTerm"></param>
        /// <returns></returns>
        public StateLogEntrySuggestion GetNextStateLogEntrySuggestionFromRequested(StateLogEntryRequest req)
        {
            StateLogEntrySuggestion le = new StateLogEntrySuggestion()
            {
                LeaderTerm = rn.NodeTerm
            };

            return(le);
        }
Example #3
0
        public StateLogEntrySuggestion GetNextStateLogEntrySuggestion(StateLogEntryRequest req)
        {
            StateLogEntrySuggestion le = new StateLogEntrySuggestion()
            {
                LeaderTerm = this.stateMachine.NodeTerm
            };
            ulong         prevId   = 0;
            ulong         prevTerm = 0;
            StateLogEntry entry    = null;

            if (req.StateLogEntryId == 0)
            {
                //send first record to sync
                entry = list.Find(s => s.IsCommitted == true);
            }
            else
            {
                for (int index = 0; index < list.Count; index++)
                {
                    if (list[index].Index > req.StateLogEntryId)
                    {
                        //find next one and set preview value
                        if (index > 0)
                        {
                            prevId   = list[index - 1].Index;
                            prevTerm = list[index - 1].Term;
                            entry    = list[index];
                            break;
                        }
                    }
                }
            }
            if (entry != null)
            {
                le.StateLogEntry           = entry;
                entry.PreviousStateLogId   = prevId;
                entry.PreviousStateLogTerm = prevTerm;
                le.IsCommitted             = entry.IsCommitted;
                return(le);
            }
            else
            {
                return(null);
            }
        }
Example #4
0
        public async Task OnRecieve(IChannelHandlerContext context, string msgstr)
        {
            //
            if (!string.IsNullOrEmpty(msgstr))
            {
                //RaftCommand  msgObj = Newtonsoft.Json.JsonConvert.DeserializeObject<RaftCommand>(msgstr, new JsonSerializerSettings()
                //{
                //    NullValueHandling = NullValueHandling.Ignore,
                //    TypeNameHandling = TypeNameHandling.All,
                //    TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full
                //}
                //);
                int         index  = msgstr.IndexOf(',');
                string      num    = msgstr.Substring(0, index);
                string      base64 = msgstr.Substring(index + 1);
                byte[]      data   = Convert.FromBase64String(base64);
                RaftCommand cmd    = new RaftCommand();
                cmd.Code = Convert.ToInt32(num);
                switch (cmd.Code)
                {
                case RaftCommand.Handshake:
                    cmd.Message = TcpMsgHandshake.BiserDecode(data);
                    break;

                case RaftCommand.HandshakeACK:
                    cmd.Message = TcpMsgHandshake.BiserDecode(data);
                    break;

                case RaftCommand.RaftMessage:
                    cmd.Message = TcpMsgRaft.BiserDecode(data);
                    TcpMsgRaft t = (TcpMsgRaft)cmd.Message;
                    switch (t.RaftSignalType)
                    {
                    case eRaftSignalType.LeaderHearthbeat:
                        t.orginalObject = LeaderHeartbeat.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.CandidateRequest:
                        t.orginalObject = CandidateRequest.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogEntryAccepted:
                        t.orginalObject = StateLogEntryApplied.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogEntryRequest:
                        t.orginalObject = StateLogEntryRequest.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogEntrySuggestion:
                        t.orginalObject = StateLogEntrySuggestion.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogRedirectRequest:
                        t.orginalObject = StateLogEntryRedirectRequest.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.VoteOfCandidate:
                        t.orginalObject = VoteOfCandidate.BiserDecode(t.Data);
                        break;
                    }
                    break;

                case RaftCommand.FreeMessage:
                    cmd.Message = TcpMsg.BiserDecode(data);
                    break;
                }
                this.packetParser(cmd as RaftCommand);
            }
        }