public ReturnValueBase ExecuteBusinessLogic(StateLogEntry entry, RaftStateMachine node)
        {
            node.NodeStateLog.LastBusinessLogicCommittedIndex = entry.Index;
            if (GlobalConfig.Verbose)
            {
                Console.WriteLine("receive entry:" + entry.Index + " on node:" + node.NodeName);
            }
            string text = System.Text.Encoding.UTF8.GetString(entry.Data);
            var    cmd  = Newtonsoft.Json.JsonConvert.DeserializeObject <LockOper>(text);

            if (cmd != null)
            {
                bool succ = this.table.GetQueue(cmd.Key).LockNoWait(cmd.Session, cmd.Type);
                return(new ReturnValueBase
                {
                    Success = succ,
                    Message = "operation finished"
                });
            }
            return(new ReturnValueBase()
            {
                Success = false,
                Message = "wrong command"
            });
        }
Example #2
0
        public RaftServiceNode(NodeSettings nodeSettings, string dbreezePath, IBusinessHandler handler, int port = 4250, string nodeName = "default", IWarningLog log = null)
        {
            if (nodeSettings == null)
            {
                nodeSettings = new NodeSettings();
            }
            this.NodeSettings = nodeSettings;
            this.NodeName     = nodeName;
            this.log          = log;
            this.port         = port;

            peerNetwork = new TcpPeerNetwork(this);
            //bool firstNode = true;
            if (this.NodeSettings.RaftEntitiesSettings == null)
            {
                this.NodeSettings.RaftEntitiesSettings = new RaftEntitySettings();
            }
            var re_settings = this.NodeSettings.RaftEntitiesSettings;

            if (String.IsNullOrEmpty(re_settings.EntityName))
            {
                throw new Exception("Raft.Net: entities must have unique names. Change RaftNodeSettings.EntityName.");
            }

            var rn = new RaftStateMachine(re_settings ?? new RaftEntitySettings(), dbreezePath, this.peerNetwork, this.log, handler);

            rn.SetNodesQuantityInTheCluster((uint)this.NodeSettings.TcpClusterEndPoints.Count);
            rn.NodeAddress.NodeAddressId = port; //for debug/emulation purposes

            rn.NodeAddress.NodeUId = Guid.NewGuid().ToByteArray().Substring(8, 8).To_Int64_BigEndian();
            rn.NodeName            = this.NodeName;
            this.raftNode          = rn;
            rn.NodeStart();
        }
        public LevelDbLogStore(RaftStateMachine rn, string path)
        {
            this.stateMachine = rn;
            string logdbFile = path + "_logdb.db";

            db = DB.Open(logdbFile, new Options()
            {
                CreateIfMissing = true
            });
        }
Example #4
0
        public RocksDBLogStore(RaftStateMachine rn, string path)
        {
            this.stateMachine = rn;
            string logdbFile = path + "_logdb.db";
            var    options   = new DbOptions()
                               .SetCreateIfMissing(true);

            db = RocksDb.Open(options, logdbFile);

            this.ReloadFromStorage();
        }
Example #5
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);
        }
Example #6
0
 public void Dispose()
 {
     if (System.Threading.Interlocked.CompareExchange(ref disposed, 1, 0) != 0)
     {
         return;
     }
     try
     {
         this.raftNode = null;
     }
     catch (Exception ex)
     {
     }
     try
     {
         if (peerNetwork != null)
         {
             peerNetwork.Dispose();
         }
     }
     catch (Exception ex)
     {
     }
 }
Example #7
0
 public StateMachineTimerLoop(TimeMaster TM, RaftEntitySettings settings, RaftStateMachine stateMachine)
 {
     this.TM             = TM;
     this.entitySettings = settings;
     this.stateMachine   = stateMachine;
 }
 public bool SetNode(RaftStateMachine raftNode)
 {
     this.raftNode = raftNode;
     return(true);
 }
 public bool SetNode(RaftStateMachine raftNode)
 {
     return(true);
 }
Example #10
0
 public static IStateLog GetLog(RaftStateMachine node, string workPath)
 {
     return(new RocksDBLogStore(node, workPath));
 }
Example #11
0
 public MemStateLog(RaftStateMachine rn, string path)
 {
     this.rn          = rn;
     tblStateLogEntry = "mem_" + tblStateLogEntry;
     leaderState      = new LeaderSyncState(rn, path);
 }
Example #12
0
 public SimpleMemLogStore(RaftStateMachine rn, string path)
 {
     this.stateMachine = rn;
 }
Example #13
0
 public StateMachineLogHandler(RaftStateMachine stateMachine, IStateLog log, IBusinessHandler handler)
 {
     this.stateMachine         = stateMachine;
     this.log                  = log;
     this.businessLogicHandler = handler;
 }
 public LeaderSyncState(RaftStateMachine rn, string path)
 {
     this.rn = rn;
 }
Example #15
0
        public bool NodeIsInLatestState()
        {
            RaftStateMachine rn = this.raftNode;

            return(rn.NodeIsInLatestState);
        }
Example #16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entityName"></param>
        public void Debug_PrintOutInMemory(string entityName = "default")
        {
            RaftStateMachine rn = this.raftNode;

            rn.Debug_PrintOutInMemory();
        }