Skip to content

Commit 3ccf833

Browse files
kolesnikovaeshelldandy
authored andcommitted
fix(v2): use better seed for shard placement (grafana#3924)
* fix(v2): use better seed for shard placement * fix(v2): update tests
1 parent c7bfdf6 commit 3ccf833

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

pkg/experiment/distributor/distributor.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,20 @@ func (p *perm) resize(n int) {
326326
}
327327
}
328328

329-
// The value is a random generated with a crypto/rand.Read,
330-
// and decoded as a little-endian uint64. No fancy math here.
331-
const randSeed = 4349576827832984783
332-
333329
var steps [4 << 10]uint32
334330

335331
func init() {
336-
r := rand.New(rand.NewSource(randSeed))
332+
// The seed impacts mapping of shards to nodes.
333+
// TODO(kolesnikovae):
334+
// Stochastic approach does not work well
335+
// in all the cases; it should be replaced
336+
// with a deterministic one.
337+
const randSeed = -3035313949336265834
338+
setSeed(randSeed)
339+
}
340+
341+
func setSeed(n int64) {
342+
r := rand.New(rand.NewSource(n))
337343
for i := range steps {
338344
steps[i] = uint32(r.Intn(i + 1))
339345
}

pkg/experiment/distributor/distributor_test.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func Test_Distributor_Distribute(t *testing.T) {
160160
// 0 1 2 3 4 5 6 7 8 9 10 11 all shards
161161
// * * * * > * * * tenant (size 8, offset 8)
162162
// > * * * dataset (size 4, offset 6+8 mod 12 = 2)
163-
// a a a b b b c c a b c c shuffling (see d.distribution.shards)
163+
// 2 1 0 1 1 2 2 0 1 0 0 2 shuffling (see d.distribution.shards)
164164
// ----------------------------------------------------------------------
165165
// 0 1 2 3 4 PickShard 0 (offset within dataset)
166166
// ^ borrowed from the tenant
@@ -170,13 +170,13 @@ func Test_Distributor_Distribute(t *testing.T) {
170170
// 1 2 3 0 4 PickShard 3
171171

172172
// Identical keys have identical placement.
173-
assert.Equal(t, []string{"a", "b", "a", "b", "c"}, collect(0, 5))
174-
assert.Equal(t, []string{"a", "b", "a", "b", "c"}, collect(0, 5))
173+
assert.Equal(t, []string{"a", "b", "b", "a", "a"}, collect(0, 5))
174+
assert.Equal(t, []string{"a", "b", "b", "a", "a"}, collect(0, 5))
175175

176176
// Placement of different keys in the dataset is bound.
177-
assert.Equal(t, []string{"b", "a", "b", "a", "c"}, collect(1, 5))
178-
assert.Equal(t, []string{"a", "b", "a", "b", "c"}, collect(2, 5))
179-
assert.Equal(t, []string{"b", "a", "b", "a", "c"}, collect(3, 5))
177+
assert.Equal(t, []string{"b", "b", "a", "a", "a"}, collect(1, 5))
178+
assert.Equal(t, []string{"b", "a", "a", "b", "a"}, collect(2, 5))
179+
assert.Equal(t, []string{"a", "a", "b", "b", "a"}, collect(3, 5))
180180

181181
// Now we're trying to collect more instances than available.
182182
// 0 1 2 3 4 5 6 7 8 9 10 11 all shards
@@ -186,8 +186,8 @@ func Test_Distributor_Distribute(t *testing.T) {
186186
// 6 7 2 3 8 9 10 11 0 1 4 5
187187
// ^ ^ ^ ^ borrowed from the tenant
188188
// ^ ^ ^ ^ borrowed from the top ring
189-
// a a a b b b c c a b c c shuffling (see d.distribution.shards)
190-
assert.Equal(t, []string{"a", "b", "a", "b", "c", "c", "a", "a", "b", "b", "c", "c"}, collect(2, 13))
189+
// 2 1 0 1 1 2 2 0 1 0 0 2 shuffling (see d.distribution.shards)
190+
assert.Equal(t, []string{"b", "a", "a", "b", "a", "c", "c", "b", "b", "c", "c", "a"}, collect(2, 13))
191191
}
192192

193193
func Test_distribution_iterator(t *testing.T) {
@@ -419,25 +419,24 @@ func Test_permutation(t *testing.T) {
419419
p.resize(i)
420420
actual = append(actual, copyP(p.v))
421421
}
422-
423422
expected := [][]uint32{
424423
{},
425-
{2, 3, 1, 0},
426-
{2, 3, 1, 5, 6, 4, 7, 0},
427-
{2, 3, 1, 5, 6, 4, 9, 11, 0, 7, 8, 10},
428-
{2, 3, 1, 5, 12, 4, 14, 11, 15, 7, 8, 13, 6, 10, 9, 0},
429-
{2, 3, 18, 5, 12, 4, 14, 11, 15, 7, 8, 13, 6, 10, 9, 19, 17, 16, 1, 0},
430-
{2, 3, 18, 5, 12, 4, 14, 11, 15, 22, 8, 13, 6, 10, 9, 19, 17, 21, 1, 20, 0, 16, 23, 7},
431-
{2, 3, 18, 5, 12, 4, 14, 11, 15, 22, 8, 13, 6, 27, 9, 19, 24, 21, 1, 20, 0, 16, 23, 26, 17, 10, 7, 25},
432-
{28, 3, 18, 5, 12, 29, 14, 11, 15, 22, 8, 13, 31, 27, 9, 19, 24, 21, 1, 20, 0, 16, 23, 26, 30, 10, 7, 25, 2, 4, 17, 6},
433-
{28, 3, 18, 5, 12, 29, 14, 11, 15, 22, 8, 13, 31, 27, 9, 19, 24, 21, 1, 20, 0, 16, 23, 26, 30, 10, 7, 25, 2, 4, 17, 6},
434-
{2, 3, 18, 5, 12, 4, 14, 11, 15, 22, 8, 13, 6, 27, 9, 19, 24, 21, 1, 20, 0, 16, 23, 26, 17, 10, 7, 25},
435-
{2, 3, 18, 5, 12, 4, 14, 11, 15, 22, 8, 13, 6, 10, 9, 19, 17, 21, 1, 20, 0, 16, 23, 7},
436-
{2, 3, 18, 5, 12, 4, 14, 11, 15, 7, 8, 13, 6, 10, 9, 19, 17, 16, 1, 0},
437-
{2, 3, 1, 5, 12, 4, 14, 11, 15, 7, 8, 13, 6, 10, 9, 0},
438-
{2, 3, 1, 5, 6, 4, 9, 11, 0, 7, 8, 10},
439-
{2, 3, 1, 5, 6, 4, 7, 0},
440-
{2, 3, 1, 0},
424+
{3, 1, 0, 2},
425+
{3, 6, 0, 5, 7, 4, 1, 2},
426+
{11, 6, 0, 5, 7, 8, 9, 2, 4, 1, 3, 10},
427+
{11, 6, 0, 5, 14, 8, 15, 2, 12, 1, 3, 13, 4, 10, 7, 9},
428+
{11, 6, 0, 19, 14, 8, 15, 2, 12, 17, 3, 18, 4, 16, 7, 9, 10, 1, 13, 5},
429+
{11, 6, 0, 19, 14, 8, 15, 2, 21, 17, 3, 18, 4, 16, 7, 22, 10, 1, 23, 5, 9, 12, 20, 13},
430+
{11, 6, 0, 19, 14, 8, 15, 2, 26, 17, 25, 18, 24, 16, 7, 22, 10, 1, 23, 5, 9, 12, 20, 13, 4, 3, 27, 21},
431+
{11, 6, 0, 28, 31, 8, 15, 2, 26, 17, 25, 18, 24, 16, 7, 22, 10, 1, 23, 5, 30, 12, 20, 13, 4, 29, 27, 21, 19, 3, 9, 14},
432+
{11, 6, 0, 28, 31, 8, 15, 2, 26, 17, 25, 18, 24, 16, 7, 22, 10, 1, 23, 5, 30, 12, 20, 13, 4, 29, 27, 21, 19, 3, 9, 14},
433+
{11, 6, 0, 19, 14, 8, 15, 2, 26, 17, 25, 18, 24, 16, 7, 22, 10, 1, 23, 5, 9, 12, 20, 13, 4, 3, 27, 21},
434+
{11, 6, 0, 19, 14, 8, 15, 2, 21, 17, 3, 18, 4, 16, 7, 22, 10, 1, 23, 5, 9, 12, 20, 13},
435+
{11, 6, 0, 19, 14, 8, 15, 2, 12, 17, 3, 18, 4, 16, 7, 9, 10, 1, 13, 5},
436+
{11, 6, 0, 5, 14, 8, 15, 2, 12, 1, 3, 13, 4, 10, 7, 9},
437+
{11, 6, 0, 5, 7, 8, 9, 2, 4, 1, 3, 10},
438+
{3, 6, 0, 5, 7, 4, 1, 2},
439+
{3, 1, 0, 2},
441440
{},
442441
}
443442

0 commit comments

Comments
 (0)