Skip to content

Commit 6fe26b1

Browse files
committed
util/admission: clarify context cancellation
Previously, whenever the AC code observed the context cancellation, it reported it as "deadline expiring". This can be confusing when the cancellation is caused by the caller explicitly canceling the context, without using the deadline option of the context. This commit clarifies the error as well as logging event when the context doesn't have the deadline set. Release note: None
1 parent 221c3df commit 6fe26b1

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

pkg/util/admission/snapshot_queue.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package admission
77

88
import (
99
"context"
10+
"fmt"
1011
"sync"
1112
"time"
1213

@@ -196,11 +197,14 @@ func (s *SnapshotQueue) Admit(ctx context.Context, count int64) error {
196197
item.mu.cancelled = true
197198
}()
198199
shouldRelease = false
199-
deadline, _ := ctx.Deadline()
200200
s.metrics.WaitDurations.RecordValue(waitDur)
201+
var deadlineSubstring string
202+
if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
203+
deadlineSubstring = fmt.Sprintf("deadline: %v, ", deadline)
204+
}
201205
return errors.Wrapf(ctx.Err(),
202-
"context canceled while waiting in queue: deadline: %v, start: %v, dur: %v",
203-
deadline, item.enqueueingTime, waitDur)
206+
"context canceled while waiting in queue: %sstart: %v, dur: %v",
207+
deadlineSubstring, item.enqueueingTime, waitDur)
204208
case <-item.admitCh:
205209
waitDur := timeutil.Since(item.enqueueingTime).Nanoseconds()
206210
s.metrics.WaitDurations.RecordValue(waitDur)

pkg/util/admission/work_queue.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,13 @@ func (q *WorkQueue) Admit(ctx context.Context, info WorkInfo) (enabled bool, err
714714
// causing entering into the work queue to be delayed.
715715
q.mu.Unlock()
716716
q.metrics.incErrored(info.Priority)
717-
deadline, _ := ctx.Deadline()
717+
var deadlineSubstring string
718+
if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
719+
deadlineSubstring = fmt.Sprintf("deadline: %v, ", deadline)
720+
}
718721
return true,
719-
errors.Wrapf(ctx.Err(), "work %s context canceled before queueing: deadline: %v, now: %v",
720-
q.workKind, deadline, startTime)
722+
errors.Wrapf(ctx.Err(), "work %s context canceled before queueing: %snow: %v",
723+
q.workKind, deadlineSubstring, startTime)
721724
}
722725
// Push onto heap(s).
723726
ordering := fifoWorkOrdering
@@ -802,12 +805,18 @@ func (q *WorkQueue) Admit(ctx context.Context, info WorkInfo) (enabled bool, err
802805
}
803806
q.metrics.incErrored(info.Priority)
804807
q.metrics.recordFinishWait(info.Priority, waitDur)
805-
deadline, _ := ctx.Deadline()
806808
recordAdmissionWorkQueueStats(span, waitDur, q.queueKind, info.Priority, true)
807-
log.Eventf(ctx, "deadline expired, waited in %s queue with pri %s for %v", q.queueKind, admissionpb.WorkPriorityDict[info.Priority], waitDur)
809+
if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
810+
log.Eventf(ctx, "deadline expired, waited in %s queue with pri %s for %v", q.queueKind, admissionpb.WorkPriorityDict[info.Priority], waitDur)
811+
return true,
812+
errors.Newf("deadline expired while waiting in queue: %s, pri: %s, deadline: %v, start: %v, dur: %v",
813+
q.queueKind, admissionpb.WorkPriorityDict[info.Priority], deadline, startTime, waitDur)
814+
}
815+
// This is a pure context cancellation.
816+
log.Eventf(ctx, "context canceled, waited in %s queue with pri %s for %v", q.queueKind, admissionpb.WorkPriorityDict[info.Priority], waitDur)
808817
return true,
809-
errors.Newf("deadline expired while waiting in queue: %s, pri: %s, deadline: %v, start: %v, dur: %v",
810-
q.queueKind, admissionpb.WorkPriorityDict[info.Priority], deadline, startTime, waitDur)
818+
errors.Newf("context canceled while waiting in queue: %s, pri: %s, start: %v, dur: %v",
819+
q.queueKind, admissionpb.WorkPriorityDict[info.Priority], startTime, waitDur)
811820
case chainID, ok := <-work.ch:
812821
if !ok {
813822
panic(errors.AssertionFailedf("channel should not be closed"))

0 commit comments

Comments
 (0)