-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathcompaction_scheduling.go
147 lines (123 loc) · 4.81 KB
/
compaction_scheduling.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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 persistedsqlstats
import (
"context"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/scheduledjobs"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/errors"
pbtypes "github.com/gogo/protobuf/types"
)
const compactionScheduleName = "sql-stats-compaction"
// ErrDuplicatedSchedules indicates that there is already a schedule for sql
// stats compaction job existing in the system.scheduled_jobs table.
var ErrDuplicatedSchedules = errors.New("creating multiple sql stats compaction is disallowed")
// CreateSQLStatsCompactionScheduleIfNotYetExist registers SQL Stats compaction job with the
// scheduled job subsystem so the compaction job can be run periodically. This
// is done during the cluster startup migration.
func CreateSQLStatsCompactionScheduleIfNotYetExist(
ctx context.Context, ie sqlutil.InternalExecutor, txn *kv.Txn, st *cluster.Settings,
) (*jobs.ScheduledJob, error) {
scheduleExists, err := checkExistingCompactionSchedule(ctx, ie, txn)
if err != nil {
return nil, err
}
if scheduleExists {
return nil, ErrDuplicatedSchedules
}
compactionSchedule := jobs.NewScheduledJob(scheduledjobs.ProdJobSchedulerEnv)
schedule := SQLStatsCleanupRecurrence.Get(&st.SV)
if err := compactionSchedule.SetSchedule(schedule); err != nil {
return nil, err
}
compactionSchedule.SetScheduleDetails(jobspb.ScheduleDetails{
Wait: jobspb.ScheduleDetails_SKIP,
OnError: jobspb.ScheduleDetails_RETRY_SCHED,
})
compactionSchedule.SetScheduleLabel(compactionScheduleName)
compactionSchedule.SetOwner(security.NodeUserName())
args, err := pbtypes.MarshalAny(&ScheduledSQLStatsCompactorExecutionArgs{})
if err != nil {
return nil, err
}
compactionSchedule.SetExecutionDetails(
tree.ScheduledSQLStatsCompactionExecutor.InternalName(),
jobspb.ExecutionArguments{Args: args},
)
compactionSchedule.SetScheduleStatus(string(jobs.StatusPending))
if err = compactionSchedule.Create(ctx, ie, txn); err != nil {
return nil, err
}
return compactionSchedule, nil
}
// CreateCompactionJob creates a system.jobs record if there is no other
// SQL Stats compaction job running. This is invoked by the scheduled job
// Executor.
func CreateCompactionJob(
ctx context.Context, createdByInfo *jobs.CreatedByInfo, txn *kv.Txn, jobRegistry *jobs.Registry,
) (jobspb.JobID, error) {
record := jobs.Record{
Description: "automatic SQL Stats compaction",
Username: security.NodeUserName(),
Details: jobspb.AutoSQLStatsCompactionDetails{},
Progress: jobspb.AutoSQLStatsCompactionProgress{},
CreatedBy: createdByInfo,
}
jobID := jobRegistry.MakeJobID()
if _, err := jobRegistry.CreateAdoptableJobWithTxn(ctx, record, jobID, txn); err != nil {
return jobspb.InvalidJobID, err
}
return jobID, nil
}
// CheckExistingCompactionJob checks for existing SQL Stats Compaction job
// that are either PAUSED, CANCELED, or RUNNING. If so, it returns a
// ErrConcurrentSQLStatsCompaction.
func CheckExistingCompactionJob(
ctx context.Context, job *jobs.Job, ie sqlutil.InternalExecutor, txn *kv.Txn,
) error {
jobID := jobspb.InvalidJobID
if job != nil {
jobID = job.ID()
}
exists, err := jobs.RunningJobExists(ctx, jobID, ie, txn, func(payload *jobspb.Payload) bool {
return payload.Type() == jobspb.TypeAutoSQLStatsCompaction
})
if err == nil && exists {
err = ErrConcurrentSQLStatsCompaction
}
return err
}
func checkExistingCompactionSchedule(
ctx context.Context, ie sqlutil.InternalExecutor, txn *kv.Txn,
) (exists bool, _ error) {
query := "SELECT count(*) FROM system.scheduled_jobs WHERE schedule_name = $1"
row, err := ie.QueryRowEx(ctx, "check-existing-sql-stats-schedule", txn,
sessiondata.InternalExecutorOverride{User: security.NodeUserName()},
query, compactionScheduleName,
)
if err != nil {
return false /* exists */, err
}
if row == nil {
return false /* exists */, errors.AssertionFailedf("unexpected empty result when querying system.scheduled_job")
}
if len(row) != 1 {
return false /* exists */, errors.AssertionFailedf("unexpectedly received %d columns", len(row))
}
// Defensively check the count.
return tree.MustBeDInt(row[0]) > 0, nil /* err */
}