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,mon: expose the memory monitors as virtual table #97657

Merged
merged 1 commit into from
Mar 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ crdb_internal node_contention_events table admin NULL NULL
crdb_internal node_distsql_flows table admin NULL NULL
crdb_internal node_execution_insights table admin NULL NULL
crdb_internal node_inflight_trace_spans table admin NULL NULL
crdb_internal node_memory_monitors table admin NULL NULL
crdb_internal node_metrics table admin NULL NULL
crdb_internal node_queries table admin NULL NULL
crdb_internal node_runtime_info table admin NULL NULL
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/testdata/zip/partial1
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ debug zip --concurrency=1 --cpu-profile-duration=0s /dev/null
[node 1] retrieving SQL data for crdb_internal.node_distsql_flows... writing output: debug/nodes/1/crdb_internal.node_distsql_flows.txt... done
[node 1] retrieving SQL data for crdb_internal.node_execution_insights... writing output: debug/nodes/1/crdb_internal.node_execution_insights.txt... done
[node 1] retrieving SQL data for crdb_internal.node_inflight_trace_spans... writing output: debug/nodes/1/crdb_internal.node_inflight_trace_spans.txt... done
[node 1] retrieving SQL data for crdb_internal.node_memory_monitors... writing output: debug/nodes/1/crdb_internal.node_memory_monitors.txt... done
[node 1] retrieving SQL data for crdb_internal.node_metrics... writing output: debug/nodes/1/crdb_internal.node_metrics.txt... done
[node 1] retrieving SQL data for crdb_internal.node_queries... writing output: debug/nodes/1/crdb_internal.node_queries.txt... done
[node 1] retrieving SQL data for crdb_internal.node_runtime_info... writing output: debug/nodes/1/crdb_internal.node_runtime_info.txt... done
Expand Down Expand Up @@ -206,6 +207,9 @@ debug zip --concurrency=1 --cpu-profile-duration=0s /dev/null
[node 2] retrieving SQL data for crdb_internal.node_inflight_trace_spans... writing output: debug/nodes/2/crdb_internal.node_inflight_trace_spans.txt...
[node 2] retrieving SQL data for crdb_internal.node_inflight_trace_spans: last request failed: dial tcp ...
[node 2] retrieving SQL data for crdb_internal.node_inflight_trace_spans: creating error output: debug/nodes/2/crdb_internal.node_inflight_trace_spans.txt.err.txt... done
[node 2] retrieving SQL data for crdb_internal.node_memory_monitors... writing output: debug/nodes/2/crdb_internal.node_memory_monitors.txt...
[node 2] retrieving SQL data for crdb_internal.node_memory_monitors: last request failed: dial tcp ...
[node 2] retrieving SQL data for crdb_internal.node_memory_monitors: creating error output: debug/nodes/2/crdb_internal.node_memory_monitors.txt.err.txt... done
[node 2] retrieving SQL data for crdb_internal.node_metrics... writing output: debug/nodes/2/crdb_internal.node_metrics.txt...
[node 2] retrieving SQL data for crdb_internal.node_metrics: last request failed: dial tcp ...
[node 2] retrieving SQL data for crdb_internal.node_metrics: creating error output: debug/nodes/2/crdb_internal.node_metrics.txt.err.txt... done
Expand Down Expand Up @@ -276,6 +280,7 @@ debug zip --concurrency=1 --cpu-profile-duration=0s /dev/null
[node 3] retrieving SQL data for crdb_internal.node_distsql_flows... writing output: debug/nodes/3/crdb_internal.node_distsql_flows.txt... done
[node 3] retrieving SQL data for crdb_internal.node_execution_insights... writing output: debug/nodes/3/crdb_internal.node_execution_insights.txt... done
[node 3] retrieving SQL data for crdb_internal.node_inflight_trace_spans... writing output: debug/nodes/3/crdb_internal.node_inflight_trace_spans.txt... done
[node 3] retrieving SQL data for crdb_internal.node_memory_monitors... writing output: debug/nodes/3/crdb_internal.node_memory_monitors.txt... done
[node 3] retrieving SQL data for crdb_internal.node_metrics... writing output: debug/nodes/3/crdb_internal.node_metrics.txt... done
[node 3] retrieving SQL data for crdb_internal.node_queries... writing output: debug/nodes/3/crdb_internal.node_queries.txt... done
[node 3] retrieving SQL data for crdb_internal.node_runtime_info... writing output: debug/nodes/3/crdb_internal.node_runtime_info.txt... done
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/testdata/zip/partial1_excluded
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ debug zip /dev/null --concurrency=1 --exclude-nodes=2 --cpu-profile-duration=0
[node 1] retrieving SQL data for crdb_internal.node_distsql_flows... writing output: debug/nodes/1/crdb_internal.node_distsql_flows.txt... done
[node 1] retrieving SQL data for crdb_internal.node_execution_insights... writing output: debug/nodes/1/crdb_internal.node_execution_insights.txt... done
[node 1] retrieving SQL data for crdb_internal.node_inflight_trace_spans... writing output: debug/nodes/1/crdb_internal.node_inflight_trace_spans.txt... done
[node 1] retrieving SQL data for crdb_internal.node_memory_monitors... writing output: debug/nodes/1/crdb_internal.node_memory_monitors.txt... done
[node 1] retrieving SQL data for crdb_internal.node_metrics... writing output: debug/nodes/1/crdb_internal.node_metrics.txt... done
[node 1] retrieving SQL data for crdb_internal.node_queries... writing output: debug/nodes/1/crdb_internal.node_queries.txt... done
[node 1] retrieving SQL data for crdb_internal.node_runtime_info... writing output: debug/nodes/1/crdb_internal.node_runtime_info.txt... done
Expand Down Expand Up @@ -185,6 +186,7 @@ debug zip /dev/null --concurrency=1 --exclude-nodes=2 --cpu-profile-duration=0
[node 3] retrieving SQL data for crdb_internal.node_distsql_flows... writing output: debug/nodes/3/crdb_internal.node_distsql_flows.txt... done
[node 3] retrieving SQL data for crdb_internal.node_execution_insights... writing output: debug/nodes/3/crdb_internal.node_execution_insights.txt... done
[node 3] retrieving SQL data for crdb_internal.node_inflight_trace_spans... writing output: debug/nodes/3/crdb_internal.node_inflight_trace_spans.txt... done
[node 3] retrieving SQL data for crdb_internal.node_memory_monitors... writing output: debug/nodes/3/crdb_internal.node_memory_monitors.txt... done
[node 3] retrieving SQL data for crdb_internal.node_metrics... writing output: debug/nodes/3/crdb_internal.node_metrics.txt... done
[node 3] retrieving SQL data for crdb_internal.node_queries... writing output: debug/nodes/3/crdb_internal.node_queries.txt... done
[node 3] retrieving SQL data for crdb_internal.node_runtime_info... writing output: debug/nodes/3/crdb_internal.node_runtime_info.txt... done
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/testdata/zip/partial2
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ debug zip --concurrency=1 --cpu-profile-duration=0 /dev/null
[node 1] retrieving SQL data for crdb_internal.node_distsql_flows... writing output: debug/nodes/1/crdb_internal.node_distsql_flows.txt... done
[node 1] retrieving SQL data for crdb_internal.node_execution_insights... writing output: debug/nodes/1/crdb_internal.node_execution_insights.txt... done
[node 1] retrieving SQL data for crdb_internal.node_inflight_trace_spans... writing output: debug/nodes/1/crdb_internal.node_inflight_trace_spans.txt... done
[node 1] retrieving SQL data for crdb_internal.node_memory_monitors... writing output: debug/nodes/1/crdb_internal.node_memory_monitors.txt... done
[node 1] retrieving SQL data for crdb_internal.node_metrics... writing output: debug/nodes/1/crdb_internal.node_metrics.txt... done
[node 1] retrieving SQL data for crdb_internal.node_queries... writing output: debug/nodes/1/crdb_internal.node_queries.txt... done
[node 1] retrieving SQL data for crdb_internal.node_runtime_info... writing output: debug/nodes/1/crdb_internal.node_runtime_info.txt... done
Expand Down Expand Up @@ -184,6 +185,7 @@ debug zip --concurrency=1 --cpu-profile-duration=0 /dev/null
[node 3] retrieving SQL data for crdb_internal.node_distsql_flows... writing output: debug/nodes/3/crdb_internal.node_distsql_flows.txt... done
[node 3] retrieving SQL data for crdb_internal.node_execution_insights... writing output: debug/nodes/3/crdb_internal.node_execution_insights.txt... done
[node 3] retrieving SQL data for crdb_internal.node_inflight_trace_spans... writing output: debug/nodes/3/crdb_internal.node_inflight_trace_spans.txt... done
[node 3] retrieving SQL data for crdb_internal.node_memory_monitors... writing output: debug/nodes/3/crdb_internal.node_memory_monitors.txt... done
[node 3] retrieving SQL data for crdb_internal.node_metrics... writing output: debug/nodes/3/crdb_internal.node_metrics.txt... done
[node 3] retrieving SQL data for crdb_internal.node_queries... writing output: debug/nodes/3/crdb_internal.node_queries.txt... done
[node 3] retrieving SQL data for crdb_internal.node_runtime_info... writing output: debug/nodes/3/crdb_internal.node_runtime_info.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 @@ -90,6 +90,7 @@ debug zip --concurrency=1 --cpu-profile-duration=1s /dev/null
[node 1] retrieving SQL data for crdb_internal.node_distsql_flows... writing output: debug/nodes/1/crdb_internal.node_distsql_flows.txt... done
[node 1] retrieving SQL data for crdb_internal.node_execution_insights... writing output: debug/nodes/1/crdb_internal.node_execution_insights.txt... done
[node 1] retrieving SQL data for crdb_internal.node_inflight_trace_spans... writing output: debug/nodes/1/crdb_internal.node_inflight_trace_spans.txt... done
[node 1] retrieving SQL data for crdb_internal.node_memory_monitors... writing output: debug/nodes/1/crdb_internal.node_memory_monitors.txt... done
[node 1] retrieving SQL data for crdb_internal.node_metrics... writing output: debug/nodes/1/crdb_internal.node_metrics.txt... done
[node 1] retrieving SQL data for crdb_internal.node_queries... writing output: debug/nodes/1/crdb_internal.node_queries.txt... done
[node 1] retrieving SQL data for crdb_internal.node_runtime_info... writing output: debug/nodes/1/crdb_internal.node_runtime_info.txt... done
Expand Down
9 changes: 9 additions & 0 deletions pkg/cli/testdata/zip/testzip_concurrent
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ zip
[node 1] retrieving SQL data for crdb_internal.node_inflight_trace_spans...
[node 1] retrieving SQL data for crdb_internal.node_inflight_trace_spans: done
[node 1] retrieving SQL data for crdb_internal.node_inflight_trace_spans: writing output: debug/nodes/1/crdb_internal.node_inflight_trace_spans.txt...
[node 1] retrieving SQL data for crdb_internal.node_memory_monitors...
[node 1] retrieving SQL data for crdb_internal.node_memory_monitors: done
[node 1] retrieving SQL data for crdb_internal.node_memory_monitors: writing output: debug/nodes/1/crdb_internal.node_memory_monitors.txt...
[node 1] retrieving SQL data for crdb_internal.node_metrics...
[node 1] retrieving SQL data for crdb_internal.node_metrics: done
[node 1] retrieving SQL data for crdb_internal.node_metrics: writing output: debug/nodes/1/crdb_internal.node_metrics.txt...
Expand Down Expand Up @@ -665,6 +668,9 @@ zip
[node 2] retrieving SQL data for crdb_internal.node_inflight_trace_spans...
[node 2] retrieving SQL data for crdb_internal.node_inflight_trace_spans: done
[node 2] retrieving SQL data for crdb_internal.node_inflight_trace_spans: writing output: debug/nodes/2/crdb_internal.node_inflight_trace_spans.txt...
[node 2] retrieving SQL data for crdb_internal.node_memory_monitors...
[node 2] retrieving SQL data for crdb_internal.node_memory_monitors: done
[node 2] retrieving SQL data for crdb_internal.node_memory_monitors: writing output: debug/nodes/2/crdb_internal.node_memory_monitors.txt...
[node 2] retrieving SQL data for crdb_internal.node_metrics...
[node 2] retrieving SQL data for crdb_internal.node_metrics: done
[node 2] retrieving SQL data for crdb_internal.node_metrics: writing output: debug/nodes/2/crdb_internal.node_metrics.txt...
Expand Down Expand Up @@ -1011,6 +1017,9 @@ zip
[node 3] retrieving SQL data for crdb_internal.node_inflight_trace_spans...
[node 3] retrieving SQL data for crdb_internal.node_inflight_trace_spans: done
[node 3] retrieving SQL data for crdb_internal.node_inflight_trace_spans: writing output: debug/nodes/3/crdb_internal.node_inflight_trace_spans.txt...
[node 3] retrieving SQL data for crdb_internal.node_memory_monitors...
[node 3] retrieving SQL data for crdb_internal.node_memory_monitors: done
[node 3] retrieving SQL data for crdb_internal.node_memory_monitors: writing output: debug/nodes/3/crdb_internal.node_memory_monitors.txt...
[node 3] retrieving SQL data for crdb_internal.node_metrics...
[node 3] retrieving SQL data for crdb_internal.node_metrics: done
[node 3] retrieving SQL data for crdb_internal.node_metrics: writing output: debug/nodes/3/crdb_internal.node_metrics.txt...
Expand Down
11 changes: 11 additions & 0 deletions pkg/cli/zip_table_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,17 @@ var zipInternalTablesPerNode = DebugZipTableRegistry{
) SELECT trace_id, parent_span_id, span_id, goroutine_id, finished, start_time, duration, operation, payload_type
FROM spans, LATERAL crdb_internal.payloads_for_span(span_id)`,
},
"crdb_internal.node_memory_monitors": {
nonSensitiveCols: NonSensitiveColumns{
"level",
"name",
"id",
"parent_id",
"used",
"reserved_used",
"reserved_reserved",
},
},
"crdb_internal.node_metrics": {
nonSensitiveCols: NonSensitiveColumns{
"store_id",
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3707,7 +3707,7 @@ func TestTransactionContentionEvents(t *testing.T) {
WHERE length(contending_key) > 0`,
)
if tc.testName == "nopermission" {
require.Contains(t, err.Error(), "requires VIEWACTIVITY")
require.Contains(t, err.Error(), "does not have VIEWACTIVITY")
} else {
require.NoError(t, err)
visibleContendingKeysCount := tree.MustBeDInt(row[0])
Expand Down
78 changes: 56 additions & 22 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ var crdbInternal = virtualSchema{
catconstants.CrdbInternalLocalSessionsTableID: crdbInternalLocalSessionsTable,
catconstants.CrdbInternalLocalMetricsTableID: crdbInternalLocalMetricsTable,
catconstants.CrdbInternalNodeExecutionInsightsTableID: crdbInternalNodeExecutionInsightsTable,
catconstants.CrdbInternalNodeMemoryMonitorsTableID: crdbInternalNodeMemoryMonitors,
catconstants.CrdbInternalNodeStmtStatsTableID: crdbInternalNodeStmtStatsTable,
catconstants.CrdbInternalNodeTxnExecutionInsightsTableID: crdbInternalNodeTxnExecutionInsightsTable,
catconstants.CrdbInternalNodeTxnStatsTableID: crdbInternalNodeTxnStatsTable,
Expand Down Expand Up @@ -1417,8 +1418,7 @@ CREATE TABLE crdb_internal.node_statement_statistics (
return err
}
if !hasViewActivityOrViewActivityRedacted {
return pgerror.Newf(pgcode.InsufficientPrivilege,
"user %s does not have %s or %s privilege", p.User(), roleoption.VIEWACTIVITY, roleoption.VIEWACTIVITYREDACTED)
return noViewActivityOrViewActivityRedactedRoleError(p.User())
}

sqlStats, err := getSQLStats(p, "crdb_internal.node_statement_statistics")
Expand Down Expand Up @@ -1643,8 +1643,7 @@ CREATE TABLE crdb_internal.node_transaction_statistics (
return err
}
if !hasViewActivityOrhasViewActivityRedacted {
return pgerror.Newf(pgcode.InsufficientPrivilege,
"user %s does not have %s or %s privilege", p.User(), roleoption.VIEWACTIVITY, roleoption.VIEWACTIVITYREDACTED)
return noViewActivityOrViewActivityRedactedRoleError(p.User())
}

sqlStats, err := getSQLStats(p, "crdb_internal.node_transaction_statistics")
Expand Down Expand Up @@ -6596,8 +6595,7 @@ CREATE TABLE crdb_internal.transaction_contention_events (
return nil, nil, err
}
if !hasPermission {
return nil, nil, errors.New("crdb_internal.transaction_contention_events " +
"requires VIEWACTIVITY or VIEWACTIVITYREDACTED role option")
return nil, nil, noViewActivityOrViewActivityRedactedRoleError(p.User())
}

// If a user has VIEWACTIVITYREDACTED role option but the user does not
Expand Down Expand Up @@ -6881,8 +6879,7 @@ func genClusterLocksGenerator(
return nil, nil, err
}
if !hasViewActivityOrViewActivityRedacted {
return nil, nil, pgerror.Newf(pgcode.InsufficientPrivilege,
"user %s does not have %s or %s privilege", p.User(), roleoption.VIEWACTIVITY, roleoption.VIEWACTIVITYREDACTED)
return nil, nil, noViewActivityOrViewActivityRedactedRoleError(p.User())
}
shouldRedactKeys := false
if !hasAdmin {
Expand Down Expand Up @@ -7197,13 +7194,7 @@ func populateTxnExecutionInsights(
return err
}
if !hasRoleOption {
return pgerror.Newf(
pgcode.InsufficientPrivilege,
"user %s does not have %s or %s privilege",
p.User(),
roleoption.VIEWACTIVITY,
roleoption.VIEWACTIVITYREDACTED,
)
return noViewActivityOrViewActivityRedactedRoleError(p.User())
}

response, err := p.extendedEvalCtx.SQLStatusServer.ListExecutionInsights(ctx, request)
Expand Down Expand Up @@ -7353,6 +7344,16 @@ var crdbInternalNodeExecutionInsightsTable = virtualSchemaTable{
},
}

func noViewActivityOrViewActivityRedactedRoleError(user username.SQLUsername) error {
return pgerror.Newf(
pgcode.InsufficientPrivilege,
"user %s does not have %s or %s privilege",
user,
roleoption.VIEWACTIVITY,
roleoption.VIEWACTIVITYREDACTED,
)
}

func populateStmtInsights(
ctx context.Context,
p *planner,
Expand All @@ -7364,13 +7365,7 @@ func populateStmtInsights(
return err
}
if !hasRoleOption {
return pgerror.Newf(
pgcode.InsufficientPrivilege,
"user %s does not have %s or %s privilege",
p.User(),
roleoption.VIEWACTIVITY,
roleoption.VIEWACTIVITYREDACTED,
)
return noViewActivityOrViewActivityRedactedRoleError(p.User())
}

response, err := p.extendedEvalCtx.SQLStatusServer.ListExecutionInsights(ctx, request)
Expand Down Expand Up @@ -7501,3 +7496,42 @@ func getContentionEventInfo(

return schemaDesc.GetName(), dbDesc.GetName(), tableDesc.GetName(), idxName, nil
}

var crdbInternalNodeMemoryMonitors = virtualSchemaTable{
comment: `node-level table listing all currently active memory monitors`,
schema: `
CREATE TABLE crdb_internal.node_memory_monitors (
level INT8,
name STRING,
id INT8,
parent_id INT8,
used INT8,
reserved_used INT8,
reserved_reserved INT8
);`,
populate: func(ctx context.Context, p *planner, _ catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error {
// The memory monitors' names can expose some information about the
// activity on the node, so we require VIEWACTIVITY or
// VIEWACTIVITYREDACTED permissions.
hasRoleOption, err := p.HasViewActivityOrViewActivityRedactedRole(ctx)
if err != nil {
return err
}
if !hasRoleOption {
return noViewActivityOrViewActivityRedactedRoleError(p.User())
}

monitorStateCb := func(monitor mon.MonitorState) error {
return addRow(
tree.NewDInt(tree.DInt(monitor.Level)),
tree.NewDString(monitor.Name),
tree.NewDInt(tree.DInt(monitor.ID)),
tree.NewDInt(tree.DInt(monitor.ParentID)),
tree.NewDInt(tree.DInt(monitor.Used)),
tree.NewDInt(tree.DInt(monitor.ReservedUsed)),
tree.NewDInt(tree.DInt(monitor.ReservedReserved)),
)
}
return p.extendedEvalCtx.ExecCfg.RootMemoryMonitor.TraverseTree(monitorStateCb)
},
}
2 changes: 1 addition & 1 deletion pkg/sql/distsql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (ds *ServerImpl) setupFlow(
}

monitor = mon.NewMonitor(
"flow",
"flow "+redact.RedactableString(req.Flow.FlowID.Short()),
mon.MemoryResource,
ds.Metrics.CurBytesCount,
ds.Metrics.MaxBytesHist,
Expand Down
38 changes: 38 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ crdb_internal node_contention_events table admin NULL NULL
crdb_internal node_distsql_flows table admin NULL NULL
crdb_internal node_execution_insights table admin NULL NULL
crdb_internal node_inflight_trace_spans table admin NULL NULL
crdb_internal node_memory_monitors table admin NULL NULL
crdb_internal node_metrics table admin NULL NULL
crdb_internal node_queries table admin NULL NULL
crdb_internal node_runtime_info table admin NULL NULL
Expand Down Expand Up @@ -1211,3 +1212,40 @@ user root

statement ok
REVOKE SYSTEM MODIFYCLUSTERSETTING FROM testuser

query TT
SELECT crdb_internal.humanize_bytes(NULL), crdb_internal.humanize_bytes(102400)
----
NULL 100 KiB

# Sanity checks of the crdb_internal.node_memory_monitors table.

# The root monitor has a standalone budget, so its 'used' field is zero.
query BB
SELECT used = 0, reserved_used > 0 FROM crdb_internal.node_memory_monitors WHERE name = 'root'
----
true true

query B
SELECT used > 0 FROM crdb_internal.node_memory_monitors WHERE name = 'sql'
----
true

# Run a query on one connection and observe it from another.
user testuser

statement async sleepQuery count 1
SELECT pg_sleep(3)

user root

query B
SELECT count(*) > 0 FROM crdb_internal.node_memory_monitors WHERE name LIKE '%flow%'
----
true

user testuser

awaitstatement sleepQuery

user root
Loading