Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: introduce crdb_internal.cluster_locks virtual table #77876

Merged
merged 2 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
264 changes: 264 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/cluster_locks_tenant
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
# LogicTest: 3node-tenant

# Create a table, write a row, lock it, then switch users.
statement ok
CREATE TABLE t (k STRING PRIMARY KEY, v STRING, FAMILY (k,v))

statement ok
GRANT ALL ON t TO testuser

statement ok
INSERT INTO t VALUES ('a', 'val1'), ('b', 'val2'), ('c', 'val3'), ('l', 'val4'), ('m', 'val5'), ('p', 'val6'), ('s', 'val7'), ('t', 'val8'), ('z', 'val9')

# Also create an additional user with VIEWACTIVITYREDACTED, with only permissions on t
statement ok
CREATE USER testuser2 WITH VIEWACTIVITYREDACTED

statement ok
GRANT ALL ON t TO testuser2

statement ok
CREATE TABLE t2 (k STRING PRIMARY KEY, v STRING, FAMILY (k,v))

statement ok
INSERT INTO t2 VALUES ('a', 'val1'), ('b', 'val2')

# Start txn1 where we acquire replicated locks
statement ok
BEGIN PRIORITY HIGH

statement ok
UPDATE t SET v = '_updated' WHERE k >= 'b' AND k < 'x'

let $root_session
SHOW session_id

user testuser

let $testuser_session
SHOW session_id

statement ok
BEGIN

# switch back to root, collect data needed for validation
user root

let $txn1
SELECT txns.id FROM crdb_internal.cluster_transactions txns WHERE txns.session_id = '$root_session'

let $txn2
SELECT txns.id FROM crdb_internal.cluster_transactions txns WHERE txns.session_id = '$testuser_session'

user testuser

query TT async,rowsort readReq
SELECT * FROM t
----
a val1
b _updated
c _updated
l _updated
m _updated
p _updated
s _updated
t _updated
z val9

user root

query TTT colnames,retry
SELECT user_name, query, phase FROM crdb_internal.cluster_queries WHERE txn_id='$txn2'
----
user_name query phase
testuser SELECT * FROM t executing

# looking at each transaction separately, validate the expected results in the lock table
query TTTTTTBB colnames,retry
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn1'
----
database_name schema_name table_name lock_key_pretty lock_strength durability granted contended
test public t /Table/106/1/"b"/0 Exclusive Replicated true true
test public t /Table/106/1/"c"/0 Exclusive Replicated true false
test public t /Table/106/1/"l"/0 Exclusive Replicated true false
test public t /Table/106/1/"m"/0 Exclusive Replicated true false
test public t /Table/106/1/"p"/0 Exclusive Replicated true false
test public t /Table/106/1/"s"/0 Exclusive Replicated true false
test public t /Table/106/1/"t"/0 Exclusive Replicated true false

query TTTTTTBB colnames
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn2'
----
database_name schema_name table_name lock_key_pretty lock_strength durability granted contended
test public t /Table/106/1/"b"/0 None Replicated false true

# check that we can't see keys, potentially revealing PII, with VIEWACTIVITYREDACTED
user testuser2

query TTTTTTBB colnames
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn1'
----
database_name schema_name table_name lock_key_pretty lock_strength durability granted contended
test public t · Exclusive Replicated true true
test public t · Exclusive Replicated true false
test public t · Exclusive Replicated true false
test public t · Exclusive Replicated true false
test public t · Exclusive Replicated true false
test public t · Exclusive Replicated true false
test public t · Exclusive Replicated true false

user root

query I
SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name = 't'
----
8

statement ok
COMMIT

query I retry
SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name = 't'
----
0

user testuser

awaitquery readReq

statement ok
COMMIT

user root

# start txn3
statement ok
BEGIN

user testuser

# start txn4
statement ok
BEGIN

user root

query TT rowsort
SELECT * FROM t FOR UPDATE
----
a val1
b _updated
c _updated
l _updated
m _updated
p _updated
s _updated
t _updated
z val9

let $txn3
SELECT txns.id FROM crdb_internal.cluster_transactions txns WHERE txns.session_id = '$root_session'

let $txn4
SELECT txns.id FROM crdb_internal.cluster_transactions txns WHERE txns.session_id = '$testuser_session'

user testuser

statement async deleteReq count 7
DELETE FROM t WHERE k >= 'b' AND k < 'x'

user root

query TTT colnames,retry
SELECT user_name, query, phase FROM crdb_internal.cluster_queries WHERE txn_id='$txn4'
----
user_name query phase
testuser DELETE FROM t WHERE (k >= 'b') AND (k < 'x') executing

# looking at each transaction separately, validate the expected results in the lock table
query TTTTTTBB colnames,retry
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn3'
----
database_name schema_name table_name lock_key_pretty lock_strength durability granted contended
test public t /Table/106/1/"a"/0 Exclusive Unreplicated true false
test public t /Table/106/1/"b"/0 Exclusive Unreplicated true true
test public t /Table/106/1/"c"/0 Exclusive Unreplicated true false
test public t /Table/106/1/"l"/0 Exclusive Unreplicated true false
test public t /Table/106/1/"m"/0 Exclusive Unreplicated true false
test public t /Table/106/1/"p"/0 Exclusive Unreplicated true false
test public t /Table/106/1/"s"/0 Exclusive Unreplicated true false
test public t /Table/106/1/"t"/0 Exclusive Unreplicated true false
test public t /Table/106/1/"z"/0 Exclusive Unreplicated true false

query TTTTTTBB colnames
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name='t' AND txn_id='$txn4'
----
database_name schema_name table_name lock_key_pretty lock_strength durability granted contended
test public t /Table/106/1/"b"/0 Exclusive Unreplicated false true

query I
SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name = 't'
----
10

statement ok
ROLLBACK

user testuser

awaitstatement deleteReq

statement ok
COMMIT

user root

query I retry
SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name = 't'
----
0

# validate that only locks on keys in privileged tables can be seen
statement ok
BEGIN

query TT rowsort
SELECT * FROM t FOR UPDATE
----
a val1
z val9

query TT rowsort
SELECT * FROM t2 FOR UPDATE
----
a val1
b val2

query I retry
SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name IN ('t','t2')
----
4

user testuser

query error pq: user testuser does not have VIEWACTIVITY or VIEWACTIVITYREDACTED privilege
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks

user testuser2

query TTTTTTBB colnames
SELECT database_name, schema_name, table_name, lock_key_pretty, lock_strength, durability, granted, contended FROM crdb_internal.cluster_locks WHERE table_name IN ('t', 't2')
----
database_name schema_name table_name lock_key_pretty lock_strength durability granted contended
test public t · Exclusive Unreplicated true false
test public t · Exclusive Unreplicated true false

user root

statement ok
ROLLBACK

query I retry
SELECT count(*) FROM crdb_internal.cluster_locks WHERE table_name IN ('t','t2')
----
0
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ crdb_internal cluster_contention_events table NULL NULL NULL
crdb_internal cluster_database_privileges table NULL NULL NULL
crdb_internal cluster_distsql_flows table NULL NULL NULL
crdb_internal cluster_inflight_traces table NULL NULL NULL
crdb_internal cluster_locks table NULL NULL NULL
crdb_internal cluster_queries table NULL NULL NULL
crdb_internal cluster_sessions table NULL NULL NULL
crdb_internal cluster_settings table NULL NULL NULL
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/testdata/zip/partial1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ debug zip --concurrency=1 --cpu-profile-duration=0s /dev/null
[cluster] retrieving SQL data for crdb_internal.cluster_contention_events... writing output: debug/crdb_internal.cluster_contention_events.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_distsql_flows... writing output: debug/crdb_internal.cluster_distsql_flows.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_database_privileges... writing output: debug/crdb_internal.cluster_database_privileges.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_locks... writing output: debug/crdb_internal.cluster_locks.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_queries... writing output: debug/crdb_internal.cluster_queries.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_sessions... writing output: debug/crdb_internal.cluster_sessions.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_settings... writing output: debug/crdb_internal.cluster_settings.txt... done
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/testdata/zip/partial1_excluded
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ debug zip /dev/null --concurrency=1 --exclude-nodes=2 --cpu-profile-duration=0
[cluster] retrieving SQL data for crdb_internal.cluster_contention_events... writing output: debug/crdb_internal.cluster_contention_events.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_distsql_flows... writing output: debug/crdb_internal.cluster_distsql_flows.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_database_privileges... writing output: debug/crdb_internal.cluster_database_privileges.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_locks... writing output: debug/crdb_internal.cluster_locks.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_queries... writing output: debug/crdb_internal.cluster_queries.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_sessions... writing output: debug/crdb_internal.cluster_sessions.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_settings... writing output: debug/crdb_internal.cluster_settings.txt... done
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/testdata/zip/partial2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ debug zip --concurrency=1 --cpu-profile-duration=0 /dev/null
[cluster] retrieving SQL data for crdb_internal.cluster_contention_events... writing output: debug/crdb_internal.cluster_contention_events.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_distsql_flows... writing output: debug/crdb_internal.cluster_distsql_flows.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_database_privileges... writing output: debug/crdb_internal.cluster_database_privileges.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_locks... writing output: debug/crdb_internal.cluster_locks.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_queries... writing output: debug/crdb_internal.cluster_queries.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_sessions... writing output: debug/crdb_internal.cluster_sessions.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_settings... writing output: debug/crdb_internal.cluster_settings.txt... done
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/testdata/zip/testzip
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ debug zip --concurrency=1 --cpu-profile-duration=1s /dev/null
[cluster] retrieving SQL data for crdb_internal.cluster_contention_events... writing output: debug/crdb_internal.cluster_contention_events.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_distsql_flows... writing output: debug/crdb_internal.cluster_distsql_flows.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_database_privileges... writing output: debug/crdb_internal.cluster_database_privileges.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_locks... writing output: debug/crdb_internal.cluster_locks.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_queries... writing output: debug/crdb_internal.cluster_queries.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_sessions... writing output: debug/crdb_internal.cluster_sessions.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_settings... writing output: debug/crdb_internal.cluster_settings.txt... done
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/testdata/zip/testzip_concurrent
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ zip
[cluster] retrieving SQL data for crdb_internal.cluster_distsql_flows...
[cluster] retrieving SQL data for crdb_internal.cluster_distsql_flows: done
[cluster] retrieving SQL data for crdb_internal.cluster_distsql_flows: writing output: debug/crdb_internal.cluster_distsql_flows.txt...
[cluster] retrieving SQL data for crdb_internal.cluster_locks...
[cluster] retrieving SQL data for crdb_internal.cluster_locks: done
[cluster] retrieving SQL data for crdb_internal.cluster_locks: writing output: debug/crdb_internal.cluster_locks.txt...
[cluster] retrieving SQL data for crdb_internal.cluster_queries...
[cluster] retrieving SQL data for crdb_internal.cluster_queries: done
[cluster] retrieving SQL data for crdb_internal.cluster_queries: writing output: debug/crdb_internal.cluster_queries.txt...
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/testdata/zip/testzip_tenant
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ debug zip --concurrency=1 --cpu-profile-duration=1s /dev/null
[cluster] retrieving SQL data for crdb_internal.cluster_contention_events... writing output: debug/crdb_internal.cluster_contention_events.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_distsql_flows... writing output: debug/crdb_internal.cluster_distsql_flows.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_database_privileges... writing output: debug/crdb_internal.cluster_database_privileges.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_locks... writing output: debug/crdb_internal.cluster_locks.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_queries... writing output: debug/crdb_internal.cluster_queries.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_sessions... writing output: debug/crdb_internal.cluster_sessions.txt... done
[cluster] retrieving SQL data for crdb_internal.cluster_settings... writing output: debug/crdb_internal.cluster_settings.txt... done
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/zip_cluster_wide.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ var debugZipTablesPerCluster = []string{
"crdb_internal.cluster_contention_events",
"crdb_internal.cluster_distsql_flows",
"crdb_internal.cluster_database_privileges",
"crdb_internal.cluster_locks",
"crdb_internal.cluster_queries",
"crdb_internal.cluster_sessions",
"crdb_internal.cluster_settings",
Expand Down
9 changes: 5 additions & 4 deletions pkg/kv/kvserver/concurrency/lock_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,13 +1110,14 @@ func (l *lockState) collectLockStateInfo(
// REQUIRES: l.mu is locked.
func (l *lockState) lockStateInfo(now time.Time) roachpb.LockStateInfo {
var txnHolder *enginepb.TxnMeta

durability := lock.Unreplicated
if l.holder.locked {
if l.holder.holder[lock.Unreplicated].txn != nil {
txnHolder = l.holder.holder[lock.Unreplicated].txn
} else {
txnHolder = l.holder.holder[lock.Replicated].txn
if l.holder.holder[lock.Replicated].txn != nil {
durability = lock.Replicated
txnHolder = l.holder.holder[lock.Replicated].txn
} else if l.holder.holder[lock.Unreplicated].txn != nil {
txnHolder = l.holder.holder[lock.Unreplicated].txn
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ go_library(
"//pkg/kv/kvclient/kvtenant",
"//pkg/kv/kvclient/rangecache",
"//pkg/kv/kvclient/rangefeed",
"//pkg/kv/kvserver/concurrency/lock",
"//pkg/kv/kvserver/kvserverbase",
"//pkg/kv/kvserver/liveness/livenesspb",
"//pkg/kv/kvserver/protectedts",
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/catalog/catconstants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const (
CrdbInternalClusterContendedTablesViewID
CrdbInternalClusterContentionEventsTableID
CrdbInternalClusterDistSQLFlowsTableID
CrdbInternalClusterLocksTableID
CrdbInternalClusterQueriesTableID
CrdbInternalClusterTransactionsTableID
CrdbInternalClusterSessionsTableID
Expand Down
Loading