Ejemplo n.º 1
0
        private void Init(NetClient client, RaftConfig raftconf, Action <Agent, Action> onLeaderChanged)
        {
            OnLeaderChanged = onLeaderChanged;

            if (null == raftconf)
            {
                raftconf = RaftConfig.Load();
            }

            RaftConfig = raftconf;
            Client     = client;

            if (Client.Config.AcceptorCount() != 0)
            {
                throw new Exception("Acceptor Found!");
            }

            if (Client.Config.ConnectorCount() != 0)
            {
                throw new Exception("Connector Found!");
            }

            foreach (var node in RaftConfig.Nodes.Values)
            {
                Client.Config.AddConnector(new ConnectorEx(node.Host, node.Port));
            }

            Client.AddFactoryHandle(new LeaderIs().TypeId, new Service.ProtocolFactoryHandle()
            {
                Factory = () => new LeaderIs(),
                Handle  = ProcessLeaderIs,
            });
        }
Ejemplo n.º 2
0
 public static void CreateAcceptor(Service service, RaftConfig raftconf)
 {
     if (false == raftconf.Nodes.TryGetValue(raftconf.Name, out var node))
     {
         throw new Exception("raft Name Not In Node");
     }
     service.Config.AddAcceptor(new Acceptor(node.Port, node.Host));
 }
Ejemplo n.º 3
0
 public Agent(
     string name,
     Application zeze,
     RaftConfig raftconf = null,
     Action <Agent, Action> onLeaderChanged = null
     )
 {
     Init(new NetClient(this, name, zeze), raftconf, onLeaderChanged);
 }
Ejemplo n.º 4
0
 public static void CreateConnector(Service service, RaftConfig raftconf)
 {
     foreach (var node in raftconf.Nodes.Values)
     {
         if (raftconf.Name.Equals(node.Name))
         {
             continue; // skip self.
         }
         service.Config.AddConnector(new ConnectorEx(node.Host, node.Port));
     }
 }
Ejemplo n.º 5
0
        public Raft(StateMachine sm,
                    string RaftName     = null,
                    RaftConfig raftconf = null,
                    Zeze.Config config  = null,
                    string name         = "Zeze.Raft.Server")
        {
            if (null == raftconf)
            {
                raftconf = RaftConfig.Load();
            }
            raftconf.Verify();

            RaftConfig   = raftconf;
            sm.Raft      = this;
            StateMachine = sm;

            if (false == string.IsNullOrEmpty(RaftName))
            {
                raftconf.Name = RaftName;
            }

            if (null == config)
            {
                config = Zeze.Config.Load();
            }

            Server = new Server(this, name, config);
            if (Server.Config.AcceptorCount() != 0)
            {
                throw new Exception("Acceptor Found!");
            }
            if (Server.Config.ConnectorCount() != 0)
            {
                throw new Exception("Connector Found!");
            }
            if (RaftConfig.Nodes.Count < 3)
            {
                throw new Exception("Startup Nodes.Count Must >= 3.");
            }

            ImportantThreadPool = new SimpleThreadPool(5, $"Raft.{Name}");
            Server.CreateAcceptor(Server, raftconf);
            Server.CreateConnector(Server, raftconf);

            LogSequence = new LogSequence(this);

            RegisterInternalRpc();
            StartLeaderLostTimerTask();
            LogSequence.StartSnapshotPerDayTimer();
            AppDomain.CurrentDomain.ProcessExit += ProcessExit;
        }
Ejemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="raftconf"></param>
        /// <param name="config"></param>
        /// <param name="onLeaderChanged"></param>
        /// <param name="name"></param>
        public Agent(
            string name,
            RaftConfig raftconf = null,
            Zeze.Config config  = null,
            Action <Agent, Action> onLeaderChanged = null
            )
        {
            if (null == config)
            {
                config = Config.Load();
            }

            Init(new NetClient(this, name, config), raftconf, onLeaderChanged);
        }
Ejemplo n.º 7
0
Archivo: Test.cs Proyecto: e2wugui/zeze
            public void StartRaft(bool resetLog = false)
            {
                lock (this)
                {
                    if (null != Raft)
                    {
                        return;
                    }
                    logger.Debug("Raft {0} Start ...", RaftName);
                    StateMachine = new TestStateMachine();

                    var raftConfig = RaftConfig.Load(RaftConfigFileName);
                    raftConfig.AppendEntriesTimeout = 1000;
                    raftConfig.LeaderHeartbeatTimer = 1500;
                    raftConfig.LeaderLostTimeout    = 2000;
                    raftConfig.DbHome = Path.Combine(".", RaftName.Replace(':', '_'));
                    if (resetLog)
                    {
                        if (Directory.Exists(raftConfig.DbHome))
                        {
                            Directory.Delete(raftConfig.DbHome, true);
                        }
                    }
                    Directory.CreateDirectory(raftConfig.DbHome);

                    Raft = new Raft(StateMachine, RaftName, raftConfig);
                    Raft.Server.AddFactoryHandle(
                        new AddCount().TypeId,
                        new Net.Service.ProtocolFactoryHandle()
                    {
                        Factory = () => new AddCount(),
                        Handle  = ProcessAddCount,
                    });

                    Raft.Server.AddFactoryHandle(
                        new GetCount().TypeId,
                        new Net.Service.ProtocolFactoryHandle()
                    {
                        Factory = () => new GetCount(),
                        Handle  = ProcessGetCount,
                    });
                    Raft.Server.Start();
                }
            }
Ejemplo n.º 8
0
Archivo: Test.cs Proyecto: e2wugui/zeze
        public void Run()
        {
            logger.Debug("Start.");
            var raftConfigStart = RaftConfig.Load(RaftConfigFileName);

            foreach (var node in raftConfigStart.Nodes)
            {
                // every node need a private config-file.
                var confName = System.IO.Path.GetTempFileName() + ".xml";
                System.IO.File.Copy(raftConfigStart.XmlFileName, confName);
                Rafts.GetOrAdd(node.Value.Name, (_) => new TestRaft(node.Value.Name, confName));
            }

            foreach (var raft in Rafts.Values)
            {
                raft.Raft.Server.Start();
            }

            Agent = new Agent("Zeze.Raft.Agent.Test", raftConfigStart);
            Agent.Client.AddFactoryHandle(
                new AddCount().TypeId,
                new Net.Service.ProtocolFactoryHandle()
            {
                Factory = () => new AddCount(),
            });
            Agent.Client.AddFactoryHandle(
                new GetCount().TypeId,
                new Net.Service.ProtocolFactoryHandle()
            {
                Factory = () => new GetCount(),
            });
            Agent.Client.Start();
            RunTrace();
            Agent.Client.Stop();
            foreach (var raft in Rafts.Values)
            {
                raft.StopRaft();
            }
            logger.Debug("End.");
        }