-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql,jobs: create SQL Stats Compaction Job and resumer
This commit introduces the SQL Stats Compaction job type and a barebones implementation of the SQL Stats compaction. Release note: None
- Loading branch information
Showing
17 changed files
with
1,430 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package sql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/jobs" | ||
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb" | ||
"github.com/cockroachdb/cockroach/pkg/settings/cluster" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/persistedsqlstats" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
type sqlStatsCompactionResumer struct { | ||
job *jobs.Job | ||
st *cluster.Settings | ||
} | ||
|
||
var _ jobs.Resumer = &sqlStatsCompactionResumer{} | ||
|
||
// Resume implements the jobs.Resumer interface. | ||
func (r *sqlStatsCompactionResumer) Resume(ctx context.Context, execCtx interface{}) error { | ||
log.Infof(ctx, "starting sql stats compaction job") | ||
p := execCtx.(JobExecContext) | ||
ie := p.ExecCfg().InternalExecutor | ||
db := p.ExecCfg().DB | ||
|
||
// We check for concurrently running SQL Stats compaction jobs. We only allow | ||
// one job to be running at the same time. | ||
if err := persistedsqlstats.CheckExistingCompactionJob(ctx, r.job, ie, nil /* txn */); err != nil { | ||
if errors.Is(err, persistedsqlstats.ErrConcurrentSQLStatsCompaction) { | ||
log.Infof(ctx, "exiting due to a running sql stats compaction job") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
statsCompactor := persistedsqlstats.NewStatsCompactor(r.st, ie, db, p.ExecCfg().SQLStatsTestingKnobs) | ||
return statsCompactor.DeleteOldestEntries(ctx) | ||
} | ||
|
||
// OnFailOrCancel implements the jobs.Resumer interface. | ||
func (r *sqlStatsCompactionResumer) OnFailOrCancel(ctx context.Context, execCtx interface{}) error { | ||
return nil | ||
} | ||
|
||
func init() { | ||
jobs.RegisterConstructor(jobspb.TypeSQLStatsCompaction, func(job *jobs.Job, settings *cluster.Settings) jobs.Resumer { | ||
return &sqlStatsCompactionResumer{ | ||
job: job, | ||
st: settings, | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.