gemini-code-assist[bot] commented on code in PR #36242:
URL: https://github.com/apache/beam/pull/36242#discussion_r2373020963
##########
sdks/go/pkg/beam/runners/prism/internal/engine/elementmanager.go:
##########
@@ -504,6 +504,40 @@ func (em *ElementManager) Bundles(ctx context.Context,
upstreamCancelFn context.
return runStageCh
}
+// DumpStages puts all the stage information into a string and returns it.
+func (em *ElementManager) DumpStages() string {
+ var stageState []string
+ ids := maps.Keys(em.stages)
+ if em.testStreamHandler != nil {
+ stageState = append(stageState, fmt.Sprintf("TestStreamHandler:
completed %v, curIndex %v of %v events: %+v, processingTime %v, %v, ptEvents %v
\n",
+ em.testStreamHandler.completed,
em.testStreamHandler.nextEventIndex, len(em.testStreamHandler.events),
em.testStreamHandler.events, em.testStreamHandler.processingTime,
mtime.FromTime(em.testStreamHandler.processingTime), em.processTimeEvents))
+ } else {
+ stageState = append(stageState, fmt.Sprintf("ElementManager
Now: %v processingTimeEvents: %v injectedBundles: %v\n",
em.ProcessingTimeNow(), em.processTimeEvents.events, em.injectedBundles))
+ }
+ sort.Strings(ids)
+ for _, id := range ids {
+ ss := em.stages[id]
+ inW := ss.InputWatermark()
+ outW := ss.OutputWatermark()
+ upPCol, upW := ss.UpstreamWatermark()
+ upS := em.pcolParents[upPCol]
+ if upS == "" {
+ upS = "IMPULSE " // (extra spaces to allow print to
align better.)
+ }
+ stageState = append(stageState, fmt.Sprintln(id, "watermark
in", inW, "out", outW, "upstream", upW, "from", upS, "pending", ss.pending,
"byKey", ss.pendingByKeys, "inprogressKeys", ss.inprogressKeys, "byBundle",
ss.inprogressKeysByBundle, "holds", ss.watermarkHolds.heap, "holdCounts",
ss.watermarkHolds.counts, "holdsInBundle", ss.inprogressHoldsByBundle,
"pttEvents", ss.processingTimeTimers.toFire, "bundlesToInject",
ss.bundlesToInject))
+
+ var outputConsumers, sideConsumers []string
+ for _, col := range ss.outputIDs {
+ outputConsumers = append(outputConsumers,
em.consumers[col]...)
+ for _, l := range em.sideConsumers[col] {
+ sideConsumers = append(sideConsumers, l.Global)
+ }
+ }
+ stageState = append(stageState, fmt.Sprintf("\tsideInputs: %v
outputCols: %v outputConsumers: %v sideConsumers: %v\n", ss.sides,
ss.outputIDs, outputConsumers, sideConsumers))
+ }
+ return strings.Join(stageState, "")
+}
Review Comment:

For building strings, especially in a loop, using `strings.Builder` is more
efficient and idiomatic in Go than appending to a slice of strings and then
joining them. It avoids multiple memory allocations for the intermediate
strings and the final joined string. This also helps improve readability by
allowing you to use `fmt.Fprintf` and `fmt.Fprintln` which can make long
formatting lines more manageable.
```go
func (em *ElementManager) DumpStages() string {
var sb strings.Builder
ids := maps.Keys(em.stages)
if em.testStreamHandler != nil {
fmt.Fprintf(&sb, "TestStreamHandler: completed %v, curIndex %v
of %v events: %+v, processingTime %v, %v, ptEvents %v \n",
em.testStreamHandler.completed,
em.testStreamHandler.nextEventIndex, len(em.testStreamHandler.events),
em.testStreamHandler.events, em.testStreamHandler.processingTime,
mtime.FromTime(em.testStreamHandler.processingTime), em.processTimeEvents)
} else {
fmt.Fprintf(&sb, "ElementManager Now: %v processingTimeEvents:
%v injectedBundles: %v\n", em.ProcessingTimeNow(), em.processTimeEvents.events,
em.injectedBundles)
}
sort.Strings(ids)
for _, id := range ids {
ss := em.stages[id]
inW := ss.InputWatermark()
outW := ss.OutputWatermark()
upPCol, upW := ss.UpstreamWatermark()
upS := em.pcolParents[upPCol]
if upS == "" {
upS = "IMPULSE " // (extra spaces to allow print to
align better.)
}
fmt.Fprintln(&sb, id, "watermark in", inW, "out", outW,
"upstream", upW, "from", upS, "pending", ss.pending, "byKey", ss.pendingByKeys,
"inprogressKeys", ss.inprogressKeys, "byBundle", ss.inprogressKeysByBundle,
"holds", ss.watermarkHolds.heap, "holdCounts", ss.watermarkHolds.counts,
"holdsInBundle", ss.inprogressHoldsByBundle, "pttEvents",
ss.processingTimeTimers.toFire, "bundlesToInject", ss.bundlesToInject)
var outputConsumers, sideConsumers []string
for _, col := range ss.outputIDs {
outputConsumers = append(outputConsumers,
em.consumers[col]...)
for _, l := range em.sideConsumers[col] {
sideConsumers = append(sideConsumers, l.Global)
}
}
fmt.Fprintf(&sb, "\tsideInputs: %v outputCols: %v
outputConsumers: %v sideConsumers: %v\n", ss.sides, ss.outputIDs,
outputConsumers, sideConsumers)
}
return sb.String()
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]