Skip to content

Commit b76a5b8

Browse files
authored
REP-5141 Add a flag to periodically collect pprof heap usage (#23)
* Update migration_verifier.go * Update migration_verifier.go * add flag to control collect interval * fix
1 parent 889bce1 commit b76a5b8

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

internal/verifier/check.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (verifier *Verifier) Check(ctx context.Context, filter map[string]any) {
4040
verifier.logger.Fatal().Err(err).Msgf("Fatal error in generation %d", verifier.generation)
4141
}
4242
}()
43+
verifier.MaybeStartPeriodicHeapProfileCollection(ctx)
4344
}
4445

4546
func (verifier *Verifier) waitForChangeStream() error {

internal/verifier/migration_verifier.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ type Verifier struct {
136136
// The filter is applied to all namespaces in both initial checking and iterative checking.
137137
// The verifier only checks documents within the filter.
138138
globalFilter map[string]any
139+
140+
pprofInterval time.Duration
139141
}
140142

141143
// VerificationStatus holds the Verification Status
@@ -360,6 +362,20 @@ func (verifier *Verifier) SetReadPreference(arg string) error {
360362
return err
361363
}
362364

365+
func (verifier *Verifier) SetPprofInterval(arg string) error {
366+
if arg == "" {
367+
return nil
368+
}
369+
370+
interval, err := time.ParseDuration(arg)
371+
if err != nil {
372+
return err
373+
}
374+
375+
verifier.pprofInterval = interval
376+
return nil
377+
}
378+
363379
// DocumentStats gets various stats (TODO clarify)
364380
func DocumentStats(ctx context.Context, client *mongo.Client, namespaces []string) {
365381

internal/verifier/pprof.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package verifier
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"runtime/pprof"
8+
"time"
9+
)
10+
11+
func (verifier *Verifier) MaybeStartPeriodicHeapProfileCollection(ctx context.Context) {
12+
if verifier.pprofInterval == 0 {
13+
return
14+
}
15+
16+
go func() {
17+
ticker := time.NewTicker(verifier.pprofInterval)
18+
defer ticker.Stop()
19+
20+
for {
21+
select {
22+
case <-ctx.Done():
23+
return
24+
case <-ticker.C:
25+
collectHeapUsage()
26+
}
27+
}
28+
}()
29+
30+
}
31+
32+
func collectHeapUsage() {
33+
heapFileName := fmt.Sprintf("heap-%s.out", time.Now().UTC().Format("20060102T150405Z"))
34+
heapFile, err := os.Create(heapFileName)
35+
defer heapFile.Close()
36+
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
err = pprof.Lookup("heap").WriteTo(heapFile, 0)
42+
if err != nil {
43+
panic(err)
44+
}
45+
}

main/migration_verifier.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040
failureDisplaySize = "failureDisplaySize"
4141
ignoreReadConcernFlag = "ignoreReadConcern"
4242
configFileFlag = "configFile"
43+
pprofInterval = "pprofInterval"
4344
)
4445

4546
func main() {
@@ -146,6 +147,10 @@ func main() {
146147
Name: ignoreReadConcernFlag,
147148
Usage: "Use connection-default read concerns rather than setting majority read concern. This option may degrade consistency, so only enable it if majority read concern (the default) doesn’t work.",
148149
}),
150+
altsrc.NewStringFlag(cli.StringFlag{
151+
Name: pprofInterval,
152+
Usage: "Interval to periodically collect pprof profiles (e.g. --pprofInterval=\"5m\")",
153+
}),
149154
}
150155

151156
app := &cli.App{
@@ -207,6 +212,7 @@ func handleArgs(ctx context.Context, cCtx *cli.Context) (*verifier.Verifier, err
207212
v.SetNumWorkers(cCtx.Int(numWorkers))
208213
v.SetGenerationPauseDelayMillis(time.Duration(cCtx.Int64(generationPauseDelay)))
209214
v.SetWorkerSleepDelayMillis(time.Duration(cCtx.Int64(workerSleepDelay)))
215+
v.SetPprofInterval(cCtx.String(pprofInterval))
210216

211217
partitionSizeMB := cCtx.Uint64(partitionSizeMB)
212218
if partitionSizeMB != 0 {

0 commit comments

Comments
 (0)