perf: share WAL command arrival state with its hold

This commit is contained in:
pingqiu committed 2026-12-02 05:50:25 -08:00
1 parent c652a6a2f7
commit 2f9a4cc689
4 files changed
+39 -24

No files matched your search

@@ -1314,8 +1314,8 @@ func expiredCommand(ctx context.Context) error {
// backend callers. The context carries one identity through all store batches.
func (b *StorageBackend) WriteCommandContext(ctx context.Context, arrival time.Time) (context.Context, context.CancelFunc) {
if s, ok := b.storage.(*storage.WALStore); ok {
ctx = storage.WithWALWriteArrival(ctx, s, arrival)
if frontend.UsesLegacyWriteBudget(ctx) {
ctx = storage.WithWALWriteArrival(ctx, s, arrival)
return frontend.WithWriteBudget(ctx, arrival)
}
cfg := b.walHold
+1 -1
View File
@@ -39,7 +39,7 @@ type walAdmission struct {
queueMu sync.Mutex
first, last *walAdmissionTicket
active *walAdmissionTicket // an admitted append cannot be preempted
nextOrder uint64
nextOrder atomic.Uint64
headVersion uint64
sem chan struct{} // counting semaphore for concurrent appenders
walUsed func() float64
@@ -15,12 +15,7 @@ type walAdmissionTicket struct {
order uint64
}
func (a *walAdmission) newCommandOrder() uint64 {
a.queueMu.Lock()
defer a.queueMu.Unlock()
a.nextOrder++
return a.nextOrder
}
func (a *walAdmission) newCommandOrder() uint64 { return a.nextOrder.Add(1) }
// Pending batches retain their command arrival order. An append that already
// owns capacity remains pinned at the head until its store call returns.
@@ -31,8 +26,7 @@ func (a *walAdmission) enqueueTicket(arrival *walWriteArrival) *walAdmissionTick
if arrival != nil {
t.arrival, t.order = arrival.arrival, arrival.order
} else {
a.nextOrder++
t.order = a.nextOrder
t.order = a.newCommandOrder()
}
priorFirst := a.first
before := a.last
+35 -14
View File
@@ -20,12 +20,7 @@ type walWriteArrival struct {
order uint64
}
// WithWALWriteArrival preserves command ordering across sequential store calls,
// independently of the transport's pressure timeout policy. It takes no ticket.
func WithWALWriteArrival(ctx context.Context, s *WALStore, arrival time.Time) context.Context {
if v, _ := ctx.Value(walWriteArrivalKey{}).(*walWriteArrival); v != nil && v.store == s {
return ctx
}
func newWALWriteArrival(s *WALStore, arrival time.Time) walWriteArrival {
if arrival.IsZero() {
arrival = time.Now()
}
@@ -33,14 +28,36 @@ func WithWALWriteArrival(ctx context.Context, s *WALStore, arrival time.Time) co
if s.admission != nil {
order = s.admission.newCommandOrder()
}
return context.WithValue(ctx, walWriteArrivalKey{}, &walWriteArrival{store: s, arrival: arrival, order: order})
return walWriteArrival{store: s, arrival: arrival, order: order}
}
func writeArrivalFor(ctx context.Context, s *WALStore) *walWriteArrival {
if v, _ := ctx.Value(walWriteArrivalKey{}).(*walWriteArrival); v != nil && v.store == s {
return v
}
if h := holdFor(ctx, s); h != nil {
return &h.walWriteArrival
}
return nil
}
// WithWALWriteArrival preserves command ordering across sequential store calls,
// independently of the transport's pressure timeout policy. It takes no ticket.
func WithWALWriteArrival(ctx context.Context, s *WALStore, arrival time.Time) context.Context {
if writeArrivalFor(ctx, s) != nil {
return ctx
}
v := newWALWriteArrival(s, arrival)
return context.WithValue(ctx, walWriteArrivalKey{}, &v)
}
func admissionArrival(ctx context.Context, a *walAdmission) *walWriteArrival {
v, _ := ctx.Value(walWriteArrivalKey{}).(*walWriteArrival)
if v != nil && v.store.admission == a {
if v, _ := ctx.Value(walWriteArrivalKey{}).(*walWriteArrival); v != nil && v.store.admission == a {
return v
}
if h := admissionHold(ctx, a); h != nil {
return &h.walWriteArrival
}
return nil
}
@@ -49,12 +66,11 @@ type walWriteHoldKey struct{}
// A command owns this state across its sequential store batches. Only the
// command's write goroutine observes progress or finishes the observations.
type walWriteHold struct {
walWriteArrival
waiting bool
waitStarted time.Time
waitNanos int64
reason walHoldReason
store *WALStore
arrival time.Time
maxHold time.Duration
final bool
window walHoldWindow
@@ -71,9 +87,14 @@ func WithWALWriteHold(ctx context.Context, s *WALStore, arrival time.Time, maxHo
if h, _ := ctx.Value(walWriteHoldKey{}).(*walWriteHold); h != nil {
return ctx, func() {}
}
ctx = WithWALWriteArrival(ctx, s, arrival)
arrival = admissionArrival(ctx, s.admission).arrival
h := &walWriteHold{store: s, arrival: arrival, maxHold: maxHold, final: final}
var command walWriteArrival
if existing := writeArrivalFor(ctx, s); existing != nil {
command = *existing
} else {
command = newWALWriteArrival(s, arrival)
}
arrival = command.arrival
h := &walWriteHold{walWriteArrival: command, maxHold: maxHold, final: final}
h.window = newWALHoldWindow(arrival, time.Now(), maxHold)
if final {
h.window = newWALNoRetryHoldWindow(arrival, time.Now(), maxHold)