Skip to content

Instantly share code, notes, and snippets.

@andreyka26-git
Created July 25, 2024 16:06
Show Gist options
  • Save andreyka26-git/bc73a7752d389748298e902aff99f473 to your computer and use it in GitHub Desktop.
Save andreyka26-git/bc73a7752d389748298e902aff99f473 to your computer and use it in GitHub Desktop.
The case when we choose bad hash function resulting in flip of the traffic
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
var dict = new Dictionary<string, int>
{
["euwe-0"] = 0,
["euno-0"] = 0,
["euwe-1"] = 0,
["euno-1"] = 0,
["euwe-2"] = 0,
["euno-2"] = 0,
["euwe-3"] = 0,
["euno-3"] = 0,
};
// we have 2 regions: euwe and euno
// we have 4 partitions: 00, 01, 02, 03
for (var i = 0; i < 100000; i++)
{
var partitionKey = $"somestatic{i}somestatic2";
var regionKey = $"{i}";
var partitionId = Math.Abs(GetHashCode(partitionKey)) % 4;
var regionId = Math.Abs(GetHashCode(regionKey)) % 2;
var reg = regionId == 0 ? "euwe" : "euno";
var dictKey = $"{reg}-{partitionId}";
if (!dict.ContainsKey(dictKey))
{
dict.Add(dictKey, 0);
}
dict[dictKey]++;
}
Console.WriteLine(dict.Count);
static int GetHashCode(string input)
{
return input.GetHashCode();
//uint hash = 0;
//for (int inputCharIndex = 0; inputCharIndex < input.Length; ++inputCharIndex)
//{
// uint inputCharValue = input[inputCharIndex];
// hash = inputCharValue + (hash << 6) + (hash << 16) - hash;
//}
//int result = (int)hash;
//return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment