Пример #1
0
        internal List <TcpClusterEndPoint> clusterEndPoints = new List <TcpClusterEndPoint>();  //init clusterEndPoints creating 1-N connection


        public TcpRaftNode(List <TcpClusterEndPoint> clusterEndPoints, List <RaftNodeSettings> raftNodes, string dbreezePath, Func <string, ulong, byte[], bool> OnCommit, int port = 4250, IWarningLog log = null)
        {
            //this.rn_settings = rn_settings ?? new RaftNodeSettings();

            this.log  = log;
            this.port = port;
            if (clusterEndPoints != null)
            {
                var bt      = clusterEndPoints.SerializeBiser();
                var decoder = new Biser.Decoder(bt);
                this.clusterEndPoints = new List <TcpClusterEndPoint>();
                decoder.GetCollection(() => { return(TcpClusterEndPoint.BiserDecode(extDecoder: decoder)); }, this.clusterEndPoints, false);

                //this.clusterEndPoints.AddRange(clusterEndPoints.SerializeProtobuf().DeserializeProtobuf<List<TcpClusterEndPoint>>());
            }
            spider = new TcpSpider(this);

            bool firstNode = true;

            foreach (var rn_settings in raftNodes)
            {
                if (firstNode)
                {
                    rn_settings.EntityName = "default";
                    firstNode = false;
                }

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

                if (this.raftNodes.ContainsKey(rn_settings.EntityName))
                {
                    throw new Exception("Raft.Net: entities must have unique names. Change RaftNodeSettings.EntityName.");
                }

                var rn = new RaftNode(rn_settings ?? new RaftNodeSettings(), dbreezePath, this.spider, this.log, OnCommit);

#if DEBUG
                rn.Verbose = rn_settings.VerboseRaft;                               //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   DEBUG PURPOSES
#endif
                rn.SetNodesQuantityInTheCluster((uint)this.clusterEndPoints.Count); //!!!!!!!!!!!!  ENABLE 1 for debug, make it dynamic (but not less then 3 if not DEBUG)
                rn.NodeAddress.NodeAddressId = port;                                //for debug/emulation purposes

                rn.NodeAddress.NodeUId = Guid.NewGuid().ToByteArray().Substring(8, 8).To_Int64_BigEndian();

                this.raftNodes[rn_settings.EntityName] = rn;

                rn.NodeStart();
            }
        }
Пример #2
0
        //public TcpRaftNode(List<TcpClusterEndPoint> clusterEndPoints, List<RaftNodeSettings> raftNodes, string dbreezePath, Func<string, ulong, byte[], bool> OnCommit, int port = 4250,  IWarningLog log = null)
        //public TcpRaftNode(List<TcpClusterEndPoint> clusterEndPoints, List<RaftEntitySettings> raftNodes, string dbreezePath, Func<string, ulong, byte[], bool> OnCommit, int port = 4250, IWarningLog log = null)
        public TcpRaftNode(NodeSettings nodeSettings, string dbreezePath, Func <string, ulong, byte[], bool> OnCommit, int port = 4250, IWarningLog log = null)
        {
            if (nodeSettings == null)
            {
                nodeSettings = new NodeSettings();
            }
            this.NodeSettings = nodeSettings;

            this.log  = log;
            this.port = port;

            DBreezeConfiguration conf = new DBreezeConfiguration()
            {
                DBreezeDataFolderName = dbreezePath,
                Storage = DBreezeConfiguration.eStorage.DISK,
            };

            conf.AlternativeTablesLocations.Add("mem_*", String.Empty);

            dbEngine = new DBreezeEngine(conf);


            //if (clusterEndPoints != null)
            //{
            //    var bt = clusterEndPoints.SerializeBiser();
            //    var decoder = new Biser.Decoder(bt);
            //    this.clusterEndPoints = new List<TcpClusterEndPoint>();
            //    decoder.GetCollection(() => { return TcpClusterEndPoint.BiserDecode(extDecoder: decoder); }, this.clusterEndPoints, false);

            //    //this.clusterEndPoints.AddRange(clusterEndPoints.SerializeProtobuf().DeserializeProtobuf<List<TcpClusterEndPoint>>());
            //}
            spider = new TcpSpider(this);

            //bool firstNode = true;
            if (this.NodeSettings.RaftEntitiesSettings == null)
            {
                this.NodeSettings.RaftEntitiesSettings = new List <RaftEntitySettings>();
            }

            if (this.NodeSettings.RaftEntitiesSettings.Where(r => r.EntityName.ToLower() == "default").Count() < 1)
            {
                this.NodeSettings.RaftEntitiesSettings.Add(new RaftEntitySettings());
            }


            foreach (var re_settings in this.NodeSettings.RaftEntitiesSettings)
            {
                //if (firstNode)
                //{
                //    re_settings.EntityName = "default";
                //    firstNode = false;
                //}

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

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

                var rn = new RaftNode(re_settings ?? new RaftEntitySettings(), this.dbEngine, this.spider, this.log, OnCommit);

#if DEBUG
                rn.Verbose = re_settings.VerboseRaft;                                               //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   DEBUG PURPOSES
#endif
                rn.SetNodesQuantityInTheCluster((uint)this.NodeSettings.TcpClusterEndPoints.Count); //!!!!!!!!!!!!  ENABLE 1 for debug, make it dynamic (but not less then 3 if not DEBUG)
                rn.NodeAddress.NodeAddressId = port;                                                //for debug/emulation purposes

                rn.NodeAddress.NodeUId = Guid.NewGuid().ToByteArray().Substring(8, 8).To_Int64_BigEndian();

                this.raftNodes[re_settings.EntityName] = rn;

                rn.NodeStart();
            }
        }