Skip to content

Commit c24cb04

Browse files
committed
asim: trace mma.ComputeChanges and save to generated artifacts
1 parent d276ed6 commit c24cb04

File tree

8 files changed

+84
-23
lines changed

8 files changed

+84
-23
lines changed

pkg/kv/kvserver/asim/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ go_library(
1818
"//pkg/kv/kvserver/asim/storerebalancer",
1919
"//pkg/kv/kvserver/asim/workload",
2020
"//pkg/util/log",
21+
"//pkg/util/tracing",
22+
"//pkg/util/tracing/tracingpb",
2123
],
2224
)
2325

pkg/kv/kvserver/asim/asim.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ import (
2121
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/storerebalancer"
2222
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/workload"
2323
"github.com/cockroachdb/cockroach/pkg/util/log"
24+
"github.com/cockroachdb/cockroach/pkg/util/tracing"
25+
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
2426
)
2527

2628
// Simulator simulates an entire cluster, and runs the allocator of each store
2729
// in that cluster.
2830
type Simulator struct {
2931
log.AmbientContext
32+
onRecording func(storeID state.StoreID, rec tracingpb.Recording)
33+
3034
curr time.Time
3135
end time.Time
3236
// interval is the step between ticks for active simulaton components, such
@@ -96,22 +100,27 @@ func NewSimulator(
96100

97101
s := &Simulator{
98102
AmbientContext: log.MakeTestingAmbientCtxWithNewTracer(),
99-
curr: settings.StartTime,
100-
end: settings.StartTime.Add(duration),
101-
interval: settings.TickInterval,
102-
generators: wgs,
103-
state: initialState,
104-
changer: changer,
105-
rqs: rqs,
106-
lqs: lqs,
107-
sqs: sqs,
108-
controllers: controllers,
109-
srs: srs,
110-
mmSRs: mmSRs,
111-
pacers: pacers,
112-
gossip: gossip.NewGossip(initialState, settings),
113-
metrics: m,
114-
shuffler: state.NewShuffler(settings.Seed),
103+
onRecording: func(storeID state.StoreID, rec tracingpb.Recording) {
104+
if fn := settings.OnRecording; fn != nil {
105+
fn(int64(storeID), rec)
106+
}
107+
},
108+
curr: settings.StartTime,
109+
end: settings.StartTime.Add(duration),
110+
interval: settings.TickInterval,
111+
generators: wgs,
112+
state: initialState,
113+
changer: changer,
114+
rqs: rqs,
115+
lqs: lqs,
116+
sqs: sqs,
117+
controllers: controllers,
118+
srs: srs,
119+
mmSRs: mmSRs,
120+
pacers: pacers,
121+
gossip: gossip.NewGossip(initialState, settings),
122+
metrics: m,
123+
shuffler: state.NewShuffler(settings.Seed),
115124
// TODO(kvoli): Keeping the state around is a bit hacky, find a better
116125
// method of reporting the ranges.
117126
history: history.History{Recorded: [][]metrics.StoreMetrics{}, S: initialState},
@@ -380,7 +389,14 @@ func (s *Simulator) tickMMStoreRebalancers(ctx context.Context, tick time.Time,
380389
stores := s.state.Stores()
381390
s.shuffler(len(stores), func(i, j int) { stores[i], stores[j] = stores[j], stores[i] })
382391
for _, store := range stores {
392+
var finishAndGetRecording func() tracingpb.Recording
393+
if s.onRecording != nil {
394+
ctx, finishAndGetRecording = tracing.ContextWithRecordingSpan(ctx, s.Tracer, "mma.ComputeChanges")
395+
}
383396
s.mmSRs[store.StoreID()].Tick(ctx, tick, state)
397+
if finishAndGetRecording != nil {
398+
s.onRecording(store.StoreID(), finishAndGetRecording())
399+
}
384400
}
385401
}
386402

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ go_library(
55
srcs = ["settings.go"],
66
importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/config",
77
visibility = ["//visibility:public"],
8-
deps = ["//pkg/settings/cluster"],
8+
deps = [
9+
"//pkg/settings/cluster",
10+
"//pkg/util/tracing/tracingpb",
11+
],
912
)

pkg/kv/kvserver/asim/config/settings.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
12+
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
1213
)
1314

1415
const (
@@ -116,6 +117,9 @@ type SimulationSettings struct {
116117
// TODO(wenyihu6): Remove any non-simulation settings from this struct and
117118
// instead override the settings below.
118119
ST *cluster.Settings
120+
// OnRecording is called with trace spans obtained by recording the allocator.
121+
// NB: we can't use state.StoreID here since that causes an import cycle.
122+
OnRecording func(storeID int64, rec tracingpb.Recording)
119123
}
120124

121125
// DefaultSimulationSettings returns a set of default settings for simulation.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ go_test(
5858
"//pkg/util/humanizeutil",
5959
"//pkg/util/leaktest",
6060
"//pkg/util/log",
61+
"//pkg/util/tracing/tracingpb",
6162
"@com_github_cockroachdb_datadriven//:datadriven",
6263
"@com_github_cockroachdb_logtags//:logtags",
64+
"@com_github_stretchr_testify//assert",
6365
"@com_github_stretchr_testify//require",
6466
],
6567
)

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"math/rand"
1414
"os"
1515
"path/filepath"
16+
"regexp"
1617
"strings"
1718
"testing"
1819
"time"
@@ -35,8 +36,10 @@ import (
3536
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
3637
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
3738
"github.com/cockroachdb/cockroach/pkg/util/log"
39+
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
3840
"github.com/cockroachdb/datadriven"
3941
"github.com/cockroachdb/logtags"
42+
"github.com/stretchr/testify/assert"
4043
"github.com/stretchr/testify/require"
4144
)
4245

@@ -165,6 +168,12 @@ var runAsimTests = envutil.EnvOrDefaultBool("COCKROACH_RUN_ASIM_TESTS", false)
165168
// random number generator that creates the seed used to generate each
166169
// simulation sample. The default values are: duration=30m (30 minutes)
167170
// samples=1 seed=random.
171+
//
172+
// To run all tests and rewrite the testdata files as well as generate the
173+
// artifacts in `testdata/generated`, you can use:
174+
/*
175+
./dev test pkg/kv/kvserver/asim/tests --ignore-cache --rewrite -v -f TestDataDriven -- --test_env COCKROACH_RUN_ASIM_TESTS=true --test_env COCKROACH_ALWAYS_KEEP_TEST_LOGS=true
176+
*/
168177
func TestDataDriven(t *testing.T) {
169178
skip.UnderDuressWithIssue(t, 149875)
170179
leakTestAfter := leaktest.AfterTest(t)
@@ -531,7 +540,34 @@ func TestDataDriven(t *testing.T) {
531540
require.NotNil(t, set, "unknown mode value: %s", mv)
532541
set(&eventGen)
533542

543+
// TODO(tbg): need to decide whether multiple evals in a single file
544+
// is a feature or an anti-pattern. If it's a feature, we should let
545+
// the `name` part below be adjustable (but not the plotDir) via a
546+
// parameter to the `eval` command.
547+
testName := name + "_" + mv
548+
534549
for sample := 0; sample < samples; sample++ {
550+
recIdx := map[int64]int{}
551+
settingsGen.Settings.OnRecording = func(storeID int64, rec tracingpb.Recording) {
552+
if !rewrite || len(rec[0].Logs) == 0 {
553+
return
554+
}
555+
traceDir := filepath.Join(plotDir, "traces", fmt.Sprintf("s%d", storeID))
556+
if recIdx[storeID] == 0 {
557+
require.NoError(t, os.MkdirAll(traceDir, 0755))
558+
}
559+
re := regexp.MustCompile(`[^a-zA-Z0-9]+`)
560+
outName := fmt.Sprintf("%s_%s_s%d", mv, re.ReplaceAllString(rec[0].Operation, "_"), storeID)
561+
if sample > 0 {
562+
outName += fmt.Sprintf("_sample%d", sample+1)
563+
}
564+
outName += "_" + fmt.Sprintf("%03d.txt", recIdx[storeID])
565+
assert.NoError(t, os.WriteFile(
566+
filepath.Join(traceDir, outName),
567+
[]byte(rec.String()), 0644))
568+
recIdx[storeID] += 1
569+
}
570+
535571
assertionFailures := []string{}
536572
var tmpStrB *strings.Builder = nil
537573
if stateStrForOnce == "" {
@@ -561,11 +597,7 @@ func TestDataDriven(t *testing.T) {
561597
// Generate artifacts. Hash artifact input data to ensure they are
562598
// up to date.
563599
hasher := fnv.New64a()
564-
// TODO(tbg): need to decide whether multiple evals in a single file
565-
// is a feature or an anti-pattern. If it's a feature, we should let
566-
// the `name` part below be adjustable (but not the plotDir) via a
567-
// parameter to the `eval` command.
568-
testName := name + "_" + mv
600+
569601
for sample, h := range run.hs {
570602
printStatsAndGenerateJSON(t, &buf, h, testName, sample+1, plotDir, hasher, rewrite,
571603
settingsGen.Settings.TickInterval, metricsMap)

pkg/kv/kvserver/asim/tests/testdata/generated/example_rebalancing/example_rebalancing_setup.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ Event
1313
Workload Set Up
1414
[1,10000): 95%r large-block [128-256B/op, 7000ops/s]
1515
Changed Settings
16-
StateExchangeDelay: 20s (default: 500ms)
16+
StateExchangeDelay: 20s (default: 500ms)
17+
OnRecording: 0x10487ef50 (default: <nil>)

pkg/kv/kvserver/asim/tests/testdata/non_rand/mma_skewed_cpu_skewed_write.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@ Workload Set Up
7373
[10001,20000): write-only large-block [0.00cpu-us/write(raft), 1000B/op, 20000ops/s]
7474
Changed Settings
7575
SplitQueueEnabled: false (default: true)
76+
OnRecording: 0x10487ef50 (default: <nil>)
7677
==========================

0 commit comments

Comments
 (0)