Skip to content

Commit 80484e0

Browse files
committed
rac2: reduce tokenCounter mutex contention
The metrics update and logging are moved out of the critical section. Epic: none Release note: None
1 parent 1c93e8a commit 80484e0

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

pkg/kv/kvserver/kvflowcontrol/rac2/token_counter.go

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,15 @@ func (t *tokenCounter) TryDeduct(
320320
expensiveLog = true
321321
}
322322
t.mu.Lock()
323-
defer t.mu.Unlock()
324323

325-
tokensAvailable := t.tokensLocked(wc)
324+
tokensAvailable := t.tokensLocked(wc) // nolint:deferunlockcheck
326325
if tokensAvailable <= 0 {
326+
t.mu.Unlock() // nolint:deferunlockcheck
327327
return 0
328328
}
329329

330330
adjust := min(tokensAvailable, tokens)
331-
t.adjustLocked(ctx, wc, -adjust, now, flag, expensiveLog)
331+
t.adjustLockedAndUnlock(ctx, wc, -adjust, now, flag, expensiveLog)
332332
return adjust
333333
}
334334

@@ -599,39 +599,45 @@ func (t *tokenCounter) adjust(
599599
if log.V(2) {
600600
expensiveLog = true
601601
}
602-
func() {
603-
t.mu.Lock()
604-
defer t.mu.Unlock()
605-
t.adjustLocked(ctx, class, delta, now, flag, expensiveLog)
606-
}()
607-
602+
t.mu.Lock()
603+
t.adjustLockedAndUnlock(ctx, class, delta, now, flag, expensiveLog) // nolint:deferunlockcheck
608604
}
609605

610-
func (t *tokenCounter) adjustLocked(
606+
func (t *tokenCounter) adjustLockedAndUnlock(
611607
ctx context.Context,
612608
class admissionpb.WorkClass,
613609
delta kvflowcontrol.Tokens,
614610
now time.Time,
615611
flag TokenAdjustFlag,
616612
expensiveLog bool,
617613
) {
614+
t.mu.AssertHeld()
618615
var adjustment, unaccounted tokensPerWorkClass
619-
switch class {
620-
case admissionpb.RegularWorkClass:
621-
adjustment.regular, unaccounted.regular =
622-
t.mu.counters[admissionpb.RegularWorkClass].adjustTokensLocked(
623-
ctx, delta, now, false /* isReset */, flag)
616+
// Only populated when expensiveLog is true.
617+
var regularTokens, elasticTokens kvflowcontrol.Tokens
618+
func() {
619+
defer t.mu.Unlock()
620+
switch class {
621+
case regular:
622+
adjustment.regular, unaccounted.regular =
623+
t.mu.counters[regular].adjustTokensLocked(
624+
ctx, delta, now, false /* isReset */, flag)
624625
// Regular {deductions,returns} also affect elastic flow tokens.
625-
adjustment.elastic, unaccounted.elastic =
626-
t.mu.counters[admissionpb.ElasticWorkClass].adjustTokensLocked(
627-
ctx, delta, now, false /* isReset */, flag)
628-
629-
case admissionpb.ElasticWorkClass:
630-
// Elastic {deductions,returns} only affect elastic flow tokens.
631-
adjustment.elastic, unaccounted.elastic =
632-
t.mu.counters[admissionpb.ElasticWorkClass].adjustTokensLocked(
633-
ctx, delta, now, false /* isReset */, flag)
634-
}
626+
adjustment.elastic, unaccounted.elastic =
627+
t.mu.counters[elastic].adjustTokensLocked(
628+
ctx, delta, now, false /* isReset */, flag)
629+
630+
case elastic:
631+
// Elastic {deductions,returns} only affect elastic flow tokens.
632+
adjustment.elastic, unaccounted.elastic =
633+
t.mu.counters[elastic].adjustTokensLocked(
634+
ctx, delta, now, false /* isReset */, flag)
635+
}
636+
if expensiveLog {
637+
regularTokens = t.tokensLocked(regular)
638+
elasticTokens = t.tokensLocked(elastic)
639+
}
640+
}()
635641

636642
// Adjust metrics if any tokens were actually adjusted or unaccounted for
637643
// tokens were detected.
@@ -643,7 +649,7 @@ func (t *tokenCounter) adjustLocked(
643649
}
644650
if expensiveLog {
645651
log.Infof(ctx, "adjusted %v flow tokens (wc=%v stream=%v delta=%v flag=%v): regular=%v elastic=%v",
646-
t.tokenType, class, t.stream, delta, flag, t.tokensLocked(regular), t.tokensLocked(elastic))
652+
t.tokenType, class, t.stream, delta, flag, regularTokens, elasticTokens)
647653
}
648654
}
649655

0 commit comments

Comments
 (0)