-
Notifications
You must be signed in to change notification settings - Fork 67
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
Adding util/shard and util/yolobuf #36
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ce7a52
Shuffle-sharding support in Ruler. (#3235)
pstibrany 16dcecf
Fixed shuffle sharding consistency when zone-awareness is enabled and…
pracucci c886cd8
Fixed max global series per user/metric limit when shuffle sharding i…
pracucci 9c35f8a
Add util/shard, util/yolo
041d6a4
Move changelog message to follow my own rules
9f87b62
Merge remote-tracking branch 'upstream/main' into util-shard
a2bd0ed
Fix comment grammar
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
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,47 @@ | ||
package shard | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/binary" | ||
"math" | ||
|
||
"github.com/grafana/dskit/yolo" | ||
) | ||
|
||
// Sharding Strategies. | ||
const ( | ||
ShardingStrategyDefault = "default" | ||
ShardingStrategyShuffle = "shuffle-sharding" | ||
) | ||
|
||
var ( | ||
seedSeparator = []byte{0} | ||
) | ||
|
||
// ShuffleShardSeed returns seed for random number generator, computed from provided identifier. | ||
func ShuffleShardSeed(identifier, zone string) int64 { | ||
// Use the identifier to compute a hash we'll use to seed the random. | ||
hasher := md5.New() | ||
hasher.Write(yolo.Buf(identifier)) // nolint:errcheck | ||
if zone != "" { | ||
hasher.Write(seedSeparator) // nolint:errcheck | ||
hasher.Write(yolo.Buf(zone)) // nolint:errcheck | ||
} | ||
checksum := hasher.Sum(nil) | ||
|
||
// Generate the seed based on the first 64 bits of the checksum. | ||
return int64(binary.BigEndian.Uint64(checksum)) | ||
} | ||
|
||
// ShuffleShardExpectedInstancesPerZone returns the number of instances that should be selected for each | ||
// zone when zone-aware replication is enabled. The algorithm expects the shard size to be divisible | ||
// by the number of zones, in order to have nodes balanced across zones. If it's not, we do round up. | ||
func ShuffleShardExpectedInstancesPerZone(shardSize, numZones int) int { | ||
return int(math.Ceil(float64(shardSize) / float64(numZones))) | ||
} | ||
|
||
// ShuffleShardExpectedInstances returns the total number of instances that should be selected for a given | ||
// tenant. If zone-aware replication is disabled, the input numZones should be 1. | ||
func ShuffleShardExpectedInstances(shardSize, numZones int) int { | ||
return ShuffleShardExpectedInstancesPerZone(shardSize, numZones) * numZones | ||
} |
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,83 @@ | ||
package shard | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestShuffleShardExpectedInstancesPerZone(t *testing.T) { | ||
tests := []struct { | ||
shardSize int | ||
numZones int | ||
expected int | ||
}{ | ||
{ | ||
shardSize: 1, | ||
numZones: 1, | ||
expected: 1, | ||
}, | ||
{ | ||
shardSize: 1, | ||
numZones: 3, | ||
expected: 1, | ||
}, | ||
{ | ||
shardSize: 3, | ||
numZones: 3, | ||
expected: 1, | ||
}, | ||
{ | ||
shardSize: 4, | ||
numZones: 3, | ||
expected: 2, | ||
}, | ||
{ | ||
shardSize: 6, | ||
numZones: 3, | ||
expected: 2, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
assert.Equal(t, test.expected, ShuffleShardExpectedInstancesPerZone(test.shardSize, test.numZones)) | ||
} | ||
} | ||
|
||
func TestShuffleShardExpectedInstances(t *testing.T) { | ||
tests := []struct { | ||
shardSize int | ||
numZones int | ||
expected int | ||
}{ | ||
{ | ||
shardSize: 1, | ||
numZones: 1, | ||
expected: 1, | ||
}, | ||
{ | ||
shardSize: 1, | ||
numZones: 3, | ||
expected: 3, | ||
}, | ||
{ | ||
shardSize: 3, | ||
numZones: 3, | ||
expected: 3, | ||
}, | ||
{ | ||
shardSize: 4, | ||
numZones: 3, | ||
expected: 6, | ||
}, | ||
{ | ||
shardSize: 6, | ||
numZones: 3, | ||
expected: 6, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
assert.Equal(t, test.expected, ShuffleShardExpectedInstances(test.shardSize, test.numZones)) | ||
} | ||
} |
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,8 @@ | ||
package yolo | ||
|
||
import "unsafe" | ||
|
||
// Buf will return an unsafe pointer to a string, as the name yolo.buf implies use at your own risk. | ||
func Buf(s string) []byte { | ||
return *((*[]byte)(unsafe.Pointer(&s))) | ||
} |
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,13 @@ | ||
package yolo | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestYoloBuf(t *testing.T) { | ||
s := Buf("hello world") | ||
|
||
require.Equal(t, []byte("hello world"), s) | ||
} |
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.
I think these should not be part of dskit.