private static void Statistics(IList <Node> nodes, ConsistentHash <Node> cluster) { var statDict = new Dictionary <String, int>(); foreach (var n in nodes) { statDict.Add(n.IP, 0); } for (int i = 0; i < ItemCount; i++) { var node = cluster.GetNode($"key_{i}"); statDict[node.IP] += 1; } var mean = statDict.Values.Average(); var std = statDict.Values.StdDev(); Console.WriteLine(@$ " Algorithm Statistics: {cluster.ToString()} -------------------- Total: {ItemCount:N0} Mean: {mean:N0} StdDev: {std:F2} "); foreach (var(k, v) in statDict) { Console.WriteLine("Node [{0}]: {1}", k, v); } }
public static void FindNode(this ConsistentHash <Node> cluster, String[] keys) { foreach (var key in keys) { cluster.FindNode(key); } }
static void Main(string[] args) { List <Server> rackServers = new List <Server>(); rackServers.Add(new Server("10.0.0.1")); rackServers.Add(new Server("10.0.0.2")); int numberOfReplicas = 2; ConsistentHash serverDistributor = new ConsistentHash(numberOfReplicas, rackServers); //add a new server to the mix Server newServer = new Server("10.0.0.3"); serverDistributor.addServerToHashRing(newServer); //Assume you have a key "key0" Server serverForKey = serverDistributor.GetServerForKey("key0"); Console.WriteLine("Server: " + serverForKey.ipAddress + " holds key: Key0"); // Now remove a server serverDistributor.removeServerFromHashRing(newServer); // Now check on which server "key0" landed up serverForKey = serverDistributor.GetServerForKey("key0"); Console.WriteLine("Server: " + serverForKey.ipAddress + " holds key: Key0"); }
static void Main(string[] args) { IList <string> nodes = new List <string>(); for (int i = 0; i < 10; i++) { nodes.Add("Node#" + i); } ConsistentHash <string> consistentHash = new ConsistentHash <string>(nodes, 200); consistentHash.ReportToFile(); }
private static void TestAddOrRemove(ConsistentHash <Node> cluster) { var keys = new[] { "66778899", "hello world", "consistent hashing" }; cluster.FindNode(keys); var newNode = new Node($"10.1.1.{NumberOfNodes}"); cluster.RemoveFromRing(newNode); Console.WriteLine($"\nAfter removed a Node [{newNode}]:\n--------------------"); cluster.FindNode(keys); cluster.AddToRing(newNode); Console.WriteLine($"\nAfter added a new Node [{newNode}]:\n--------------------"); cluster.FindNode(keys); }
static void Main(string[] args) { var nodes = new List <Node>(); for (int i = 1; i <= NumberOfNodes; i++) { nodes.Add(new Node($"10.1.1.{i}")); } var hashFuncs = new IHash[] { new MurmurHash3(4049661204), new MD5Hash(), new FNVHash() }; var serverCluster = new ConsistentHash <Node>(hashFuncs[0], NumberOfReplicas, nodes); // serverCluster.Print(); TestAddOrRemove(serverCluster); Statistics(nodes, serverCluster); }
public static void FindNode(this ConsistentHash <Node> cluster, String key) { var found = cluster.GetNode(key); Console.WriteLine($"Key [{key}] - Hash [{cluster.GetHashKey(key)}], route to Node [{found.IP}]"); }