Exemple #1
0
        public async Task <bool> AddLogEntryAsync(byte[] data, string entityName = "default", int timeoutMs = 20000)
        {
            if (System.Threading.Interlocked.Read(ref disposed) == 1)
            {
                return(false);
            }

            RaftNode rn = null;

            if (this.raftNodes.TryGetValue(entityName, out rn))
            {
                //Generating externalId
                var msgId    = AsyncResponseHandler.GetMessageId();
                var msgIdStr = msgId.ToBytesString();
                var resp     = new ResponseCrate();
                resp.TimeoutsMs = timeoutMs; //enable for amre
                                             //resp.TimeoutsMs = Int32.MaxValue; //using timeout of the wait handle (not the timer), enable for mre

                //resp.Init_MRE();
                resp.Init_AMRE();

                AsyncResponseHandler.df[msgIdStr] = resp;

                var aler = rn.AddLogEntry(data, msgId);

                switch (aler.AddResult)
                {
                case AddLogEntryResult.eAddLogEntryResult.LOG_ENTRY_IS_CACHED:
                case AddLogEntryResult.eAddLogEntryResult.NODE_NOT_A_LEADER:

                    //async waiting
                    await resp.amre.WaitAsync();        //enable for amre

                    resp.Dispose_MRE();

                    if (AsyncResponseHandler.df.TryRemove(msgIdStr, out resp))
                    {
                        if (resp.IsRespOk)
                        {
                            return(true);
                        }
                    }

                    break;

                default:
                    //case AddLogEntryResult.eAddLogEntryResult.ERROR_OCCURED:
                    //case AddLogEntryResult.eAddLogEntryResult.NO_LEADER_YET:

                    resp.Dispose_MRE();
                    AsyncResponseHandler.df.TryRemove(msgIdStr, out resp);

                    return(false);
                }
            }

            //return new AddLogEntryResult { AddResult = AddLogEntryResult.eAddLogEntryResult.NODE_NOT_FOUND_BY_NAME };
            //return new Tuple<bool, byte[]>(false, null);
            return(false);
        }
Exemple #2
0
        /// <summary>
        /// called by external service
        /// </summary>
        /// <param name="data"></param>
        /// <param name="entityName"></param>
        /// <param name="timeoutMs"></param>
        /// <returns></returns>

        public async Task <object> AddLogEntryRequestAsync(byte[] data, string entityName = "default", int timeoutMs = 20000)
        {
            if (System.Threading.Interlocked.Read(ref disposed) == 1)
            {
                return(false);
            }

            RaftStateMachine rn = this.raftNode;;

            {
                //Generating externalId
                var msgId    = AsyncResponseHandler.GetMessageId();
                var msgIdStr = msgId.ToBytesString();
                var resp     = new ResponseCrate();
                resp.TimeoutsMs = timeoutMs; //enable for amre
                resp.Init_AMRE();
                AsyncResponseHandler.df[msgIdStr] = resp;
                var aler = rn.logHandler.ProcessAddLogRequest(data, msgId);
                switch (aler.AddResult)
                {
                case AddLogEntryResult.eAddLogEntryResult.LOG_ENTRY_IS_CACHED:
                case AddLogEntryResult.eAddLogEntryResult.NODE_NOT_A_LEADER:
                    //async waiting
                    resp.amre.Wait();        //enable for amre
                    resp.Dispose_MRE();
                    if (AsyncResponseHandler.df.TryRemove(msgIdStr, out resp))
                    {
                        if (resp.IsRespOk)
                        {
                            return(resp.ReturnValue);
                        }
                    }
                    break;

                default:
                    resp.Dispose_MRE();
                    AsyncResponseHandler.df.TryRemove(msgIdStr, out resp);
                    return(resp.ReturnValue);
                }
            }
            return(null);
        }