Skip to content

Commit a3789d3

Browse files
craig[bot]alyshanjahani-crl
andcommitted
Merge #158055
158055: execstats: Extract AdmissionWorkQueueStats from traces r=alyshanjahani-crl a=alyshanjahani-crl This commit extracts the AdmissionWorkQueueStats that we are recording on traces and adds it to a new field AdmissionWaitTime on the QueryLevelStats object. The total admission wait time is now displayed in explain analyze. A follow up PR will further plumb this through to SQL Stats. Part of: https://cockroachlabs.atlassian.net/browse/CRDB-55925 Release note (sql change): Admission control wait time is now displayed as part of EXPLAIN ANALYZE when non zero. Co-authored-by: alyshanjahani-crl <[email protected]>
2 parents 9346663 + 76ceae4 commit a3789d3

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

pkg/sql/execstats/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
"//pkg/kv/kvpb",
1515
"//pkg/sql/execinfrapb",
1616
"//pkg/util",
17+
"//pkg/util/admission/admissionpb",
1718
"//pkg/util/buildutil",
1819
"//pkg/util/optional",
1920
"//pkg/util/protoutil",

pkg/sql/execstats/traceanalyzer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
1313
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
1414
"github.com/cockroachdb/cockroach/pkg/util"
15+
"github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb"
1516
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
1617
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
1718
"github.com/cockroachdb/errors"
@@ -123,6 +124,7 @@ type QueryLevelStats struct {
123124
ContentionEvents []kvpb.ContentionEvent
124125
RUEstimate float64
125126
CPUTime time.Duration
127+
AdmissionWaitTime time.Duration
126128
// SQLInstanceIDs is an ordered list of SQL instances that were involved in
127129
// query processing.
128130
SQLInstanceIDs []int32
@@ -188,6 +190,7 @@ func (s *QueryLevelStats) Accumulate(other QueryLevelStats) {
188190
s.ContentionEvents = append(s.ContentionEvents, other.ContentionEvents...)
189191
s.RUEstimate += other.RUEstimate
190192
s.CPUTime += other.CPUTime
193+
s.AdmissionWaitTime += other.AdmissionWaitTime
191194
s.SQLInstanceIDs = util.CombineUnique(s.SQLInstanceIDs, other.SQLInstanceIDs)
192195
s.KVNodeIDs = util.CombineUnique(s.KVNodeIDs, other.KVNodeIDs)
193196
s.Regions = util.CombineUnique(s.Regions, other.Regions)
@@ -397,6 +400,25 @@ func getAllContentionEvents(trace []tracingpb.RecordedSpan) []kvpb.ContentionEve
397400
return contentionEvents
398401
}
399402

403+
// getAdmissionWaitTime returns the total admission wait time accumulated
404+
// from all AdmissionWorkQueueStats events found in the given trace.
405+
func getAdmissionWaitTime(trace []tracingpb.RecordedSpan) time.Duration {
406+
var totalWaitTime time.Duration
407+
var ev admissionpb.AdmissionWorkQueueStats
408+
for i := range trace {
409+
trace[i].Structured(func(any *pbtypes.Any, _ time.Time) {
410+
if !pbtypes.Is(any, &ev) {
411+
return
412+
}
413+
if err := pbtypes.UnmarshalAny(any, &ev); err != nil {
414+
return
415+
}
416+
totalWaitTime += ev.WaitDurationNanos
417+
})
418+
}
419+
return totalWaitTime
420+
}
421+
400422
// GetQueryLevelStats returns all the top-level stats in a QueryLevelStats
401423
// struct. GetQueryLevelStats tries to process as many stats as possible. If
402424
// errors occur while processing stats, GetQueryLevelStats returns the combined
@@ -417,5 +439,6 @@ func GetQueryLevelStats(
417439
queryLevelStats.Accumulate(analyzer.GetQueryLevelStats())
418440
}
419441
queryLevelStats.ContentionEvents = getAllContentionEvents(trace)
442+
queryLevelStats.AdmissionWaitTime = getAdmissionWaitTime(trace)
420443
return queryLevelStats, errs
421444
}

pkg/sql/execstats/traceanalyzer_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ func TestQueryLevelStatsAccumulate(t *testing.T) {
244244
ContentionTime: 7 * time.Second,
245245
LockWaitTime: 4 * time.Second,
246246
LatchWaitTime: 3 * time.Second,
247+
AdmissionWaitTime: 1 * time.Second,
247248
ContentionEvents: []kvpb.ContentionEvent{aEvent},
248249
MaxDiskUsage: 8,
249250
RUEstimate: 9,
@@ -280,6 +281,7 @@ func TestQueryLevelStatsAccumulate(t *testing.T) {
280281
ContentionTime: 14 * time.Second,
281282
LockWaitTime: 10 * time.Second,
282283
LatchWaitTime: 4 * time.Second,
284+
AdmissionWaitTime: 2 * time.Second,
283285
ContentionEvents: []kvpb.ContentionEvent{bEvent},
284286
MaxDiskUsage: 15,
285287
RUEstimate: 16,
@@ -315,6 +317,7 @@ func TestQueryLevelStatsAccumulate(t *testing.T) {
315317
ContentionTime: 21 * time.Second,
316318
LockWaitTime: 14 * time.Second,
317319
LatchWaitTime: 7 * time.Second,
320+
AdmissionWaitTime: 3 * time.Second,
318321
ContentionEvents: []kvpb.ContentionEvent{aEvent, bEvent},
319322
MaxDiskUsage: 15,
320323
RUEstimate: 25,

pkg/sql/instrumentation.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,9 @@ func (ih *instrumentationHelper) emitExplainAnalyzePlanToOutputBuilder(
888888
if queryStats.LatchWaitTime != 0 {
889889
ob.AddLatchWaitTime(queryStats.LatchWaitTime)
890890
}
891+
if queryStats.AdmissionWaitTime != 0 {
892+
ob.AddAdmissionWaitTime(queryStats.AdmissionWaitTime)
893+
}
891894

892895
ob.AddMaxMemUsage(queryStats.MaxMemUsage)
893896
ob.AddDistSQLNetworkStats(queryStats.DistSQLNetworkMessages, queryStats.DistSQLNetworkBytesSent)

pkg/sql/opt/exec/explain/output.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ func (ob *OutputBuilder) AddLatchWaitTime(latchWaitTime time.Duration) {
418418
)
419419
}
420420

421+
// AddAdmissionWaitTime adds a top-level field for the cumulative time spent waiting
422+
// in admission control.
423+
func (ob *OutputBuilder) AddAdmissionWaitTime(admissionWaitTime time.Duration) {
424+
ob.AddFlakyTopLevelField(
425+
DeflakeVolatile,
426+
"cumulative time spent waiting in admission control",
427+
string(humanizeutil.Duration(admissionWaitTime)),
428+
)
429+
}
430+
421431
// AddMaxMemUsage adds a top-level field for the memory used by the query.
422432
func (ob *OutputBuilder) AddMaxMemUsage(bytes int64) {
423433
ob.AddFlakyTopLevelField(

0 commit comments

Comments
 (0)