-
Notifications
You must be signed in to change notification settings - Fork 252
feat(pusher): Add circuit breaker to halt queue on target failure #1975
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
agarakan
wants to merge
28
commits into
enable-multithreaded-logging-by-default
Choose a base branch
from
sender-block-on-failure
base: enable-multithreaded-logging-by-default
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
28 commits
Select commit
Hold shift + click to select a range
6b231dc
introduce retry metadata to batch struct
agarakan 8521373
Remove unused reset method
agarakan 7244af8
add unit tests for retryMetadata
agarakan d2f21e1
fix lint
agarakan 66186a7
Introduce retryHeap and retryHeapProcessor
agarakan 83224b4
Exchange pushch for semaphor to enformce heap size and blocking
agarakan 7cfc794
Add conditional logic to sender to call batch.Fail() during concurrency
agarakan b4ffd7a
Add unit tests
agarakan 0e4b0bc
Instantiate RetryHeap and RetryHeapProcessor if concurrency enabled
agarakan 9c1332a
Add unit tests for retryheap instantiation
agarakan dddb691
Update sender to reference retryHeap to call push on fail
agarakan 02bc5c6
Add unit tests for sender logic
agarakan ef7d627
Implement halt on target logic
agarakan 309f904
lint
agarakan d0727b6
Merge branch 'enable-multithreaded-logging-by-default' into sender-bl…
the-mann 800c06b
Merge branch 'enable-multithreaded-logging-by-default' into sender-bl…
the-mann d9296a6
lint
the-mann a5621fc
Merge remote-tracking branch 'origin/sender-block-on-failure' into se…
the-mann 7051a0c
fix tests
the-mann fd185db
Fix race condition in RetryHeap Stop and Push methods
the-mann d79ae7f
Add failing test for circuit breaker resume on batch expiry
the-mann de410f1
lx
the-mann 28ba902
test(pusher): Add automated recovery tests for poison pill
the-mann 11b1d26
Add test filtering to integration test workflows
the-mann 78c947b
Merge remote-tracking branch 'origin/main' into sender-block-on-failure
the-mann 60b6f49
Remove test filtering feature (moved to separate PR)
the-mann 1b1973b
Trigger PR diff refresh
the-mann 8a4960f
Merge remote-tracking branch 'origin/enable-multithreaded-logging-by-…
the-mann 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
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
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
101 changes: 101 additions & 0 deletions
101
plugins/outputs/cloudwatchlogs/internal/pusher/circuitbreaker_test.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,101 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package pusher | ||
|
|
||
| import ( | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/aws/amazon-cloudwatch-agent/sdk/service/cloudwatchlogs" | ||
| "github.com/aws/amazon-cloudwatch-agent/tool/testutil" | ||
| ) | ||
|
|
||
| // TestCircuitBreakerBlocksTargetAfterFailure verifies that when a batch fails | ||
| // for a target, the circuit breaker prevents additional batches from that target | ||
| // from being sent until the failing batch is retried successfully. | ||
| // | ||
| // Without a circuit breaker, a problematic target continues producing new batches | ||
| // that flood the SenderQueue/WorkerPool, starving healthy targets. | ||
| func TestCircuitBreakerBlocksTargetAfterFailure(t *testing.T) { | ||
| logger := testutil.NewNopLogger() | ||
|
|
||
| failingTarget := Target{Group: "failing-group", Stream: "stream"} | ||
| healthyTarget := Target{Group: "healthy-group", Stream: "stream"} | ||
|
|
||
| var failingTargetSendCount atomic.Int32 | ||
| var healthyTargetSendCount atomic.Int32 | ||
|
|
||
| service := &stubLogsService{ | ||
| ple: func(input *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error) { | ||
| if *input.LogGroupName == failingTarget.Group { | ||
| failingTargetSendCount.Add(1) | ||
| return nil, &cloudwatchlogs.ServiceUnavailableException{} | ||
| } | ||
| healthyTargetSendCount.Add(1) | ||
| return &cloudwatchlogs.PutLogEventsOutput{}, nil | ||
| }, | ||
| cls: func(_ *cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error) { | ||
| return &cloudwatchlogs.CreateLogStreamOutput{}, nil | ||
| }, | ||
| clg: func(_ *cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error) { | ||
| return &cloudwatchlogs.CreateLogGroupOutput{}, nil | ||
| }, | ||
| dlg: func(_ *cloudwatchlogs.DescribeLogGroupsInput) (*cloudwatchlogs.DescribeLogGroupsOutput, error) { | ||
| return &cloudwatchlogs.DescribeLogGroupsOutput{}, nil | ||
| }, | ||
| } | ||
|
|
||
| concurrency := 5 | ||
| workerPool := NewWorkerPool(concurrency) | ||
| retryHeap := NewRetryHeap(concurrency, logger) | ||
| defer workerPool.Stop() | ||
| defer retryHeap.Stop() | ||
|
|
||
| tm := NewTargetManager(logger, service) | ||
|
|
||
| var wg sync.WaitGroup | ||
| flushTimeout := 50 * time.Millisecond | ||
| retryDuration := time.Hour | ||
|
|
||
| failingPusher := NewPusher(logger, failingTarget, service, tm, nil, workerPool, flushTimeout, retryDuration, &wg, retryHeap) | ||
| healthyPusher := NewPusher(logger, healthyTarget, service, tm, nil, workerPool, flushTimeout, retryDuration, &wg, retryHeap) | ||
| defer failingPusher.Stop() | ||
| defer healthyPusher.Stop() | ||
|
|
||
| now := time.Now() | ||
|
|
||
| // Send events to both targets. The failing target will fail on PutLogEvents, | ||
| // and the circuit breaker should block it from sending more batches. | ||
| for i := 0; i < 10; i++ { | ||
| failingPusher.AddEvent(newStubLogEvent("fail", now)) | ||
| healthyPusher.AddEvent(newStubLogEvent("ok", now)) | ||
| } | ||
|
|
||
| // Wait for flushes to occur | ||
| time.Sleep(500 * time.Millisecond) | ||
|
|
||
| // Send more events - the failing target should be blocked by circuit breaker | ||
| for i := 0; i < 10; i++ { | ||
| failingPusher.AddEvent(newStubLogEvent("fail-more", now)) | ||
| healthyPusher.AddEvent(newStubLogEvent("ok-more", now)) | ||
| } | ||
|
|
||
| time.Sleep(500 * time.Millisecond) | ||
|
|
||
| // Circuit breaker assertion: after the first failure, the failing target should | ||
| // NOT have sent additional batches. Only 1 send attempt should have been made | ||
| // before the circuit breaker blocks it. | ||
| assert.LessOrEqual(t, failingTargetSendCount.Load(), int32(1), | ||
| "Circuit breaker should block failing target from sending more than 1 batch, "+ | ||
| "but %d batches were sent. Without a circuit breaker, the failing target "+ | ||
| "continues flooding the worker pool with bad requests.", failingTargetSendCount.Load()) | ||
|
|
||
| // Healthy target should continue sending successfully | ||
| assert.Greater(t, healthyTargetSendCount.Load(), int32(0), | ||
| "Healthy target should continue sending while failing target is blocked") | ||
| } |
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
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
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
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.
Say a bad batch from a target caused this to halt. Now that bad batch is re-tried for 14 days and eventually dropped - but this never gets resumed in that case right? So this target is blocked forever in that scenario?
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes valid, the design details a resume on batch expiry to avoid this. I missed including that here, added now
Uh oh!
There was an error while loading. Please reload this page.
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.
just addressed this here: https://github.com/aws/amazon-cloudwatch-agent/pull/1975/files#diff-455d672bafb83d8d386d757af7ddf43568251099619d23607883c15b29f86dd9R223