-
Notifications
You must be signed in to change notification settings - Fork 2.3k
actor: add drop counter and first-drop log to BackpressureMailbox #10761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gijswijs
wants to merge
3
commits into
lightningnetwork:master
Choose a base branch
from
gijswijs:backpressure-mailbox-drop-counter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,19 +9,59 @@ import ( | |||||||||||||
| "github.com/lightningnetwork/lnd/queue" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| // BackpressureMailboxCfg holds optional configuration for a | ||||||||||||||
| // BackpressureMailbox. A zero-value config is valid and disables | ||||||||||||||
| // automatic first-drop logging. | ||||||||||||||
| type BackpressureMailboxCfg struct { | ||||||||||||||
| // Name is a human-readable label included in the first-drop log | ||||||||||||||
| // line. When empty, no automatic logging is performed and the | ||||||||||||||
| // caller is expected to use FirstDropClaim() to drive its own | ||||||||||||||
| // logging. | ||||||||||||||
| Name string | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // BackpressureMailbox implements the Mailbox interface using a | ||||||||||||||
| // queue.BackpressureQueue as its core buffer. The BackpressureQueue's drop | ||||||||||||||
| // predicate is consulted on every Send/TrySend, allowing RED-style load | ||||||||||||||
| // shedding before the mailbox is full. | ||||||||||||||
| // | ||||||||||||||
| // Every predicate rejection is counted and exposed via Dropped(). Two | ||||||||||||||
| // independent one-shot flags exist for first-drop signaling: | ||||||||||||||
| // | ||||||||||||||
| // - When a Name is configured, the mailbox emits a single info-level | ||||||||||||||
| // log the first time the predicate fires (gated by firstLog). | ||||||||||||||
| // - FirstDropClaim() exposes a separate one-shot flag (firstDrop) | ||||||||||||||
| // for callers that want to emit their own log at the call site. | ||||||||||||||
| // | ||||||||||||||
| // The two flags are independent: using one does not consume the other. | ||||||||||||||
| type BackpressureMailbox[M Message, R any] struct { | ||||||||||||||
| // queue is the underlying backpressure-aware buffer. | ||||||||||||||
| queue *queue.BackpressureQueue[envelope[M, R]] | ||||||||||||||
|
|
||||||||||||||
| // closed tracks whether the mailbox has been closed. | ||||||||||||||
| closed atomic.Bool | ||||||||||||||
|
|
||||||||||||||
| // mu protects Send/TrySend operations to prevent send-on-closed-channel | ||||||||||||||
| // panics. Close() acquires write lock, Send/TrySend acquire read lock. | ||||||||||||||
| // dropped counts the total number of messages rejected by the | ||||||||||||||
| // drop predicate since the mailbox was created. | ||||||||||||||
| dropped atomic.Uint64 | ||||||||||||||
|
|
||||||||||||||
| // firstLog is consumed internally by the counting predicate | ||||||||||||||
| // wrapper. When Name is set, the wrapper CAS-flips this flag | ||||||||||||||
| // on the first rejection and emits an info-level log line. | ||||||||||||||
| firstLog atomic.Bool | ||||||||||||||
|
|
||||||||||||||
| // firstDrop is exposed to callers via FirstDropClaim(). It is | ||||||||||||||
| // independent of firstLog so that the internal auto-log and an | ||||||||||||||
| // external caller-driven log can coexist without racing for | ||||||||||||||
| // the same flag. | ||||||||||||||
| firstDrop atomic.Bool | ||||||||||||||
|
|
||||||||||||||
| // name is the optional label from BackpressureMailboxCfg.Name. | ||||||||||||||
| name string | ||||||||||||||
|
|
||||||||||||||
| // mu protects Send/TrySend operations to prevent | ||||||||||||||
| // send-on-closed-channel panics. Close() acquires write lock, | ||||||||||||||
| // Send/TrySend acquire read lock. | ||||||||||||||
| mu sync.RWMutex | ||||||||||||||
|
|
||||||||||||||
| // closeOnce ensures Close() executes exactly once. | ||||||||||||||
|
|
@@ -31,25 +71,71 @@ type BackpressureMailbox[M Message, R any] struct { | |||||||||||||
| actorCtx context.Context | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // NewBackpressureMailbox creates a new mailbox backed by a BackpressureQueue. | ||||||||||||||
| // The shouldDrop function is called with the current queue depth on every send | ||||||||||||||
| // attempt; if it returns true the message is silently dropped. | ||||||||||||||
| // NewBackpressureMailbox creates a new mailbox backed by a | ||||||||||||||
| // BackpressureQueue. The shouldDrop function is called with the current | ||||||||||||||
| // queue depth on every send attempt; if it returns true the message is | ||||||||||||||
| // dropped and the internal drop counter is incremented. | ||||||||||||||
| func NewBackpressureMailbox[M Message, R any]( | ||||||||||||||
| actorCtx context.Context, | ||||||||||||||
| capacity int, | ||||||||||||||
| shouldDrop queue.DropCheckFunc, | ||||||||||||||
| cfg BackpressureMailboxCfg, | ||||||||||||||
| ) *BackpressureMailbox[M, R] { | ||||||||||||||
|
|
||||||||||||||
| if capacity <= 0 { | ||||||||||||||
| capacity = 1 | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pred := queue.AsDropPredicate[envelope[M, R]](shouldDrop) | ||||||||||||||
|
|
||||||||||||||
| return &BackpressureMailbox[M, R]{ | ||||||||||||||
| queue: queue.NewBackpressureQueue(capacity, pred), | ||||||||||||||
| mb := &BackpressureMailbox[M, R]{ | ||||||||||||||
| actorCtx: actorCtx, | ||||||||||||||
| name: cfg.Name, | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Wrap the caller's predicate so every rejection increments | ||||||||||||||
| // the drop counter and, when a name is configured, emits a | ||||||||||||||
| // one-shot info log on the first drop. | ||||||||||||||
| inner := queue.AsDropPredicate[envelope[M, R]](shouldDrop) | ||||||||||||||
| counting := func(queueLen int, item envelope[M, R]) bool { | ||||||||||||||
| if !inner(queueLen, item) { | ||||||||||||||
| return false | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| mb.dropped.Add(1) | ||||||||||||||
|
|
||||||||||||||
| if mb.name != "" && | ||||||||||||||
| mb.firstLog.CompareAndSwap(false, true) { | ||||||||||||||
|
|
||||||||||||||
| log.Infof("Mailbox(%s): first message "+ | ||||||||||||||
| "dropped (queue_depth=%d)", | ||||||||||||||
| mb.name, queueLen) | ||||||||||||||
|
Comment on lines
+108
to
+110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Repository Style Guide (lines 237-253) requires using structured logging for static messages. Instead of
Suggested change
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return true | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| mb.queue = queue.NewBackpressureQueue(capacity, counting) | ||||||||||||||
|
|
||||||||||||||
| return mb | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Dropped returns the total number of messages rejected by the drop | ||||||||||||||
| // predicate since the mailbox was created. | ||||||||||||||
| func (m *BackpressureMailbox[M, R]) Dropped() uint64 { | ||||||||||||||
| return m.dropped.Load() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // FirstDropClaim atomically returns true exactly once, and only after | ||||||||||||||
| // at least one message has actually been dropped by the predicate. It | ||||||||||||||
| // is intended for call sites that want to emit a one-shot log or | ||||||||||||||
| // metric when the mailbox first starts shedding load. This flag is | ||||||||||||||
| // independent of the built-in first-drop log gated by | ||||||||||||||
| // BackpressureMailboxCfg.Name; using one does not consume the other. | ||||||||||||||
| func (m *BackpressureMailbox[M, R]) FirstDropClaim() bool { | ||||||||||||||
| if m.dropped.Load() == 0 { | ||||||||||||||
| return false | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return m.firstDrop.CompareAndSwap(false, true) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Send attempts to send an envelope to the mailbox. The BackpressureQueue's | ||||||||||||||
|
|
||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To support structured logging as required by the repository style guide, the
log/slogpackage should be imported.