Skip to content

Commit a12a2ad

Browse files
committed
scheduledjobs: move MaybeRewriteCronExpr into package
This was moved from the schematelemetrycontroller package. There are no code changes in this commit. Release note: None
1 parent 6155338 commit a12a2ad

File tree

11 files changed

+87
-63
lines changed

11 files changed

+87
-63
lines changed

pkg/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ ALL_TESTS = [
279279
"//pkg/rpc/nodedialer:nodedialer_test",
280280
"//pkg/rpc:rpc_test",
281281
"//pkg/scheduledjobs/schedulebase:schedulebase_test",
282+
"//pkg/scheduledjobs:scheduledjobs_test",
282283
"//pkg/security/certmgr:certmgr_test",
283284
"//pkg/security/password:password_test",
284285
"//pkg/security/sessionrevival:sessionrevival_test",
@@ -352,7 +353,6 @@ ALL_TESTS = [
352353
"//pkg/sql/catalog/resolver:resolver_test",
353354
"//pkg/sql/catalog/schemadesc:schemadesc_test",
354355
"//pkg/sql/catalog/schemaexpr:schemaexpr_test",
355-
"//pkg/sql/catalog/schematelemetry/schematelemetrycontroller:schematelemetrycontroller_test",
356356
"//pkg/sql/catalog/schematelemetry:schematelemetry_test",
357357
"//pkg/sql/catalog/seqexpr:seqexpr_disallowed_imports_test",
358358
"//pkg/sql/catalog/seqexpr:seqexpr_test",
@@ -1495,6 +1495,7 @@ GO_TARGETS = [
14951495
"//pkg/scheduledjobs/schedulebase:schedulebase",
14961496
"//pkg/scheduledjobs/schedulebase:schedulebase_test",
14971497
"//pkg/scheduledjobs:scheduledjobs",
1498+
"//pkg/scheduledjobs:scheduledjobs_test",
14981499
"//pkg/security/certmgr:certmgr",
14991500
"//pkg/security/certmgr:certmgr_test",
15001501
"//pkg/security/certnames:certnames",
@@ -1664,7 +1665,6 @@ GO_TARGETS = [
16641665
"//pkg/sql/catalog/schemaexpr:schemaexpr",
16651666
"//pkg/sql/catalog/schemaexpr:schemaexpr_test",
16661667
"//pkg/sql/catalog/schematelemetry/schematelemetrycontroller:schematelemetrycontroller",
1667-
"//pkg/sql/catalog/schematelemetry/schematelemetrycontroller:schematelemetrycontroller_test",
16681668
"//pkg/sql/catalog/schematelemetry:schematelemetry",
16691669
"//pkg/sql/catalog/schematelemetry:schematelemetry_test",
16701670
"//pkg/sql/catalog/seqexpr:seqexpr",

pkg/scheduledjobs/BUILD.bazel

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "scheduledjobs",
5-
srcs = ["env.go"],
5+
srcs = [
6+
"env.go",
7+
"rewrite_cron_expr.go",
8+
],
69
importpath = "github.com/cockroachdb/cockroach/pkg/scheduledjobs",
710
visibility = ["//visibility:public"],
811
deps = [
@@ -14,5 +17,21 @@ go_library(
1417
"//pkg/sql/isql",
1518
"//pkg/util/hlc",
1619
"//pkg/util/timeutil",
20+
"//pkg/util/uuid",
21+
],
22+
)
23+
24+
go_test(
25+
name = "scheduledjobs_test",
26+
srcs = ["rewrite_cron_expr_test.go"],
27+
args = ["-test.timeout=295s"],
28+
data = glob(["testdata/**"]),
29+
embed = [":scheduledjobs"],
30+
deps = [
31+
"//pkg/testutils/datapathutils",
32+
"//pkg/util/uuid",
33+
"@com_github_cockroachdb_datadriven//:datadriven",
34+
"@com_github_robfig_cron_v3//:cron",
35+
"@com_github_stretchr_testify//require",
1736
],
1837
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2023 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the file licenses/BSL.txt.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0, included in the file
9+
// licenses/APL.txt.
10+
11+
package scheduledjobs
12+
13+
import (
14+
"fmt"
15+
"hash/fnv"
16+
"math/rand"
17+
18+
"github.com/cockroachdb/cockroach/pkg/util/uuid"
19+
)
20+
21+
const (
22+
cronWeekly = "@weekly"
23+
cronDaily = "@daily"
24+
cronHourly = "@hourly"
25+
)
26+
27+
// MaybeRewriteCronExpr is used to rewrite the interval-oriented cron exprs
28+
// into an equivalent frequency interval but with an offset derived from the
29+
// uuid. For a given pair of inputs, the output of this function will always
30+
// be the same. If the input cronExpr is not a special form as denoted by
31+
// the keys of cronExprRewrites, it will be returned unmodified. This rewrite
32+
// occurs in order to uniformly distribute the production of telemetry logs
33+
// over the intended time interval to avoid bursts.
34+
func MaybeRewriteCronExpr(id uuid.UUID, cronExpr string) string {
35+
if f, ok := cronExprRewrites[cronExpr]; ok {
36+
hash := fnv.New64a() // arbitrary hash function
37+
_, _ = hash.Write(id.GetBytes())
38+
return f(rand.New(rand.NewSource(int64(hash.Sum64()))))
39+
}
40+
return cronExpr
41+
}
42+
43+
var cronExprRewrites = map[string]func(r *rand.Rand) string{
44+
cronWeekly: func(r *rand.Rand) string {
45+
return fmt.Sprintf("%d %d * * %d", r.Intn(60), r.Intn(23), r.Intn(7))
46+
},
47+
cronDaily: func(r *rand.Rand) string {
48+
return fmt.Sprintf("%d %d * * *", r.Intn(60), r.Intn(23))
49+
},
50+
cronHourly: func(r *rand.Rand) string {
51+
return fmt.Sprintf("%d * * * *", r.Intn(60))
52+
},
53+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// by the Apache License, Version 2.0, included in the file
99
// licenses/APL.txt.
1010

11-
package schematelemetrycontroller
11+
package scheduledjobs
1212

1313
import (
1414
"bufio"

pkg/sql/catalog/schematelemetry/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ go_test(
4949
"//pkg/base",
5050
"//pkg/jobs",
5151
"//pkg/jobs/jobstest",
52+
"//pkg/scheduledjobs",
5253
"//pkg/security/securityassets",
5354
"//pkg/security/securitytest",
5455
"//pkg/server",

pkg/sql/catalog/schematelemetry/schema_telemetry_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/cockroachdb/cockroach/pkg/base"
2020
"github.com/cockroachdb/cockroach/pkg/jobs"
2121
"github.com/cockroachdb/cockroach/pkg/jobs/jobstest"
22+
"github.com/cockroachdb/cockroach/pkg/scheduledjobs"
2223
"github.com/cockroachdb/cockroach/pkg/sql"
2324
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schematelemetry/schematelemetrycontroller"
2425
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins/builtinconstants"
@@ -82,7 +83,7 @@ func TestSchemaTelemetrySchedule(t *testing.T) {
8283

8384
clusterID := s.ExecutorConfig().(sql.ExecutorConfig).NodeInfo.
8485
LogicalClusterID()
85-
exp := schematelemetrycontroller.MaybeRewriteCronExpr(clusterID, "@weekly")
86+
exp := scheduledjobs.MaybeRewriteCronExpr(clusterID, "@weekly")
8687
tdb.CheckQueryResultsRetry(t, qExists, [][]string{{exp, "1"}})
8788
tdb.ExecSucceedsSoon(t, qSet)
8889
tdb.CheckQueryResultsRetry(t, qExists, [][]string{{"* * * * *", "1"}})

pkg/sql/catalog/schematelemetry/schematelemetrycontroller/BUILD.bazel

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@rules_proto//proto:defs.bzl", "proto_library")
2-
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
33
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
44

55
proto_library(
@@ -43,18 +43,3 @@ go_library(
4343
"@com_github_robfig_cron_v3//:cron",
4444
],
4545
)
46-
47-
go_test(
48-
name = "schematelemetrycontroller_test",
49-
srcs = ["rewrite_cron_expr_test.go"],
50-
args = ["-test.timeout=295s"],
51-
data = glob(["testdata/**"]),
52-
embed = [":schematelemetrycontroller"],
53-
deps = [
54-
"//pkg/testutils/datapathutils",
55-
"//pkg/util/uuid",
56-
"@com_github_cockroachdb_datadriven//:datadriven",
57-
"@com_github_robfig_cron_v3//:cron",
58-
"@com_github_stretchr_testify//require",
59-
],
60-
)

pkg/sql/catalog/schematelemetry/schematelemetrycontroller/controller.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ package schematelemetrycontroller
1212

1313
import (
1414
"context"
15-
"fmt"
16-
"hash/fnv"
17-
"math/rand"
1815
"time"
1916

2017
"github.com/cockroachdb/cockroach/pkg/jobs"
@@ -39,19 +36,13 @@ import (
3936
// SchemaTelemetryScheduleName is the name of the schema telemetry schedule.
4037
const SchemaTelemetryScheduleName = "sql-schema-telemetry"
4138

42-
const (
43-
cronWeekly = "@weekly"
44-
cronDaily = "@daily"
45-
cronHourly = "@hourly"
46-
)
47-
4839
// SchemaTelemetryRecurrence is the cron-tab string specifying the recurrence
4940
// for schema telemetry job.
5041
var SchemaTelemetryRecurrence = settings.RegisterValidatedStringSetting(
5142
settings.TenantReadOnly,
5243
"sql.schema.telemetry.recurrence",
5344
"cron-tab recurrence for SQL schema telemetry job",
54-
cronWeekly, /* defaultValue */
45+
"@weekly", /* defaultValue */
5546
func(_ *settings.Values, s string) error {
5647
if _, err := cron.ParseStandard(s); err != nil {
5748
return errors.Wrap(err, "invalid cron expression")
@@ -166,7 +157,7 @@ func updateSchedule(ctx context.Context, db isql.DB, st *cluster.Settings, clust
166157
}
167158
}
168159
// Update schedule with new recurrence, if different.
169-
cronExpr := MaybeRewriteCronExpr(
160+
cronExpr := scheduledjobs.MaybeRewriteCronExpr(
170161
clusterID, SchemaTelemetryRecurrence.Get(&st.SV),
171162
)
172163
if sj.ScheduleExpr() == cronExpr {
@@ -185,34 +176,6 @@ func updateSchedule(ctx context.Context, db isql.DB, st *cluster.Settings, clust
185176
}
186177
}
187178

188-
// MaybeRewriteCronExpr is used to rewrite the interval-oriented cron exprs
189-
// into an equivalent frequency interval but with an offset derived from the
190-
// uuid. For a given pair of inputs, the output of this function will always
191-
// be the same. If the input cronExpr is not a special form as denoted by
192-
// the keys of cronExprRewrites, it will be returned unmodified. This rewrite
193-
// occurs in order to uniformly distribute the production of telemetry logs
194-
// over the intended time interval to avoid bursts.
195-
func MaybeRewriteCronExpr(id uuid.UUID, cronExpr string) string {
196-
if f, ok := cronExprRewrites[cronExpr]; ok {
197-
hash := fnv.New64a() // arbitrary hash function
198-
_, _ = hash.Write(id.GetBytes())
199-
return f(rand.New(rand.NewSource(int64(hash.Sum64()))))
200-
}
201-
return cronExpr
202-
}
203-
204-
var cronExprRewrites = map[string]func(r *rand.Rand) string{
205-
cronWeekly: func(r *rand.Rand) string {
206-
return fmt.Sprintf("%d %d * * %d", r.Intn(60), r.Intn(23), r.Intn(7))
207-
},
208-
cronDaily: func(r *rand.Rand) string {
209-
return fmt.Sprintf("%d %d * * *", r.Intn(60), r.Intn(23))
210-
},
211-
cronHourly: func(r *rand.Rand) string {
212-
return fmt.Sprintf("%d * * * *", r.Intn(60))
213-
},
214-
}
215-
216179
// CreateSchemaTelemetryJob is part of the eval.SchemaTelemetryController
217180
// interface.
218181
func (c *Controller) CreateSchemaTelemetryJob(

pkg/upgrade/upgrades/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ go_test(
148148
"//pkg/keys",
149149
"//pkg/kv",
150150
"//pkg/roachpb",
151+
"//pkg/scheduledjobs",
151152
"//pkg/security/securityassets",
152153
"//pkg/security/securitytest",
153154
"//pkg/security/username",

0 commit comments

Comments
 (0)