Fix and improve hash token distribution algorithm #145
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are
2^32
distinct "hash tokens" in our hash space, but we were usingINT32_MAX
(2^32 - 1
) in the code instead. Because of this, shard counts which one might expect to divide evenly into the space (such as 16, 32, or 256) had fewer tokens than they should have. The remainder of the tokens were stuffed into the last shard, causing uneven load.Though fixing the
INT32_MAX
bug solves the above case, it still doesn't deal with the remainder, which can be as large asshardCount - 1
. We could continue stuffing it into the top shard, but I find it nicer to have all shard sizes be within one token of one another.We previously divided the shard count into the hash token count to get a "hash token increment" and added that increment each iteration: this gives something like
shardIndex * (hashCount / shardCount)
. By changing the grouping to(shardIndex * hashCount) / shardCount
, the issue with distributing the remainder goes away entirely and we get "nice" shards.