Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public void EmulationSetValue(byte[] data, string entityName = "default")
        {
            RaftNode rn = null;

            if (this.raftNodes.TryGetValue(entityName, out rn))
            {
                rn.AddLogEntry(data);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Test method
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="data"></param>
        public void SendData(int nodeId, string data)
        {
            RaftNode node = null;

            if (!nodes.TryGetValue(nodeId, out node))
            {
                return;
            }

            node.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(data), 0);
        }
Exemplo n.º 4
0
        public AddLogEntryResult AddLogEntry(byte[] data, string entityName = "default")
        {
            RaftNode rn = null;

            if (this.raftNodes.TryGetValue(entityName, out rn))
            {
                return(rn.AddLogEntry(data));
            }

            return(new AddLogEntryResult {
                AddResult = AddLogEntryResult.eAddLogEntryResult.NODE_NOT_FOUND_BY_NAME
            });
        }