-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
captured_index_usage_stats.go
322 lines (292 loc) · 10.5 KB
/
captured_index_usage_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2022 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 scheduledlogging
import (
"context"
"fmt"
"time"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)
var telemetryCaptureIndexUsageStatsEnabled = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.telemetry.capture_index_usage_stats.enabled",
"enable/disable capturing index usage statistics to the telemetry logging channel",
true,
)
var telemetryCaptureIndexUsageStatsInterval = settings.RegisterDurationSetting(
settings.SystemOnly,
"sql.telemetry.capture_index_usage_stats.interval",
"the scheduled interval time between capturing index usage statistics when capturing index usage statistics is enabled",
8*time.Second,
settings.NonNegativeDuration,
)
var telemetryCaptureIndexUsageStatsStatusCheckEnabledInterval = settings.RegisterDurationSetting(
settings.SystemOnly,
"sql.telemetry.capture_index_usage_stats.check_enabled_interval",
"the scheduled interval time between checks to see if index usage statistics has been enabled",
8*time.Second,
settings.NonNegativeDuration,
)
var telemetryCaptureIndexUsageStatsLoggingDelay = settings.RegisterDurationSetting(
settings.SystemOnly,
"sql.telemetry.capture_index_usage_stats.logging_delay",
"the time delay between emitting individual index usage stats logs, this is done to "+
"mitigate the log-line limit of 10 logs per second on the telemetry pipeline",
500*time.Millisecond,
settings.NonNegativeDuration,
)
// CaptureIndexUsageStatsTestingKnobs provides hooks and knobs for unit tests.
type CaptureIndexUsageStatsTestingKnobs struct {
// getLoggingDuration allows tests to override the duration of the index
// usage stats logging operation.
getLoggingDuration func() time.Duration
// getOverlapDuration allows tests to override the duration until the next
// scheduled interval in the case that the logging duration exceeds the
// default scheduled interval duration.
getOverlapDuration func() time.Duration
}
// ModuleTestingKnobs implements base.ModuleTestingKnobs interface.
func (*CaptureIndexUsageStatsTestingKnobs) ModuleTestingKnobs() {}
// CaptureIndexUsageStatsLoggingScheduler is responsible for logging index usage stats
// on a scheduled interval.
type CaptureIndexUsageStatsLoggingScheduler struct {
DB *kv.DB
cs *cluster.Settings
ie sqlutil.InternalExecutor
knobs *CaptureIndexUsageStatsTestingKnobs
currentCaptureStartTime time.Time
}
func (s *CaptureIndexUsageStatsLoggingScheduler) getLoggingDuration() time.Duration {
if s.knobs != nil && s.knobs.getLoggingDuration != nil {
return s.knobs.getLoggingDuration()
}
return timeutil.Since(s.currentCaptureStartTime)
}
func (s *CaptureIndexUsageStatsLoggingScheduler) durationOnOverlap() time.Duration {
if s.knobs != nil && s.knobs.getOverlapDuration != nil {
return s.knobs.getOverlapDuration()
}
// If the logging duration overlaps into the next scheduled interval, start
// the next scheduled interval immediately instead of waiting.
return 0 * time.Second
}
func (s *CaptureIndexUsageStatsLoggingScheduler) durationUntilNextInterval() time.Duration {
// If telemetry is disabled, return the disabled interval duration.
if !telemetryCaptureIndexUsageStatsEnabled.Get(&s.cs.SV) {
return telemetryCaptureIndexUsageStatsStatusCheckEnabledInterval.Get(&s.cs.SV)
}
// If the previous logging operation took longer than or equal to the set
// schedule interval, schedule the next interval immediately.
if s.getLoggingDuration() >= telemetryCaptureIndexUsageStatsInterval.Get(&s.cs.SV) {
return s.durationOnOverlap()
}
// Otherwise, schedule the next interval normally.
return telemetryCaptureIndexUsageStatsInterval.Get(&s.cs.SV)
}
// Start starts the capture index usage statistics logging scheduler.
func Start(
ctx context.Context,
stopper *stop.Stopper,
db *kv.DB,
cs *cluster.Settings,
ie sqlutil.InternalExecutor,
knobs *CaptureIndexUsageStatsTestingKnobs,
) {
scheduler := CaptureIndexUsageStatsLoggingScheduler{
DB: db,
cs: cs,
ie: ie,
knobs: knobs,
}
scheduler.start(ctx, stopper)
}
func (s *CaptureIndexUsageStatsLoggingScheduler) start(ctx context.Context, stopper *stop.Stopper) {
_ = stopper.RunAsyncTask(ctx, "capture-index-usage-stats", func(ctx context.Context) {
// Start the scheduler immediately.
for timer := time.NewTimer(0 * time.Second); ; timer.Reset(s.durationUntilNextInterval()) {
select {
case <-stopper.ShouldQuiesce():
timer.Stop()
return
case <-timer.C:
s.currentCaptureStartTime = timeutil.Now()
if !telemetryCaptureIndexUsageStatsEnabled.Get(&s.cs.SV) {
continue
}
err := s.DB.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
return captureIndexUsageStats(ctx, s.ie, stopper, telemetryCaptureIndexUsageStatsLoggingDelay.Get(&s.cs.SV))
})
if err != nil {
log.Errorf(ctx, "error capturing index usage stats: %+v", err)
}
}
}
})
}
func captureIndexUsageStats(
ctx context.Context,
ie sqlutil.InternalExecutor,
stopper *stop.Stopper,
loggingDelay time.Duration,
) error {
allDatabaseNames, err := getAllDatabaseNames(ctx, ie)
if err != nil {
return err
}
// Capture index usage statistics for each database.
var ok bool
expectedNumDatums := 10
var allCapturedIndexUsageStats []eventpb.EventPayload
for _, databaseName := range allDatabaseNames {
// Omit index usage statistics of the 'system' database.
if databaseName == "system" {
continue
}
stmt := fmt.Sprintf(`
SELECT
'%s' as database_name,
ti.descriptor_name as table_name,
ti.descriptor_id as table_id,
ti.index_name,
ti.index_id,
ti.index_type,
ti.is_unique,
ti.is_inverted,
total_reads,
last_read
FROM %s.crdb_internal.index_usage_statistics AS us
JOIN %s.crdb_internal.table_indexes ti
ON us.index_id = ti.index_id
AND us.table_id = ti.descriptor_id
ORDER BY total_reads ASC;
`, databaseName, databaseName, databaseName)
it, err := ie.QueryIteratorEx(
ctx,
"capture-index-usage-stats",
nil,
sessiondata.InternalExecutorOverride{User: security.NodeUserName()},
stmt,
)
if err != nil {
return err
}
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
var row tree.Datums
if err != nil {
return err
}
if row = it.Cur(); row == nil {
return errors.New("unexpected null row while capturing index usage stats")
}
if row.Len() != expectedNumDatums {
return errors.Newf("expected %d columns, received %d while capturing index usage stats", expectedNumDatums, row.Len())
}
databaseName := tree.MustBeDString(row[0])
tableName := tree.MustBeDString(row[1])
tableID := tree.MustBeDInt(row[2])
indexName := tree.MustBeDString(row[3])
indexID := tree.MustBeDInt(row[4])
indexType := tree.MustBeDString(row[5])
isUnique := tree.MustBeDBool(row[6])
isInverted := tree.MustBeDBool(row[7])
totalReads := tree.MustBeDInt(row[8])
lastRead := time.Time{}
if row[9] != tree.DNull {
lastRead = tree.MustBeDTimestampTZ(row[9]).Time
}
capturedIndexStats := &eventpb.CapturedIndexUsageStats{
TableID: uint32(roachpb.TableID(tableID)),
IndexID: uint32(roachpb.IndexID(indexID)),
TotalReadCount: uint64(totalReads),
LastRead: lastRead.String(),
DatabaseName: string(databaseName),
TableName: string(tableName),
IndexName: string(indexName),
IndexType: string(indexType),
IsUnique: bool(isUnique),
IsInverted: bool(isInverted),
}
allCapturedIndexUsageStats = append(allCapturedIndexUsageStats, capturedIndexStats)
}
err = it.Close()
if err != nil {
return err
}
}
logIndexUsageStatsWithDelay(ctx, allCapturedIndexUsageStats, stopper, loggingDelay)
return nil
}
// logIndexUsageStatsWithDelay logs an eventpb.EventPayload at each
// telemetryCaptureIndexUsageStatsLoggingDelay to avoid exceeding the 10
// log-line per second limit per node on the telemetry logging pipeline.
// Currently, this log-line limit is only shared with 1 other telemetry event,
// SampledQuery, which now has a logging frequency of 8 logs per second.
func logIndexUsageStatsWithDelay(
ctx context.Context, events []eventpb.EventPayload, stopper *stop.Stopper, delay time.Duration,
) {
// Log the first event immediately.
timer := time.NewTimer(0 * time.Second)
for len(events) > 0 {
select {
case <-stopper.ShouldQuiesce():
timer.Stop()
return
case <-timer.C:
event := events[0]
log.StructuredEvent(ctx, event)
events = events[1:]
// Apply a delay to subsequent events.
timer.Reset(delay)
}
}
timer.Stop()
}
func getAllDatabaseNames(ctx context.Context, ie sqlutil.InternalExecutor) ([]string, error) {
var allDatabaseNames []string
var ok bool
var expectedNumDatums = 1
it, err := ie.QueryIteratorEx(
ctx,
"get-all-db-names",
nil,
sessiondata.InternalExecutorOverride{User: security.NodeUserName()},
`SELECT database_name FROM [SHOW DATABASES]`,
)
if err != nil {
return []string{}, err
}
// We have to make sure to close the iterator since we might return from the
// for loop early (before Next() returns false).
defer func() { err = errors.CombineErrors(err, it.Close()) }()
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
var row tree.Datums
if row = it.Cur(); row == nil {
return []string{}, errors.New("unexpected null row while capturing index usage stats")
}
if row.Len() != expectedNumDatums {
return []string{}, errors.Newf("expected %d columns, received %d while capturing index usage stats", expectedNumDatums, row.Len())
}
databaseName := string(tree.MustBeDString(row[0]))
allDatabaseNames = append(allDatabaseNames, databaseName)
}
return allDatabaseNames, nil
}