-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathregistry_test.go
354 lines (323 loc) · 12.1 KB
/
registry_test.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright 2017 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"
"fmt"
"strconv"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/stretchr/testify/require"
)
// writeColumnMutation adds column as a mutation and writes the
// descriptor to the DB.
func writeColumnMutation(
t *testing.T,
kvDB *kv.DB,
tableDesc *tabledesc.Mutable,
column string,
m descpb.DescriptorMutation,
) {
col, err := tableDesc.FindColumnWithName(tree.Name(column))
if err != nil {
t.Fatal(err)
}
for i := range tableDesc.Columns {
if col.GetID() == tableDesc.Columns[i].ID {
// Use [:i:i] to prevent reuse of existing slice, or outstanding refs
// to ColumnDescriptors may unexpectedly change.
tableDesc.Columns = append(tableDesc.Columns[:i:i], tableDesc.Columns[i+1:]...)
break
}
}
m.Descriptor_ = &descpb.DescriptorMutation_Column{Column: col.ColumnDesc()}
writeMutation(t, kvDB, tableDesc, m)
}
// writeMutation writes the mutation to the table descriptor.
func writeMutation(
t *testing.T, kvDB *kv.DB, tableDesc *tabledesc.Mutable, m descpb.DescriptorMutation,
) {
tableDesc.Mutations = append(tableDesc.Mutations, m)
tableDesc.Version++
if err := catalog.ValidateSelf(tableDesc); err != nil {
t.Fatal(err)
}
if err := kvDB.Put(
context.Background(),
catalogkeys.MakeDescMetadataKey(keys.SystemSQLCodec, tableDesc.ID),
tableDesc.DescriptorProto(),
); err != nil {
t.Fatal(err)
}
}
func writeGCMutation(
t *testing.T,
kvDB *kv.DB,
tableDesc *tabledesc.Mutable,
m descpb.TableDescriptor_GCDescriptorMutation,
) {
tableDesc.GCMutations = append(tableDesc.GCMutations, m)
tableDesc.Version++
if err := catalog.ValidateSelf(tableDesc); err != nil {
t.Fatal(err)
}
if err := kvDB.Put(
context.Background(),
catalogkeys.MakeDescMetadataKey(keys.SystemSQLCodec, tableDesc.GetID()),
tableDesc.DescriptorProto(),
); err != nil {
t.Fatal(err)
}
}
type mutationOptions struct {
// Set if the desc should have any mutations of any sort.
hasMutation bool
// Set if the mutation being inserted is a GCMutation.
hasGCMutation bool
// Set if the desc should have a job that is dropping it.
hasDropJob bool
}
func (m mutationOptions) string() string {
return fmt.Sprintf("hasMutation=%s_hasGCMutation=%s_hasDropJob=%s",
strconv.FormatBool(m.hasMutation), strconv.FormatBool(m.hasGCMutation),
strconv.FormatBool(m.hasDropJob))
}
func TestRegistryGC(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
db := sqlutils.MakeSQLRunner(sqlDB)
ts := timeutil.Now()
earlier := ts.Add(-1 * time.Hour)
muchEarlier := ts.Add(-2 * time.Hour)
setDropJob := func(dbName, tableName string) {
desc := catalogkv.TestingGetMutableExistingTableDescriptor(
kvDB, keys.SystemSQLCodec, dbName, tableName)
desc.DropJobID = 123
if err := kvDB.Put(
context.Background(),
catalogkeys.MakeDescMetadataKey(keys.SystemSQLCodec, desc.GetID()),
desc.DescriptorProto(),
); err != nil {
t.Fatal(err)
}
}
constructTableName := func(prefix string, mutOptions mutationOptions) string {
return fmt.Sprintf("%s_%s", prefix, mutOptions.string())
}
writeJob := func(name string, created, finished time.Time, status Status, mutOptions mutationOptions) string {
tableName := constructTableName(name, mutOptions)
if _, err := sqlDB.Exec(fmt.Sprintf(`
CREATE DATABASE IF NOT EXISTS t;
CREATE TABLE t."%s" (k VARCHAR PRIMARY KEY DEFAULT 'default', v VARCHAR,i VARCHAR NOT NULL DEFAULT 'i');
INSERT INTO t."%s" VALUES('a', 'foo');
`, tableName, tableName)); err != nil {
t.Fatal(err)
}
tableDesc := catalogkv.TestingGetMutableExistingTableDescriptor(
kvDB, keys.SystemSQLCodec, "t", tableName)
if mutOptions.hasDropJob {
setDropJob("t", tableName)
}
if mutOptions.hasMutation {
writeColumnMutation(t, kvDB, tableDesc, "i", descpb.DescriptorMutation{State: descpb.
DescriptorMutation_DELETE_AND_WRITE_ONLY, Direction: descpb.DescriptorMutation_DROP})
}
if mutOptions.hasGCMutation {
writeGCMutation(t, kvDB, tableDesc, descpb.TableDescriptor_GCDescriptorMutation{})
}
payload, err := protoutil.Marshal(&jobspb.Payload{
Description: name,
// register a mutation on the table so that jobs that reference
// the table are not considered orphaned
DescriptorIDs: []descpb.ID{
tableDesc.GetID(),
descpb.InvalidID, // invalid id to test handling of missing descriptors.
},
Details: jobspb.WrapPayloadDetails(jobspb.SchemaChangeDetails{}),
StartedMicros: timeutil.ToUnixMicros(created),
FinishedMicros: timeutil.ToUnixMicros(finished),
})
if err != nil {
t.Fatal(err)
}
progress, err := protoutil.Marshal(&jobspb.Progress{
Details: jobspb.WrapProgressDetails(jobspb.SchemaChangeProgress{}),
})
if err != nil {
t.Fatal(err)
}
var id jobspb.JobID
db.QueryRow(t,
`INSERT INTO system.jobs (status, payload, progress, created) VALUES ($1, $2, $3, $4) RETURNING id`,
status, payload, progress, created).Scan(&id)
return strconv.Itoa(int(id))
}
// Test the descriptor when any of the following are set.
// 1. Mutations
// 2. GC Mutations
// 3. A drop job
for _, hasMutation := range []bool{true, false} {
for _, hasGCMutation := range []bool{true, false} {
for _, hasDropJob := range []bool{true, false} {
if !hasMutation && !hasGCMutation && !hasDropJob {
continue
}
mutOptions := mutationOptions{
hasMutation: hasMutation,
hasGCMutation: hasGCMutation,
hasDropJob: hasDropJob,
}
oldRunningJob := writeJob("old_running", muchEarlier, time.Time{}, StatusRunning, mutOptions)
oldSucceededJob := writeJob("old_succeeded", muchEarlier, muchEarlier.Add(time.Minute), StatusSucceeded, mutOptions)
oldFailedJob := writeJob("old_failed", muchEarlier, muchEarlier.Add(time.Minute),
StatusFailed, mutOptions)
oldRevertFailedJob := writeJob("old_revert_failed", muchEarlier, muchEarlier.Add(time.Minute),
StatusRevertFailed, mutOptions)
oldCanceledJob := writeJob("old_canceled", muchEarlier, muchEarlier.Add(time.Minute),
StatusCanceled, mutOptions)
newRunningJob := writeJob("new_running", earlier, earlier.Add(time.Minute), StatusRunning,
mutOptions)
newSucceededJob := writeJob("new_succeeded", earlier, earlier.Add(time.Minute), StatusSucceeded, mutOptions)
newFailedJob := writeJob("new_failed", earlier, earlier.Add(time.Minute), StatusFailed, mutOptions)
newRevertFailedJob := writeJob("new_revert_failed", earlier, earlier.Add(time.Minute), StatusRevertFailed, mutOptions)
newCanceledJob := writeJob("new_canceled", earlier, earlier.Add(time.Minute),
StatusCanceled, mutOptions)
db.CheckQueryResults(t, `SELECT id FROM system.jobs ORDER BY id`, [][]string{
{oldRunningJob}, {oldSucceededJob}, {oldFailedJob}, {oldRevertFailedJob}, {oldCanceledJob},
{newRunningJob}, {newSucceededJob}, {newFailedJob}, {newRevertFailedJob}, {newCanceledJob}})
if err := s.JobRegistry().(*Registry).cleanupOldJobs(ctx, earlier); err != nil {
t.Fatal(err)
}
db.CheckQueryResults(t, `SELECT id FROM system.jobs ORDER BY id`, [][]string{
{oldRunningJob}, {oldRevertFailedJob}, {newRunningJob}, {newSucceededJob},
{newFailedJob}, {newRevertFailedJob}, {newCanceledJob}})
if err := s.JobRegistry().(*Registry).cleanupOldJobs(ctx, ts.Add(time.Minute*-10)); err != nil {
t.Fatal(err)
}
db.CheckQueryResults(t, `SELECT id FROM system.jobs ORDER BY id`, [][]string{
{oldRunningJob}, {oldRevertFailedJob}, {newRunningJob}, {newRevertFailedJob}})
// Delete the revert failed, and running jobs for the next run of the
// test.
_, err := sqlDB.Exec(`DELETE FROM system.jobs WHERE id = $1 OR id = $2 OR id = $3 OR id = $4`,
oldRevertFailedJob, newRevertFailedJob, oldRunningJob, newRunningJob)
require.NoError(t, err)
}
}
}
}
func TestRegistryGCPagination(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, sqlDB, _ := serverutils.StartServer(t, base.TestServerArgs{})
db := sqlutils.MakeSQLRunner(sqlDB)
defer s.Stopper().Stop(ctx)
for i := 0; i < 2*cleanupPageSize+1; i++ {
payload, err := protoutil.Marshal(&jobspb.Payload{})
require.NoError(t, err)
db.Exec(t,
`INSERT INTO system.jobs (status, created, payload) VALUES ($1, $2, $3)`,
StatusCanceled, timeutil.Now().Add(-time.Hour), payload)
}
ts := timeutil.Now()
require.NoError(t, s.JobRegistry().(*Registry).cleanupOldJobs(ctx, ts.Add(-10*time.Minute)))
var count int
db.QueryRow(t, `SELECT count(1) FROM system.jobs`).Scan(&count)
require.Zero(t, count)
}
func TestBatchJobsCreation(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
for _, test := range []struct {
name string
batchSize int
}{
{"small batch", 10},
{"medium batch", 501},
{"large batch", 1001},
{"extra large batch", 5001},
} {
t.Run(test.name, func(t *testing.T) {
{
if test.batchSize > 10 {
skip.UnderStress(t, "skipping stress test for batch size ", test.batchSize)
skip.UnderRace(t, "skipping test for batch size ", test.batchSize)
}
skip.UnderStress(t)
args := base.TestServerArgs{
Knobs: base.TestingKnobs{
JobsTestingKnobs: NewTestingKnobsWithShortIntervals(),
},
}
ctx := context.Background()
s, sqlDB, kvDB := serverutils.StartServer(t, args)
tdb := sqlutils.MakeSQLRunner(sqlDB)
defer s.Stopper().Stop(ctx)
r := s.JobRegistry().(*Registry)
RegisterConstructor(jobspb.TypeImport, func(job *Job, cs *cluster.Settings) Resumer {
return FakeResumer{
OnResume: func(ctx context.Context) error {
return nil
},
}
})
// Create a batch of job specifications.
var records []*Record
for i := 0; i < test.batchSize; i++ {
records = append(records, &Record{
JobID: r.MakeJobID(),
Details: jobspb.ImportDetails{},
Progress: jobspb.ImportProgress{},
})
}
// Create jobs in a batch.
var jobs []*Job
require.NoError(t, kvDB.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
var err error
jobs, err = r.CreateJobsWithTxn(ctx, txn, records)
return err
}))
require.Equal(t, len(jobs), test.batchSize)
// Wait for the jobs to complete.
tdb.CheckQueryResultsRetry(t, "SELECT count(*) FROM [SHOW JOBS]",
[][]string{{fmt.Sprintf("%d", test.batchSize)}})
for _, job := range jobs {
tdb.CheckQueryResultsRetry(t, fmt.Sprintf("SELECT status FROM system.jobs WHERE id = '%d'", job.id),
[][]string{{"succeeded"}})
}
// TODO(sajjad): To discuss: What should we expect the values of job_type
// and description? I was expecting that the type will be "IMPORT" in
// the jobs table when the job is created, but it occurred to be NULL. Is
// that expected? Similarly, description is NULL.
}
})
}
}