Skip to content

Commit 4109373

Browse files
craig[bot]wenyihu6
andcommitted
Merge #153688
153688: asim: fix more one-off formatting with rand output r=tbg a=wenyihu6 Rebased on top of #153545. Only the last commit is for this PR. Informs: #153572 Release note: none ---- Previously, some of the rand testing output formats were one-off because "\t" wasn’t explicitly passed by the caller, and the same string functions were reused for both random and non-random output. This commit fixes that by explicitly passing a tag for strings, giving the caller control over the number of tabs. Co-authored-by: wenyihu6 <[email protected]>
2 parents 42fc6e8 + 67b998e commit 4109373

18 files changed

+247
-179
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ type EventGen interface {
2020
// Generate returns an eventExecutor storing a sorted list of events and
2121
// being ready execute events for the simulation execution.
2222
Generate(seed int64, settings *config.SimulationSettings) scheduled.EventExecutor
23-
// String returns the concise string representation of the event executor,
24-
// detailing the number of scheduled events.
25-
String() string
23+
// StringWithTag returns the concise string representation of the event
24+
// executor, detailing the number of scheduled events.
25+
StringWithTag(tag string) string
2626
}
2727

2828
// StaticEvents implements the EventGen interface. For proper initialization,
@@ -85,14 +85,14 @@ func (se StaticEvents) ScheduleMutationWithAssertionEvent(
8585

8686
}
8787

88-
// String returns the concise string representation of the event executor,
89-
// detailing the number of scheduled events.
90-
func (se StaticEvents) String() string {
88+
// StringWithTag returns the concise string representation of the event
89+
// executor, detailing the number of scheduled events.
90+
func (se StaticEvents) StringWithTag(tag string) string {
9191
if se.eventExecutor == nil {
9292
panic("StaticEvents.eventExecutor is a nil interface; " +
9393
"use NewStaticEventsWithNoEvents for proper initialization.")
9494
}
95-
return se.eventExecutor.PrintEventSummary()
95+
return se.eventExecutor.PrintEventSummary(tag)
9696
}
9797

9898
// Generate returns an eventExecutor populated with a sorted list of events. It

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type LoadGen interface {
3131
// Generate returns a workload generator that is parameterized randomly by
3232
// the seed and simulation settings provided.
3333
Generate(seed int64, settings *config.SimulationSettings) []workload.Generator
34-
String() string
34+
StringWithTag(tag string) string
3535
}
3636

3737
// ClusterGen provides a method to generate the initial cluster state, given a
@@ -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, string)
54+
Generate(tag string, seed int64, settings *config.SimulationSettings, s state.State) (state.State, string)
5555
String() string
5656
}
5757

@@ -66,10 +66,11 @@ func GenerateSimulation(
6666
eventGen EventGen,
6767
seed int64,
6868
buf *strings.Builder,
69+
tag string,
6970
) *asim.Simulator {
7071
settings := settingsGen.Generate(seed)
7172
s := clusterGen.Generate(seed, &settings)
72-
s, rangeStateStr := rangeGen.Generate(seed, &settings, s)
73+
s, rangeStateStr := rangeGen.Generate(tag, seed, &settings, s)
7374
eventExecutor := eventGen.Generate(seed, &settings)
7475
generateClusterVisualization(buf, s, loadGen, eventGen, rangeStateStr, settings)
7576
return asim.NewSimulator(
@@ -102,10 +103,10 @@ type MultiLoad []BasicLoad
102103
// BasicLoad.
103104
var _ LoadGen = MultiLoad{}
104105

105-
func (ml MultiLoad) String() string {
106+
func (ml MultiLoad) StringWithTag(tag string) string {
106107
var buf strings.Builder
107108
for i, load := range ml {
108-
_, _ = fmt.Fprintf(&buf, "%s", load.String())
109+
_, _ = fmt.Fprintf(&buf, "%s", load.StringWithTag(tag))
109110
if i != len(ml)-1 {
110111
_, _ = fmt.Fprintf(&buf, "\n")
111112
}
@@ -135,9 +136,9 @@ type BasicLoad struct {
135136

136137
var _ LoadGen = BasicLoad{}
137138

138-
func (bl BasicLoad) String() string {
139+
func (bl BasicLoad) StringWithTag(tag string) string {
139140
var buf strings.Builder
140-
fmt.Fprintf(&buf, "\t[%d,%d): ", bl.MinKey, bl.MaxKey)
141+
fmt.Fprintf(&buf, "%s[%d,%d): ", tag, bl.MinKey, bl.MaxKey)
141142
if bl.RWRatio == 1 {
142143
_, _ = fmt.Fprint(&buf, "read-only")
143144
} else if bl.RWRatio == 0 {
@@ -214,7 +215,7 @@ func (lc LoadedCluster) Generate(seed int64, settings *config.SimulationSettings
214215
}
215216

216217
func (lc LoadedCluster) String() string {
217-
return fmt.Sprintf("cluster: %s", lc.Info.String())
218+
return fmt.Sprintf("cluster: \n%s", lc.Info.String())
218219
}
219220

220221
func (lc LoadedCluster) Regions() []state.Region {
@@ -356,6 +357,11 @@ type BaseRanges struct {
356357
ReplicaPlacement state.ReplicaPlacement
357358
}
358359

360+
func (br BaseRanges) String() string {
361+
return fmt.Sprintf("[%d,%d): %d(rf=%d), %dMiB",
362+
br.MinKey, br.MaxKey, br.Ranges, br.ReplicationFactor, br.Bytes>>20)
363+
}
364+
359365
// GetRangesInfo generates and distributes ranges across stores based on
360366
// PlacementType while using other BaseRanges fields for range configuration.
361367
func (b BaseRanges) GetRangesInfo(
@@ -404,15 +410,15 @@ func (br BasicRanges) String() string {
404410
// ranges generated based on the parameters specified in the fields of
405411
// BasicRanges.
406412
func (br BasicRanges) Generate(
407-
seed int64, settings *config.SimulationSettings, s state.State,
413+
tag string, seed int64, settings *config.SimulationSettings, s state.State,
408414
) (state.State, string) {
409415
if br.PlacementType == Random || br.PlacementType == WeightedRandom {
410416
panic("BasicRanges generate only uniform or skewed distributions")
411417
}
412418
rangesInfo, str := br.GetRangesInfo(br.PlacementType, len(s.Stores()), nil, []float64{})
413419
br.LoadRangeInfo(s, rangesInfo)
414420
var buf strings.Builder
415-
_, _ = fmt.Fprintf(&buf, "\t%s, %s", br, str)
421+
_, _ = fmt.Fprintf(&buf, "%s%s, %s", tag, br, str)
416422
return s, buf.String()
417423
}
418424

@@ -434,14 +440,14 @@ func (mr MultiRanges) String() string {
434440
}
435441

436442
func (mr MultiRanges) Generate(
437-
seed int64, settings *config.SimulationSettings, s state.State,
443+
tag string, seed int64, settings *config.SimulationSettings, s state.State,
438444
) (state.State, string) {
439445
var rangeInfos []state.RangeInfo
440446
var rangeInfoStrings []string
441447
for _, ranges := range mr {
442448
rangeInfo, rangeInfoStr := ranges.GetRangesInfo(ranges.PlacementType, len(s.Stores()), nil, []float64{})
443449
rangeInfos = append(rangeInfos, rangeInfo...)
444-
rangeInfoStrings = append(rangeInfoStrings, fmt.Sprintf("\t%s, %s", ranges.String(), rangeInfoStr))
450+
rangeInfoStrings = append(rangeInfoStrings, fmt.Sprintf("%s%s, %s", tag, ranges.String(), rangeInfoStr))
445451
}
446452
state.LoadRangeInfo(s, rangeInfos...)
447453
var buf strings.Builder

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ func generateClusterVisualization(
8787
}
8888
return s
8989
}
90-
clusterSetUp := emptyIfBlank(s.NodesString())
90+
clusterSetUp := emptyIfBlank(s.NodesStringWithTag("\t"))
9191
rangeState := emptyIfBlank(rangeStateStr)
92-
event := emptyIfBlank(eventGen.String())
93-
workloadSetUp := emptyIfBlank(loadGen.String())
92+
event := emptyIfBlank(eventGen.StringWithTag("\t"))
93+
workloadSetUp := emptyIfBlank(loadGen.StringWithTag("\t"))
9494
settingsChanges := emptyIfBlank(compareSettingsToDefault(settings))
9595
_, _ = fmt.Fprintf(buf, "Cluster Set Up\n%s\n", clusterSetUp)
9696
_, _ = fmt.Fprintf(buf, "Key Space\n%s\n", rangeState)

pkg/kv/kvserver/asim/scheduled/scheduled_event.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ type ScheduledEvent struct {
3939
TargetEvent event.Event
4040
}
4141

42-
func (se ScheduledEvent) String() string {
42+
func (se ScheduledEvent) StringWithTag(tag string) string {
4343
buf := strings.Builder{}
44-
buf.WriteString(fmt.Sprintf("\t%s", se.TargetEvent.String()))
44+
buf.WriteString(fmt.Sprintf("%s%s", tag, se.TargetEvent.String()))
4545
if !se.At.Equal(config.DefaultStartTime) {
4646
buf.WriteString(fmt.Sprintf(" at %s", se.At.Format("15:04:05")))
4747
}

pkg/kv/kvserver/asim/scheduled/scheduled_event_executor.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type EventExecutor interface {
3131
TickEvents(context.Context, time.Time, state.State, history.History) bool
3232
// PrintEventSummary returns a string summarizing the executed mutation and
3333
// assertion events.
34-
PrintEventSummary() string
34+
PrintEventSummary(tag string) string
3535
// PrintEventsExecuted returns a detailed string representation of executed
3636
// events including details of mutation events, assertion checks, and assertion
3737
// results.
@@ -68,16 +68,16 @@ func newExecutorWithNoEvents() *eventExecutor {
6868

6969
// PrintEventSummary returns a string summarizing the executed mutation and
7070
// assertion events.
71-
func (e *eventExecutor) PrintEventSummary() string {
71+
func (e *eventExecutor) PrintEventSummary(tag string) string {
7272
mutationEvents, assertionEvents := 0, 0
7373
var buf strings.Builder
7474
sort.Sort(e.scheduledEvents)
75-
for i, event := range e.scheduledEvents {
76-
_, _ = fmt.Fprintf(&buf, "%v", event)
75+
for i, ev := range e.scheduledEvents {
76+
_, _ = fmt.Fprintf(&buf, "%v", ev.StringWithTag(tag))
7777
if i != len(e.scheduledEvents)-1 {
7878
_, _ = fmt.Fprintf(&buf, "\n")
7979
}
80-
if event.IsMutationEvent() {
80+
if ev.IsMutationEvent() {
8181
mutationEvents++
8282
} else {
8383
assertionEvents++
@@ -110,7 +110,7 @@ func (e *eventExecutor) PrintEventsExecuted() string {
110110
buf := strings.Builder{}
111111
buf.WriteString(fmt.Sprintf("%d events executed:\n", len(e.scheduledEvents)))
112112
for i, event := range e.scheduledEvents {
113-
_, _ = fmt.Fprintf(&buf, "%v", event.String())
113+
_, _ = fmt.Fprintf(&buf, "%v", event.StringWithTag(""))
114114
if i != len(e.scheduledEvents)-1 {
115115
_, _ = fmt.Fprintf(&buf, "\n")
116116
}

pkg/kv/kvserver/asim/state/config_loader.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,7 @@ func (c ClusterInfo) String() (s string) {
294294
buf.WriteString(", ")
295295
}
296296
}
297-
buf.WriteString("]")
298-
buf.WriteString("\n")
297+
buf.WriteString("]\n")
299298
}
300299
buf.WriteString(fmt.Sprintf("store_disk_capacity=%d bytes, node_cpu_rate_capacity=%d cpu-ns/sec",
301300
c.StoreDiskCapacityBytes, c.NodeCPURateCapacityNanos))

pkg/kv/kvserver/asim/state/impl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ func (s *state) SetSimulationSettings(Key string, Value interface{}) {
14251425
}
14261426
}
14271427

1428-
func (s *state) NodesString() string {
1428+
func (s *state) NodesStringWithTag(tag string) string {
14291429
var buf strings.Builder
14301430

14311431
nodes := make([]*node, 0, len(s.nodes))
@@ -1437,7 +1437,7 @@ func (s *state) NodesString() string {
14371437
})
14381438

14391439
for nID, n := range nodes {
1440-
_, _ = fmt.Fprintf(&buf, "\tn%d(", n.nodeID)
1440+
_, _ = fmt.Fprintf(&buf, "%sn%d(", tag, n.nodeID)
14411441
for _, locality := range n.desc.Locality.Tiers {
14421442
_, _ = fmt.Fprintf(&buf, "%s,", locality.Value)
14431443
}

pkg/kv/kvserver/asim/state/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type State interface {
6464
// Nodes returns all nodes that exist in this state.
6565
Nodes() []Node
6666
// NodesString returns a string representation of all nodes.
67-
NodesString() string
67+
NodesStringWithTag(tag string) string
6868
// RangeFor returns the range containing Key in [StartKey, EndKey). This
6969
// cannot fail.
7070
RangeFor(Key) Range

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func TestDataDriven(t *testing.T) {
539539
}
540540
simulator := gen.GenerateSimulation(
541541
duration, clusterGen, rangeGen, loadGen,
542-
settingsGen, eventGen, seedGen.Int63(), tmpStrB,
542+
settingsGen, eventGen, seedGen.Int63(), tmpStrB, "\t",
543543
)
544544
if stateStrForOnce == "" {
545545
stateStrForOnce = tmpStrB.String()

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,22 @@ func (tr testResultsReport) String() string {
150150
if failed || tr.flags.Has(OutputConfigGen) {
151151
buf.WriteString(fmt.Sprintf("configurations generated using seed %d\n", output.seed))
152152
clusterGenStr, rangeGenStr, loadGenStr, eventGenStr :=
153-
output.clusterGen.String(), output.rangeGen.String(), output.loadGen.String(), output.eventGen.String()
154-
emptyIfBlank := func(s string) string {
153+
output.clusterGen.String(), output.rangeGen.String(), output.loadGen.StringWithTag(""), output.eventGen.StringWithTag("")
154+
emptyIfBlank := func(tag string, s string) string {
155155
if s == "" {
156-
return "empty"
156+
return fmt.Sprintf("%s: empty", tag)
157157
}
158158
return s
159159
}
160-
buf.WriteString(fmt.Sprintf("\t%v\n", emptyIfBlank(clusterGenStr)))
161-
buf.WriteString(fmt.Sprintf("\t%v\n", emptyIfBlank(rangeGenStr)))
162-
buf.WriteString(fmt.Sprintf("%v\n", emptyIfBlank(loadGenStr)))
163-
buf.WriteString(fmt.Sprintf("\t%v\n", emptyIfBlank(eventGenStr)))
160+
buf.WriteString(fmt.Sprintf("\t%v\n", emptyIfBlank("cluster", clusterGenStr)))
161+
buf.WriteString(fmt.Sprintf("\t%v\n", emptyIfBlank("range", rangeGenStr)))
162+
buf.WriteString(fmt.Sprintf("\t%v\n", emptyIfBlank("load", loadGenStr)))
163+
buf.WriteString("\tscheduled_event:\n")
164+
if eventGenStr == "" {
165+
eventGenStr = "\t\tempty"
166+
}
167+
buf.WriteString(fmt.Sprintf("%v\n", eventGenStr))
168+
164169
}
165170
if failed || tr.flags.Has(OutputInitialState) {
166171
buf.WriteString(fmt.Sprintf("initial state at %s:\n", output.initialTime.Format("2006-01-02 15:04:05")))

0 commit comments

Comments
 (0)