Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: fix TestPebbleMetricEventListener #96651

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions pkg/storage/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ type Pebble struct {
syncutil.Mutex
flushCompletedCallback func()
}
asyncDone sync.WaitGroup

// supportsRangeKeys is 1 if the database supports range keys. It must
// be accessed atomically.
Expand Down Expand Up @@ -1006,7 +1007,7 @@ func NewPebble(ctx context.Context, cfg PebbleConfig) (p *Pebble, err error) {
oldDiskSlow := lel.DiskSlow
lel.DiskSlow = func(info pebble.DiskSlowInfo) {
// Run oldDiskSlow asynchronously.
go oldDiskSlow(info)
p.async(func() { oldDiskSlow(info) })
}
el := pebble.TeeEventListener(
p.makeMetricEtcEventListener(ctx),
Expand Down Expand Up @@ -1087,6 +1088,17 @@ func NewPebble(ctx context.Context, cfg PebbleConfig) (p *Pebble, err error) {
return p, nil
}

// async launches the provided function in a new goroutine. It uses a wait group
// to synchronize with (*Pebble).Close to ensure all launched goroutines have
// exited before Close returns.
func (p *Pebble) async(fn func()) {
p.asyncDone.Add(1)
go func() {
defer p.asyncDone.Done()
fn()
}()
}

func (p *Pebble) makeMetricEtcEventListener(ctx context.Context) pebble.EventListener {
return pebble.EventListener{
WriteStallBegin: func(info pebble.WriteStallBeginInfo) {
Expand Down Expand Up @@ -1132,7 +1144,7 @@ func (p *Pebble) makeMetricEtcEventListener(ctx context.Context) pebble.EventLis

log.Fatalf(ctx, "file write stall detected: %s", info)
} else {
go log.Errorf(ctx, "file write stall detected: %s", info)
p.async(func() { log.Errorf(ctx, "file write stall detected: %s", info) })
}
return
}
Expand Down Expand Up @@ -1172,6 +1184,9 @@ func (p *Pebble) Close() {
}
p.closed = true

// Wait for any asynchronous goroutines to exit.
p.asyncDone.Wait()

handleErr := func(err error) {
if err == nil {
return
Expand Down