-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathgranter_test.go
595 lines (540 loc) · 18.6 KB
/
granter_test.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright 2021 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 admission
import (
"context"
"fmt"
"math"
"math/rand"
"sort"
"strings"
"testing"
"time"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/pebble"
"github.com/stretchr/testify/require"
)
type testRequester struct {
workKind WorkKind
granter granter
usesTokens bool
buf *strings.Builder
waitingRequests bool
returnValueFromGranted int64
grantChainID grantChainID
}
var _ requester = &testRequester{}
var _ storeRequester = &testRequester{}
func (tr *testRequester) hasWaitingRequests() bool {
return tr.waitingRequests
}
func (tr *testRequester) granted(grantChainID grantChainID) int64 {
fmt.Fprintf(tr.buf, "%s: granted in chain %d, and returning %d\n", workKindString(tr.workKind),
grantChainID, tr.returnValueFromGranted)
tr.grantChainID = grantChainID
return tr.returnValueFromGranted
}
func (tr *testRequester) close() {}
func (tr *testRequester) tryGet(count int64) {
rv := tr.granter.tryGet(count)
fmt.Fprintf(tr.buf, "%s: tryGet(%d) returned %t\n", workKindString(tr.workKind), count, rv)
}
func (tr *testRequester) returnGrant(count int64) {
fmt.Fprintf(tr.buf, "%s: returnGrant(%d)\n", workKindString(tr.workKind), count)
tr.granter.returnGrant(count)
}
func (tr *testRequester) tookWithoutPermission(count int64) {
fmt.Fprintf(tr.buf, "%s: tookWithoutPermission(%d)\n", workKindString(tr.workKind), count)
tr.granter.tookWithoutPermission(count)
}
func (tr *testRequester) continueGrantChain() {
fmt.Fprintf(tr.buf, "%s: continueGrantChain\n", workKindString(tr.workKind))
tr.granter.continueGrantChain(tr.grantChainID)
}
func (tr *testRequester) getStoreAdmissionStats() storeAdmissionStats {
// Only used by ioLoadListener, so don't bother.
return storeAdmissionStats{}
}
func (tr *testRequester) setStoreRequestEstimates(estimates storeRequestEstimates) {
// Only used by ioLoadListener, so don't bother.
}
// TestGranterBasic is a datadriven test with the following commands:
//
// init-grant-coordinator min-cpu=<int> max-cpu=<int> sql-kv-tokens=<int>
// sql-sql-tokens=<int> sql-leaf=<int> sql-root=<int>
// set-has-waiting-requests work=<kind> v=<true|false>
// set-return-value-from-granted work=<kind> v=<int>
// try-get work=<kind> [v=<int>]
// return-grant work=<kind> [v=<int>]
// took-without-permission work=<kind> [v=<int>]
// continue-grant-chain work=<kind>
// cpu-load runnable=<int> procs=<int> [infrequent=<bool>]
// init-store-grant-coordinator
// set-io-tokens tokens=<int>
func TestGranterBasic(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
var ambientCtx log.AmbientContext
var requesters [numWorkKinds]*testRequester
var coord *GrantCoordinator
clearRequesterAndCoord := func() {
coord = nil
for i := range requesters {
requesters[i] = nil
}
}
var buf strings.Builder
flushAndReset := func() string {
fmt.Fprintf(&buf, "GrantCoordinator:\n%s\n", coord.String())
str := buf.String()
buf.Reset()
buf.Reset()
buf.Reset()
return str
}
settings := cluster.MakeTestingClusterSettings()
KVSlotAdjusterOverloadThreshold.Override(context.Background(), &settings.SV, 1)
datadriven.RunTest(t, testutils.TestDataPath(t, "granter"), func(t *testing.T, d *datadriven.TestData) string {
switch d.Cmd {
case "init-grant-coordinator":
clearRequesterAndCoord()
var opts Options
opts.Settings = settings
d.ScanArgs(t, "min-cpu", &opts.MinCPUSlots)
d.ScanArgs(t, "max-cpu", &opts.MaxCPUSlots)
var burstTokens int
d.ScanArgs(t, "sql-kv-tokens", &burstTokens)
opts.SQLKVResponseBurstTokens = int64(burstTokens)
d.ScanArgs(t, "sql-sql-tokens", &burstTokens)
opts.SQLSQLResponseBurstTokens = int64(burstTokens)
d.ScanArgs(t, "sql-leaf", &opts.SQLStatementLeafStartWorkSlots)
d.ScanArgs(t, "sql-root", &opts.SQLStatementRootStartWorkSlots)
opts.makeRequesterFunc = func(
_ log.AmbientContext, workKind WorkKind, granter granter, _ *cluster.Settings,
opts workQueueOptions) requester {
req := &testRequester{
workKind: workKind,
granter: granter,
usesTokens: opts.usesTokens,
buf: &buf,
returnValueFromGranted: 1,
}
requesters[workKind] = req
return req
}
delayForGrantChainTermination = 0
coords, _ := NewGrantCoordinators(ambientCtx, opts)
coord = coords.Regular
return flushAndReset()
case "init-store-grant-coordinator":
clearRequesterAndCoord()
metrics := makeGranterMetrics()
storeCoordinators := &StoreGrantCoordinators{
settings: settings,
makeStoreRequesterFunc: func(
ambientCtx log.AmbientContext, granter granter, settings *cluster.Settings,
opts workQueueOptions) storeRequester {
req := &testRequester{
workKind: KVWork,
granter: granter,
usesTokens: true,
buf: &buf,
returnValueFromGranted: 0,
}
requesters[KVWork] = req
return req
},
kvIOTokensExhaustedDuration: metrics.KVIOTokensExhaustedDuration,
workQueueMetrics: makeWorkQueueMetrics(""),
disableTickerForTesting: true,
}
var testMetricsProvider testMetricsProvider
testMetricsProvider.setMetricsForStores([]int32{1}, pebble.Metrics{})
storeCoordinators.SetPebbleMetricsProvider(context.Background(), &testMetricsProvider)
unsafeGranter, ok := storeCoordinators.gcMap.Load(int64(1))
require.True(t, ok)
coord = (*GrantCoordinator)(unsafeGranter)
return flushAndReset()
case "set-has-waiting-requests":
var v bool
d.ScanArgs(t, "v", &v)
requesters[scanWorkKind(t, d)].waitingRequests = v
return flushAndReset()
case "set-return-value-from-granted":
var v int
d.ScanArgs(t, "v", &v)
requesters[scanWorkKind(t, d)].returnValueFromGranted = int64(v)
return flushAndReset()
case "try-get":
v := 1
if d.HasArg("v") {
d.ScanArgs(t, "v", &v)
}
requesters[scanWorkKind(t, d)].tryGet(int64(v))
return flushAndReset()
case "return-grant":
v := 1
if d.HasArg("v") {
d.ScanArgs(t, "v", &v)
}
requesters[scanWorkKind(t, d)].returnGrant(int64(v))
return flushAndReset()
case "took-without-permission":
v := 1
if d.HasArg("v") {
d.ScanArgs(t, "v", &v)
}
requesters[scanWorkKind(t, d)].tookWithoutPermission(int64(v))
return flushAndReset()
case "continue-grant-chain":
requesters[scanWorkKind(t, d)].continueGrantChain()
return flushAndReset()
case "cpu-load":
var runnable, procs int
d.ScanArgs(t, "runnable", &runnable)
d.ScanArgs(t, "procs", &procs)
infrequent := false
if d.HasArg("infrequent") {
d.ScanArgs(t, "infrequent", &infrequent)
}
samplePeriod := time.Millisecond
if infrequent {
samplePeriod = 250 * time.Millisecond
}
coord.CPULoad(runnable, procs, samplePeriod)
return flushAndReset()
case "set-io-tokens":
var tokens int
d.ScanArgs(t, "tokens", &tokens)
// We are not using a real ioLoadListener, and simply setting the
// tokens (the ioLoadListener has its own test).
coord.mu.Lock()
coord.granters[KVWork].(*kvStoreTokenGranter).setAvailableIOTokensLocked(int64(tokens))
coord.mu.Unlock()
coord.testingTryGrant()
return flushAndReset()
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
}
func scanWorkKind(t *testing.T, d *datadriven.TestData) WorkKind {
var kindStr string
d.ScanArgs(t, "work", &kindStr)
switch kindStr {
case "kv":
return KVWork
case "sql-kv-response":
return SQLKVResponseWork
case "sql-sql-response":
return SQLSQLResponseWork
case "sql-leaf-start":
return SQLStatementLeafStartWork
case "sql-root-start":
return SQLStatementRootStartWork
}
panic("unknown WorkKind")
}
type testMetricsProvider struct {
metrics []StoreMetrics
}
func (m *testMetricsProvider) GetPebbleMetrics() []StoreMetrics {
return m.metrics
}
func (m *testMetricsProvider) setMetricsForStores(stores []int32, metrics pebble.Metrics) {
m.metrics = m.metrics[:0]
for _, s := range stores {
m.metrics = append(m.metrics, StoreMetrics{
StoreID: s,
Metrics: &metrics,
})
}
}
// TestStoreCoordinators tests only the setup of GrantCoordinators per store.
// Testing of IO load functionality happens in TestIOLoadListener.
func TestStoreCoordinators(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
var ambientCtx log.AmbientContext
var buf strings.Builder
settings := cluster.MakeTestingClusterSettings()
// All the KVWork requesters. The first one is for all KVWork and the
// remaining are the per-store ones.
var requesters []*testRequester
makeRequesterFunc := func(
_ log.AmbientContext, workKind WorkKind, granter granter, _ *cluster.Settings,
opts workQueueOptions) requester {
req := &testRequester{
workKind: workKind,
granter: granter,
usesTokens: opts.usesTokens,
buf: &buf,
}
if workKind == KVWork {
requesters = append(requesters, req)
}
return req
}
opts := Options{
Settings: settings,
makeRequesterFunc: makeRequesterFunc,
makeStoreRequesterFunc: func(
ctx log.AmbientContext, granter granter, settings *cluster.Settings,
opts workQueueOptions) storeRequester {
req := makeRequesterFunc(ctx, KVWork, granter, settings, opts)
return req.(*testRequester)
},
}
coords, _ := NewGrantCoordinators(ambientCtx, opts)
// There is only 1 KVWork requester at this point in initialization, for the
// Regular GrantCoordinator.
require.Equal(t, 1, len(requesters))
storeCoords := coords.Stores
metrics := pebble.Metrics{}
mp := testMetricsProvider{}
mp.setMetricsForStores([]int32{10, 20}, metrics)
// Setting the metrics provider will cause the initialization of two
// GrantCoordinators for the two stores.
storeCoords.SetPebbleMetricsProvider(context.Background(), &mp)
// Now we have 1+2 = 3 KVWork requesters.
require.Equal(t, 3, len(requesters))
// Confirm that the store IDs are as expected.
var actualStores []int32
storeCoords.gcMap.Range(func(s int64, _ unsafe.Pointer) bool {
// The int32 conversion is lossless since we only store int32s in the
// gcMap.
actualStores = append(actualStores, int32(s))
// true indicates that iteration should continue after the
// current entry has been processed.
return true
})
sort.Slice(actualStores, func(i, j int) bool { return actualStores[i] < actualStores[j] })
require.Equal(t, []int32{10, 20}, actualStores)
// Do tryGet on all requesters. The requester for the Regular
// GrantCoordinator will return false since it has 0 CPU slots. We are
// interested in the other ones, which have unlimited slots at this point in
// time, so will return true.
for i := range requesters {
requesters[i].tryGet(1)
}
require.Equal(t,
"kv: tryGet(1) returned false\nkv: tryGet(1) returned true\nkv: tryGet(1) returned true\n",
buf.String())
coords.Close()
}
type testRequesterForIOLL struct {
stats storeAdmissionStats
buf strings.Builder
}
var _ storeRequester = &testRequesterForIOLL{}
func (r *testRequesterForIOLL) hasWaitingRequests() bool {
panic("unimplemented")
}
func (r *testRequesterForIOLL) granted(grantChainID grantChainID) int64 {
panic("unimplemented")
}
func (r *testRequesterForIOLL) close() {}
func (r *testRequesterForIOLL) getStoreAdmissionStats() storeAdmissionStats {
return r.stats
}
func (r *testRequesterForIOLL) setStoreRequestEstimates(estimates storeRequestEstimates) {
fmt.Fprintf(&r.buf,
"store-request-estimates: fractionOfIngestIntoL0: %.2f, workByteAddition: %d",
estimates.fractionOfIngestIntoL0, estimates.workByteAddition)
}
type testGranterWithIOTokens struct {
buf strings.Builder
}
func (g *testGranterWithIOTokens) setAvailableIOTokensLocked(tokens int64) {
fmt.Fprintf(&g.buf, "setAvailableIOTokens: %s", tokensFor1sToString(tokens))
}
func tokensForIntervalToString(tokens int64) string {
if tokens == unlimitedTokens {
return "unlimited"
}
return fmt.Sprintf("%d", tokens)
}
func tokensFor1sToString(tokens int64) string {
if tokens >= unlimitedTokens/adjustmentInterval {
return "unlimited"
}
return fmt.Sprintf("%d", tokens)
}
// TestIOLoadListener is a datadriven test with the following command that
// sets the state for token calculation and then ticks adjustmentInterval
// times to cause tokens to be set in the testGranterWithIOTokens:
// set-state admitted=<int> l0-bytes=<int> l0-added=<int> l0-files=<int> l0-sublevels=<int>
func TestIOLoadListener(t *testing.T) {
req := &testRequesterForIOLL{}
kvGranter := &testGranterWithIOTokens{}
var ioll *ioLoadListener
ctx := context.Background()
st := cluster.MakeTestingClusterSettings()
datadriven.RunTest(t, testutils.TestDataPath(t, "io_load_listener"),
func(t *testing.T, d *datadriven.TestData) string {
switch d.Cmd {
case "init":
ioll = &ioLoadListener{
settings: st,
kvRequester: req,
}
// The mutex is needed by ioLoadListener but is not useful in this
// test -- the channels provide synchronization and prevent this
// test code and the ioLoadListener from being concurrently
// active.
ioll.mu.Mutex = &syncutil.Mutex{}
ioll.mu.kvGranter = kvGranter
return ""
case "prep-admission-stats":
req.stats = storeAdmissionStats{
admittedCount: 0,
admittedWithBytesCount: 0,
admittedBytes: 0,
ingestedBytes: 0,
ingestedIntoL0Bytes: 0,
}
d.ScanArgs(t, "admitted", &req.stats.admittedCount)
if d.HasArg("admitted-bytes") {
d.ScanArgs(t, "admitted-bytes", &req.stats.admittedBytes)
}
if d.HasArg("ingested-bytes") {
d.ScanArgs(t, "ingested-bytes", &req.stats.ingestedBytes)
}
if d.HasArg("ingested-into-l0") {
d.ScanArgs(t, "ingested-into-l0", &req.stats.ingestedIntoL0Bytes)
}
return fmt.Sprintf("%+v", req.stats)
case "set-state":
// Setup state used as input for token adjustment.
var metrics pebble.Metrics
var l0Bytes uint64
d.ScanArgs(t, "l0-bytes", &l0Bytes)
metrics.Levels[0].Size = int64(l0Bytes)
var l0Added uint64
d.ScanArgs(t, "l0-added", &l0Added)
metrics.Levels[0].BytesIngested = l0Added / 2
metrics.Levels[0].BytesFlushed = l0Added - metrics.Levels[0].BytesIngested
var l0Files int
d.ScanArgs(t, "l0-files", &l0Files)
metrics.Levels[0].NumFiles = int64(l0Files)
var l0SubLevels int
d.ScanArgs(t, "l0-sublevels", &l0SubLevels)
metrics.Levels[0].Sublevels = int32(l0SubLevels)
ioll.pebbleMetricsTick(ctx, &metrics)
// Do the ticks until just before next adjustment.
var buf strings.Builder
fmt.Fprintf(&buf, "admitted: %d, bytes: %d, added-bytes: %d,\nsmoothed-removed: %d, "+
"smoothed-byte-tokens: %d, smoothed-bytes-unaccounted-per-work: %d,\ntokens: %s, tokens-allocated: %s\n",
ioll.admissionStats.admittedCount,
ioll.l0Bytes, ioll.l0AddedBytes, ioll.smoothedBytesRemoved,
int64(ioll.smoothedNumByteTokens), int64(ioll.smoothedPerWorkUnaccountedBytesAdded),
tokensForIntervalToString(ioll.totalTokens),
tokensFor1sToString(ioll.tokensAllocated))
if req.buf.Len() > 0 {
fmt.Fprintf(&buf, "%s\n", req.buf.String())
req.buf.Reset()
}
for i := 0; i < adjustmentInterval; i++ {
ioll.allocateTokensTick()
fmt.Fprintf(&buf, "tick: %d, %s\n", i, kvGranter.buf.String())
kvGranter.buf.Reset()
}
return buf.String()
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
}
func TestIOLoadListenerOverflow(t *testing.T) {
req := &testRequesterForIOLL{}
kvGranter := &testGranterWithIOTokens{}
ctx := context.Background()
st := cluster.MakeTestingClusterSettings()
ioll := ioLoadListener{
settings: st,
kvRequester: req,
}
ioll.mu.Mutex = &syncutil.Mutex{}
ioll.mu.kvGranter = kvGranter
// Bug 1: overflow when totalTokens is too large.
for i := int64(0); i < adjustmentInterval; i++ {
// Override the totalTokens manually to trigger the overflow bug.
ioll.totalTokens = math.MaxInt64 - i
ioll.tokensAllocated = 0
for j := 0; j < adjustmentInterval; j++ {
ioll.allocateTokensTick()
}
}
// Bug2: overflow when bytes added delta is 0.
m := pebble.Metrics{}
m.Levels[0] = pebble.LevelMetrics{
Sublevels: 100,
NumFiles: 10000,
}
ioll.pebbleMetricsTick(ctx, &m)
ioll.pebbleMetricsTick(ctx, &m)
ioll.allocateTokensTick()
}
type testGranterNonNegativeTokens struct {
t *testing.T
}
func (g *testGranterNonNegativeTokens) setAvailableIOTokensLocked(tokens int64) {
require.LessOrEqual(g.t, int64(0), tokens)
}
// TestBadIOLoadListenerStats tests that bad stats (non-monotonic cumulative
// stats and negative values) don't cause panics or tokens to be negative.
func TestBadIOLoadListenerStats(t *testing.T) {
var m pebble.Metrics
req := &testRequesterForIOLL{}
ctx := context.Background()
randomValues := func() {
// Use uints, and cast so that we get bad negative values.
m.Levels[0].Sublevels = int32(rand.Uint32())
m.Levels[0].NumFiles = int64(rand.Uint64())
m.Levels[0].Size = int64(rand.Uint64())
m.Levels[0].BytesFlushed = rand.Uint64()
m.Levels[0].BytesIngested = rand.Uint64()
req.stats.admittedCount = rand.Uint64()
req.stats.admittedWithBytesCount = rand.Uint64()
req.stats.admittedBytes = rand.Uint64()
req.stats.ingestedBytes = rand.Uint64()
req.stats.ingestedIntoL0Bytes = rand.Uint64()
}
kvGranter := &testGranterNonNegativeTokens{t: t}
st := cluster.MakeTestingClusterSettings()
ioll := ioLoadListener{
settings: st,
kvRequester: req,
}
ioll.mu.Mutex = &syncutil.Mutex{}
ioll.mu.kvGranter = kvGranter
for i := 0; i < 100; i++ {
randomValues()
ioll.pebbleMetricsTick(ctx, &m)
for j := 0; j < adjustmentInterval; j++ {
ioll.allocateTokensTick()
require.LessOrEqual(t, int64(0), ioll.smoothedBytesRemoved)
require.LessOrEqual(t, float64(0), ioll.smoothedPerWorkUnaccountedBytesAdded)
require.LessOrEqual(t, float64(0), ioll.smoothedFractionOfIngestIntoL0)
require.LessOrEqual(t, float64(0), ioll.smoothedNumByteTokens)
require.LessOrEqual(t, int64(0), ioll.totalTokens)
require.LessOrEqual(t, int64(0), ioll.tokensAllocated)
}
}
}
// TODO(sumeer):
// - Test metrics
// - Test GrantCoordinator with multi-tenant configurations