Exemple #1
0
        private const int VIRTUAL_NODE_COUNT = NODE_COUNT * 32;//虚拟节点数

        public static void Test()
        {
            HashAlgorithmTest test = new HashAlgorithmTest();

            /** Records the times of locating node*/
            Dictionary <string, int> nodeRecord = new Dictionary <string, int>();

            List <string>     allNodes = test.getNodes(NODE_COUNT);
            KetamaNodeLocator locator  = new KetamaNodeLocator(allNodes, VIRTUAL_NODE_COUNT);

            List <String> allKeys = test.getAllStrings();

            foreach (string key in allKeys)
            {
                //最终使用的节点
                string node = locator.GetPrimary(key);

                if (!nodeRecord.ContainsKey(node))
                {
                    nodeRecord[node] = 1;
                }
                else
                {
                    nodeRecord[node] = nodeRecord[node] + 1;
                }
            }

            Console.WriteLine("Nodes count : " + NODE_COUNT + ", Keys count : " + EXE_TIMES + ", Normal percent : " + (float)100 / NODE_COUNT + "%");
            Console.WriteLine("-------------------- boundary  ----------------------");
            foreach (string key in nodeRecord.Keys)
            {
                Console.WriteLine("Node name :" + key + " - Times : " + nodeRecord[key] + " - Percent : " + (float)nodeRecord[key] / EXE_TIMES * 100 + "%");
            }
            Console.ReadLine();
        }
Exemple #2
0
        public void TestKetamaNodeHash()
        {
            var list = new Dictionary <string, int>();

            var nodes = new List <string> {
                "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9"
            };
            var ms = new List <string> {
                "CNT_BTC", "CNT_LTC", "CNT_IFC", "CNT_NXT", "CNT_DOGE", "BTC_LTC", "BTC_IFC", "BTC_DOGE", "BTC_NXT"
            };
            var KetamaNode = new KetamaNodeLocator(nodes, 500);

            var d = new Random();

            for (int i = 0; i < 10000; i++)
            {
                var key   = ms[d.Next(9)] + d.Next(100);
                var value = KetamaNode.GetPrimary(key.ToString());

                if (list.ContainsKey(value))
                {
                    list[value] += 1;
                }
                else
                {
                    list[value] = 1;
                }
            }

            //foreach (var key in list.Keys)
            //{
            //    var s = key + ":" + (list[key] / 10000.0D).ToString("P");
            //    Console.WriteLine(s);
            //}
        }
Exemple #3
0
        public static string getConnectionFromId(string id, string spliter, string connectionKeyList, string connectionList)
        {
            List <string>     keys = new List <string>(connectionKeyList.Split(new string[] { spliter }, StringSplitOptions.RemoveEmptyEntries));
            KetamaNodeLocator k    = new KetamaNodeLocator(keys);
            var key = k.GetPrimary(id);

            List <string> conns    = new List <string>(connectionList.Split(new string[] { spliter }, StringSplitOptions.RemoveEmptyEntries));
            int           resindex = 0;

            Int32.TryParse(key, out resindex);
            return(conns[resindex - 1]);
        }
        private static void call(List <string> nodes, Dictionary <string, List <string> > map)
        {
            KetamaNodeLocator locator = new KetamaNodeLocator(nodes, VIRTUAL_NODE_COUNT);

            foreach (string key in map.Keys)
            {
                string node = locator.GetPrimary(key);

                if (node != null)
                {
                    List <string> list = map[key];
                    list.Add(node);
                }
            }
        }
Exemple #5
0
        public string getChild(string node, string id, Watcher watcher)
        {
            ZooKeeper zk       = createClientForCall(watcher);
            var       children = GetChild(ref zk, node, watcher);

            List <string> childList = children?.Children;

            if (childList?.Count > 0)
            {
                int i = childList.Count;
                KetamaNodeLocator k = new KetamaNodeLocator(childList);
                var key             = k.GetPrimary(id);
                return(GetServer(zk, $"{node}/{key}"));
            }
            else
            {
                return("");
            }
        }
        public static void OpenRPCServer(IConfiguration configuration)
        {
            var hosts = configuration.GetSection("XRPCHosts").Value;
            var arr   = hosts.Split(";").ToList();

            KetamaNodeLocator ketamaNodeLocator = new KetamaNodeLocator(arr);
            var host = ketamaNodeLocator.GetPrimary(Guid.NewGuid().ToString());

            XRPCServer xRPCServer = new XRPCServer();

            xRPCServer.RPCOptions.LogToConsole          = true;
            xRPCServer.ServerOptions.DefaultListen.Host = host.Split(":")[0];
            xRPCServer.ServerOptions.DefaultListen.Port = Convert.ToInt32(host.Split(":")[1]);
            xRPCServer.ServerOptions.LogLevel           = BeetleX.EventArgs.LogType.Debug;
            var assembly = Assembly.Load("Lmf.Purchasing.Service");

            xRPCServer.Register(assembly);
            xRPCServer.Open();
        }
Exemple #7
0
        /// <summary>
        /// 注册XRPC服务
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddXRPCClient(this IServiceCollection services, IConfiguration configuration)
        {
            var hosts   = configuration.GetSection("XRPCClient").Value;
            var timeOut = configuration.GetSection("XRPCClientTimeOut").Value;
            var arr     = hosts.Split(";").ToList();
            KetamaNodeLocator ketamaNodeLocator = new KetamaNodeLocator(arr);
            var host = ketamaNodeLocator.GetPrimary(Guid.NewGuid().ToString());

            var client = new XRPCClient(host.Split(":")[0], Convert.ToInt32(host.Split(":")[1]));

            client.TimeOut = Convert.ToInt32(timeOut);
            client.Connect();
            client.NetError = (c, e) =>
            {
                Console.WriteLine(e.Error.Message);
            };

            services.AddSingleton(client);
            return(services);
        }
Exemple #8
0
        static void Main(string[] args)
        {
            List <string> nodes = new List <string>()
            {
                "192.168.128:8080",
                "192.168.129:8520",
                "192.168.130:9898"
            };

            KetamaNodeLocator ketamaNodeLocator = new KetamaNodeLocator(nodes);

            for (int i = 0; i < 100; i++)
            {
                var h = ketamaNodeLocator.GetPrimary(Guid.NewGuid().ToString("N"));
                Console.WriteLine(h);
            }

            RedisClient redisHelper = new RedisClient(0, "127.0.0.1:6379,allowadmin=true");
            var         a           = redisHelper.StringGet("hello");

            Console.WriteLine(a);
            Console.ReadLine();
        }