Skip to content

Commit 0c9b934

Browse files
committed
hints: separate out hint protobuf into separate package
This commit adds a new package `hintpb` for the protobuf structs used to store hints in the `system.statement_hints` table. This will avoid dependency cycles in future commits. Epic: None Release note: None
1 parent 78a301f commit 0c9b934

File tree

8 files changed

+44
-34
lines changed

8 files changed

+44
-34
lines changed

pkg/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,7 @@ GO_TARGETS = [
20702070
"//pkg/sql/gcjob:gcjob",
20712071
"//pkg/sql/gcjob:gcjob_test",
20722072
"//pkg/sql/gcjob_test:gcjob_test_test",
2073+
"//pkg/sql/hintpb:hintpb",
20732074
"//pkg/sql/hints:hints",
20742075
"//pkg/sql/hints:hints_test",
20752076
"//pkg/sql/idxrecommendations:idxrecommendations",

pkg/gen/protobuf.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ PROTOBUF_SRCS = [
6363
"//pkg/sql/catalog/schematelemetry/schematelemetrycontroller:schematelemetrycontroller_go_proto",
6464
"//pkg/sql/contentionpb:contentionpb_go_proto",
6565
"//pkg/sql/execinfrapb:execinfrapb_go_proto",
66-
"//pkg/sql/hints:hints_go_proto",
66+
"//pkg/sql/hintpb:hintpb_go_proto",
6767
"//pkg/sql/inverted:inverted_go_proto",
6868
"//pkg/sql/lex:lex_go_proto",
6969
"//pkg/sql/pgwire/pgerror:pgerror_go_proto",

pkg/sql/hintpb/BUILD.bazel

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
3+
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
4+
5+
proto_library(
6+
name = "hintpb_proto",
7+
srcs = ["statement_hint.proto"],
8+
strip_import_prefix = "/pkg",
9+
visibility = ["//visibility:public"],
10+
deps = ["@com_github_gogo_protobuf//gogoproto:gogo_proto"],
11+
)
12+
13+
go_proto_library(
14+
name = "hintpb_go_proto",
15+
compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_compiler"],
16+
importpath = "github.com/cockroachdb/cockroach/pkg/sql/hintpb",
17+
proto = ":hintpb_proto",
18+
visibility = ["//visibility:public"],
19+
deps = ["@com_github_gogo_protobuf//gogoproto"],
20+
)
21+
22+
go_library(
23+
name = "hintpb",
24+
srcs = ["statement_hint.go"],
25+
embed = [":hintpb_go_proto"],
26+
importpath = "github.com/cockroachdb/cockroach/pkg/sql/hintpb",
27+
visibility = ["//visibility:public"],
28+
deps = ["//pkg/util/protoutil"],
29+
)

pkg/sql/hints/statement_hint.go renamed to pkg/sql/hintpb/statement_hint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Use of this software is governed by the CockroachDB Software License
44
// included in the /LICENSE file.
55

6-
package hints
6+
package hintpb
77

88
import "github.com/cockroachdb/cockroach/pkg/util/protoutil"
99

pkg/sql/hints/statement_hint.proto renamed to pkg/sql/hintpb/statement_hint.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// included in the /LICENSE file.
55

66
syntax = "proto3";
7-
package hints;
7+
package hintpb;
88

99
import "gogoproto/gogo.proto";
1010

pkg/sql/hints/BUILD.bazel

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
load("@rules_proto//proto:defs.bzl", "proto_library")
2-
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
31
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
42

53
go_library(
64
name = "hints",
7-
srcs = [
8-
"hint_cache.go",
9-
"statement_hint.go",
10-
],
11-
embed = [":hints_go_proto"],
5+
srcs = ["hint_cache.go"],
126
importpath = "github.com/cockroachdb/cockroach/pkg/sql/hints",
137
visibility = ["//visibility:public"],
148
deps = [
@@ -25,6 +19,7 @@ go_library(
2519
"//pkg/sql/catalog/descpb",
2620
"//pkg/sql/catalog/descs",
2721
"//pkg/sql/catalog/systemschema",
22+
"//pkg/sql/hintpb",
2823
"//pkg/sql/rowenc",
2924
"//pkg/sql/sem/tree",
3025
"//pkg/sql/sessiondata",
@@ -34,7 +29,6 @@ go_library(
3429
"//pkg/util/hlc",
3530
"//pkg/util/log",
3631
"//pkg/util/metamorphic",
37-
"//pkg/util/protoutil",
3832
"//pkg/util/retry",
3933
"//pkg/util/startup",
4034
"//pkg/util/stop",
@@ -43,23 +37,6 @@ go_library(
4337
],
4438
)
4539

46-
go_proto_library(
47-
name = "hints_go_proto",
48-
compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_compiler"],
49-
importpath = "github.com/cockroachdb/cockroach/pkg/sql/hints",
50-
proto = ":hints_proto",
51-
visibility = ["//visibility:public"],
52-
deps = ["@com_github_gogo_protobuf//gogoproto"],
53-
)
54-
55-
proto_library(
56-
name = "hints_proto",
57-
srcs = ["statement_hint.proto"],
58-
strip_import_prefix = "/pkg",
59-
visibility = ["//visibility:public"],
60-
deps = ["@com_github_gogo_protobuf//gogoproto:gogo_proto"],
61-
)
62-
6340
go_test(
6441
name = "hints_test",
6542
size = "medium",
@@ -80,6 +57,7 @@ go_test(
8057
"//pkg/server",
8158
"//pkg/sql/catalog",
8259
"//pkg/sql/catalog/descs",
60+
"//pkg/sql/hintpb",
8361
"//pkg/sql/randgen",
8462
"//pkg/sql/stats",
8563
"//pkg/testutils",

pkg/sql/hints/hint_cache.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
2626
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
2727
"github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema"
28+
"github.com/cockroachdb/cockroach/pkg/sql/hintpb"
2829
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
2930
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
3031
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
@@ -392,7 +393,7 @@ func (c *StatementHintsCache) GetGeneration() int64 {
392393
// retrieving them.
393394
func (c *StatementHintsCache) MaybeGetStatementHints(
394395
ctx context.Context, statementFingerprint string,
395-
) (hints []StatementHint, ids []int64) {
396+
) (hints []hintpb.StatementHint, ids []int64) {
396397
hash := fnv.New64()
397398
_, err := hash.Write([]byte(statementFingerprint))
398399
if err != nil {
@@ -450,7 +451,7 @@ func (c *StatementHintsCache) maybeWaitForRefreshLocked(
450451
// released while reading from the db, and then reacquired.
451452
func (c *StatementHintsCache) addCacheEntryLocked(
452453
ctx context.Context, statementHash int64, statementFingerprint string,
453-
) (hints []StatementHint, ids []int64) {
454+
) (hints []hintpb.StatementHint, ids []int64) {
454455
c.mu.AssertHeld()
455456

456457
// Add a cache entry that other queries can find and wait on until we have the
@@ -515,7 +516,7 @@ func (c *StatementHintsCache) getStatementHintsFromDB(
515516
datums := it.Cur()
516517
rowID := int64(tree.MustBeDInt(datums[0]))
517518
fingerprint := string(tree.MustBeDString(datums[1]))
518-
hint, err := NewStatementHint([]byte(tree.MustBeDBytes(datums[2])))
519+
hint, err := hintpb.NewStatementHint([]byte(tree.MustBeDBytes(datums[2])))
519520
if err != nil {
520521
return err
521522
}
@@ -542,7 +543,7 @@ type cacheEntry struct {
542543
// be duplicate entries in the fingerprints slice.
543544
// TODO(drewk): consider de-duplicating the fingerprint strings to reduce
544545
// memory usage.
545-
hints []StatementHint
546+
hints []hintpb.StatementHint
546547
fingerprints []string
547548
ids []int64
548549
}
@@ -551,7 +552,7 @@ type cacheEntry struct {
551552
// fingerprint, or nil if they don't exist. The results are in order of row ID.
552553
func (entry *cacheEntry) getMatchingHints(
553554
statementFingerprint string,
554-
) (hints []StatementHint, ids []int64) {
555+
) (hints []hintpb.StatementHint, ids []int64) {
555556
for i := range entry.hints {
556557
if entry.fingerprints[i] == statementFingerprint {
557558
hints = append(hints, entry.hints[i])

pkg/sql/hints/hint_cache_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed"
1616
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
1717
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
18+
"github.com/cockroachdb/cockroach/pkg/sql/hintpb"
1819
"github.com/cockroachdb/cockroach/pkg/sql/hints"
1920
"github.com/cockroachdb/cockroach/pkg/testutils"
2021
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
@@ -541,7 +542,7 @@ func checkIDOrder(t *testing.T, ids []int64) {
541542
// insertStatementHint inserts an empty statement hint into the
542543
// system.statement_hints table.
543544
func insertStatementHint(t *testing.T, r *sqlutils.SQLRunner, fingerprint string) {
544-
emptyHint := &hints.StatementHintUnion{}
545+
emptyHint := &hintpb.StatementHintUnion{}
545546
hintBytes, err := emptyHint.ToBytes()
546547
require.NoError(t, err)
547548
const insertStmt = `INSERT INTO system.statement_hints ("fingerprint", "hint") VALUES ($1, $2)`

0 commit comments

Comments
 (0)