-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtesting_knobs.go
158 lines (127 loc) · 5.52 KB
/
testing_knobs.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
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2020 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 jobs
import (
"context"
"time"
"github.com/cockroachdb/cockroach/pkg/scheduledjobs"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
)
// TestingKnobs are base.ModuleTestingKnobs for testing jobs related infra.
type TestingKnobs struct {
// SchedulerDaemonInitialScanDelay overrides the initial scan delay.
SchedulerDaemonInitialScanDelay func() time.Duration
// SchedulerDaemonScanDelay overrides the delay between successive scans.
SchedulerDaemonScanDelay func() time.Duration
// JobSchedulerEnv overrides the environment to use for scheduled jobs.
JobSchedulerEnv scheduledjobs.JobSchedulerEnv
// TakeOverJobScheduling is a function which replaces the normal job scheduler
// daemon logic.
// This function will be passed in a function which the caller
// may invoke directly, bypassing normal job scheduler daemon logic.
TakeOverJobsScheduling func(func(ctx context.Context, maxSchedules int64) error)
// CaptureJobScheduler is a function which will be passed a fully constructed job scheduler.
// The scheduler is passed in as interface{} because jobScheduler is an unexported type.
// This testing knob is useful only for job scheduler tests.
CaptureJobScheduler func(scheduler interface{})
// CaptureJobExecutionConfig is a callback invoked with a job execution config
// which will be used when executing job schedules.
// The reason this callback exists is due to a circular dependency issues that exists
// if trying to initialize this config outside of sql.Server -- namely, we cannot
// initialize PlanHookMaker outside of sql package.
CaptureJobExecutionConfig func(config *scheduledjobs.JobExecutionConfig)
// OverrideAsOfClause is a function which has a chance of modifying
// tree.AsOfClause.
OverrideAsOfClause func(clause *tree.AsOfClause, stmtTimestamp time.Time)
// BeforeUpdate is called in the update transaction after the update function
// has run. If an error is returned, it will be propagated and the update will
// not be committed.
BeforeUpdate func(orig, updated JobMetadata) error
// IntervalOverrides consists of override knobs for job intervals.
IntervalOverrides TestingIntervalOverrides
// AfterJobStateMachine is called once the running instance of the job has
// returned from the state machine that transitions it from one state to
// another.
AfterJobStateMachine func()
// TimeSource replaces registry's clock.
TimeSource *hlc.Clock
// DisableAdoptions disables job adoptions.
DisableAdoptions bool
// BeforeWaitForJobsQuery is called once per invocation of the
// poll-show-jobs query in WaitForJobs.
BeforeWaitForJobsQuery func()
}
// ModuleTestingKnobs is part of the base.ModuleTestingKnobs interface.
func (*TestingKnobs) ModuleTestingKnobs() {}
// TestingIntervalOverrides contains variables to override the intervals and
// settings of periodic tasks.
type TestingIntervalOverrides struct {
// Adopt overrides the adoptIntervalSetting cluster setting.
Adopt *time.Duration
// Cancel overrides the cancelIntervalSetting cluster setting.
Cancel *time.Duration
// CollectMetrics overrides the pollJobsMetricsInterval cluster setting.
PollMetrics *time.Duration
// Gc overrides the gcIntervalSetting cluster setting.
Gc *time.Duration
// RetentionTime overrides the RetentionTimeSetting cluster setting.
RetentionTime *time.Duration
// RetryInitialDelay overrides retryInitialDelaySetting cluster setting.
RetryInitialDelay *time.Duration
// RetryMaxDelay overrides retryMaxDelaySetting cluster setting.
RetryMaxDelay *time.Duration
// WaitForJobsInitialDelay is the initial delay used in
// WaitForJobs calls.
WaitForJobsInitialDelay *time.Duration
// WaitForJobsMaxDelay
WaitForJobsMaxDelay *time.Duration
}
const defaultShortInterval = 10 * time.Millisecond
// NewTestingKnobsWithShortIntervals return a TestingKnobs structure with
// overrides for short adopt, cancel, and retry intervals.
func NewTestingKnobsWithShortIntervals() *TestingKnobs {
interval := defaultShortInterval
if util.RaceEnabled {
interval *= 5
}
return NewTestingKnobsWithIntervals(
interval, interval, interval, interval,
)
}
// NewTestingKnobsWithShortIntervalsWithMetricsPolling return a TestingKnobs structure with
// overrides for short adopt, cancel, retry, and metrics polling intervals.
func NewTestingKnobsWithShortIntervalsWithMetricsPolling() *TestingKnobs {
interval := defaultShortInterval
if util.RaceEnabled {
interval *= 5
}
shortIntervalsKnobs := NewTestingKnobsWithShortIntervals()
shortIntervalsKnobs.IntervalOverrides.PollMetrics = &interval
return shortIntervalsKnobs
}
// NewTestingKnobsWithIntervals return a TestingKnobs structure with overrides
// for adopt and cancel intervals.
func NewTestingKnobsWithIntervals(
adopt, cancel, initialDelay, maxDelay time.Duration,
) *TestingKnobs {
var zero time.Duration
return &TestingKnobs{
IntervalOverrides: TestingIntervalOverrides{
Adopt: &adopt,
Cancel: &cancel,
RetryInitialDelay: &initialDelay,
RetryMaxDelay: &maxDelay,
// Disable automatic metrics polling during tests by default.
PollMetrics: &zero,
},
}
}