-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathstats.go
165 lines (148 loc) · 5.16 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2018 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 rowexec
import (
"context"
"time"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/row"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/rowinfra"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)
// inputStatCollector wraps an execinfra.RowSource and collects stats from it.
type inputStatCollector struct {
execinfra.RowSource
stats execinfrapb.InputStats
}
var _ execinfra.RowSource = &inputStatCollector{}
var _ execinfra.OpNode = &inputStatCollector{}
// newInputStatCollector creates a new inputStatCollector that wraps the given
// input.
func newInputStatCollector(input execinfra.RowSource) *inputStatCollector {
res := &inputStatCollector{RowSource: input}
res.stats.NumTuples.Set(0)
return res
}
// ChildCount is part of the OpNode interface.
func (isc *inputStatCollector) ChildCount(verbose bool) int {
if _, ok := isc.RowSource.(execinfra.OpNode); ok {
return 1
}
return 0
}
// Child is part of the OpNode interface.
func (isc *inputStatCollector) Child(nth int, verbose bool) execinfra.OpNode {
if nth == 0 {
return isc.RowSource.(execinfra.OpNode)
}
panic(errors.AssertionFailedf("invalid index %d", nth))
}
// Next implements the RowSource interface. It calls Next on the embedded
// RowSource and collects stats.
func (isc *inputStatCollector) Next() (rowenc.EncDatumRow, *execinfrapb.ProducerMetadata) {
start := timeutil.Now()
row, meta := isc.RowSource.Next()
if row != nil {
isc.stats.NumTuples.Add(1)
}
isc.stats.WaitTime.Add(timeutil.Since(start))
return row, meta
}
// rowFetcherStatCollector is a wrapper on top of a row.Fetcher that collects stats.
//
// Only row.Fetcher methods that collect stats are overridden.
type rowFetcherStatCollector struct {
*row.Fetcher
// stats contains the collected stats.
stats execinfrapb.InputStats
startScanStallTime time.Duration
}
var _ rowFetcher = &rowFetcherStatCollector{}
// newRowFetcherStatCollector returns a new rowFetcherStatCollector.
func newRowFetcherStatCollector(f *row.Fetcher) *rowFetcherStatCollector {
res := &rowFetcherStatCollector{Fetcher: f}
res.stats.NumTuples.Set(0)
return res
}
// NextRow is part of the rowFetcher interface.
func (c *rowFetcherStatCollector) NextRow(
ctx context.Context,
) (rowenc.EncDatumRow, catalog.TableDescriptor, catalog.Index, error) {
start := timeutil.Now()
row, t, i, err := c.Fetcher.NextRow(ctx)
if row != nil {
c.stats.NumTuples.Add(1)
}
c.stats.WaitTime.Add(timeutil.Since(start))
return row, t, i, err
}
// StartScan is part of the rowFetcher interface.
func (c *rowFetcherStatCollector) StartScan(
ctx context.Context,
txn *kv.Txn,
spans roachpb.Spans,
batchBytesLimit rowinfra.BytesLimit,
limitHint rowinfra.RowLimit,
traceKV bool,
forceProductionKVBatchSize bool,
) error {
start := timeutil.Now()
err := c.Fetcher.StartScan(ctx, txn, spans, batchBytesLimit, limitHint, traceKV, forceProductionKVBatchSize)
c.startScanStallTime += timeutil.Since(start)
return err
}
// StartInconsistentScan is part of the rowFetcher interface.
func (c *rowFetcherStatCollector) StartInconsistentScan(
ctx context.Context,
db *kv.DB,
initialTimestamp hlc.Timestamp,
maxTimestampAge time.Duration,
spans roachpb.Spans,
batchBytesLimit rowinfra.BytesLimit,
limitHint rowinfra.RowLimit,
traceKV bool,
forceProductionKVBatchSize bool,
) error {
start := timeutil.Now()
err := c.Fetcher.StartInconsistentScan(
ctx, db, initialTimestamp, maxTimestampAge, spans, batchBytesLimit, limitHint, traceKV, forceProductionKVBatchSize,
)
c.startScanStallTime += timeutil.Since(start)
return err
}
// getInputStats is a utility function to check whether the given input is
// collecting stats, returning true and the stats if so. If false is returned,
// the input is not collecting stats.
func getInputStats(input execinfra.RowSource) (execinfrapb.InputStats, bool) {
isc, ok := input.(*inputStatCollector)
if !ok {
return execinfrapb.InputStats{}, false
}
return isc.stats, true
}
// getFetcherInputStats is a utility function to check whether the given input
// is collecting row fetcher stats, returning true and the stats if so. If
// false is returned, the input is not collecting row fetcher stats.
func getFetcherInputStats(f rowFetcher) (execinfrapb.InputStats, bool) {
rfsc, ok := f.(*rowFetcherStatCollector)
if !ok {
return execinfrapb.InputStats{}, false
}
// Add row fetcher start scan stall time to Next() stall time.
rfsc.stats.WaitTime.Add(rfsc.startScanStallTime)
return rfsc.stats, true
}