Skip to content

Commit 81ea4fa

Browse files
wenyihu6tbg
authored andcommitted
asim: support region and nodes_per_region with gen_cluster
Previously, gen_cluster did not support specifying node localities, making constraint testing difficult; users had to add nodes manually with add_node. This commit adds region and nodes_per_region options to gen_cluster. Note taht len(region) == len(nodes_per_region). For example, region=(a,b,c) and nodes_per_region=(1,1,2) assigns 1 node to a, 1 to b, and 2 to c. No new tests are added yet, but future commits will utilize this feature. Epic: none Release note: none
1 parent 207c442 commit 81ea4fa

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

pkg/kv/kvserver/asim/gen/generator.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package gen
88
import (
99
"fmt"
1010
"math/rand"
11+
"strings"
1112
"time"
1213

1314
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim"
@@ -190,26 +191,49 @@ type BasicCluster struct {
190191
Nodes int
191192
StoresPerNode int
192193
StoreByteCapacity int64
194+
Region []string
195+
NodesPerRegion []int
193196
}
194197

195198
func (bc BasicCluster) String() string {
196-
return fmt.Sprintf("basic cluster with nodes=%d, stores_per_node=%d, store_byte_capacity=%d",
199+
var b strings.Builder
200+
_, _ = fmt.Fprintf(&b, "basic cluster with nodes=%d, stores_per_node=%d, store_byte_capacity=%d",
197201
bc.Nodes, bc.StoresPerNode, bc.StoreByteCapacity)
202+
if len(bc.Region) != 0 {
203+
_, _ = fmt.Fprintf(&b, ", region=%v, nodes_per_region=%v", bc.Region, bc.NodesPerRegion)
204+
}
205+
return b.String()
198206
}
199207

200208
// Generate returns a new simulator state, where the cluster is created with all
201209
// nodes having the same locality and with the specified number of stores/nodes
202210
// created. The cluster is created based on the stores and stores-per-node
203211
// values the basic cluster generator is created with.
204212
func (bc BasicCluster) Generate(seed int64, settings *config.SimulationSettings) state.State {
205-
info := state.ClusterInfoWithStoreCount(bc.Nodes, bc.StoresPerNode)
213+
info := bc.info()
206214
info.StoreDiskCapacityBytes = bc.StoreByteCapacity
207215
return state.LoadClusterInfo(info, settings)
208216
}
209217

210218
func (bc BasicCluster) Regions() []state.Region {
211-
info := state.ClusterInfoWithStoreCount(bc.Nodes, bc.StoresPerNode)
212-
return info.Regions
219+
return bc.info().Regions
220+
}
221+
222+
func (bc BasicCluster) info() state.ClusterInfo {
223+
if len(bc.Region) == 0 {
224+
return state.ClusterInfoWithStoreCount(bc.Nodes, bc.StoresPerNode)
225+
}
226+
227+
regionNodeWeights := make([]float64, len(bc.NodesPerRegion))
228+
totalNodes := 0
229+
for i, nodes := range bc.NodesPerRegion {
230+
regionNodeWeights[i] = float64(nodes) / float64(bc.Nodes)
231+
totalNodes += nodes
232+
}
233+
if totalNodes != bc.Nodes {
234+
panic(fmt.Sprintf("total nodes %d does not match expected nodes %d", totalNodes, bc.Nodes))
235+
}
236+
return state.ClusterInfoWithDistribution(bc.Nodes, bc.StoresPerNode, bc.Region, regionNodeWeights)
213237
}
214238

215239
// LoadedRanges implements the RangeGen interface.

pkg/kv/kvserver/asim/tests/datadriven_simulation_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,19 @@ func TestDataDriven(t *testing.T) {
258258
var nodes = 3
259259
var storesPerNode = 1
260260
var storeByteCapacity int64 = 256 << 30 /* 256 GiB */
261+
var region []string
262+
var nodesPerRegion []int
261263
scanIfExists(t, d, "nodes", &nodes)
262264
scanIfExists(t, d, "stores_per_node", &storesPerNode)
263265
scanIfExists(t, d, "store_byte_capacity", &storeByteCapacity)
266+
scanIfExists(t, d, "region", &region)
267+
scanIfExists(t, d, "nodes_per_region", &nodesPerRegion)
264268
clusterGen = gen.BasicCluster{
265269
Nodes: nodes,
266270
StoresPerNode: storesPerNode,
267271
StoreByteCapacity: storeByteCapacity,
272+
Region: region,
273+
NodesPerRegion: nodesPerRegion,
268274
}
269275
return ""
270276
case "load_cluster":

pkg/kv/kvserver/asim/tests/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func getNodeLivenessStatus(s string) livenesspb.NodeLivenessStatus {
4141
func scanArg(t *testing.T, d *datadriven.TestData, key string, dest interface{}) {
4242
var tmp string
4343
switch dest := dest.(type) {
44-
case *string, *int, *int64, *uint64, *bool, *time.Duration, *float64, *[]int, *[]float64:
44+
case *string, *int, *int64, *uint64, *bool, *time.Duration, *float64, *[]int, *[]float64, *[]string:
4545
d.ScanArgs(t, key, dest)
4646
case *OutputFlags:
4747
var flagsTmp []string

pkg/kv/kvserver/asim/tests/testdata/non_rand/decommission_conformance

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,7 @@
44
# constraint conjunction currently satisfied by the decommissioning replica
55
# being replaced, it was possible that a valid replacement target would never
66
# be selected.
7-
gen_cluster nodes=4
8-
----
9-
10-
set_locality node=1 locality=region=a
11-
----
12-
13-
set_locality node=2 locality=region=b
14-
----
15-
16-
set_locality node=3 locality=region=c
17-
----
18-
19-
set_locality node=4 locality=region=c
7+
gen_cluster nodes=4 region=(a,b,c) nodes_per_region=(1,1,2)
208
----
219

2210
# Generate 5 ranges, where initially there will be two replicas in region c and
@@ -45,10 +33,13 @@ OK
4533
topology
4634
----
4735
a
48-
└── [1]
36+
a_1
37+
│ └── [1]
4938
b
50-
└── [2]
39+
b_1
40+
│ └── [2]
5141
c
52-
└── [3 4]
42+
c_1
43+
└── [3 4]
5344

5445
# vim:ft=sh

0 commit comments

Comments
 (0)