-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathss_mem_iterator.go
187 lines (158 loc) · 5.21 KB
/
ss_mem_iterator.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package ssmemstorage
import (
"sort"
"github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
)
type baseIterator struct {
container *Container
idx int
}
// StmtStatsIterator is an iterator that iterates over the statement statistics
// inside of a ssmemstorage.Container.
type StmtStatsIterator struct {
baseIterator
stmtKeys stmtList
currentValue *appstatspb.CollectedStatementStatistics
}
// NewStmtStatsIterator returns a StmtStatsIterator.
func NewStmtStatsIterator(
container *Container, options sqlstats.IteratorOptions,
) StmtStatsIterator {
var stmtKeys stmtList
func() {
container.mu.RLock()
defer container.mu.RUnlock()
for k := range container.mu.stmts {
stmtKeys = append(stmtKeys, k)
}
}()
if options.SortedKey {
sort.Sort(stmtKeys)
}
return StmtStatsIterator{
baseIterator: baseIterator{
container: container,
idx: -1,
},
stmtKeys: stmtKeys,
}
}
// Initialized returns true if the iterator has been initialized, false otherwise.
func (s *StmtStatsIterator) Initialized() bool {
return s.container != nil
}
// Next updates the current value returned by the subsequent Cur() call. Next()
// returns true if the following Cur() call is valid, false otherwise.
func (s *StmtStatsIterator) Next() bool {
s.idx++
if s.idx >= len(s.stmtKeys) {
return false
}
stmtKey := s.stmtKeys[s.idx]
statementStats, _, _ :=
s.container.getStatsForStmtWithKey(stmtKey, invalidStmtFingerprintID, false /* createIfNonexistent */)
// If the key is not found (and we expected to find it), the table must
// have been cleared between now and the time we read all the keys. In
// that case we simply skip this key and keep advancing the iterator.
if statementStats == nil {
return s.Next()
}
statementStats.mu.Lock()
data := statementStats.mu.data
distSQLUsed := statementStats.mu.distSQLUsed
vectorized := statementStats.mu.vectorized
fullScan := statementStats.mu.fullScan
database := statementStats.mu.database
querySummary := statementStats.mu.querySummary
statementStats.mu.Unlock()
s.currentValue = &appstatspb.CollectedStatementStatistics{
Key: appstatspb.StatementStatisticsKey{
Query: stmtKey.stmtNoConstants,
QuerySummary: querySummary,
DistSQL: distSQLUsed,
Vec: vectorized,
ImplicitTxn: stmtKey.implicitTxn,
FullScan: fullScan,
App: s.container.appName,
Database: database,
PlanHash: stmtKey.planHash,
TransactionFingerprintID: stmtKey.transactionFingerprintID,
},
ID: statementStats.ID,
Stats: data,
}
return true
}
// Cur returns the appstatspb.CollectedStatementStatistics at the current internal
// counter.
func (s *StmtStatsIterator) Cur() *appstatspb.CollectedStatementStatistics {
return s.currentValue
}
// TxnStatsIterator is an iterator that iterates over the transaction statistics
// inside a ssmemstorage.Container.
type TxnStatsIterator struct {
baseIterator
txnKeys txnList
curValue *appstatspb.CollectedTransactionStatistics
}
// NewTxnStatsIterator returns a new instance of TxnStatsIterator.
func NewTxnStatsIterator(container *Container, options sqlstats.IteratorOptions) TxnStatsIterator {
var txnKeys txnList
container.mu.Lock()
for k := range container.mu.txns {
txnKeys = append(txnKeys, k)
}
container.mu.Unlock()
if options.SortedKey {
sort.Sort(txnKeys)
}
return TxnStatsIterator{
baseIterator: baseIterator{
container: container,
idx: -1,
},
txnKeys: txnKeys,
}
}
// Initialized returns true if the iterator has been initialized, false otherwise.
func (t *TxnStatsIterator) Initialized() bool {
return t.container != nil
}
// Next updates the current value returned by the subsequent Cur() call. Next()
// returns true if the following Cur() call is valid, false otherwise.
func (t *TxnStatsIterator) Next() bool {
t.idx++
if t.idx >= len(t.txnKeys) {
return false
}
txnKey := t.txnKeys[t.idx]
// We don't want to create the key if it doesn't exist, so it's okay to
// pass nil for the statementFingerprintIDs, as they are only set when a key is
// constructed.
txnStats, _, _ := t.container.getStatsForTxnWithKey(txnKey, nil /* stmtFingerprintIDs */, false /* createIfNonexistent */)
// If the key is not found (and we expected to find it), the table must
// have been cleared between now and the time we read all the keys. In
// that case we simply skip this key and advance the iterator.
if txnStats == nil {
return t.Next()
}
txnStats.mu.Lock()
defer txnStats.mu.Unlock()
t.curValue = &appstatspb.CollectedTransactionStatistics{
StatementFingerprintIDs: txnStats.statementFingerprintIDs,
App: t.container.appName,
Stats: txnStats.mu.data,
TransactionFingerprintID: txnKey,
}
return true
}
// Cur returns the appstatspb.CollectedTransactionStatistics at the current internal
// counter.
func (t *TxnStatsIterator) Cur() *appstatspb.CollectedTransactionStatistics {
return t.curValue
}