Skip to content

Commit

Permalink
Merge #35552
Browse files Browse the repository at this point in the history
35552: sql/stats: read the list of initial tables asynchronously r=knz a=knz

Recommended by @RaduBerinde and @bdarnell in #35549 

This patch changes the refresher init code to perform the initial scan
through all descriptors using ASOT, to avoid possible txn conflicts
with the first client connections (or tests).


Release note: None

Co-authored-by: Raphael 'kena' Poss <[email protected]>
  • Loading branch information
craig[bot] and knz committed Mar 9, 2019
2 parents 38bb1e7 + 2a078b9 commit efc57fa
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions pkg/sql/stats/automatic_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ func (r *Refresher) Start(
ctx context.Context, st *settings.Values, stopper *stop.Stopper, refreshInterval time.Duration,
) error {
stopper.RunWorker(context.Background(), func(ctx context.Context) {
// Ensure that read-only tables will have stats created at least once
// on startup.
r.ensureAllTables(ctx, st)

// We always sleep for r.asOfTime at the beginning of each refresh, so
// subtract it from the refreshInterval.
refreshInterval -= r.asOfTime
Expand All @@ -206,8 +202,16 @@ func (r *Refresher) Start(
timer := time.NewTimer(refreshInterval)
defer timer.Stop()

// Ensure that read-only tables will have stats created at least
// once on startup.
const initialTableCollectionDelay = time.Second
initialTableCollection := time.After(initialTableCollectionDelay)

for {
select {
case <-initialTableCollection:
r.ensureAllTables(ctx, st, initialTableCollectionDelay)

case <-timer.C:
mutationCounts := r.mutationCounts
if err := stopper.RunAsyncTask(
Expand Down Expand Up @@ -259,17 +263,24 @@ func (r *Refresher) Start(

// ensureAllTables ensures that an entry exists in r.mutationCounts for each
// table in the database.
func (r *Refresher) ensureAllTables(ctx context.Context, settings *settings.Values) {
func (r *Refresher) ensureAllTables(
ctx context.Context, settings *settings.Values, initialTableCollectionDelay time.Duration,
) {
if !AutomaticStatisticsClusterMode.Get(settings) {
// Automatic stats are disabled.
return
}

// Use a historical read so as to disable txn contention resolution.
getAllTablesQuery := fmt.Sprintf(
`SELECT table_id FROM crdb_internal.tables AS OF SYSTEM TIME '-%s'`,
initialTableCollectionDelay)

rows, err := r.ex.Query(
ctx,
"get-tables",
nil, /* txn */
`SELECT table_id FROM crdb_internal.tables;`,
getAllTablesQuery,
)
if err != nil {
log.Errorf(ctx, "failed to get tables for automatic stats: %v", err)
Expand Down

0 comments on commit efc57fa

Please sign in to comment.