-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
schema_telemetry_test.go
112 lines (97 loc) · 3.68 KB
/
schema_telemetry_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
// Copyright 2022 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 schematelemetry_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobstest"
"github.com/cockroachdb/cockroach/pkg/scheduledjobs"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schematelemetry/schematelemetrycontroller"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins/builtinconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"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/timeutil"
"github.com/stretchr/testify/require"
)
func makeTestServerArgs() (args base.TestServerArgs) {
args.Knobs.JobsTestingKnobs = &jobs.TestingKnobs{
JobSchedulerEnv: jobstest.NewJobSchedulerTestEnv(
jobstest.UseSystemTables,
timeutil.Now(),
tree.ScheduledSchemaTelemetryExecutor,
),
}
aostDuration := time.Nanosecond
args.Knobs.SchemaTelemetry = &sql.SchemaTelemetryTestingKnobs{
AOSTDuration: &aostDuration,
}
return args
}
var (
qExists = fmt.Sprintf(`
SELECT recurrence, count(*)
FROM [SHOW SCHEDULES]
WHERE label = '%s'
AND schedule_status = 'ACTIVE'
GROUP BY recurrence`,
schematelemetrycontroller.SchemaTelemetryScheduleName)
qID = fmt.Sprintf(`
SELECT id
FROM [SHOW SCHEDULES]
WHERE label = '%s'
AND schedule_status = 'ACTIVE'`,
schematelemetrycontroller.SchemaTelemetryScheduleName)
qSet = fmt.Sprintf(`SET CLUSTER SETTING %s = '* * * * *'`,
schematelemetrycontroller.SchemaTelemetryRecurrence.InternalKey())
qJob = fmt.Sprintf(`SELECT %s()`,
builtinconstants.CreateSchemaTelemetryJobBuiltinName)
)
const qHasJob = `SELECT count(*) FROM crdb_internal.jobs WHERE job_type = 'AUTO SCHEMA TELEMETRY' AND status = 'succeeded'`
func TestSchemaTelemetrySchedule(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, db, _ := serverutils.StartServer(t, makeTestServerArgs())
defer s.Stopper().Stop(ctx)
tdb := sqlutils.MakeSQLRunner(db)
clusterID := s.ExecutorConfig().(sql.ExecutorConfig).NodeInfo.
LogicalClusterID()
exp := scheduledjobs.MaybeRewriteCronExpr(clusterID, "@weekly")
tdb.CheckQueryResultsRetry(t, qExists, [][]string{{exp, "1"}})
tdb.ExecSucceedsSoon(t, qSet)
tdb.CheckQueryResultsRetry(t, qExists, [][]string{{"* * * * *", "1"}})
}
func TestSchemaTelemetryJob(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, db, _ := serverutils.StartServer(t, makeTestServerArgs())
defer s.Stopper().Stop(ctx)
tdb := sqlutils.MakeSQLRunner(db)
tdb.Exec(t, `SET CLUSTER SETTING server.eventlog.enabled = true`)
// Pause the existing schema telemetry schedule so that it doesn't interfere.
res := tdb.QueryStr(t, qID)
require.NotEmpty(t, res)
require.NotEmpty(t, res[0])
id := res[0][0]
tdb.ExecSucceedsSoon(t, fmt.Sprintf("PAUSE SCHEDULE %s", id))
tdb.CheckQueryResults(t, qHasJob, [][]string{{"0"}})
// Run a schema telemetry job and wait for it to succeed.
tdb.Exec(t, qJob)
tdb.CheckQueryResultsRetry(t, qHasJob, [][]string{{"1"}})
}