diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 3e740d3913c4..d2cb4a1cba6e 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -1305,6 +1305,7 @@ GO_TARGETS = [ "//pkg/kv/kvserver/asim/gen:gen", "//pkg/kv/kvserver/asim/gossip:gossip", "//pkg/kv/kvserver/asim/gossip:gossip_test", + "//pkg/kv/kvserver/asim/history:history", "//pkg/kv/kvserver/asim/metrics:metrics", "//pkg/kv/kvserver/asim/metrics:metrics_test", "//pkg/kv/kvserver/asim/op:op", diff --git a/pkg/kv/kvserver/asim/BUILD.bazel b/pkg/kv/kvserver/asim/BUILD.bazel index ec6c8e127985..68690594c0f7 100644 --- a/pkg/kv/kvserver/asim/BUILD.bazel +++ b/pkg/kv/kvserver/asim/BUILD.bazel @@ -9,6 +9,7 @@ go_library( "//pkg/kv/kvserver/asim/config", "//pkg/kv/kvserver/asim/event", "//pkg/kv/kvserver/asim/gossip", + "//pkg/kv/kvserver/asim/history", "//pkg/kv/kvserver/asim/metrics", "//pkg/kv/kvserver/asim/op", "//pkg/kv/kvserver/asim/queue", @@ -26,6 +27,7 @@ go_test( deps = [ ":asim", "//pkg/kv/kvserver/asim/config", + "//pkg/kv/kvserver/asim/history", "//pkg/kv/kvserver/asim/metrics", "//pkg/kv/kvserver/asim/state", "//pkg/kv/kvserver/asim/workload", diff --git a/pkg/kv/kvserver/asim/asim.go b/pkg/kv/kvserver/asim/asim.go index 4156ece3d903..9126ae915bb0 100644 --- a/pkg/kv/kvserver/asim/asim.go +++ b/pkg/kv/kvserver/asim/asim.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/config" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/event" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/gossip" + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/history" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/metrics" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/op" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/queue" @@ -60,7 +61,7 @@ type Simulator struct { settings *config.SimulationSettings metrics *metrics.Tracker - history History + history history.History } func (s *Simulator) Curr() time.Time { @@ -71,19 +72,6 @@ func (s *Simulator) State() state.State { return s.state } -// History contains recorded information that summarizes a simulation run. -// Currently it only contains the store metrics of the run. -// TODO(kvoli): Add a range log like structure to the history. -type History struct { - Recorded [][]metrics.StoreMetrics - S state.State -} - -// Listen implements the metrics.StoreMetricListener interface. -func (h *History) Listen(ctx context.Context, sms []metrics.StoreMetrics) { - h.Recorded = append(h.Recorded, sms) -} - // NewSimulator constructs a valid Simulator. func NewSimulator( duration time.Duration, @@ -118,7 +106,7 @@ func NewSimulator( shuffler: state.NewShuffler(settings.Seed), // TODO(kvoli): Keeping the state around is a bit hacky, find a better // method of reporting the ranges. - history: History{Recorded: [][]metrics.StoreMetrics{}, S: initialState}, + history: history.History{Recorded: [][]metrics.StoreMetrics{}, S: initialState}, events: events, settings: settings, } @@ -190,7 +178,7 @@ func (s *Simulator) GetNextTickTime() (done bool, tick time.Time) { // History returns the current recorded history of a simulation run. Calling // this on a Simulator that has not begun will return an empty history. -func (s *Simulator) History() History { +func (s *Simulator) History() history.History { return s.history } diff --git a/pkg/kv/kvserver/asim/asim_test.go b/pkg/kv/kvserver/asim/asim_test.go index adda0bfb3c28..5b373495100f 100644 --- a/pkg/kv/kvserver/asim/asim_test.go +++ b/pkg/kv/kvserver/asim/asim_test.go @@ -18,6 +18,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/config" + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/history" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/metrics" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/state" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/workload" @@ -57,7 +58,7 @@ func TestAsimDeterministic(t *testing.T) { // be larger than 3 keys per range. keyspace := 3 * ranges // Track the run to compare against for determinism. - var refRun asim.History + var refRun history.History for run := 0; run < runs; run++ { rwg := make([]workload.Generator, 1) diff --git a/pkg/kv/kvserver/asim/history/BUILD.bazel b/pkg/kv/kvserver/asim/history/BUILD.bazel new file mode 100644 index 000000000000..779e74282ad8 --- /dev/null +++ b/pkg/kv/kvserver/asim/history/BUILD.bazel @@ -0,0 +1,12 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "history", + srcs = ["history.go"], + importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/history", + visibility = ["//visibility:public"], + deps = [ + "//pkg/kv/kvserver/asim/metrics", + "//pkg/kv/kvserver/asim/state", + ], +) diff --git a/pkg/kv/kvserver/asim/history/history.go b/pkg/kv/kvserver/asim/history/history.go new file mode 100644 index 000000000000..ed5a52451cb0 --- /dev/null +++ b/pkg/kv/kvserver/asim/history/history.go @@ -0,0 +1,31 @@ +// Copyright 2022 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 history + +import ( + "context" + + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/metrics" + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/state" +) + +// History contains recorded information that summarizes a simulation run. +// Currently it only contains the store metrics of the run. +// TODO(kvoli): Add a range log like structure to the history. +type History struct { + Recorded [][]metrics.StoreMetrics + S state.State +} + +// Listen implements the metrics.StoreMetricListener interface. +func (h *History) Listen(ctx context.Context, sms []metrics.StoreMetrics) { + h.Recorded = append(h.Recorded, sms) +} diff --git a/pkg/kv/kvserver/asim/tests/BUILD.bazel b/pkg/kv/kvserver/asim/tests/BUILD.bazel index c5585ac9b335..73fd8a3ccfac 100644 --- a/pkg/kv/kvserver/asim/tests/BUILD.bazel +++ b/pkg/kv/kvserver/asim/tests/BUILD.bazel @@ -12,10 +12,10 @@ go_test( embed = [":tests"], deps = [ "//pkg/kv/kvserver/allocator/allocatorimpl", - "//pkg/kv/kvserver/asim", "//pkg/kv/kvserver/asim/config", "//pkg/kv/kvserver/asim/event", "//pkg/kv/kvserver/asim/gen", + "//pkg/kv/kvserver/asim/history", "//pkg/kv/kvserver/asim/metrics", "//pkg/kv/kvserver/asim/state", "//pkg/kv/kvserver/liveness/livenesspb", @@ -41,10 +41,10 @@ go_library( importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/tests", visibility = ["//visibility:public"], deps = [ - "//pkg/kv/kvserver/asim", "//pkg/kv/kvserver/asim/config", "//pkg/kv/kvserver/asim/event", "//pkg/kv/kvserver/asim/gen", + "//pkg/kv/kvserver/asim/history", "//pkg/kv/kvserver/asim/metrics", "//pkg/kv/kvserver/asim/state", "//pkg/roachpb", diff --git a/pkg/kv/kvserver/asim/tests/assert.go b/pkg/kv/kvserver/asim/tests/assert.go index a0f2a527312b..d75d20ef7d02 100644 --- a/pkg/kv/kvserver/asim/tests/assert.go +++ b/pkg/kv/kvserver/asim/tests/assert.go @@ -16,7 +16,7 @@ import ( "math" "strings" - "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim" + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/history" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/metrics" "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigtestutils" @@ -83,7 +83,7 @@ type SimulationAssertion interface { // Assert looks at a simulation run history and returns true if the // assertion holds and false if not. When the assertion does not hold, the // reason is also returned. - Assert(context.Context, asim.History) (holds bool, reason string) + Assert(context.Context, history.History) (holds bool, reason string) // String returns the string representation of the assertion. String() string } @@ -107,7 +107,7 @@ type steadyStateAssertion struct { // assertion tick. If violated, holds is returned as false along with the // reason. func (sa steadyStateAssertion) Assert( - ctx context.Context, h asim.History, + ctx context.Context, h history.History, ) (holds bool, reason string) { m := h.Recorded ticks := len(m) @@ -193,7 +193,9 @@ type balanceAssertion struct { // stat's maximum/mean (over all stores) in the cluster meets the threshold // constraint at each assertion tick. If violated, holds is returned as false // along with the reason. -func (ba balanceAssertion) Assert(ctx context.Context, h asim.History) (holds bool, reason string) { +func (ba balanceAssertion) Assert( + ctx context.Context, h history.History, +) (holds bool, reason string) { m := h.Recorded ticks := len(m) if ba.ticks > ticks { @@ -255,7 +257,7 @@ type storeStatAssertion struct { // assertion holds and false if not. When the assertion does not hold, the // reason is also returned. func (sa storeStatAssertion) Assert( - ctx context.Context, h asim.History, + ctx context.Context, h history.History, ) (holds bool, reason string) { m := h.Recorded ticks := len(m) @@ -313,7 +315,7 @@ const conformanceAssertionSentinel = -1 // assertion holds and false if not. When the assertion does not hold, the // reason is also returned. func (ca conformanceAssertion) Assert( - ctx context.Context, h asim.History, + ctx context.Context, h history.History, ) (holds bool, reason string) { report := h.S.Report() buf := strings.Builder{} diff --git a/pkg/kv/kvserver/asim/tests/datadriven_simulation_test.go b/pkg/kv/kvserver/asim/tests/datadriven_simulation_test.go index 6fccd97d3e6a..5f31d77833f5 100644 --- a/pkg/kv/kvserver/asim/tests/datadriven_simulation_test.go +++ b/pkg/kv/kvserver/asim/tests/datadriven_simulation_test.go @@ -19,10 +19,10 @@ import ( "time" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/allocator/allocatorimpl" - "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/config" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/event" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/gen" + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/history" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/metrics" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/state" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb" @@ -171,7 +171,7 @@ func TestDataDriven(t *testing.T) { settingsGen := gen.StaticSettings{Settings: config.DefaultSimulationSettings()} eventGen := gen.StaticEvents{DelayedEvents: event.DelayedEventList{}} assertions := []SimulationAssertion{} - runs := []asim.History{} + runs := []history.History{} datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string { switch d.Cmd { case "gen_load": diff --git a/pkg/kv/kvserver/asim/tests/rand_framework.go b/pkg/kv/kvserver/asim/tests/rand_framework.go index c78326a3c47b..2437d1b6cc97 100644 --- a/pkg/kv/kvserver/asim/tests/rand_framework.go +++ b/pkg/kv/kvserver/asim/tests/rand_framework.go @@ -18,8 +18,8 @@ import ( "strings" "time" - "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/gen" + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/history" "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/state" ) @@ -167,7 +167,7 @@ func loadClusterInfo(configName string) gen.LoadedCluster { // checkAssertions checks the given history and assertions, returning (bool, // reason) indicating any failures and reasons if any assertions fail. func checkAssertions( - ctx context.Context, history asim.History, assertions []SimulationAssertion, + ctx context.Context, history history.History, assertions []SimulationAssertion, ) (bool, string) { assertionFailures := []string{} failureExists := false