Skip to content

Commit

Permalink
sql: introduce contention event store
Browse files Browse the repository at this point in the history
This commit introduces contention event store, an in-memory key-value
FIFO store that stores historical contention events and map collected
contention events to their corresponding transaction fingerprint IDs.

Release note (sql change): A new cluster setting
`sql.contention.event_store.capacity` is added . This cluster setting
can be used to control the in-memory capacity of contention event store.
When this setting is set to zero, the contention event store is
disabled.
  • Loading branch information
Azhng committed Feb 17, 2022
1 parent 4f136df commit b5cb37d
Show file tree
Hide file tree
Showing 13 changed files with 667 additions and 29 deletions.
1 change: 1 addition & 0 deletions docs/generated/settings/settings-for-tenants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ server.web_session.purge.max_deletions_per_cycle integer 10 the maximum number o
server.web_session.purge.period duration 1h0m0s the time until old sessions are deleted
server.web_session.purge.ttl duration 1h0m0s if nonzero, entries in system.web_sessions older than this duration are periodically purged
server.web_session_timeout duration 168h0m0s the duration that a newly created web session will be valid
sql.contention.event_store.capacity byte size 64 MiB the in-memory storage capacity per-node of contention event store
sql.contention.txn_id_cache.max_size byte size 64 MiB the maximum byte size TxnID cache will use (set to 0 to disable)
sql.cross_db_fks.enabled boolean false if true, creating foreign key references across databases is allowed
sql.cross_db_sequence_owners.enabled boolean false if true, creating sequences owned by tables from other databases is allowed
Expand Down
1 change: 1 addition & 0 deletions docs/generated/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<tr><td><code>server.web_session.purge.period</code></td><td>duration</td><td><code>1h0m0s</code></td><td>the time until old sessions are deleted</td></tr>
<tr><td><code>server.web_session.purge.ttl</code></td><td>duration</td><td><code>1h0m0s</code></td><td>if nonzero, entries in system.web_sessions older than this duration are periodically purged</td></tr>
<tr><td><code>server.web_session_timeout</code></td><td>duration</td><td><code>168h0m0s</code></td><td>the duration that a newly created web session will be valid</td></tr>
<tr><td><code>sql.contention.event_store.capacity</code></td><td>byte size</td><td><code>64 MiB</code></td><td>the in-memory storage capacity per-node of contention event store</td></tr>
<tr><td><code>sql.contention.txn_id_cache.max_size</code></td><td>byte size</td><td><code>64 MiB</code></td><td>the maximum byte size TxnID cache will use (set to 0 to disable)</td></tr>
<tr><td><code>sql.cross_db_fks.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating foreign key references across databases is allowed</td></tr>
<tr><td><code>sql.cross_db_sequence_owners.enabled</code></td><td>boolean</td><td><code>false</code></td><td>if true, creating sequences owned by tables from other databases is allowed</td></tr>
Expand Down
4 changes: 3 additions & 1 deletion pkg/server/server_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,8 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
if hasNodeLiveness {
traceCollector = collector.New(cfg.nodeDialer, nodeLiveness, cfg.Tracer)
}
contentionRegistry := contention.NewRegistry(cfg.Settings, cfg.sqlStatusServer.TxnIDResolution)
contentionRegistry.Start(ctx, cfg.stopper)

*execCfg = sql.ExecutorConfig{
Settings: cfg.Settings,
Expand All @@ -682,7 +684,7 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
SQLStatusServer: cfg.sqlStatusServer,
RegionsServer: cfg.regionsServer,
SessionRegistry: cfg.sessionRegistry,
ContentionRegistry: contention.NewRegistry(),
ContentionRegistry: contentionRegistry,
SQLLiveness: cfg.sqlLivenessProvider,
JobRegistry: jobRegistry,
VirtualSchemas: virtualSchemas,
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/contention/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "contention",
srcs = [
"cluster_settings.go",
"event_store.go",
"registry.go",
"resolver.go",
"test_utils.go",
Expand All @@ -13,10 +15,16 @@ go_library(
"//pkg/keys",
"//pkg/roachpb",
"//pkg/server/serverpb",
"//pkg/settings",
"//pkg/settings/cluster",
"//pkg/sql/catalog/descpb",
"//pkg/sql/contention/contentionutils",
"//pkg/sql/contentionpb",
"//pkg/util/cache",
"//pkg/util/log",
"//pkg/util/stop",
"//pkg/util/syncutil",
"//pkg/util/timeutil",
"//pkg/util/uuid",
"@com_github_biogo_store//llrb",
"@com_github_cockroachdb_errors//:errors",
Expand All @@ -27,6 +35,7 @@ go_test(
name = "contention_test",
size = "small",
srcs = [
"event_store_test.go",
"registry_test.go",
"resolver_test.go",
"utils_test.go",
Expand All @@ -36,13 +45,15 @@ go_test(
deps = [
"//pkg/keys",
"//pkg/roachpb",
"//pkg/settings/cluster",
"//pkg/sql/contentionpb",
"//pkg/storage/enginepb",
"//pkg/testutils",
"//pkg/util/cache",
"//pkg/util/encoding",
"//pkg/util/leaktest",
"//pkg/util/randutil",
"//pkg/util/stop",
"//pkg/util/uuid",
"@com_github_cockroachdb_datadriven//:datadriven",
"@com_github_cockroachdb_errors//:errors",
Expand Down
35 changes: 35 additions & 0 deletions pkg/sql/contention/cluster_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 contention

import (
"time"

"github.com/cockroachdb/cockroach/pkg/settings"
)

// TxnIDResolutionInterval is the cluster setting that controls how often the
// Transaction ID Resolution is performed.
var TxnIDResolutionInterval = settings.RegisterDurationSetting(
settings.TenantWritable,
"sql.contention.event_store.resolution_interval",
"the interval at which transaction ID resolution is performed (set to 0 to disable)",
time.Second*30,
)

// StoreCapacity is the cluster setting that controls the
// maximum size of the contention event store.
var StoreCapacity = settings.RegisterByteSizeSetting(
settings.TenantWritable,
"sql.contention.event_store.capacity",
"the in-memory storage capacity per-node of contention event store",
64*1024*1024, // 64 MB per node.
).WithPublic()
Loading

0 comments on commit b5cb37d

Please sign in to comment.