Skip to content

Commit

Permalink
go/cmd/vtbackup: wait for plugins to finish initializing (#14113)
Browse files Browse the repository at this point in the history
Signed-off-by: Max Englander <max@planetscale.com>
  • Loading branch information
maxenglander authored Sep 28, 2023
1 parent fa56f27 commit 6f36b9a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go/cmd/vtbackup/cli/vtbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ func run(_ *cobra.Command, args []string) error {
}()

go servenv.RunDefault()
// Some stats plugins use OnRun to initialize. Wait for them to finish
// initializing before continuing, so we don't lose any stats.
if err := stats.AwaitBackend(ctx); err != nil {
return fmt.Errorf("failed to await stats backend: %w", err)
}

if detachedMode {
// this method will call os.Exit and kill this process
Expand Down
17 changes: 17 additions & 0 deletions go/stats/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package stats

import (
"bytes"
"context"
"expvar"
"fmt"
"strconv"
Expand All @@ -45,6 +46,7 @@ var (
emitStats bool
statsEmitPeriod = 60 * time.Second
statsBackend string
statsBackendInit = make(chan struct{})
combineDimensions string
dropVariables string
)
Expand Down Expand Up @@ -209,6 +211,18 @@ var pushBackends = make(map[string]PushBackend)
var pushBackendsLock sync.Mutex
var once sync.Once

func AwaitBackend(ctx context.Context) error {
if statsBackend == "" {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-statsBackendInit:
return nil
}
}

// RegisterPushBackend allows modules to register PushBackend implementations.
// Should be called on init().
func RegisterPushBackend(name string, backend PushBackend) {
Expand All @@ -218,6 +232,9 @@ func RegisterPushBackend(name string, backend PushBackend) {
log.Fatalf("PushBackend %s already exists; can't register the same name multiple times", name)
}
pushBackends[name] = backend
if name == statsBackend {
close(statsBackendInit)
}
if emitStats {
// Start a single goroutine to emit stats periodically
once.Do(func() {
Expand Down

0 comments on commit 6f36b9a

Please sign in to comment.