Skip to content

Commit 2628863

Browse files
tbgwenyihu6
authored andcommitted
state: add TestReplicaPlacement
This makes it easy to explore the behavior of replica placements.
1 parent db762a8 commit 2628863

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

pkg/kv/kvserver/asim/state/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ go_test(
5555
"change_test.go",
5656
"config_loader_test.go",
5757
"liveness_test.go",
58+
"parser_replica_placement_test.go",
5859
"split_decider_test.go",
5960
"state_test.go",
6061
],
62+
data = glob(["testdata/**"]),
6163
embed = [":state"],
6264
deps = [
6365
"//pkg/kv/kvpb",
@@ -66,7 +68,9 @@ go_test(
6668
"//pkg/kv/kvserver/liveness/livenesspb",
6769
"//pkg/kv/kvserver/load",
6870
"//pkg/roachpb",
71+
"//pkg/testutils/datapathutils",
6972
"//pkg/util/hlc",
73+
"@com_github_cockroachdb_datadriven//:datadriven",
7074
"@com_github_stretchr_testify//require",
7175
],
7276
)

pkg/kv/kvserver/asim/state/parser_replica_placement.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import (
1616

1717
// ReplicaPlacement is a list of replica placement ratios. Each ratio represents
1818
// a % of ranges that should be placed using the given store IDs and types. For
19-
// example, a ratio of {s1:*,s2,s3:NON_VOTER}:1 means 1/3 of ranges should be
20-
// placed with replicas on s1, s2, and s3, s1 as the leaseholder, and the replica
21-
// on s3 as a non-voter.
19+
// example, a ratio of {s1:*,s2,s3:NON_VOTER}:1 present in a ReplicaPlacement of
20+
// total weight 3 means 1/3 of ranges should be placed with replicas on s1, s2,
21+
// and s3, s1 as the leaseholder, and the replica on s3 as a non-voter.
22+
//
23+
// See TestReplicaPlacement for more examples.
2224
type ReplicaPlacement []Ratio
2325

2426
// findReplicaPlacementForEveryStoreSet finds the replica placement for every
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package state
7+
8+
import (
9+
"testing"
10+
11+
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
12+
"github.com/cockroachdb/datadriven"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
// TestReplicaPlacement tests the ReplicaPlacement parser.
17+
func TestReplicaPlacement(t *testing.T) {
18+
datadriven.Walk(t, datapathutils.TestDataPath(t, t.Name()), func(t *testing.T, path string) {
19+
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string {
20+
require.Equal(t, "parse", d.Cmd)
21+
rp := ParseStoreWeights(d.Input)
22+
rp.findReplicaPlacementForEveryStoreSet(1000)
23+
return rp.String()
24+
})
25+
})
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This test tests ReplicaPlacement parser which is used for
2+
# placement_type=replica_placement option for gen_ranges command.
3+
parse
4+
{s3}:1
5+
----
6+
{s3:leaseholder}:1000
7+
8+
parse
9+
{s3}:7
10+
----
11+
{s3:leaseholder}:1000
12+
13+
parse
14+
{s2:*}:100
15+
----
16+
{s2:leaseholder}:1000
17+
18+
parse
19+
{s2:NON_VOTER,s4:*}:100
20+
----
21+
{s2:NON_VOTER,s4:leaseholder}:1000
22+
23+
parse
24+
{s2:NON_VOTER,s4:*, s1}:100
25+
----
26+
{s2:NON_VOTER,s4:leaseholder,s1}:1000
27+
28+
parse
29+
{s1:*,s2,s3:NON_VOTER}:1 {s4:*,s5,s6}:1
30+
----
31+
{s1:leaseholder,s2,s3:NON_VOTER}:500
32+
{s4:leaseholder,s5,s6}:500
33+
34+
parse
35+
{s1:NON_VOTER,s2,s3}:10 {s4,s5:*,s6}:10 {s7,s2}:1
36+
----
37+
{s1:NON_VOTER:leaseholder,s2,s3}:477
38+
{s4,s5:leaseholder,s6}:476
39+
{s7:leaseholder,s2}:47

0 commit comments

Comments
 (0)