You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When we specify two or more shards, our current algorithm leads to uneven key distribution, causing some data to be overwritten and resulting in incorrect cache lengths. Please refer to #17, For example:
Note that this issue doesn't occur with a single shard, and once #24 is merged, there won't be any problems even when storing zero values.
This means each shard can store a maximum of 128 elements, And we use the following code to calculating which shard should store when set a key-value pair:
funcSet(key, value) {
hash:=hasher(key)
shardIndex:=hash&c.mask// 0 or 1returnshards[shardIndex].Set(...)
}
Since the hash algorithm cannot guarantee even distribution, many keys might be assigned to the same shard. When the number of elements in a shard exceeds 128, previous data gets deleted. This leads to incorrect cache lengths as mentioned in #17.
fori:=0; i<256; i++ {
cache.Set(i, i, 0) // assume 200 keys are assigned to shard 0
}
// The length should be 256, but it's actually shards[0].len + shards[1].len = 128 + 56 = 184// Since 200-128 = 72 keys are overwritten in shard 0fmt.Println(cache.Len())
Suggestion
Currently, there isn't a better solution to avoid this issue. Perhaps we could consider increasing the shardsize length, for example, setting it to size * 0.8.
Description
When we specify two or more shards, our current algorithm leads to uneven key distribution, causing some data to be overwritten and resulting in incorrect cache lengths. Please refer to #17, For example:
When initializing the above code, we set each shard's list length to 128:
This means each shard can store a maximum of 128 elements, And we use the following code to calculating which shard should store when set a key-value pair:
Since the hash algorithm cannot guarantee even distribution, many keys might be assigned to the same shard. When the number of elements in a shard exceeds 128, previous data gets deleted. This leads to incorrect cache lengths as mentioned in #17.
Suggestion
Currently, there isn't a better solution to avoid this issue. Perhaps we could consider increasing the shardsize length, for example, setting it to size * 0.8.
However, this will still waste a lot of space either way. Please let me know your thoughts.
The text was updated successfully, but these errors were encountered: