Skip to content

Commit da46ac6

Browse files
committed
sql/hints: add tests for statement hints cache
This commit adds a set of tests for the behavior of the hint cache. The tests verify that the cache is correctly populated in response to existing hints as well as new insertions and deletions. Epic: None Release note: None
1 parent ca786ee commit da46ac6

File tree

5 files changed

+551
-1
lines changed

5 files changed

+551
-1
lines changed

pkg/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ ALL_TESTS = [
506506
"//pkg/sql/gcjob/gcjobnotifier:gcjobnotifier_test",
507507
"//pkg/sql/gcjob:gcjob_test",
508508
"//pkg/sql/gcjob_test:gcjob_test_test",
509+
"//pkg/sql/hints:hints_test",
509510
"//pkg/sql/idxrecommendations:idxrecommendations_test",
510511
"//pkg/sql/idxusage:idxusage_test",
511512
"//pkg/sql/importer:importer_test",
@@ -2066,6 +2067,7 @@ GO_TARGETS = [
20662067
"//pkg/sql/gcjob:gcjob_test",
20672068
"//pkg/sql/gcjob_test:gcjob_test_test",
20682069
"//pkg/sql/hints:hints",
2070+
"//pkg/sql/hints:hints_test",
20692071
"//pkg/sql/idxrecommendations:idxrecommendations",
20702072
"//pkg/sql/idxrecommendations:idxrecommendations_test",
20712073
"//pkg/sql/idxusage:idxusage",

pkg/sql/hints/BUILD.bazel

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_proto//proto:defs.bzl", "proto_library")
22
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
3-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
3+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
44

55
go_library(
66
name = "hints",
@@ -59,3 +59,32 @@ proto_library(
5959
visibility = ["//visibility:public"],
6060
deps = ["@com_github_gogo_protobuf//gogoproto:gogo_proto"],
6161
)
62+
63+
go_test(
64+
name = "hints_test",
65+
srcs = [
66+
"hint_cache_test.go",
67+
"main_test.go",
68+
],
69+
deps = [
70+
":hints",
71+
"//pkg/base",
72+
"//pkg/kv/kvclient/rangefeed",
73+
"//pkg/security/securityassets",
74+
"//pkg/security/securitytest",
75+
"//pkg/server",
76+
"//pkg/sql/catalog",
77+
"//pkg/sql/catalog/descs",
78+
"//pkg/sql/randgen",
79+
"//pkg/sql/stats",
80+
"//pkg/testutils",
81+
"//pkg/testutils/serverutils",
82+
"//pkg/testutils/sqlutils",
83+
"//pkg/testutils/testcluster",
84+
"//pkg/util/leaktest",
85+
"//pkg/util/log",
86+
"//pkg/util/randutil",
87+
"@com_github_cockroachdb_errors//:errors",
88+
"@com_github_stretchr_testify//require",
89+
],
90+
)

pkg/sql/hints/hint_cache.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ type StatementHintsCache struct {
6868
// system.statement_hints table. Cached hints are never modified after being
6969
// added to the cache, and so can be returned directly without copying.
7070
hintCache *cache.UnorderedCache
71+
72+
// Used for testing; keeps track of how many times we actually read hints
73+
// from the system table. Note that this count only includes reads used for
74+
// hintCache, not for hintedHashes.
75+
// TODO(drewk): consider making this a metric.
76+
numInternalQueries int
7177
}
7278

7379
// Used to start/coordinate the rangefeed.
@@ -445,6 +451,7 @@ func (c *StatementHintsCache) addCacheEntryLocked(
445451
waitCond: sync.Cond{L: &c.mu},
446452
}
447453
c.mu.hintCache.Add(statementHash, entry)
454+
c.mu.numInternalQueries++
448455

449456
var err error
450457
func() {
@@ -535,3 +542,31 @@ func (entry *cacheEntry) getMatchingHints(statementFingerprint string) []Stateme
535542
}
536543
return res
537544
}
545+
546+
// ============================================================================
547+
// Test helpers.
548+
// ============================================================================
549+
550+
// TestingHashCount returns the number of hashes with hints.
551+
func (c *StatementHintsCache) TestingHashCount() int {
552+
c.mu.Lock()
553+
defer c.mu.Unlock()
554+
return len(c.mu.hintedHashes)
555+
}
556+
557+
// TestingHashHasHints returns true if the given hash has any hints, and false
558+
// otherwise.
559+
func (c *StatementHintsCache) TestingHashHasHints(hash int64) bool {
560+
c.mu.Lock()
561+
defer c.mu.Unlock()
562+
_, hasHints := c.mu.hintedHashes[hash]
563+
return hasHints
564+
}
565+
566+
// TestingNumTableReads returns the number of times hints have been read from
567+
// the system.statement_hints table.
568+
func (c *StatementHintsCache) TestingNumTableReads() int {
569+
c.mu.Lock()
570+
defer c.mu.Unlock()
571+
return c.mu.numInternalQueries
572+
}

0 commit comments

Comments
 (0)