コード例 #1
0
        public void Start(int nodeId)
        {
            IEmulatedNode node = null;

            lock (sync_nodes)
            {
                nodes.TryGetValue(nodeId, out node);
            }
            //var node = nodes.Where(r => r.NodeAddress.NodeAddressId == nodeId).FirstOrDefault();
            if (node != null)
            {
                if (node is TcpRaftNode)
                {
                    if (!((TcpRaftNode)node).Disposed)
                    {
                        return;
                    }
                    node = null;

                    TcpRaftNode trn = null;

                    lock (sync_nodes)
                    {
                        trn = new TcpRaftNode(new NodeSettings()
                        {
                            TcpClusterEndPoints = eps, RaftEntitiesSettings = new List <RaftEntitySettings> {
                                re_settings
                            }
                        },
                                              @"D:\Temp\RaftDBreeze\node" + nodeId,
                                              new DefaultHandler(),
                                              nodeId,
                                              null,
                                              this);
                        nodes[trn.GetNodeByEntityName("default").NodeAddress.NodeAddressId] = trn;
                    }
                    trn.Start();
                }
                else
                {
                    node.EmulationStart();
                }
            }
        }
コード例 #2
0
ファイル: Emulator.cs プロジェクト: ww-it/Raft.Net
        ///// <summary>
        ///// Test method
        ///// </summary>
        ///// <param name="nodeId"></param>
        ///// <param name="stateLogId"></param>
        ///// <returns></returns>
        //public bool ContainsStateLogIdData(int nodeId, ulong stateLogId)
        //{
        //    IEmulatedNode node = null;
        //    lock (sync_nodes)
        //    {
        //        nodes.TryGetValue(nodeId, out node);
        //    }
        //    //var node = nodes.Where(r => r.NodeAddress.NodeAddressId == nodeId).FirstOrDefault();
        //    if (node == null)
        //        return false;

        //    return ((RaftNode)node).ContainsStateLogEntryId(stateLogId);
        //}


        public void Start(int nodeId)
        {
            IEmulatedNode node = null;

            lock (sync_nodes)
            {
                nodes.TryGetValue(nodeId, out node);
            }
            //var node = nodes.Where(r => r.NodeAddress.NodeAddressId == nodeId).FirstOrDefault();
            if (node != null)
            {
                if (node is TcpRaftNode)
                {
                    if (!((TcpRaftNode)node).Disposed)
                    {
                        return;
                    }
                    node = null;

                    TcpRaftNode trn = null;

                    lock (sync_nodes)
                    {
                        trn = new TcpRaftNode(new NodeSettings()
                        {
                            TcpClusterEndPoints = eps, RaftEntitiesSettings = new List <RaftEntitySettings> {
                                re_settings
                            }
                        }, @"D:\Temp\RaftDBreeze\node" + nodeId,
                                              (entityName, index, data) => { Console.WriteLine($"wow committed {entityName}/{index}"); return(true); },
                                              nodeId, this);
                        nodes[trn.GetNodeByEntityName("default").NodeAddress.NodeAddressId] = trn;
                    }
                    trn.Start();
                }
                else
                {
                    node.EmulationStart();
                }
            }
        }
コード例 #3
0
        public RaftOption(IConfiguration configuration)
        {
            var appSetting = configuration.GetSection("AppSetting");
            //设定端口
            var port = Convert.ToInt32(appSetting.GetSection("Port").Value);
            var name = appSetting.GetSection("Name").Value;

            Console.Title = name + "   port:" + port;
            var log = new Logger();
            //读取配置文件
            var config = File.ReadAllText(Directory.GetCurrentDirectory() + @"\config.txt");

            _node = TcpRaftNode.GetFromConfig(1, config,
                                              Directory.GetCurrentDirectory() + $@"\DBreeze\{name}", port, log,
                                              (entityName, index, data) =>
            {
                Console.WriteLine($"{entityName}/{index} { Encoding.UTF8.GetString(data)}");
                return(true);
            });
            //to do现在的问题是,必需启动三个节点,另一个IP是通过配置文件来加载的,不够灵活。
            _node.Start();
        }
コード例 #4
0
ファイル: Emulator.cs プロジェクト: axzxs2001/Raft.Net
        public void StartEmulateTcpNodes(int nodesQuantity)
        {
            TcpRaftNode trn = null;

            rn_settings = new RaftNodeSettings()
            {
                VerboseRaft = true,
                //VerboseRaft = false,
                VerboseTransport = false,

                DelayedPersistenceIsActive = true,

                //InMemoryEntity = true,
                //InMemoryEntityStartSyncFromLatestEntity = true
            };

            for (int i = 0; i < nodesQuantity; i++)
            {
                eps.Add(new TcpClusterEndPoint()
                {
                    Host = "127.0.0.1", Port = 4250 + i
                });
            }

            for (int i = 0; i < nodesQuantity; i++)
            {
                lock (sync_nodes)
                {
                    //S:\temp\RaftDbr
                    trn = new TcpRaftNode(eps, new List <RaftNodeSettings> {
                        rn_settings
                    }, @"D:\Temp\RaftDBreeze\node" + (4250 + i),
                                          (entityName, index, data) => { Console.WriteLine($"wow committed {entityName}/{index}"); return(true); },
                                          4250 + i, this);

                    //rn = new TcpRaftNode(eps, @"S:\temp\RaftDbr\node" + (4250 + i), 4250 + i,
                    //       (data) => {
                    //           Console.WriteLine($"wow committed");
                    //       }, this, rn_settings);

                    nodes.Add(trn.GetNodeByEntityName("default").NodeAddress.NodeAddressId, trn);
                }

                trn.Start();

                //new Thread(() =>
                //{
                //    rn.Start();
                //    //Thread.CurrentThread.IsBackground = true;

                //    //lock (sync_nodes)
                //    //{
                //    //    rn = new TcpRaftNode(eps, 4250 + i, this, rn_settings);
                //    //    nodes.Add(rn.rn.NodeAddress.NodeAddressId, rn);
                //    //    rn.Start();
                //    //}

                //}).Start();

                //Task.Run(() =>
                //{
                //    rn = new TcpRaftNode(eps, 4250 + i, this, rn_settings);
                //    lock (sync_nodes)
                //    {
                //        nodes.Add(rn.rn.NodeAddress.NodeAddressId, rn);
                //    }
                //    rn.Start();
                //});


                //rn.Verbose = true;
                //rn.SetNodesQuantityInTheCluster((uint)nodesQuantity);
                //rn.NodeAddress.NodeAddressId = i + 1;
                //lock (sync_nodes)
                //{
                //    nodes.Add(4250 + i, rn);
                //}
                System.Threading.Thread.Sleep((new Random()).Next(30, 350));
                //// System.Threading.Thread.Sleep(500);
                //rn.NodeStart();
                //rn.Start();
            }
        }
コード例 #5
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;
                }
            }
        }
コード例 #6
0
        static void Main(string[] args)
        {
            log = new Logger();

            var         config = System.IO.File.ReadAllText(System.IO.Directory.GetCurrentDirectory() + @"\config.txt");
            TcpRaftNode rn1    = TcpRaftNode.GetFromConfig(1, config,
                                                           System.IO.Directory.GetCurrentDirectory() + @"\DBreeze\Node1", 4250, log,
                                                           (entityName, index, data) =>
            {
                Console.WriteLine($"Committed {entityName}/{index}");
                Console.WriteLine("0000000000000      4250数据:" + System.Text.Encoding.UTF8.GetString(data));
                return(true);
            });

            TcpRaftNode rn2 = TcpRaftNode.GetFromConfig(1, config,
                                                        System.IO.Directory.GetCurrentDirectory() + @"\DBreeze\Node2", 4251, log,
                                                        (entityName, index, data) =>
            {
                Console.WriteLine($"Committed {entityName}/{index}");
                Console.WriteLine("111111111111      4251数据:" + System.Text.Encoding.UTF8.GetString(data));
                var random = new Random();
                var num    = random.Next(1, 3) % 2;
                Console.WriteLine(num);

                if (num == 1)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            });

            TcpRaftNode rn3 = TcpRaftNode.GetFromConfig(1, config,
                                                        System.IO.Directory.GetCurrentDirectory() + @"\DBreeze\Node3", 4252, log,
                                                        (entityName, index, data) =>
            {
                Console.WriteLine($"Committed {entityName}/{index}");
                Console.WriteLine("222222222222       4252数据:" + System.Text.Encoding.UTF8.GetString(data));
                return(true);
            });

            rn1.Start();
            rn2.Start();
            rn3.Start();


            while (true)
            {
                Console.Clear();
                Console.WriteLine("选择要发送的:1、4250     2、4251     3、4252");
                switch (Console.ReadLine())
                {
                case "1":
                    rn1.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()), "inMemory1");
                    break;

                case "2":
                    rn2.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()), "inMemory1");
                    break;

                case "3":
                    rn3.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()), "inMemory1");
                    break;
                }
            }
        }