-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consistent hashing: consistently retry another host #119
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Package consistent implements consistent hashing for cache nodes. | ||
package consistent | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/dgryski/go-jump" | ||
"github.com/mitchellh/hashstructure/v2" | ||
) | ||
|
||
type cacheKey struct { | ||
Key any | ||
Attempt int | ||
} | ||
|
||
// HashBucket returns a bucket from [0,buckets). If you want to implement a | ||
// retry, you can pass previousBuckets, which indicates buckets which must be | ||
// avoided in the output. HashBucket will modify the previousBuckets slice by | ||
// sorting it. | ||
func HashBucket(key any, buckets int, previousBuckets ...int) (int, error) { | ||
if len(previousBuckets) >= buckets { | ||
return -1, fmt.Errorf("No more buckets left: %d buckets available but %d already attempted", buckets, previousBuckets) | ||
} | ||
// we set IgnoreZeroValue so that we can add fields to the hash key | ||
// later without breaking things. | ||
// note that it's not safe to share a HashOptions so we create a fresh one each time. | ||
hashopts := &hashstructure.HashOptions{IgnoreZeroValue: true} | ||
hash, err := hashstructure.Hash(cacheKey{Key: key, Attempt: len(previousBuckets)}, hashstructure.FormatV2, hashopts) | ||
if err != nil { | ||
return -1, fmt.Errorf("error calculating hash of key: %w", err) | ||
} | ||
|
||
// jump is an implementation of Google's Jump Consistent Hash. | ||
// | ||
// See http://arxiv.org/abs/1406.2294 for details. | ||
bucket := int(jump.Hash(hash, buckets-len(previousBuckets))) | ||
slices.Sort(previousBuckets) | ||
for _, prev := range previousBuckets { | ||
if bucket >= prev { | ||
bucket++ | ||
} | ||
} | ||
return bucket, nil | ||
} | ||
Comment on lines
+21
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so nice to have it extracted! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package consistent_test | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
|
||
"github.com/replicate/pget/pkg/consistent" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHashingDoesNotChangeWhenZeroValueFieldsAreAdded(t *testing.T) { | ||
a, err := consistent.HashBucket(struct{}{}, 1024) | ||
require.NoError(t, err) | ||
b, err := consistent.HashBucket(struct{ I int }{}, 1024) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, a, b) | ||
} | ||
|
||
func TestRetriesScatterBuckets(t *testing.T) { | ||
// This test is tricky! We want an example of hash keys which map to the | ||
// same bucket, but after one retry map to different buckets. | ||
// | ||
// These two keys happen to have this property for 10 buckets: | ||
strA := "abcdefg" | ||
strB := "1234567" | ||
a, err := consistent.HashBucket(strA, 10) | ||
require.NoError(t, err) | ||
b, err := consistent.HashBucket(strB, 10) | ||
require.NoError(t, err) | ||
|
||
// strA and strB to map to the same bucket | ||
require.Equal(t, a, b) | ||
|
||
aRetry, err := consistent.HashBucket(strA, 10, a) | ||
require.NoError(t, err) | ||
bRetry, err := consistent.HashBucket(strB, 10, b) | ||
require.NoError(t, err) | ||
|
||
// but after retry they map to different buckets | ||
assert.NotEqual(t, aRetry, bRetry) | ||
} | ||
|
||
func FuzzRetriesMostNotRepeatIndices(f *testing.F) { | ||
f.Add("test.replicate.delivery", 5) | ||
f.Add("test.replicate.delivery", 0) | ||
f.Fuzz(func(t *testing.T, key string, excessBuckets int) { | ||
if excessBuckets < 0 { | ||
t.Skip("invalid value") | ||
} | ||
attempts := 20 | ||
buckets := attempts + excessBuckets | ||
if buckets < 0 { | ||
t.Skip("integer overflow") | ||
} | ||
previous := []int{} | ||
for i := 0; i < attempts; i++ { | ||
next, err := consistent.HashBucket(key, buckets, previous...) | ||
require.NoError(t, err) | ||
|
||
// we must be in range | ||
assert.Less(t, next, buckets) | ||
assert.GreaterOrEqual(t, next, 0) | ||
|
||
// we shouldn't repeat any previous value | ||
assert.NotContains(t, previous, next) | ||
|
||
previous = append(previous, next) | ||
slices.Sort(previous) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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 was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really nice. I like the derived/wrapped key a lot.