Skip to content

Commit 67af0f5

Browse files
committed
sql: use a random minute for the sql-stats-compaction job default recurrence
Now, the sql-stats-compaction job that is created during cluster initialization will be scheduled on a random minute in the hour, rather than at the top of the hour. This will only affect clusters that are initialized after this change is released. Any existing clusters will continue to keep whatever recurrence they had before, which defaulted to @hourly. This change was made because we have observed that this job can cause CPU spikes on the serverless host clusters, since different tenants all had this job scheduled for the same time. Release note: None
1 parent a12a2ad commit 67af0f5

File tree

6 files changed

+19
-6
lines changed

6 files changed

+19
-6
lines changed

pkg/sql/conn_executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ func NewServer(cfg *ExecutorConfig, pool *mon.BytesMonitor) *Server {
471471
DB: NewInternalDB(
472472
s, MemoryMetrics{}, sqlStatsInternalExecutorMonitor,
473473
),
474+
ClusterID: s.cfg.NodeInfo.LogicalClusterID,
474475
SQLIDContainer: cfg.NodeInfo.NodeID,
475476
JobRegistry: s.cfg.JobRegistry,
476477
Knobs: cfg.SQLStatsTestingKnobs,

pkg/sql/sqlstats/persistedsqlstats/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ go_library(
4949
"//pkg/util/stop",
5050
"//pkg/util/syncutil",
5151
"//pkg/util/timeutil",
52+
"//pkg/util/uuid",
5253
"@com_github_cockroachdb_errors//:errors",
5354
"@com_github_gogo_protobuf//types",
5455
"@com_github_robfig_cron_v3//:cron",

pkg/sql/sqlstats/persistedsqlstats/compaction_scheduling.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/cockroachdb/cockroach/pkg/sql/isql"
2222
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
2323
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
24+
"github.com/cockroachdb/cockroach/pkg/util/uuid"
2425
"github.com/cockroachdb/errors"
2526
pbtypes "github.com/gogo/protobuf/types"
2627
)
@@ -35,7 +36,7 @@ var ErrDuplicatedSchedules = errors.New("creating multiple sql stats compaction
3536
// scheduled job subsystem so the compaction job can be run periodically. This
3637
// is done during the cluster startup upgrade.
3738
func CreateSQLStatsCompactionScheduleIfNotYetExist(
38-
ctx context.Context, txn isql.Txn, st *cluster.Settings,
39+
ctx context.Context, txn isql.Txn, st *cluster.Settings, clusterID uuid.UUID,
3940
) (*jobs.ScheduledJob, error) {
4041
scheduleExists, err := checkExistingCompactionSchedule(ctx, txn)
4142
if err != nil {
@@ -48,7 +49,9 @@ func CreateSQLStatsCompactionScheduleIfNotYetExist(
4849

4950
compactionSchedule := jobs.NewScheduledJob(scheduledjobs.ProdJobSchedulerEnv)
5051

51-
schedule := SQLStatsCleanupRecurrence.Get(&st.SV)
52+
schedule := scheduledjobs.MaybeRewriteCronExpr(
53+
clusterID, SQLStatsCleanupRecurrence.Get(&st.SV),
54+
)
5255
if err := compactionSchedule.SetSchedule(schedule); err != nil {
5356
return nil, err
5457
}

pkg/sql/sqlstats/persistedsqlstats/controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/cockroachdb/cockroach/pkg/sql/isql"
2020
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
2121
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/sslocal"
22+
"github.com/cockroachdb/cockroach/pkg/util/uuid"
2223
)
2324

2425
// Controller implements the SQL Stats subsystem control plane. This exposes
@@ -27,8 +28,9 @@ import (
2728
// subsystem.
2829
type Controller struct {
2930
*sslocal.Controller
30-
db isql.DB
31-
st *cluster.Settings
31+
db isql.DB
32+
st *cluster.Settings
33+
clusterID func() uuid.UUID
3234
}
3335

3436
// NewController returns a new instance of sqlstats.Controller.
@@ -39,14 +41,15 @@ func NewController(
3941
Controller: sslocal.NewController(sqlStats.SQLStats, status),
4042
db: db,
4143
st: sqlStats.cfg.Settings,
44+
clusterID: sqlStats.cfg.ClusterID,
4245
}
4346
}
4447

4548
// CreateSQLStatsCompactionSchedule implements the tree.SQLStatsController
4649
// interface.
4750
func (s *Controller) CreateSQLStatsCompactionSchedule(ctx context.Context) error {
4851
return s.db.Txn(ctx, func(ctx context.Context, txn isql.Txn) error {
49-
_, err := CreateSQLStatsCompactionScheduleIfNotYetExist(ctx, txn, s.st)
52+
_, err := CreateSQLStatsCompactionScheduleIfNotYetExist(ctx, txn, s.st, s.clusterID())
5053
return err
5154
})
5255
}

pkg/sql/sqlstats/persistedsqlstats/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ import (
3333
"github.com/cockroachdb/cockroach/pkg/util/stop"
3434
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
3535
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
36+
"github.com/cockroachdb/cockroach/pkg/util/uuid"
3637
)
3738

3839
// Config is a configuration struct for the persisted SQL stats subsystem.
3940
type Config struct {
4041
Settings *cluster.Settings
4142
InternalExecutorMonitor *mon.BytesMonitor
4243
DB isql.DB
44+
ClusterID func() uuid.UUID
4345
SQLIDContainer *base.SQLIDContainer
4446
JobRegistry *jobs.Registry
4547

@@ -100,6 +102,7 @@ func New(cfg *Config, memSQLStats *sslocal.SQLStats) *PersistedSQLStats {
100102

101103
p.jobMonitor = jobMonitor{
102104
st: cfg.Settings,
105+
clusterID: cfg.ClusterID,
103106
db: cfg.DB,
104107
scanInterval: defaultScanInterval,
105108
jitterFn: p.jitterInterval,

pkg/sql/sqlstats/persistedsqlstats/scheduled_job_monitor.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/cockroachdb/cockroach/pkg/util/retry"
2626
"github.com/cockroachdb/cockroach/pkg/util/stop"
2727
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
28+
"github.com/cockroachdb/cockroach/pkg/util/uuid"
2829
"github.com/cockroachdb/errors"
2930
)
3031

@@ -60,6 +61,7 @@ var longIntervalWarningThreshold = time.Hour * 24
6061
// periodically every scanInterval (subject to jittering).
6162
type jobMonitor struct {
6263
st *cluster.Settings
64+
clusterID func() uuid.UUID
6365
db isql.DB
6466
scanInterval time.Duration
6567
jitterFn func(time.Duration) time.Duration
@@ -171,7 +173,7 @@ func (j *jobMonitor) updateSchedule(ctx context.Context, cronExpr string) {
171173
if !jobs.HasScheduledJobNotFoundError(err) && !errors.Is(err, errScheduleNotFound) {
172174
return err
173175
}
174-
sj, err = CreateSQLStatsCompactionScheduleIfNotYetExist(ctx, txn, j.st)
176+
sj, err = CreateSQLStatsCompactionScheduleIfNotYetExist(ctx, txn, j.st, j.clusterID())
175177
if err != nil {
176178
return err
177179
}

0 commit comments

Comments
 (0)