-
Notifications
You must be signed in to change notification settings - Fork 252
Introduce RetryHeap and RetryHeapProcessor #1972
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
Merged
agarakan
merged 7 commits into
enable-multithreaded-logging-by-default
from
retryheap-and-processor
Jan 16, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
efc65e2
Introduce retryHeap and retryHeapProcessor
agarakan 0242e64
Exchange pushch for semaphor to enformce heap size and blocking
agarakan 77d99af
Address nits
agarakan 2e08031
Update unit test to use channel
agarakan 11e8984
Reduce unit test sleep time
agarakan bd25663
Reuse existing mockSender in unit tests
agarakan 5614e48
Make ticker local to method not struct
agarakan 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
189 changes: 189 additions & 0 deletions
189
plugins/outputs/cloudwatchlogs/internal/pusher/retryheap.go
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 |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package pusher | ||
|
|
||
| import ( | ||
| "container/heap" | ||
| "errors" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/influxdata/telegraf" | ||
| ) | ||
|
|
||
| // retryHeapImpl implements heap.Interface for logEventBatch sorted by nextRetryTime | ||
| type retryHeapImpl []*logEventBatch | ||
|
agarakan marked this conversation as resolved.
|
||
|
|
||
| var _ heap.Interface = (*retryHeapImpl)(nil) | ||
|
|
||
| func (h retryHeapImpl) Len() int { return len(h) } | ||
|
|
||
| func (h retryHeapImpl) Less(i, j int) bool { | ||
| return h[i].nextRetryTime.Before(h[j].nextRetryTime) | ||
| } | ||
|
|
||
| func (h retryHeapImpl) Swap(i, j int) { h[i], h[j] = h[j], h[i] } | ||
|
|
||
| func (h *retryHeapImpl) Push(x interface{}) { | ||
| *h = append(*h, x.(*logEventBatch)) | ||
| } | ||
|
|
||
| func (h *retryHeapImpl) Pop() interface{} { | ||
| old := *h | ||
| n := len(old) | ||
| item := old[n-1] | ||
| old[n-1] = nil // don't stop the GC from reclaiming the item eventually | ||
| *h = old[0 : n-1] | ||
| return item | ||
| } | ||
|
agarakan marked this conversation as resolved.
|
||
|
|
||
| // RetryHeap manages failed batches during their retry wait periods | ||
| type RetryHeap interface { | ||
|
agarakan marked this conversation as resolved.
agarakan marked this conversation as resolved.
|
||
| Push(batch *logEventBatch) error | ||
| PopReady() []*logEventBatch | ||
| Size() int | ||
| Stop() | ||
| } | ||
|
|
||
| type retryHeap struct { | ||
| heap retryHeapImpl | ||
| mutex sync.RWMutex | ||
| semaphore chan struct{} // Size enforcer | ||
| stopCh chan struct{} | ||
| maxSize int | ||
| } | ||
|
|
||
| var _ RetryHeap = (*retryHeap)(nil) | ||
|
|
||
| // NewRetryHeap creates a new retry heap with the specified maximum size | ||
| func NewRetryHeap(maxSize int) RetryHeap { | ||
| rh := &retryHeap{ | ||
| heap: make(retryHeapImpl, 0, maxSize), | ||
| maxSize: maxSize, | ||
| semaphore: make(chan struct{}, maxSize), // Semaphore for size enforcement | ||
| stopCh: make(chan struct{}), | ||
| } | ||
| heap.Init(&rh.heap) | ||
| return rh | ||
| } | ||
|
|
||
| // Push adds a batch to the heap, blocking if full | ||
| func (rh *retryHeap) Push(batch *logEventBatch) error { | ||
| // Acquire semaphore slot (blocks if at maxSize capacity) | ||
| select { | ||
| case rh.semaphore <- struct{}{}: | ||
| // add batch to heap with mutex protection | ||
| rh.mutex.Lock() | ||
| heap.Push(&rh.heap, batch) | ||
| rh.mutex.Unlock() | ||
| return nil | ||
| case <-rh.stopCh: | ||
| return errors.New("retry heap stopped") | ||
| } | ||
| } | ||
|
|
||
| // PopReady returns all batches that are ready for retry (nextRetryTime <= now) | ||
| func (rh *retryHeap) PopReady() []*logEventBatch { | ||
| rh.mutex.Lock() | ||
| defer rh.mutex.Unlock() | ||
|
|
||
| now := time.Now() | ||
| var ready []*logEventBatch | ||
|
|
||
| // Pop all batches that are ready for retry | ||
| for len(rh.heap) > 0 && !rh.heap[0].nextRetryTime.After(now) { | ||
| batch := heap.Pop(&rh.heap).(*logEventBatch) | ||
| ready = append(ready, batch) | ||
| // Release semaphore slot for each popped batch | ||
| <-rh.semaphore | ||
| } | ||
|
|
||
| return ready | ||
| } | ||
|
|
||
| // Size returns the current number of batches in the heap | ||
| func (rh *retryHeap) Size() int { | ||
| rh.mutex.RLock() | ||
| defer rh.mutex.RUnlock() | ||
| return len(rh.heap) | ||
| } | ||
|
|
||
| // Stop stops the retry heap | ||
| func (rh *retryHeap) Stop() { | ||
|
agarakan marked this conversation as resolved.
|
||
| close(rh.stopCh) | ||
| } | ||
|
|
||
| // RetryHeapProcessor manages the retry heap and moves ready batches back to sender queue | ||
| type RetryHeapProcessor struct { | ||
| retryHeap RetryHeap | ||
| senderPool Sender | ||
| stopCh chan struct{} | ||
| logger telegraf.Logger | ||
| stopped bool | ||
| maxRetryDuration time.Duration | ||
| } | ||
|
|
||
| // NewRetryHeapProcessor creates a new retry heap processor | ||
| func NewRetryHeapProcessor(retryHeap RetryHeap, senderPool Sender, logger telegraf.Logger, maxRetryDuration time.Duration) *RetryHeapProcessor { | ||
| return &RetryHeapProcessor{ | ||
| retryHeap: retryHeap, | ||
| senderPool: senderPool, | ||
| stopCh: make(chan struct{}), | ||
| logger: logger, | ||
| stopped: false, | ||
| maxRetryDuration: maxRetryDuration, | ||
| } | ||
| } | ||
|
|
||
| // Start begins processing the retry heap every 100ms | ||
| func (p *RetryHeapProcessor) Start() { | ||
| go p.processLoop() | ||
| } | ||
|
|
||
| // Stop stops the retry heap processor | ||
| func (p *RetryHeapProcessor) Stop() { | ||
|
agarakan marked this conversation as resolved.
|
||
| if p.stopped { | ||
| return | ||
| } | ||
|
|
||
| // Process any remaining batches before stopping | ||
| p.processReadyMessages() | ||
|
|
||
| close(p.stopCh) | ||
| p.stopped = true | ||
| } | ||
|
|
||
| // processLoop runs the main processing loop | ||
| func (p *RetryHeapProcessor) processLoop() { | ||
| ticker := time.NewTicker(100 * time.Millisecond) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ticker.C: | ||
| p.processReadyMessages() | ||
| case <-p.stopCh: | ||
| return | ||
| } | ||
| } | ||
|
agarakan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // processReadyMessages checks the heap for ready batches and moves them back to sender queue | ||
| func (p *RetryHeapProcessor) processReadyMessages() { | ||
| readyBatches := p.retryHeap.PopReady() | ||
|
|
||
| for _, batch := range readyBatches { | ||
| // Check if batch has expired | ||
| if batch.isExpired(p.maxRetryDuration) { | ||
| p.logger.Errorf("Dropping expired batch for %v/%v", batch.Group, batch.Stream) | ||
| batch.updateState() | ||
| continue | ||
| } | ||
|
|
||
| // Submit the batch back to the sender pool (blocks if full) | ||
| p.senderPool.Send(batch) | ||
| p.logger.Debugf("Moved batch from retry heap back to sender pool for %v/%v", | ||
| batch.Group, batch.Stream) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.