Skip to content
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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
* [CHANGE] Removed global metrics for KV package. Making a KV object will now require a prometheus registerer that will
be used to register all relevant KV class metrics. #22
* [CHANGE] Added CHANGELOG.md and Pull Request template to reference the changelog
* [ENHANCEMENT] Added yolobuf and shard from cortex pkg/util #36
* [ENHANCEMENT] Add middleware package. #38
47 changes: 47 additions & 0 deletions shard/shard.go
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"
Comment on lines +13 to +14
Copy link
Contributor

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.

)

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
}
83 changes: 83 additions & 0 deletions shard/shard_test.go
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))
}
}
8 changes: 8 additions & 0 deletions yolo/yolo.go
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)))
}
13 changes: 13 additions & 0 deletions yolo/yolo_test.go
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)
}