Skip to content

Commit 2b99f4e

Browse files
craig[bot]wenyihu6
andcommitted
Merge #153545
153545: asim: output test set up if eval has full = true r=tbg a=wenyihu6 Resolves: #153572 Release note: none --- **asim: add a separator line after each run** This commit updates asim output to add a separate line after each simulation run to improve readability. --- **asim: export DefaultStartTime** This commit exports the simulator’s default start time, which will be used in future commits to compare against scheduled event timestamps in the scheduler package, ensuring timestamp is printed only when not at the start time. --- **asim: move some variables to an outer scope** This commit moves certain variables above the configuration declarations so they can be reused in an outer scope. --- **asim: generate {TESTNAME}_setup.txt** This commit adds a test setup string which will be outputted to {TESTNAME}_setup.txt. Only the first configuration and first sample are emitted, since the setup should effectively be the same across runs (other than LBRebalancingMode) and duplicating it would add unnecessary runtime churn. One downside is that the scheduled event for LBRebalancingMode will only reflect the first occurrence. If a datadriven test contains multiple evals, the last one wins. --- **asim: remove run.stateStrAcrossSamples append** This commit removes an unused run.stateStrAcrossSamples append. I left the field itself in place in the struct since it causes no harm, though we can consider deleting it entirely. --- **asim: print simulation setting as part of * _setup.txt** This commit includes simulation settings in the setup output. Only values that differ from the default simulation settings are printed. Note that cluster settings and the seed are omitted. The simulation seed is derived from rand.New(rand.NewSource(42)).Int63() by default, while the default simulation setting itself uses 42. Note that we rely on users to explicitly reset cluster settings between runs. It isn’t an issue now, but it may become a potential footgun in the future. --- **asim: print store info to * _setup.txt** This commit outputs store capacity and store attributes t.o the setup file. --- **asim: output test set up if eval has full = true** This commit adds an optional parameter to eval that outputs the test setup. By default, the setup is only written to the generated folder as {TESTNAME}_setup.txt. Co-authored-by: wenyihu6 <[email protected]>
2 parents d8e21ad + a7fe80d commit 2b99f4e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+771
-364
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ const (
3333
)
3434

3535
var (
36-
// defaultStartTime is used as the default beginning time for simulation
36+
// DefaultStartTime is used as the default beginning time for simulation
3737
// runs. It isn't necessarily meaningful other than for logging and having
3838
// "some" start time for components taking a time.Time.
39-
defaultStartTime = time.Date(2022, 03, 21, 11, 0, 0, 0, time.UTC)
39+
DefaultStartTime = time.Date(2022, 03, 21, 11, 0, 0, 0, time.UTC)
4040
)
4141

4242
// SimulationSettings controls
@@ -116,7 +116,7 @@ type SimulationSettings struct {
116116
// DefaultSimulationSettings returns a set of default settings for simulation.
117117
func DefaultSimulationSettings() *SimulationSettings {
118118
return &SimulationSettings{
119-
StartTime: defaultStartTime,
119+
StartTime: DefaultStartTime,
120120
TickInterval: defaultTickInteval,
121121
MetricsInterval: defaultMetricsInterval,
122122
Seed: defaultSeed,

pkg/kv/kvserver/asim/event/mutation_event.go

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package event
88
import (
99
"context"
1010
"fmt"
11+
"strings"
1112

1213
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/state"
1314
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb"
@@ -73,7 +74,51 @@ func (se SetSpanConfigEvent) Func() EventFunc {
7374
}
7475

7576
func (se SetSpanConfigEvent) String() string {
76-
return fmt.Sprintf("set span config event with span=%v, config=%v", se.Span, &se.Config)
77+
var buf strings.Builder
78+
voter, nonvoter := se.Config.GetNumVoters(), se.Config.GetNumNonVoters()
79+
var nonvoterStr string
80+
if nonvoter != 0 {
81+
nonvoterStr = fmt.Sprintf(",%dnonvoters", nonvoter)
82+
}
83+
_, _ = fmt.Fprintf(&buf, "[%s,%s): %dvoters%s", se.Span.Key, se.Span.EndKey, voter, nonvoterStr)
84+
85+
printConstraint := func(tag string, constraints []roachpb.ConstraintsConjunction) {
86+
if len(constraints) != 0 {
87+
_, _ = fmt.Fprintf(&buf, "%s", tag)
88+
for _, c := range constraints {
89+
_, _ = fmt.Fprintf(&buf, "{%d:", c.NumReplicas)
90+
for j, constraint := range c.Constraints {
91+
_, _ = fmt.Fprintf(&buf, "%s=%s", constraint.Key, constraint.Value)
92+
if j != len(c.Constraints)-1 {
93+
_, _ = fmt.Fprintf(&buf, ",")
94+
}
95+
}
96+
_, _ = fmt.Fprintf(&buf, "}")
97+
}
98+
_, _ = fmt.Fprintf(&buf, "]")
99+
}
100+
}
101+
printConstraint(" [replicas:", se.Config.Constraints)
102+
printConstraint(" [voters:", se.Config.VoterConstraints)
103+
if len(se.Config.LeasePreferences) != 0 {
104+
_, _ = fmt.Fprint(&buf, " [lease:")
105+
for i, lp := range se.Config.LeasePreferences {
106+
_, _ = fmt.Fprint(&buf, "{")
107+
for j, c := range lp.Constraints {
108+
_, _ = fmt.Fprintf(&buf, "%s=%s", c.Key, c.Value)
109+
if j != len(lp.Constraints)-1 {
110+
_, _ = fmt.Fprintf(&buf, ",")
111+
}
112+
}
113+
_, _ = fmt.Fprintf(&buf, "}")
114+
if i != len(se.Config.LeasePreferences)-1 {
115+
_, _ = fmt.Fprintf(&buf, ">")
116+
}
117+
}
118+
_, _ = fmt.Fprint(&buf, "]")
119+
}
120+
noQuotesBuf := strings.ReplaceAll(buf.String(), "\"", "")
121+
return noQuotesBuf
77122
}
78123

79124
func (ae AddNodeEvent) Func() EventFunc {
@@ -95,7 +140,12 @@ func (ae AddNodeEvent) Func() EventFunc {
95140
}
96141

97142
func (ae AddNodeEvent) String() string {
98-
return fmt.Sprintf("add node event with num_of_stores=%d, locality_string=%s", ae.NumStores, ae.LocalityString)
143+
var buf strings.Builder
144+
_, _ = fmt.Fprintf(&buf, "add node with %d stores", ae.NumStores)
145+
if ae.LocalityString != "" {
146+
_, _ = fmt.Fprintf(&buf, ", locality_string=%s", ae.LocalityString)
147+
}
148+
return buf.String()
99149
}
100150

101151
func (sne SetNodeLivenessEvent) Func() EventFunc {
@@ -108,7 +158,7 @@ func (sne SetNodeLivenessEvent) Func() EventFunc {
108158
}
109159

110160
func (sne SetNodeLivenessEvent) String() string {
111-
return fmt.Sprintf("set node liveness event with nodeID=%d, liveness_status=%v", sne.NodeId, sne.LivenessStatus)
161+
return fmt.Sprintf("set n%d to %v", sne.NodeId, sne.LivenessStatus)
112162
}
113163

114164
func (sce SetCapacityOverrideEvent) Func() EventFunc {
@@ -119,7 +169,7 @@ func (sce SetCapacityOverrideEvent) Func() EventFunc {
119169
}
120170

121171
func (sce SetCapacityOverrideEvent) String() string {
122-
return fmt.Sprintf("set capacity override event with storeID=%d, capacity_override=%v", sce.StoreID, sce.CapacityOverride)
172+
return fmt.Sprintf("override s%d capacity to %v", sce.StoreID, sce.CapacityOverride)
123173
}
124174

125175
func (sne SetNodeLocalityEvent) Func() EventFunc {
@@ -150,5 +200,5 @@ func (se SetSimulationSettingsEvent) Func() EventFunc {
150200
}
151201

152202
func (se SetSimulationSettingsEvent) String() string {
153-
return fmt.Sprintf("set simulation settings event with key=%s, value=%v", se.Key, se.Value)
203+
return fmt.Sprintf("set %s to %v", se.Key, se.Value)
154204
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go_library(
55
srcs = [
66
"event_generator.go",
77
"generator.go",
8+
"printer.go",
89
],
910
importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/gen",
1011
visibility = ["//visibility:public"],

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

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type RangeGen interface {
5151
// Generate returns an updated state, given the initial state, seed and
5252
// simulation settings provided. In the updated state, ranges will have been
5353
// created, replicas and leases assigned to stores in the cluster.
54-
Generate(seed int64, settings *config.SimulationSettings, s state.State) state.State
54+
Generate(seed int64, settings *config.SimulationSettings, s state.State) (state.State, string)
5555
String() string
5656
}
5757

@@ -65,11 +65,13 @@ func GenerateSimulation(
6565
settingsGen SettingsGen,
6666
eventGen EventGen,
6767
seed int64,
68+
buf *strings.Builder,
6869
) *asim.Simulator {
6970
settings := settingsGen.Generate(seed)
7071
s := clusterGen.Generate(seed, &settings)
71-
s = rangeGen.Generate(seed, &settings, s)
72+
s, rangeStateStr := rangeGen.Generate(seed, &settings, s)
7273
eventExecutor := eventGen.Generate(seed, &settings)
74+
generateClusterVisualization(buf, s, loadGen, eventGen, rangeStateStr, settings)
7375
return asim.NewSimulator(
7476
duration,
7577
loadGen.Generate(seed, &settings),
@@ -101,11 +103,14 @@ type MultiLoad []BasicLoad
101103
var _ LoadGen = MultiLoad{}
102104

103105
func (ml MultiLoad) String() string {
104-
var str string
105-
for _, load := range ml {
106-
str += fmt.Sprintf("%s\n", load.String())
106+
var buf strings.Builder
107+
for i, load := range ml {
108+
_, _ = fmt.Fprintf(&buf, "%s", load.String())
109+
if i != len(ml)-1 {
110+
_, _ = fmt.Fprintf(&buf, "\n")
111+
}
107112
}
108-
return str
113+
return buf.String()
109114
}
110115

111116
func (ml MultiLoad) Generate(seed int64, settings *config.SimulationSettings) []workload.Generator {
@@ -131,11 +136,36 @@ type BasicLoad struct {
131136
var _ LoadGen = BasicLoad{}
132137

133138
func (bl BasicLoad) String() string {
134-
return fmt.Sprintf(
135-
"basic load with rw_ratio=%0.2f, rate=%0.2f, skewed_access=%t, min_block_size=%d, max_block_size=%d, "+
136-
"min_key=%d, max_key=%d, request_cpu_per_access=%d, raft_cpu_per_write=%d",
137-
bl.RWRatio, bl.Rate, bl.SkewedAccess, bl.MinBlockSize, bl.MaxBlockSize,
138-
bl.MinKey, bl.MaxKey, bl.RequestCPUPerAccess, bl.RaftCPUPerWrite)
139+
var buf strings.Builder
140+
fmt.Fprintf(&buf, "\t[%d,%d): ", bl.MinKey, bl.MaxKey)
141+
if bl.RWRatio == 1 {
142+
_, _ = fmt.Fprint(&buf, "read-only")
143+
} else if bl.RWRatio == 0 {
144+
_, _ = fmt.Fprint(&buf, "write-only")
145+
} else {
146+
_, _ = fmt.Fprintf(&buf, "%d%%r", int(bl.RWRatio*100))
147+
}
148+
if bl.RequestCPUPerAccess > 0 {
149+
_, _ = fmt.Fprint(&buf, " high-cpu")
150+
}
151+
if bl.MinBlockSize > 1 && bl.MaxBlockSize > 1 {
152+
_, _ = fmt.Fprint(&buf, " large-block")
153+
}
154+
_, _ = fmt.Fprint(&buf, " [")
155+
156+
if bl.RequestCPUPerAccess > 0 {
157+
_, _ = fmt.Fprintf(&buf, "%.2fcpu-us/op, ", float64(bl.RequestCPUPerAccess/time.Microsecond.Nanoseconds()))
158+
}
159+
if bl.RaftCPUPerWrite > 0 {
160+
_, _ = fmt.Fprintf(&buf, "%.2fcpu-us/write(raft), ", float64(bl.RaftCPUPerWrite/time.Microsecond.Nanoseconds()))
161+
}
162+
if bl.MinBlockSize == bl.MaxBlockSize {
163+
_, _ = fmt.Fprintf(&buf, "%dB/op, ", bl.MinBlockSize)
164+
} else {
165+
_, _ = fmt.Fprintf(&buf, "%d-%dB/op, ", bl.MinBlockSize, bl.MaxBlockSize)
166+
}
167+
fmt.Fprintf(&buf, "%gops/s]", bl.Rate)
168+
return buf.String()
139169
}
140170

141171
// Generate returns a new list of workload generators where the generator
@@ -184,7 +214,7 @@ func (lc LoadedCluster) Generate(seed int64, settings *config.SimulationSettings
184214
}
185215

186216
func (lc LoadedCluster) String() string {
187-
return fmt.Sprintf("loaded cluster with\n %v", lc.Info)
217+
return fmt.Sprintf("cluster: %s", lc.Info.String())
188218
}
189219

190220
func (lc LoadedCluster) Regions() []state.Region {
@@ -204,11 +234,12 @@ type BasicCluster struct {
204234
func (bc BasicCluster) String() string {
205235
var b strings.Builder
206236
_, _ = fmt.Fprintf(&b,
207-
"basic cluster with nodes=%d, stores_per_node=%d, store_byte_capacity=%d, node_cpu_rate_capacity=%d",
208-
bc.Nodes, bc.StoresPerNode, bc.StoreByteCapacity, bc.NodeCPURateCapacity)
237+
"[nodes: %d, stores_per_node:%d, store_disk_capacity: %dGiB, node_capacity: %dcpu-sec/sec",
238+
bc.Nodes, bc.StoresPerNode, bc.StoreByteCapacity>>30, bc.NodeCPURateCapacity/time.Second.Nanoseconds())
209239
if len(bc.Region) != 0 {
210-
_, _ = fmt.Fprintf(&b, ", region=%v, nodes_per_region=%v", bc.Region, bc.NodesPerRegion)
240+
_, _ = fmt.Fprintf(&b, ", region: %v, nodes_per_region: %v", bc.Region, bc.NodesPerRegion)
211241
}
242+
b.WriteString("]")
212243
return b.String()
213244
}
214245

@@ -325,15 +356,11 @@ type BaseRanges struct {
325356
ReplicaPlacement state.ReplicaPlacement
326357
}
327358

328-
func (b BaseRanges) String() string {
329-
return fmt.Sprintf("ranges=%d, min_key=%d, max_key=%d, replication_factor=%d, bytes=%d", b.Ranges, b.MinKey, b.MaxKey, b.ReplicationFactor, b.Bytes)
330-
}
331-
332359
// GetRangesInfo generates and distributes ranges across stores based on
333360
// PlacementType while using other BaseRanges fields for range configuration.
334361
func (b BaseRanges) GetRangesInfo(
335362
pType PlacementType, numOfStores int, randSource *rand.Rand, weightedRandom []float64,
336-
) state.RangesInfo {
363+
) (state.RangesInfo, string) {
337364
switch pType {
338365
case Even:
339366
return state.RangesInfoEvenDistribution(numOfStores, b.Ranges, b.MinKey, b.MaxKey, b.ReplicationFactor, b.Bytes)
@@ -369,21 +396,24 @@ type BasicRanges struct {
369396
}
370397

371398
func (br BasicRanges) String() string {
372-
return fmt.Sprintf("basic ranges with placement_type=%v, %v", br.PlacementType, br.BaseRanges)
399+
return fmt.Sprintf("[%d,%d): %d(rf=%d), %dMiB",
400+
br.MinKey, br.MaxKey, br.Ranges, br.ReplicationFactor, br.Bytes>>20)
373401
}
374402

375403
// Generate returns an updated simulator state, where the cluster is loaded with
376404
// ranges generated based on the parameters specified in the fields of
377405
// BasicRanges.
378406
func (br BasicRanges) Generate(
379407
seed int64, settings *config.SimulationSettings, s state.State,
380-
) state.State {
408+
) (state.State, string) {
381409
if br.PlacementType == Random || br.PlacementType == WeightedRandom {
382410
panic("BasicRanges generate only uniform or skewed distributions")
383411
}
384-
rangesInfo := br.GetRangesInfo(br.PlacementType, len(s.Stores()), nil, []float64{})
412+
rangesInfo, str := br.GetRangesInfo(br.PlacementType, len(s.Stores()), nil, []float64{})
385413
br.LoadRangeInfo(s, rangesInfo)
386-
return s
414+
var buf strings.Builder
415+
_, _ = fmt.Fprintf(&buf, "\t%s, %s", br, str)
416+
return s, buf.String()
387417
}
388418

389419
// MultiRanges implements the RangeGen interface, supporting multiple
@@ -393,21 +423,33 @@ type MultiRanges []BasicRanges
393423
var _ RangeGen = MultiRanges{}
394424

395425
func (mr MultiRanges) String() string {
396-
var str string
397-
for _, ranges := range mr {
398-
str += fmt.Sprintf("%s\n", ranges.String())
426+
var buf strings.Builder
427+
for i, ranges := range mr {
428+
_, _ = fmt.Fprintf(&buf, "%s", ranges.String())
429+
if i != len(mr)-1 {
430+
_, _ = fmt.Fprintf(&buf, "\n")
431+
}
399432
}
400-
return str
433+
return buf.String()
401434
}
402435

403436
func (mr MultiRanges) Generate(
404437
seed int64, settings *config.SimulationSettings, s state.State,
405-
) state.State {
438+
) (state.State, string) {
406439
var rangeInfos []state.RangeInfo
440+
var rangeInfoStrings []string
407441
for _, ranges := range mr {
408-
rangeInfos = append(rangeInfos,
409-
ranges.GetRangesInfo(ranges.PlacementType, len(s.Stores()), nil, []float64{})...)
442+
rangeInfo, rangeInfoStr := ranges.GetRangesInfo(ranges.PlacementType, len(s.Stores()), nil, []float64{})
443+
rangeInfos = append(rangeInfos, rangeInfo...)
444+
rangeInfoStrings = append(rangeInfoStrings, fmt.Sprintf("\t%s, %s", ranges.String(), rangeInfoStr))
410445
}
411446
state.LoadRangeInfo(s, rangeInfos...)
412-
return s
447+
var buf strings.Builder
448+
for i, str := range rangeInfoStrings {
449+
_, _ = fmt.Fprintf(&buf, "%s", str)
450+
if i != len(rangeInfoStrings)-1 {
451+
_, _ = fmt.Fprintf(&buf, "\n")
452+
}
453+
}
454+
return s, buf.String()
413455
}

0 commit comments

Comments
 (0)