/// <summary> /// Maps sharding ID to processor ID. This method is used to map process unqiue mutex id, or GDID to processor ID - which denotes a /// set of actual executor hosts /// </summary> public int MapShardingKeyToProcessorID(string shardingKey) { var ids = ProcessorMap.Keys.ToArray(); if (ids.Length == 0) { throw new MetabaseException("TODO: MapShardingKeyToProcessorID(ProcessorMap is empty)"); } var hash = ShardingUtils.StringToShardingID(shardingKey); var idx = hash % (ulong)ids.Length; return(ids[idx]); }
public void RandomWebSafeStringToShardingIDTest() { var count = 1000000L; var counters = new long[8]; for (var i = 0L; i < count; i++) { var shardID = ShardingUtils.StringToShardingID(ExternalRandomGenerator.Instance.NextRandomWebSafeString(4, 12)); counters[shardID % (ulong)counters.Length]++; } for (int i = 0, length = counters.Length; i < length; i++) { var percent = (counters[i] * 10000 / count) / 100.0; Console.WriteLine("counter[{0}] = {1}; // {2}%".Args(i, counters[i], percent)); Aver.IsTrue(Math.Abs((100.0 / counters.Length) - percent) <= 1.0); } }
/// <summary> /// Assigns a worker from the set for the supplied sharding key. /// If key is null then a random member is assigned. /// Returns null if there is no host available for assignment /// </summary> public virtual HostPair AssignHost(object shardingKey) { var hosts = m_Hosts;//thread-safe copy, as during execution another may swap if (hosts == null || hosts.Length == 0) { return(new HostPair()); } if (shardingKey == null) { shardingKey = App.Random.NextRandomInteger; } var idx = (uint)ShardingUtils.ObjectToShardingID(shardingKey) % hosts.Length; var idx1 = -1L; for (var c = 0; c < hosts.Length; c++) { var current = idx; idx = (current + 1) % hosts.Length; var host = hosts[current]; if (!host.LastDownTime.HasValue) { idx1 = current; break; } } var idx2 = -1L; for (var c = 0; c < hosts.Length; c++) { var current = idx; idx = (current + 1) % hosts.Length; var host = hosts[current]; if (!host.LastDownTime.HasValue && current != idx1) { idx2 = current; break; } } return(new HostPair(idx1 >= 0 ? hosts[idx1].Section : null, idx2 >= 0 ? hosts[idx2].Section : null)); }
/// <summary> /// Finds appropriate shard for key. See MDB.ShardingUtils /// </summary> public Shard GetShardForKey(byte[] key) { ulong subid = ShardingUtils.ObjectToShardingID(key); return(Shards[subid % (ulong)Shards.Length]); }