Skip to content

Commit 22ebc1c

Browse files
committed
sqlstats/insights: extract proto into a separate package
This allows us to break dependency of `serverpb` on `sql/parser` (which was recently introduced when we added support for SQL Commenter query tags). Release note: None
1 parent f00ff8d commit 22ebc1c

20 files changed

+201
-160
lines changed

pkg/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ ALL_TESTS = [
360360
"//pkg/server/pgurl:pgurl_test",
361361
"//pkg/server/privchecker:privchecker_test",
362362
"//pkg/server/profiler:profiler_test",
363+
"//pkg/server/serverpb:serverpb_disallowed_imports_test",
363364
"//pkg/server/serverpb:serverpb_test",
364365
"//pkg/server/serverrules:serverrules_test",
365366
"//pkg/server/settingswatcher:settingswatcher_test",
@@ -2373,6 +2374,7 @@ GO_TARGETS = [
23732374
"//pkg/sql/sqlstats/insights/integration:integration_test",
23742375
"//pkg/sql/sqlstats/insights:insights",
23752376
"//pkg/sql/sqlstats/insights:insights_test",
2377+
"//pkg/sql/sqlstats/insightspb:insightspb",
23762378
"//pkg/sql/sqlstats/persistedsqlstats/sqlstatstestutil:sqlstatstestutil",
23772379
"//pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil:sqlstatsutil",
23782380
"//pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil:sqlstatsutil_test",

pkg/gen/protobuf.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ PROTOBUF_SRCS = [
7272
"//pkg/sql/sem/idxtype:idxtype_go_proto",
7373
"//pkg/sql/sem/semenumpb:semenumpb_go_proto",
7474
"//pkg/sql/sessiondatapb:sessiondatapb_go_proto",
75-
"//pkg/sql/sqlstats/insights:insights_go_proto",
75+
"//pkg/sql/sqlstats/insightspb:insightspb_go_proto",
7676
"//pkg/sql/sqlstats/persistedsqlstats:persistedsqlstats_go_proto",
7777
"//pkg/sql/stats:stats_go_proto",
7878
"//pkg/sql/types:types_go_proto",

pkg/protos.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ SERVER_PROTOS = [
3838
"//pkg/sql/sem/idxtype:idxtype_proto",
3939
"//pkg/sql/sem/semenumpb:semenumpb_proto",
4040
"//pkg/sql/sessiondatapb:sessiondatapb_proto",
41-
"//pkg/sql/sqlstats/insights:insights_proto",
41+
"//pkg/sql/sqlstats/insightspb:insightspb_proto",
4242
"//pkg/sql/types:types_proto",
4343
"//pkg/sql/vecindex/vecpb:vecpb_proto",
4444
"//pkg/storage/enginepb:enginepb_proto",
@@ -132,7 +132,7 @@ PROTO_FILES = [
132132
"//pkg/sql/sessiondatapb:session_data.proto",
133133
"//pkg/sql/sessiondatapb:session_migration.proto",
134134
"//pkg/sql/sessiondatapb:session_revival_token.proto",
135-
"//pkg/sql/sqlstats/insights:insights.proto",
135+
"//pkg/sql/sqlstats/insightspb:insights.proto",
136136
"//pkg/sql/types:types.proto",
137137
"//pkg/sql/vecindex/vecpb:vec.proto",
138138
"//pkg/storage/enginepb:file_registry.proto",

pkg/server/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ go_library(
278278
"//pkg/sql/sqlliveness/slinstance",
279279
"//pkg/sql/sqlliveness/slprovider",
280280
"//pkg/sql/sqlstats",
281-
"//pkg/sql/sqlstats/insights",
281+
"//pkg/sql/sqlstats/insightspb",
282282
"//pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil",
283283
"//pkg/sql/sqlstats/sslocal",
284284
"//pkg/sql/stats",
@@ -567,6 +567,7 @@ go_test(
567567
"//pkg/sql/sessiondata",
568568
"//pkg/sql/sqlstats",
569569
"//pkg/sql/sqlstats/insights",
570+
"//pkg/sql/sqlstats/insightspb",
570571
"//pkg/sql/sqlstats/persistedsqlstats/sqlstatstestutil",
571572
"//pkg/sql/tablemetadatacache",
572573
"//pkg/sql/tablemetadatacache/util",

pkg/server/serverpb/BUILD.bazel

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
44
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
55
load("@rules_proto//proto:defs.bzl", "proto_library")
6+
load("//pkg/testutils:buildutil/buildutil.bzl", "disallowed_imports_test")
67

78
proto_library(
89
name = "serverpb_proto",
@@ -33,7 +34,7 @@ proto_library(
3334
"//pkg/server/status/statuspb:statuspb_proto",
3435
"//pkg/sql/appstatspb:appstatspb_proto",
3536
"//pkg/sql/contentionpb:contentionpb_proto",
36-
"//pkg/sql/sqlstats/insights:insights_proto",
37+
"//pkg/sql/sqlstats/insightspb:insightspb_proto",
3738
"//pkg/storage/enginepb:enginepb_proto",
3839
"//pkg/ts/catalog:catalog_proto",
3940
"//pkg/util:util_proto",
@@ -88,7 +89,7 @@ go_proto_library(
8889
"//pkg/server/status/statuspb",
8990
"//pkg/sql/appstatspb",
9091
"//pkg/sql/contentionpb",
91-
"//pkg/sql/sqlstats/insights",
92+
"//pkg/sql/sqlstats/insightspb",
9293
"//pkg/storage/enginepb",
9394
"//pkg/ts/catalog",
9495
"//pkg/util",
@@ -126,3 +127,8 @@ go_test(
126127
embed = [":serverpb"],
127128
deps = ["@com_github_stretchr_testify//assert"],
128129
)
130+
131+
disallowed_imports_test(
132+
"serverpb",
133+
disallowed_list = ["//pkg/sql/parser"],
134+
)

pkg/server/serverpb/status.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import "server/serverpb/index_recommendations.proto";
2323
import "server/status/statuspb/status.proto";
2424
import "sql/appstatspb/app_stats.proto";
2525
import "sql/contentionpb/contention.proto";
26-
import "sql/sqlstats/insights/insights.proto";
26+
import "sql/sqlstats/insightspb/insights.proto";
2727
import "storage/enginepb/key_registry.proto";
2828
import "storage/enginepb/mvcc.proto";
2929
import "storage/enginepb/stats.proto";

pkg/server/status.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import (
7474
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
7575
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
7676
"github.com/cockroachdb/cockroach/pkg/sql/sqlinstance"
77-
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insights"
77+
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insightspb"
7878
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
7979
"github.com/cockroachdb/cockroach/pkg/util/hlc"
8080
"github.com/cockroachdb/cockroach/pkg/util/httputil"
@@ -395,18 +395,18 @@ func (b *baseStatusServer) localExecutionInsights(
395395
) (*serverpb.ListExecutionInsightsResponse, error) {
396396
var response serverpb.ListExecutionInsightsResponse
397397

398-
highContentionInsights := make(map[uuid.UUID]insights.Insight, 0)
398+
highContentionInsights := make(map[uuid.UUID]insightspb.Insight, 0)
399399
reader := b.sqlServer.pgServer.SQLServer.GetInsightsReader()
400-
reader.IterateInsights(ctx, func(ctx context.Context, insight *insights.Insight) {
400+
reader.IterateInsights(ctx, func(ctx context.Context, insight *insightspb.Insight) {
401401
if insight == nil {
402402
return
403403
}
404404

405405
// Copy statements slice - these insights objects can be read concurrently.
406406
insightsCopy := *insight
407-
insightsCopy.Statements = make([]*insights.Statement, len(insight.Statements))
407+
insightsCopy.Statements = make([]*insightspb.Statement, len(insight.Statements))
408408
copy(insightsCopy.Statements, insight.Statements)
409-
if insight.Transaction != nil && slices.Contains(insight.Transaction.Causes, insights.Cause_HighContention) {
409+
if insight.Transaction != nil && slices.Contains(insight.Transaction.Causes, insightspb.Cause_HighContention) {
410410
// Collect high contention insights seperately, they need additional validation / filtering.
411411
// If it is valid we will add it to the response later.
412412
highContentionInsights[insightsCopy.Transaction.ID] = insightsCopy
@@ -437,7 +437,7 @@ func (b *baseStatusServer) localExecutionInsights(
437437
// events that are related to the passed in (contention) insights. The insights that have
438438
// valid (resolved BlockingTxnFingerprintID) contention events are returned.
439439
func validContentionInsights(
440-
registry *contention.Registry, contentionInsights map[uuid.UUID]insights.Insight,
440+
registry *contention.Registry, contentionInsights map[uuid.UUID]insightspb.Insight,
441441
) (map[uuid.UUID]bool, error) {
442442
valid := make(map[uuid.UUID]bool, len(contentionInsights))
443443
err := registry.ForEachEvent(func(event *contentionpb.ExtendedContentionEvent) error {

pkg/server/status_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/cockroachdb/cockroach/pkg/sql/contention"
3131
"github.com/cockroachdb/cockroach/pkg/sql/contentionpb"
3232
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insights"
33+
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insightspb"
3334
tablemetadatacacheutil "github.com/cockroachdb/cockroach/pkg/sql/tablemetadatacache/util"
3435
"github.com/cockroachdb/cockroach/pkg/testutils"
3536
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
@@ -534,7 +535,7 @@ func TestLocalExecutionInsights(t *testing.T) {
534535
id1 := uuid.MakeV4()
535536
id2 := uuid.MakeV4()
536537
id3 := uuid.MakeV4()
537-
insights := map[uuid.UUID]insights.Insight{
538+
insights := map[uuid.UUID]insightspb.Insight{
538539
id1: {},
539540
id2: {},
540541
id3: {},

pkg/sql/sqlstats/insights/BUILD.bazel

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
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(
@@ -14,7 +12,6 @@ go_library(
1412
"store.go",
1513
"util.go",
1614
],
17-
embed = [":insights_go_proto"],
1815
importpath = "github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insights",
1916
visibility = ["//visibility:public"],
2017
deps = [
@@ -25,6 +22,7 @@ go_library(
2522
"//pkg/sql/pgwire/pgerror",
2623
"//pkg/sql/sqlcommenter",
2724
"//pkg/sql/sqlstats",
25+
"//pkg/sql/sqlstats/insightspb",
2826
"//pkg/util/cache",
2927
"//pkg/util/intsets",
3028
"//pkg/util/metric",
@@ -55,33 +53,10 @@ go_test(
5553
"//pkg/sql/pgwire/pgcode",
5654
"//pkg/sql/pgwire/pgerror",
5755
"//pkg/sql/sqlstats",
56+
"//pkg/sql/sqlstats/insightspb",
5857
"//pkg/util/stop",
5958
"//pkg/util/uint128",
6059
"//pkg/util/uuid",
6160
"@com_github_stretchr_testify//require",
6261
],
6362
)
64-
65-
proto_library(
66-
name = "insights_proto",
67-
srcs = ["insights.proto"],
68-
strip_import_prefix = "/pkg",
69-
visibility = ["//visibility:public"],
70-
deps = [
71-
"@com_github_gogo_protobuf//gogoproto:gogo_proto",
72-
"@com_google_protobuf//:duration_proto",
73-
"@com_google_protobuf//:timestamp_proto",
74-
],
75-
)
76-
77-
go_proto_library(
78-
name = "insights_go_proto",
79-
compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_compiler"],
80-
importpath = "github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insights",
81-
proto = ":insights_proto",
82-
visibility = ["//visibility:public"],
83-
deps = [
84-
"//pkg/util/uuid", # keep
85-
"@com_github_gogo_protobuf//gogoproto",
86-
],
87-
)

pkg/sql/sqlstats/insights/causes.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,31 @@
55

66
package insights
77

8-
import "github.com/cockroachdb/cockroach/pkg/settings/cluster"
8+
import (
9+
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
10+
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/insightspb"
11+
)
912

1013
type causes struct {
1114
st *cluster.Settings
1215
}
1316

1417
// examine will append all causes of the statement's problems to buf and
1518
// return the result. Buf allows the slice to be pooled.
16-
func (c *causes) examine(buf []Cause, stmt *Statement) (result []Cause) {
19+
func (c *causes) examine(
20+
buf []insightspb.Cause, stmt *insightspb.Statement,
21+
) (result []insightspb.Cause) {
1722
result = buf
1823
if len(stmt.IndexRecommendations) > 0 {
19-
result = append(result, Cause_SuboptimalPlan)
24+
result = append(result, insightspb.Cause_SuboptimalPlan)
2025
}
2126

2227
if stmt.Contention != nil && *stmt.Contention >= LatencyThreshold.Get(&c.st.SV) {
23-
result = append(result, Cause_HighContention)
28+
result = append(result, insightspb.Cause_HighContention)
2429
}
2530

2631
if stmt.Retries >= HighRetryCountThreshold.Get(&c.st.SV) {
27-
result = append(result, Cause_HighRetryCount)
32+
result = append(result, insightspb.Cause_HighRetryCount)
2833
}
2934

3035
return result

0 commit comments

Comments
 (0)