Example #1
0
        static void Scenario1(string[] args)
        {
            Console.WriteLine("Scenario 1 is running");
            if (args.Length < 1)
            {
                Console.WriteLine("RaftCluster TCP listening port is not specified");
                return;
            }

            if (args.Length < 2 || !(new System.IO.FileInfo(args[2])).Exists)
            {
                Console.WriteLine("Path to configuration file is not supplied or file is not found");
                return;
            }

            string dbreezePath = "";

            if (args.Length >= 3)
            {
                dbreezePath = args[3];
            }

            Console.WriteLine($"Listening port: {args[1]}; Path to config: {args[2]}; Path to DBreeze folder: {args[3]}");
            var configLines = System.IO.File.ReadAllLines(args[2]);



            TcpRaftNode rn = null;

            //rn = TcpRaftNode.GetFromConfig(1, System.IO.File.ReadAllText(args[2]),
            //    dbreezePath, Convert.ToInt32(args[1]), log,
            //    (entName, index, data) => { Console.WriteLine($"wow committed {entName}/{index}"); return true; });

            rn = TcpRaftNode.GetFromConfig(System.IO.File.ReadAllText(args[2]),
                                           dbreezePath, Convert.ToInt32(args[1]), log,
                                           (entName, index, data) => { Console.WriteLine($"wow committed {entName}/{index}"); return(true); });

            rn.Start();

            AddLogEntryResult addRes = null;

            while (true)
            {
                var cmd = Console.ReadLine();
                switch (cmd)
                {
                case "set1":
                    addRes = rn.AddLogEntry(new byte[] { 23 });
                    Console.WriteLine($"Adding: {addRes.AddResult.ToString()}");
                    break;

                case "set1a":
                    addRes = rn.AddLogEntry(new byte[] { 27 }, entityName: "inMemory1");
                    Console.WriteLine($"Adding: {addRes.AddResult.ToString()}");
                    break;

                case "set10":
                    for (int k = 0; k < 10; k++)
                    {
                        addRes = rn.AddLogEntry(new byte[] { 23 });
                        Console.WriteLine($"Adding: {addRes.AddResult.ToString()}");
                    }
                    break;

                case "set10a":
                    for (int k = 0; k < 10; k++)
                    {
                        addRes = rn.AddLogEntry(new byte[] { 23 }, entityName: "inMemory1");
                        Console.WriteLine($"Adding: {addRes.AddResult.ToString()}");
                    }
                    break;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Leader and followers via redirect. (later callback info for followers is needed)
        /// </summary>
        /// <param name="data"></param>
        /// <param name="logEntryExternalId"></param>
        /// <returns></returns>
        public AddLogEntryResult ProcessAddLogRequest(byte[] iData, byte[] externalId = null)
        {
            AddLogEntryResult res = new AddLogEntryResult();

            try
            {
                lock (this.stateMachine.lock_Operations)
                {
                    if (iData != null)
                    {
                        this.rediretQueue.Enqueue(new Tuple <byte[], byte[]>(iData, externalId));
                    }

                    if (this.stateMachine.States.NodeState == eNodeState.Leader)
                    {
                        this.stateMachine.timerLoop.StopNoLeaderAddCommandTimeLoop();

                        while (this.rediretQueue.Count > 0)
                        {
                            var nlc = this.rediretQueue.Dequeue();
                            this.AddStateLogEntryForDistribution(nlc.Item1, nlc.Item2);
                            EnqueueAndDistrbuteLog();
                        }
                        res.LeaderAddress = this.stateMachine.NodeAddress;
                        res.AddResult     = AddLogEntryResult.eAddLogEntryResult.LOG_ENTRY_IS_CACHED;
                    }
                    else
                    {
                        if (this.stateMachine.LeaderNodeAddress == null)
                        {
                            res.AddResult = AddLogEntryResult.eAddLogEntryResult.NO_LEADER_YET;
                            this.stateMachine.timerLoop.EnterNoLeaderAddCommandTimeLoop();
                        }
                        else
                        {
                            this.stateMachine.timerLoop.StopNoLeaderAddCommandTimeLoop();
                            res.AddResult     = AddLogEntryResult.eAddLogEntryResult.NODE_NOT_A_LEADER;
                            res.LeaderAddress = this.stateMachine.LeaderNodeAddress;

                            //Redirecting only in case if there is a leader
                            while (this.rediretQueue.Count > 0)
                            {
                                var nlc = this.rediretQueue.Dequeue();
                                this.stateMachine.network.SendTo(this.stateMachine.LeaderNodeAddress, eRaftSignalType.StateLogRedirectRequest,
                                                                 (
                                                                     new StateLogEntryRedirectRequest
                                {
                                    Data = nlc.Item1,
                                    ExternalID = nlc.Item2
                                }
                                                                 ), this.stateMachine.NodeAddress, this.stateMachine.entitySettings.EntityName);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.stateMachine.Log.Log(new WarningLogEntry()
                {
                    Exception = ex, Method = "Raft.RaftNode.AddLogEntryLeader"
                });
                res.AddResult = AddLogEntryResult.eAddLogEntryResult.ERROR_OCCURED;
            }
            return(res);
        }