From 7a6867c5ece6382c8309e5f116981030f7f42afb Mon Sep 17 00:00:00 2001 From: wenyihu6 Date: Mon, 17 Jul 2023 22:59:01 -0400 Subject: [PATCH] asim: add random predefined cluster config selection This patch takes the first step towards a randomized framework by enabling asim testing to randomly select a cluster information configuration from a set of predefined choices. These choices are hardcoded and represent common cluster configurations. TestRandomized now takes in `true` for randOptions.cluster to enable random cluster config selection. This provides two modes for cluster generation: 1. Default: currently set to 3 nodes and 1 store per node 2. Random: randomly select a predefined cluster info Part of: https://github.com/cockroachdb/cockroach/issues/106311 Release note: None --- pkg/kv/kvserver/asim/tests/BUILD.bazel | 1 + pkg/kv/kvserver/asim/tests/rand_framework.go | 2 +- pkg/kv/kvserver/asim/tests/rand_gen.go | 25 ++++++++++++++++++++ pkg/kv/kvserver/asim/tests/rand_test.go | 2 +- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 pkg/kv/kvserver/asim/tests/rand_gen.go diff --git a/pkg/kv/kvserver/asim/tests/BUILD.bazel b/pkg/kv/kvserver/asim/tests/BUILD.bazel index f84a355a05cf..6f9bd09ebd65 100644 --- a/pkg/kv/kvserver/asim/tests/BUILD.bazel +++ b/pkg/kv/kvserver/asim/tests/BUILD.bazel @@ -36,6 +36,7 @@ go_library( "assert.go", "default_settings.go", "rand_framework.go", + "rand_gen.go", ], importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/tests", visibility = ["//visibility:public"], diff --git a/pkg/kv/kvserver/asim/tests/rand_framework.go b/pkg/kv/kvserver/asim/tests/rand_framework.go index cb40c85a5b63..4b2d926fd8c4 100644 --- a/pkg/kv/kvserver/asim/tests/rand_framework.go +++ b/pkg/kv/kvserver/asim/tests/rand_framework.go @@ -50,7 +50,7 @@ func (f randTestingFramework) getCluster() gen.ClusterGen { if !f.s.randOptions.cluster { return defaultBasicClusterGen() } - return gen.BasicCluster{} + return f.randomClusterInfoGen(f.s.randSource) } func (f randTestingFramework) getRanges() gen.RangeGen { diff --git a/pkg/kv/kvserver/asim/tests/rand_gen.go b/pkg/kv/kvserver/asim/tests/rand_gen.go new file mode 100644 index 000000000000..2c4111dc13e9 --- /dev/null +++ b/pkg/kv/kvserver/asim/tests/rand_gen.go @@ -0,0 +1,25 @@ +// Copyright 2023 The Cockroach Authors. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +package tests + +import ( + "math/rand" + + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/gen" + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/state" +) + +// randomClusterInfoGen returns a randomly picked predefined configuration. +func (f randTestingFramework) randomClusterInfoGen(randSource *rand.Rand) gen.LoadedCluster { + chosenIndex := randSource.Intn(len(state.ClusterOptions)) + chosenType := state.ClusterOptions[chosenIndex] + return loadClusterInfo(chosenType) +} diff --git a/pkg/kv/kvserver/asim/tests/rand_test.go b/pkg/kv/kvserver/asim/tests/rand_test.go index 2081d2098395..e47e155b722c 100644 --- a/pkg/kv/kvserver/asim/tests/rand_test.go +++ b/pkg/kv/kvserver/asim/tests/rand_test.go @@ -70,7 +70,7 @@ func defaultSettings(randOptions testRandOptions) testSettings { // default func TestRandomized(t *testing.T) { randOptions := testRandOptions{ - cluster: false, + cluster: true, ranges: false, load: false, staticSettings: false,