-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathgrant_coordinator.go
1024 lines (942 loc) · 39.2 KB
/
grant_coordinator.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 admission
import (
"context"
"time"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)
// GrantCoordinators holds {regular,elastic} GrantCoordinators for
// {regular,elastic} work, and a StoreGrantCoordinators that allows for
// per-store GrantCoordinators for KVWork that involves writes.
type GrantCoordinators struct {
Regular *GrantCoordinator
Elastic *ElasticCPUGrantCoordinator
Stores *StoreGrantCoordinators
}
// Close implements the stop.Closer interface.
func (gcs GrantCoordinators) Close() {
gcs.Stores.close()
gcs.Regular.Close()
gcs.Elastic.close()
}
// StoreGrantCoordinators is a container for GrantCoordinators for each store,
// that is used for KV work admission that takes into account store health.
// Currently it is intended only for writes to stores.
type StoreGrantCoordinators struct {
ambientCtx log.AmbientContext
settings *cluster.Settings
makeStoreRequesterFunc makeStoreRequesterFunc
kvIOTokensExhaustedDuration *metric.Counter
// These metrics are shared by WorkQueues across stores.
workQueueMetrics *WorkQueueMetrics
gcMap syncutil.IntMap // map[int64(StoreID)]*GrantCoordinator
// numStores is used to track the number of stores which have been added
// to the gcMap. This is used because the IntMap doesn't expose a size
// api.
numStores int
pebbleMetricsProvider PebbleMetricsProvider
closeCh chan struct{}
disableTickerForTesting bool
}
// SetPebbleMetricsProvider sets a PebbleMetricsProvider and causes the load
// on the various storage engines to be used for admission control.
func (sgc *StoreGrantCoordinators) SetPebbleMetricsProvider(
startupCtx context.Context, pmp PebbleMetricsProvider, iotc IOThresholdConsumer,
) {
if sgc.pebbleMetricsProvider != nil {
panic(errors.AssertionFailedf("SetPebbleMetricsProvider called more than once"))
}
sgc.pebbleMetricsProvider = pmp
sgc.closeCh = make(chan struct{})
metrics := sgc.pebbleMetricsProvider.GetPebbleMetrics()
for _, m := range metrics {
gc := sgc.initGrantCoordinator(m.StoreID)
// Defensive call to LoadAndStore even though Store ought to be sufficient
// since SetPebbleMetricsProvider can only be called once. This code
// guards against duplication of stores returned by GetPebbleMetrics.
_, loaded := sgc.gcMap.LoadOrStore(int64(m.StoreID), unsafe.Pointer(gc))
if !loaded {
sgc.numStores++
}
gc.pebbleMetricsTick(startupCtx, m)
gc.allocateIOTokensTick()
}
if sgc.disableTickerForTesting {
return
}
// Attach tracer and log tags.
ctx := sgc.ambientCtx.AnnotateCtx(context.Background())
go func() {
var ticks int64
ticker := time.NewTicker(ioTokenTickDuration)
done := false
for !done {
select {
case <-ticker.C:
ticks++
if ticks%ticksInAdjustmentInterval == 0 {
metrics := sgc.pebbleMetricsProvider.GetPebbleMetrics()
if len(metrics) != sgc.numStores {
log.Warningf(ctx,
"expected %d store metrics and found %d metrics", sgc.numStores, len(metrics))
}
for _, m := range metrics {
if unsafeGc, ok := sgc.gcMap.Load(int64(m.StoreID)); ok {
gc := (*GrantCoordinator)(unsafeGc)
gc.pebbleMetricsTick(ctx, m)
iotc.UpdateIOThreshold(roachpb.StoreID(m.StoreID), gc.ioLoadListener.ioThreshold)
} else {
log.Warningf(ctx,
"seeing metrics for unknown storeID %d", m.StoreID)
}
}
}
sgc.gcMap.Range(func(_ int64, unsafeGc unsafe.Pointer) bool {
gc := (*GrantCoordinator)(unsafeGc)
gc.allocateIOTokensTick()
// true indicates that iteration should continue after the
// current entry has been processed.
return true
})
case <-sgc.closeCh:
done = true
}
}
ticker.Stop()
}()
}
func (sgc *StoreGrantCoordinators) initGrantCoordinator(storeID int32) *GrantCoordinator {
coord := &GrantCoordinator{
settings: sgc.settings,
useGrantChains: false,
numProcs: 1,
}
kvg := &kvStoreTokenGranter{
coord: coord,
// Setting tokens to unlimited is defensive. We expect that
// pebbleMetricsTick and allocateIOTokensTick will get called during
// initialization, which will also set these to unlimited.
availableIOTokens: unlimitedTokens / ticksInAdjustmentInterval,
startingIOTokens: unlimitedTokens / ticksInAdjustmentInterval,
ioTokensExhaustedDurationMetric: sgc.kvIOTokensExhaustedDuration,
elasticDiskBWTokensAvailable: unlimitedTokens / ticksInAdjustmentInterval,
}
opts := makeWorkQueueOptions(KVWork)
// This is IO work, so override the usesTokens value.
opts.usesTokens = true
// TODO(sumeer): add per-store WorkQueue state for debug.zip and db console.
granters := [admissionpb.NumWorkClasses]granterWithStoreWriteDone{
&kvStoreTokenChildGranter{
workClass: admissionpb.RegularWorkClass,
parent: kvg,
},
&kvStoreTokenChildGranter{
workClass: admissionpb.ElasticWorkClass,
parent: kvg,
},
}
storeReq := sgc.makeStoreRequesterFunc(sgc.ambientCtx, granters, sgc.settings, sgc.workQueueMetrics, opts)
coord.queues[KVWork] = storeReq
requesters := storeReq.getRequesters()
kvg.regularRequester = requesters[admissionpb.RegularWorkClass]
kvg.elasticRequester = requesters[admissionpb.ElasticWorkClass]
coord.granters[KVWork] = kvg
coord.ioLoadListener = &ioLoadListener{
storeID: storeID,
settings: sgc.settings,
kvRequester: storeReq,
perWorkTokenEstimator: makeStorePerWorkTokenEstimator(),
diskBandwidthLimiter: makeDiskBandwidthLimiter(),
}
coord.ioLoadListener.mu.Mutex = &coord.mu
coord.ioLoadListener.mu.kvGranter = kvg
return coord
}
// TryGetQueueForStore returns a WorkQueue for the given storeID, or nil if
// the storeID is not known.
func (sgc *StoreGrantCoordinators) TryGetQueueForStore(storeID int32) *StoreWorkQueue {
if unsafeGranter, ok := sgc.gcMap.Load(int64(storeID)); ok {
granter := (*GrantCoordinator)(unsafeGranter)
return granter.queues[KVWork].(*StoreWorkQueue)
}
return nil
}
func (sgc *StoreGrantCoordinators) close() {
// closeCh can be nil in tests that never called SetPebbleMetricsProvider.
if sgc.closeCh != nil {
close(sgc.closeCh)
}
sgc.gcMap.Range(func(_ int64, unsafeGc unsafe.Pointer) bool {
gc := (*GrantCoordinator)(unsafeGc)
gc.Close()
// true indicates that iteration should continue after the
// current entry has been processed.
return true
})
}
// GrantCoordinator is the top-level object that coordinates grants across
// different WorkKinds (for more context see the comment in admission.go, and
// the comment where WorkKind is declared). Typically there will be one
// GrantCoordinator in a node for CPU intensive regular work, and for nodes that
// also have the KV layer, one GrantCoordinator per store (these are managed by
// StoreGrantCoordinators) for KVWork that uses that store. See the
// NewGrantCoordinators and NewGrantCoordinatorSQL functions.
type GrantCoordinator struct {
ambientCtx log.AmbientContext
settings *cluster.Settings
lastCPULoadSamplePeriod time.Duration
// mu is ordered before any mutex acquired in a requester implementation.
// TODO(sumeer): move everything covered by mu into a nested struct.
mu syncutil.Mutex
// NB: Some granters can be nil.
granters [numWorkKinds]granterWithLockedCalls
// The WorkQueues behaving as requesters in each granterWithLockedCalls.
// This is kept separately only to service GetWorkQueue calls and to call
// close().
queues [numWorkKinds]requesterClose
// The cpu fields can be nil, and the IO field can be nil, since a
// GrantCoordinator typically handles one of these two resources.
cpuOverloadIndicator cpuOverloadIndicator
cpuLoadListener CPULoadListener
ioLoadListener *ioLoadListener
// The latest value of GOMAXPROCS, received via CPULoad. Only initialized if
// the cpu resource is being handled by this GrantCoordinator.
numProcs int
// See the comment at continueGrantChain that explains how a grant chain
// functions and the motivation. When !useGrantChains, grant chains are
// disabled.
useGrantChains bool
// The admission control code needs high sampling frequency of the cpu load,
// and turns off admission control enforcement when the sampling frequency
// is too low. For testing queueing behavior, we do not want the enforcement
// to be turned off in a non-deterministic manner so add a testing flag to
// disable that feature.
testingDisableSkipEnforcement bool
// grantChainActive indicates whether a grant chain is active. If active,
// grantChainID is the ID of that chain. If !active, grantChainID is the ID
// of the next chain that will become active. IDs are assigned by
// incrementing grantChainID. If !useGrantChains, grantChainActive is never
// true.
grantChainActive bool
grantChainID grantChainID
// Index into granters, which represents the current WorkKind at which the
// grant chain is operating. Only relevant when grantChainActive is true.
grantChainIndex WorkKind
// See the comment at delayForGrantChainTermination for motivation.
grantChainStartTime time.Time
}
var _ CPULoadListener = &GrantCoordinator{}
// Options for constructing GrantCoordinators.
type Options struct {
MinCPUSlots int
MaxCPUSlots int
SQLKVResponseBurstTokens int64
SQLSQLResponseBurstTokens int64
SQLStatementLeafStartWorkSlots int
SQLStatementRootStartWorkSlots int
TestingDisableSkipEnforcement bool
// Only non-nil for tests.
makeRequesterFunc makeRequesterFunc
makeStoreRequesterFunc makeStoreRequesterFunc
}
var _ base.ModuleTestingKnobs = &Options{}
// ModuleTestingKnobs implements the base.ModuleTestingKnobs interface.
func (*Options) ModuleTestingKnobs() {}
// DefaultOptions are the default settings for various admission control knobs.
var DefaultOptions = Options{
MinCPUSlots: 1,
MaxCPUSlots: 100000, /* TODO(sumeer): add cluster setting */
SQLKVResponseBurstTokens: 100000, /* TODO(sumeer): add cluster setting */
SQLSQLResponseBurstTokens: 100000, /* TODO(sumeer): add cluster setting */
SQLStatementLeafStartWorkSlots: 100, /* arbitrary, and unused */
SQLStatementRootStartWorkSlots: 100, /* arbitrary, and unused */
}
// Override applies values from "override" to the receiver that differ from Go
// defaults.
func (o *Options) Override(override *Options) {
if override.MinCPUSlots != 0 {
o.MinCPUSlots = override.MinCPUSlots
}
if override.MaxCPUSlots != 0 {
o.MaxCPUSlots = override.MaxCPUSlots
}
if override.SQLKVResponseBurstTokens != 0 {
o.SQLKVResponseBurstTokens = override.SQLKVResponseBurstTokens
}
if override.SQLSQLResponseBurstTokens != 0 {
o.SQLSQLResponseBurstTokens = override.SQLSQLResponseBurstTokens
}
if override.SQLStatementLeafStartWorkSlots != 0 {
o.SQLStatementLeafStartWorkSlots = override.SQLStatementLeafStartWorkSlots
}
if override.SQLStatementRootStartWorkSlots != 0 {
o.SQLStatementRootStartWorkSlots = override.SQLStatementRootStartWorkSlots
}
if override.TestingDisableSkipEnforcement {
o.TestingDisableSkipEnforcement = true
}
}
type makeRequesterFunc func(
_ log.AmbientContext, workKind WorkKind, granter granter, settings *cluster.Settings,
metrics *WorkQueueMetrics, opts workQueueOptions) requester
type makeStoreRequesterFunc func(
_ log.AmbientContext, granters [admissionpb.NumWorkClasses]granterWithStoreWriteDone,
settings *cluster.Settings, metrics *WorkQueueMetrics, opts workQueueOptions) storeRequester
// NewGrantCoordinators constructs GrantCoordinators and WorkQueues for a
// regular cluster node. Caller is responsible for:
// - hooking up GrantCoordinators.Regular to receive calls to CPULoad, and
// - to set a PebbleMetricsProvider on GrantCoordinators.Stores
//
// Regular and elastic requests pass through GrantCoordinators.{Regular,Elastic}
// respectively, and a subset of requests pass through each store's
// GrantCoordinator. We arrange these such that requests (that need to) first
// pass through a store's GrantCoordinator and then through the
// {regular,elastic} one. This ensures that we are not using slots/elastic CPU
// tokens in the latter level on requests that are blocked elsewhere for
// admission. Additionally, we don't want the CPU scheduler signal that is
// implicitly used in grant chains to delay admission through the per store
// GrantCoordinators since they are not trying to control CPU usage, so we turn
// off grant chaining in those coordinators.
func NewGrantCoordinators(
ambientCtx log.AmbientContext, st *cluster.Settings, opts Options, registry *metric.Registry,
) GrantCoordinators {
metrics := makeGrantCoordinatorMetrics()
registry.AddMetricStruct(metrics)
return GrantCoordinators{
Stores: makeStoresGrantCoordinators(ambientCtx, opts, st, metrics, registry),
Regular: makeRegularGrantCoordinator(ambientCtx, opts, st, metrics, registry),
Elastic: makeElasticGrantCoordinator(ambientCtx, st, registry),
}
}
func makeElasticGrantCoordinator(
ambientCtx log.AmbientContext, st *cluster.Settings, registry *metric.Registry,
) *ElasticCPUGrantCoordinator {
schedulerLatencyListenerMetrics := makeSchedulerLatencyListenerMetrics()
registry.AddMetricStruct(schedulerLatencyListenerMetrics)
elasticCPUGranterMetrics := makeElasticCPUGranterMetrics()
registry.AddMetricStruct(elasticCPUGranterMetrics)
elasticWorkQueueMetrics := makeWorkQueueMetrics("elastic-cpu", registry,
admissionpb.BulkNormalPri, admissionpb.NormalPri)
elasticCPUGranter := newElasticCPUGranter(ambientCtx, st, elasticCPUGranterMetrics)
schedulerLatencyListener := newSchedulerLatencyListener(ambientCtx, st, schedulerLatencyListenerMetrics, elasticCPUGranter)
elasticCPUInternalWorkQueue := &WorkQueue{}
initWorkQueue(elasticCPUInternalWorkQueue, ambientCtx, KVWork, elasticCPUGranter, st,
elasticWorkQueueMetrics,
workQueueOptions{usesTokens: true}) // will be closed by the embedding *ElasticCPUWorkQueue
elasticCPUWorkQueue := makeElasticCPUWorkQueue(st, elasticCPUInternalWorkQueue, elasticCPUGranter, elasticCPUGranterMetrics)
elasticCPUGrantCoordinator := makeElasticCPUGrantCoordinator(elasticCPUGranter, elasticCPUWorkQueue, schedulerLatencyListener)
elasticCPUGranter.setRequester(elasticCPUInternalWorkQueue)
schedulerLatencyListener.setCoord(elasticCPUGrantCoordinator)
return elasticCPUGrantCoordinator
}
func makeStoresGrantCoordinators(
ambientCtx log.AmbientContext,
opts Options,
st *cluster.Settings,
metrics GrantCoordinatorMetrics,
registry *metric.Registry,
) *StoreGrantCoordinators {
// These metrics are shared across all stores and broken down by priority for
// the common priorities.
// TODO(baptist): Add per-store metrics.
storeWorkQueueMetrics :=
makeWorkQueueMetrics(workKindString(KVWork)+"-stores", registry,
admissionpb.TTLLowPri, admissionpb.BulkNormalPri,
admissionpb.NormalPri, admissionpb.LockingPri)
makeStoreRequester := makeStoreWorkQueue
if opts.makeStoreRequesterFunc != nil {
makeStoreRequester = opts.makeStoreRequesterFunc
}
storeCoordinators := &StoreGrantCoordinators{
ambientCtx: ambientCtx,
settings: st,
makeStoreRequesterFunc: makeStoreRequester,
kvIOTokensExhaustedDuration: metrics.KVIOTokensExhaustedDuration,
workQueueMetrics: storeWorkQueueMetrics,
}
return storeCoordinators
}
func makeRegularGrantCoordinator(
ambientCtx log.AmbientContext,
opts Options,
st *cluster.Settings,
metrics GrantCoordinatorMetrics,
registry *metric.Registry,
) *GrantCoordinator {
makeRequester := makeWorkQueue
if opts.makeRequesterFunc != nil {
makeRequester = opts.makeRequesterFunc
}
kvSlotAdjuster := &kvSlotAdjuster{
settings: st,
minCPUSlots: opts.MinCPUSlots,
maxCPUSlots: opts.MaxCPUSlots,
totalSlotsMetric: metrics.KVTotalSlots,
cpuLoadShortPeriodDurationMetric: metrics.KVCPULoadShortPeriodDuration,
cpuLoadLongPeriodDurationMetric: metrics.KVCPULoadLongPeriodDuration,
slotAdjusterIncrementsMetric: metrics.KVSlotAdjusterIncrements,
slotAdjusterDecrementsMetric: metrics.KVSlotAdjusterDecrements,
}
coord := &GrantCoordinator{
ambientCtx: ambientCtx,
settings: st,
cpuOverloadIndicator: kvSlotAdjuster,
cpuLoadListener: kvSlotAdjuster,
useGrantChains: true,
testingDisableSkipEnforcement: opts.TestingDisableSkipEnforcement,
numProcs: 1,
grantChainID: 1,
}
kvg := &slotGranter{
coord: coord,
workKind: KVWork,
totalSlots: opts.MinCPUSlots,
usedSlotsMetric: metrics.KVUsedSlots,
slotsExhaustedDurationMetric: metrics.KVSlotsExhaustedDuration,
}
kvSlotAdjuster.granter = kvg
wqMetrics := makeWorkQueueMetrics(workKindString(KVWork), registry, admissionpb.NormalPri, admissionpb.LockingPri)
req := makeRequester(ambientCtx, KVWork, kvg, st, wqMetrics, makeWorkQueueOptions(KVWork))
coord.queues[KVWork] = req
kvg.requester = req
coord.granters[KVWork] = kvg
tg := &tokenGranter{
coord: coord,
workKind: SQLKVResponseWork,
availableBurstTokens: opts.SQLKVResponseBurstTokens,
maxBurstTokens: opts.SQLKVResponseBurstTokens,
cpuOverload: kvSlotAdjuster,
}
wqMetrics = makeWorkQueueMetrics(workKindString(SQLKVResponseWork), registry, admissionpb.NormalPri, admissionpb.LockingPri)
req = makeRequester(
ambientCtx, SQLKVResponseWork, tg, st, wqMetrics, makeWorkQueueOptions(SQLKVResponseWork))
coord.queues[SQLKVResponseWork] = req
tg.requester = req
coord.granters[SQLKVResponseWork] = tg
tg = &tokenGranter{
coord: coord,
workKind: SQLSQLResponseWork,
availableBurstTokens: opts.SQLSQLResponseBurstTokens,
maxBurstTokens: opts.SQLSQLResponseBurstTokens,
cpuOverload: kvSlotAdjuster,
}
wqMetrics = makeWorkQueueMetrics(workKindString(SQLSQLResponseWork), registry, admissionpb.NormalPri, admissionpb.LockingPri)
req = makeRequester(ambientCtx,
SQLSQLResponseWork, tg, st, wqMetrics, makeWorkQueueOptions(SQLSQLResponseWork))
coord.queues[SQLSQLResponseWork] = req
tg.requester = req
coord.granters[SQLSQLResponseWork] = tg
sg := &slotGranter{
coord: coord,
workKind: SQLStatementLeafStartWork,
totalSlots: opts.SQLStatementLeafStartWorkSlots,
cpuOverload: kvSlotAdjuster,
usedSlotsMetric: metrics.SQLLeafStartUsedSlots,
}
wqMetrics = makeWorkQueueMetrics(workKindString(SQLStatementLeafStartWork), registry, admissionpb.NormalPri, admissionpb.LockingPri)
req = makeRequester(ambientCtx,
SQLStatementLeafStartWork, sg, st, wqMetrics, makeWorkQueueOptions(SQLStatementLeafStartWork))
coord.queues[SQLStatementLeafStartWork] = req
sg.requester = req
coord.granters[SQLStatementLeafStartWork] = sg
sg = &slotGranter{
coord: coord,
workKind: SQLStatementRootStartWork,
totalSlots: opts.SQLStatementRootStartWorkSlots,
cpuOverload: kvSlotAdjuster,
usedSlotsMetric: metrics.SQLRootStartUsedSlots,
}
wqMetrics = makeWorkQueueMetrics(workKindString(SQLStatementRootStartWork), registry, admissionpb.NormalPri, admissionpb.LockingPri)
req = makeRequester(ambientCtx,
SQLStatementRootStartWork, sg, st, wqMetrics, makeWorkQueueOptions(SQLStatementRootStartWork))
coord.queues[SQLStatementRootStartWork] = req
sg.requester = req
coord.granters[SQLStatementRootStartWork] = sg
return coord
}
// Prevent the linter from emitting unused warnings until this is hooked up.
var _ = NewGrantCoordinatorSQL
// NewGrantCoordinatorSQL constructs a GrantCoordinator and WorkQueues for a
// single-tenant SQL node in a multi-tenant cluster. Caller is responsible for
// hooking this up to receive calls to CPULoad.
func NewGrantCoordinatorSQL(
ambientCtx log.AmbientContext, st *cluster.Settings, registry *metric.Registry, opts Options,
) *GrantCoordinator {
makeRequester := makeWorkQueue
if opts.makeRequesterFunc != nil {
makeRequester = opts.makeRequesterFunc
}
metrics := makeGrantCoordinatorMetrics()
registry.AddMetricStruct(metrics)
sqlNodeCPU := &sqlNodeCPUOverloadIndicator{}
coord := &GrantCoordinator{
ambientCtx: ambientCtx,
settings: st,
cpuOverloadIndicator: sqlNodeCPU,
cpuLoadListener: sqlNodeCPU,
useGrantChains: true,
numProcs: 1,
grantChainID: 1,
}
tg := &tokenGranter{
coord: coord,
workKind: SQLKVResponseWork,
availableBurstTokens: opts.SQLKVResponseBurstTokens,
maxBurstTokens: opts.SQLKVResponseBurstTokens,
cpuOverload: sqlNodeCPU,
}
wqMetrics := makeWorkQueueMetrics(workKindString(SQLKVResponseWork), registry)
req := makeRequester(ambientCtx,
SQLKVResponseWork, tg, st, wqMetrics, makeWorkQueueOptions(SQLKVResponseWork))
coord.queues[SQLKVResponseWork] = req
tg.requester = req
coord.granters[SQLKVResponseWork] = tg
tg = &tokenGranter{
coord: coord,
workKind: SQLSQLResponseWork,
availableBurstTokens: opts.SQLSQLResponseBurstTokens,
maxBurstTokens: opts.SQLSQLResponseBurstTokens,
cpuOverload: sqlNodeCPU,
}
wqMetrics = makeWorkQueueMetrics(workKindString(SQLSQLResponseWork), registry)
req = makeRequester(ambientCtx,
SQLSQLResponseWork, tg, st, wqMetrics, makeWorkQueueOptions(SQLSQLResponseWork))
coord.queues[SQLSQLResponseWork] = req
tg.requester = req
coord.granters[SQLSQLResponseWork] = tg
sg := &slotGranter{
coord: coord,
workKind: SQLStatementLeafStartWork,
totalSlots: opts.SQLStatementLeafStartWorkSlots,
cpuOverload: sqlNodeCPU,
usedSlotsMetric: metrics.SQLLeafStartUsedSlots,
}
wqMetrics = makeWorkQueueMetrics(workKindString(SQLStatementLeafStartWork), registry)
req = makeRequester(ambientCtx,
SQLStatementLeafStartWork, sg, st, wqMetrics, makeWorkQueueOptions(SQLStatementLeafStartWork))
coord.queues[SQLStatementLeafStartWork] = req
sg.requester = req
coord.granters[SQLStatementLeafStartWork] = sg
sg = &slotGranter{
coord: coord,
workKind: SQLStatementRootStartWork,
totalSlots: opts.SQLStatementRootStartWorkSlots,
cpuOverload: sqlNodeCPU,
usedSlotsMetric: metrics.SQLRootStartUsedSlots,
}
wqMetrics = makeWorkQueueMetrics(workKindString(SQLStatementRootStartWork), registry)
req = makeRequester(ambientCtx,
SQLStatementRootStartWork, sg, st, wqMetrics, makeWorkQueueOptions(SQLStatementRootStartWork))
coord.queues[SQLStatementRootStartWork] = req
sg.requester = req
coord.granters[SQLStatementRootStartWork] = sg
return coord
}
// pebbleMetricsTick is called every adjustmentInterval seconds and passes
// through to the ioLoadListener, so that it can adjust the plan for future IO
// token allocations.
func (coord *GrantCoordinator) pebbleMetricsTick(ctx context.Context, m StoreMetrics) {
coord.ioLoadListener.pebbleMetricsTick(ctx, m)
}
// allocateIOTokensTick tells the ioLoadListener to allocate tokens.
func (coord *GrantCoordinator) allocateIOTokensTick() {
coord.ioLoadListener.allocateTokensTick()
coord.mu.Lock()
defer coord.mu.Unlock()
if !coord.grantChainActive {
coord.tryGrant()
}
// Else, let the grant chain finish. NB: we turn off grant chains on the
// GrantCoordinators used for IO, so the if-condition is always true.
}
// testingTryGrant is only for unit tests, since they sometimes cut out
// support classes like the ioLoadListener.
func (coord *GrantCoordinator) testingTryGrant() {
coord.mu.Lock()
defer coord.mu.Unlock()
if !coord.grantChainActive {
coord.tryGrant()
}
}
// GetWorkQueue returns the WorkQueue for a particular WorkKind. Can be nil if
// the NewGrantCoordinator* function does not construct a WorkQueue for that
// work.
// Implementation detail: don't use this method when the GrantCoordinator is
// created by the StoreGrantCoordinators since those have a StoreWorkQueues.
// The TryGetQueueForStore is the external facing method in that case since
// the individual GrantCoordinators are hidden.
func (coord *GrantCoordinator) GetWorkQueue(workKind WorkKind) *WorkQueue {
return coord.queues[workKind].(*WorkQueue)
}
// CPULoad implements CPULoadListener and is called periodically (see
// CPULoadListener for details). The same frequency is used for refilling the
// burst tokens since synchronizing the two means that the refilled burst can
// take into account the latest schedulers stats (indirectly, via the
// implementation of cpuOverloadIndicator).
func (coord *GrantCoordinator) CPULoad(runnable int, procs int, samplePeriod time.Duration) {
ctx := coord.ambientCtx.AnnotateCtx(context.Background())
if log.V(1) {
if coord.lastCPULoadSamplePeriod != 0 && coord.lastCPULoadSamplePeriod != samplePeriod &&
KVAdmissionControlEnabled.Get(&coord.settings.SV) {
log.Infof(ctx, "CPULoad switching to period %s", samplePeriod.String())
}
}
coord.lastCPULoadSamplePeriod = samplePeriod
coord.mu.Lock()
defer coord.mu.Unlock()
coord.numProcs = procs
coord.cpuLoadListener.CPULoad(runnable, procs, samplePeriod)
// Slot adjustment and token refilling requires 1ms periods to work well. If
// the CPULoad ticks are less frequent, there is no guarantee that the
// tokens or slots will be sufficient to service requests. This is
// particularly the case for slots where we dynamically adjust them, and
// high contention can suddenly result in high slot utilization even while
// cpu utilization stays low. We don't want to artificially bottleneck
// request processing when we are in this slow CPULoad ticks regime since we
// can't adjust slots or refill tokens fast enough. So we explicitly tell
// the granters to not do token or slot enforcement.
skipEnforcement := samplePeriod > time.Millisecond
coord.granters[SQLKVResponseWork].(*tokenGranter).refillBurstTokens(skipEnforcement)
coord.granters[SQLSQLResponseWork].(*tokenGranter).refillBurstTokens(skipEnforcement)
if coord.granters[KVWork] != nil {
if !coord.testingDisableSkipEnforcement {
kvg := coord.granters[KVWork].(*slotGranter)
kvg.skipSlotEnforcement = skipEnforcement
}
}
if coord.grantChainActive && !coord.tryTerminateGrantChain() {
return
}
coord.tryGrant()
}
// tryGet is called by granter.tryGet with the WorkKind.
func (coord *GrantCoordinator) tryGet(
workKind WorkKind, count int64, demuxHandle int8,
) (granted bool) {
coord.mu.Lock()
defer coord.mu.Unlock()
// It is possible that a grant chain is active, and has not yet made its way
// to this workKind. So it may be more reasonable to queue. But we have some
// concerns about incurring the delay of multiple goroutine context switches
// so we ignore this case.
res := coord.granters[workKind].tryGetLocked(count, demuxHandle)
switch res {
case grantSuccess:
// Grant chain may be active, but it did not get in the way of this grant,
// and the effect of this grant in terms of overload will be felt by the
// grant chain.
return true
case grantFailDueToSharedResource:
// This could be a transient overload, that may not be noticed by the
// grant chain. We don't want it to continue granting to lower priority
// WorkKinds, while a higher priority one is waiting, so we terminate it.
if coord.grantChainActive && coord.grantChainIndex >= workKind {
coord.tryTerminateGrantChain()
}
return false
case grantFailLocal:
return false
default:
panic(errors.AssertionFailedf("unknown grantResult"))
}
}
// returnGrant is called by granter.returnGrant with the WorkKind.
func (coord *GrantCoordinator) returnGrant(workKind WorkKind, count int64, demuxHandle int8) {
coord.mu.Lock()
defer coord.mu.Unlock()
coord.granters[workKind].returnGrantLocked(count, demuxHandle)
if coord.grantChainActive {
if coord.grantChainIndex > workKind &&
coord.granters[workKind].requesterHasWaitingRequests() {
// There are waiting requests that will not be served by the grant chain.
// Better to terminate it and start afresh.
if !coord.tryTerminateGrantChain() {
return
}
} else {
// Else either the grant chain will get to this workKind, or there are no waiting requests.
return
}
}
coord.tryGrant()
}
// tookWithoutPermission is called by granter.tookWithoutPermission with the
// WorkKind.
func (coord *GrantCoordinator) tookWithoutPermission(
workKind WorkKind, count int64, demuxHandle int8,
) {
coord.mu.Lock()
defer coord.mu.Unlock()
coord.granters[workKind].tookWithoutPermissionLocked(count, demuxHandle)
}
// continueGrantChain is called by granter.continueGrantChain with the
// WorkKind. Never called if !coord.useGrantChains.
func (coord *GrantCoordinator) continueGrantChain(workKind WorkKind, grantChainID grantChainID) {
if grantChainID == noGrantChain {
return
}
coord.mu.Lock()
defer coord.mu.Unlock()
if coord.grantChainID != grantChainID {
// Someone terminated grantChainID by incrementing coord.grantChainID.
return
}
coord.tryGrant()
}
// delayForGrantChainTermination causes a delay in terminating a grant chain.
// Terminating a grant chain immediately typically causes a new one to start
// immediately that can burst up to its maximum initial grant burst. Which
// means frequent terminations followed by new starts impose little control
// over the rate at which tokens are granted (slots are better controlled
// since we know when the work finishes). This causes huge spikes in the
// runnable goroutine count, observed at 1ms granularity. This spike causes
// the kvSlotAdjuster to ratchet down the totalSlots for KV work all the way
// down to 1, which later causes the runnable gorouting count to crash down
// to a value close to 0, leading to under-utilization.
//
// TODO(sumeer): design admission behavior metrics that can be used to
// understand the behavior in detail and to quantify improvements when changing
// heuristics. One metric would be mean and variance of the runnable count,
// computed using the 1ms samples, and exported/logged every 60s.
var delayForGrantChainTermination = 100 * time.Millisecond
// tryTerminateGrantChain attempts to terminate the current grant chain, and
// returns true iff it is terminated, in which case a new one can be
// immediately started.
// REQUIRES: coord.grantChainActive==true
func (coord *GrantCoordinator) tryTerminateGrantChain() bool {
now := timeutil.Now()
if delayForGrantChainTermination > 0 &&
now.Sub(coord.grantChainStartTime) < delayForGrantChainTermination {
return false
}
// Incrementing the ID will cause the existing grant chain to die out when
// the grantee calls continueGrantChain.
coord.grantChainID++
coord.grantChainActive = false
coord.grantChainStartTime = time.Time{}
return true
}
// tryGrant tries to either continue an existing grant chain, or if no grant
// chain is active, tries to start a new grant chain when grant chaining is
// enabled, or grants as much as it can when grant chaining is disabled.
func (coord *GrantCoordinator) tryGrant() {
startingChain := false
if !coord.grantChainActive {
// NB: always set to true when !coord.useGrantChains, and we won't
// actually use this to start a grant chain (see below).
startingChain = true
coord.grantChainIndex = 0
}
// Assume that we will not be able to start a new grant chain, or that the
// existing one will die out. The code below will set it to true if neither
// is true.
coord.grantChainActive = false
grantBurstCount := 0
// Grant in a burst proportional to numProcs, to generate a runnable for
// each.
grantBurstLimit := coord.numProcs
// Additionally, increase the burst size proportional to a fourth of the
// overload threshold. We experimentally observed that this resulted in
// better CPU utilization. We don't use the full overload threshold since we
// don't want to over grant for non-KV work since that causes the KV slots
// to (unfairly) start decreasing, since we lose control over how many
// goroutines are runnable.
multiplier := int(KVSlotAdjusterOverloadThreshold.Get(&coord.settings.SV) / 4)
if multiplier == 0 {
multiplier = 1
}
grantBurstLimit *= multiplier
// Only the case of a grant chain being active returns from within the
// OuterLoop.
OuterLoop:
for ; coord.grantChainIndex < numWorkKinds; coord.grantChainIndex++ {
localDone := false
granter := coord.granters[coord.grantChainIndex]
if granter == nil {
// A GrantCoordinator can be limited to certain WorkKinds, and the
// remaining will be nil.
continue
}
for granter.requesterHasWaitingRequests() && !localDone {
chainID := noGrantChain
if grantBurstCount+1 == grantBurstLimit && coord.useGrantChains {
chainID = coord.grantChainID
}
res := granter.tryGrantLocked(chainID)
switch res {
case grantSuccess:
grantBurstCount++
if grantBurstCount == grantBurstLimit && coord.useGrantChains {
coord.grantChainActive = true
if startingChain {
coord.grantChainStartTime = timeutil.Now()
}
return
}
case grantFailDueToSharedResource:
break OuterLoop
case grantFailLocal:
localDone = true
default:
panic(errors.AssertionFailedf("unknown grantResult"))
}
}
}
// INVARIANT: !grantChainActive. The chain either did not start or the
// existing one died. If the existing one died, we increment grantChainID
// since it represents the ID to be used for the next chain. Note that
// startingChain is always true when !useGrantChains, so this if-block is
// not executed.
if !startingChain {
coord.grantChainID++
}
}
// Close implements the stop.Closer interface.
func (coord *GrantCoordinator) Close() {
for i := range coord.queues {
if coord.queues[i] != nil {
coord.queues[i].close()
}
}
}
func (coord *GrantCoordinator) String() string {
return redact.StringWithoutMarkers(coord)
}
// SafeFormat implements the redact.SafeFormatter interface.
func (coord *GrantCoordinator) SafeFormat(s redact.SafePrinter, verb rune) {
coord.mu.Lock()
defer coord.mu.Unlock()
s.Printf("(chain: id: %d active: %t index: %d)",
coord.grantChainID, coord.grantChainActive, coord.grantChainIndex,
)
spaceStr := redact.RedactableString(" ")
newlineStr := redact.RedactableString("\n")
curSep := spaceStr
for i := range coord.granters {
kind := WorkKind(i)
switch kind {
case KVWork:
switch g := coord.granters[i].(type) {
case *slotGranter:
s.Printf("%s%s: used: %d, total: %d", curSep, workKindString(kind), g.usedSlots, g.totalSlots)
case *kvStoreTokenGranter:
s.Printf(" io-avail: %d, elastic-disk-bw-tokens-avail: %d", g.availableIOTokens,
g.elasticDiskBWTokensAvailable)
}
case SQLStatementLeafStartWork, SQLStatementRootStartWork:
if coord.granters[i] != nil {
g := coord.granters[i].(*slotGranter)
s.Printf("%s%s: used: %d, total: %d", curSep, workKindString(kind), g.usedSlots, g.totalSlots)
}
case SQLKVResponseWork, SQLSQLResponseWork:
if coord.granters[i] != nil {
g := coord.granters[i].(*tokenGranter)
s.Printf("%s%s: avail: %d", curSep, workKindString(kind), g.availableBurstTokens)
if kind == SQLKVResponseWork {
curSep = newlineStr
} else {
curSep = spaceStr
}
}
}
}
}
// GrantCoordinatorMetrics are metrics associated with a GrantCoordinator.
type GrantCoordinatorMetrics struct {
KVTotalSlots *metric.Gauge
KVUsedSlots *metric.Gauge
KVSlotsExhaustedDuration *metric.Counter
KVCPULoadShortPeriodDuration *metric.Counter
KVCPULoadLongPeriodDuration *metric.Counter
KVSlotAdjusterIncrements *metric.Counter
KVSlotAdjusterDecrements *metric.Counter
KVIOTokensExhaustedDuration *metric.Counter
SQLLeafStartUsedSlots *metric.Gauge
SQLRootStartUsedSlots *metric.Gauge
}
// MetricStruct implements the metric.Struct interface.
func (GrantCoordinatorMetrics) MetricStruct() {}
func makeGrantCoordinatorMetrics() GrantCoordinatorMetrics {
m := GrantCoordinatorMetrics{
KVTotalSlots: metric.NewGauge(totalSlots),
KVUsedSlots: metric.NewGauge(addName(workKindString(KVWork), usedSlots)),
KVSlotsExhaustedDuration: metric.NewCounter(kvSlotsExhaustedDuration),
KVCPULoadShortPeriodDuration: metric.NewCounter(kvCPULoadShortPeriodDuration),
KVCPULoadLongPeriodDuration: metric.NewCounter(kvCPULoadLongPeriodDuration),
KVSlotAdjusterIncrements: metric.NewCounter(kvSlotAdjusterIncrements),
KVSlotAdjusterDecrements: metric.NewCounter(kvSlotAdjusterDecrements),
KVIOTokensExhaustedDuration: metric.NewCounter(kvIOTokensExhaustedDuration),
SQLLeafStartUsedSlots: metric.NewGauge(addName(workKindString(SQLStatementLeafStartWork), usedSlots)),
SQLRootStartUsedSlots: metric.NewGauge(addName(workKindString(SQLStatementRootStartWork), usedSlots)),
}
return m
}
// ElasticCPUGrantCoordinator coordinates grants for elastic CPU tokens, it has
// a single granter-requester pair. Since it's used for elastic CPU work, and
// the total allotment of CPU available for such work is reduced before getting
// close to CPU saturation (we observe 1ms+ p99 scheduling latencies when
// running at 65% utilization on 8vCPU machines, which is enough to affect
// foreground latencies), we don't want it to serve as a gatekeeper for
// SQL-level admission. All this informs why its structured as a separate grant
// coordinator.
//
// TODO(irfansharif): Ideally we wouldn't use this separate
// ElasticGrantCoordinator and just make this part of the one GrantCoordinator
// above but given we're dealing with a different workClass (elasticWorkClass)
// but for an existing WorkKind (KVWork), and not all APIs on the grant
// coordinator currently segment across the two, it was easier to copy over some
// of the mediating code instead (grant chains also don't apply in this scheme).
// Try to do something better here and revisit the existing abstractions; see
// github.com/cockroachdb/cockroach/pull/86638#pullrequestreview-1084437330.
type ElasticCPUGrantCoordinator struct {
SchedulerLatencyListener SchedulerLatencyListener
ElasticCPUWorkQueue *ElasticCPUWorkQueue
elasticCPUGranter *elasticCPUGranter
}
func makeElasticCPUGrantCoordinator(
elasticCPUGranter *elasticCPUGranter,
elasticCPUWorkQueue *ElasticCPUWorkQueue,
listener *schedulerLatencyListener,
) *ElasticCPUGrantCoordinator {
return &ElasticCPUGrantCoordinator{
elasticCPUGranter: elasticCPUGranter,
ElasticCPUWorkQueue: elasticCPUWorkQueue,