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

sqlstats: stats flush should honor stopper quiesce #135336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 27 additions & 8 deletions pkg/sql/sqlstats/sslocal/sslocal_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package sslocal
import (
"context"
"sort"
"sync"
"time"

"github.com/cockroachdb/cockroach/pkg/server/serverpb"
Expand Down Expand Up @@ -162,11 +161,16 @@ func (s *SQLStats) ConsumeStats(
// the system statement|transaction_statistics tables.
// In-memory stats storage is not locked here and it is safe to call stmtVisitor or txnVisitor functions
// that might be time consuming operations.
var wg sync.WaitGroup
wg.Add(2)

// We use a done channel instead of waitGroup because we need to
// fail if the stopper is quiescing and the tasks are never going
// to run.
doneCh := make(chan struct{}, 2)

err := stopper.RunAsyncTask(ctx, "sql-stmt-stats-flush", func(ctx context.Context) {
defer wg.Done()
defer func() {
doneCh <- struct{}{}
}()
for _, stat := range stmtStats {
stat := stat
if err := stmtVisitor(ctx, stat); err != nil {
Expand All @@ -176,12 +180,13 @@ func (s *SQLStats) ConsumeStats(
})
if err != nil {
log.Warningf(ctx, "failed to execute sql-stmt-stats-flush task, %s", err.Error())
wg.Done()
return
}

err = stopper.RunAsyncTask(ctx, "sql-txn-stats-flush", func(ctx context.Context) {
defer wg.Done()
defer func() {
doneCh <- struct{}{}
}()
for _, stat := range txnStats {
stat := stat
if err := txnVisitor(ctx, stat); err != nil {
Expand All @@ -191,10 +196,24 @@ func (s *SQLStats) ConsumeStats(
})
if err != nil {
log.Warningf(ctx, "failed to execute sql-txn-stats-flush task, %s", err.Error())
wg.Done()
return
}
wg.Wait()

numComplete := 0
for {
select {
case <-doneCh:
numComplete++
if numComplete >= 2 {
close(doneCh)
return
}
case <-stopper.ShouldQuiesce():
// Don't close `doneCh` because the tasks could still be
// running.
return
}
}
}
}

Expand Down
Loading