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, }); }
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; }
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(); } }
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."); }