-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add Circuit breaker support for gRPC #20203
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
karenyrx
merged 12 commits into
opensearch-project:main
from
sakrah:sakrah/3.x/circuit-breaker
Jan 9, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d389a61
Add Circuit breaker support
akrahdan ebaf0d1
Add CHANGELOG entry for circuit breaker support
akrahdan eafd4e5
Add atomic guard for circuit breaker byte release.
akrahdan 5881c81
Update circuit breaker implementation with final test fixes and impor…
akrahdan 8400431
Add Javadoc to CircuitBreakerStreamObserver
akrahdan e980164
Fix imports
akrahdan 70b2e52
Apply IDE formatting fixes
akrahdan 1837d3f
Remove incorrect byte decrement from CircuitBreakingException catch b…
akrahdan 902e1af
Add null validation for constructor parameters in DocumentServiceImpl
akrahdan dfadc12
Merge branch 'main' into sakrah/3.x/circuit-breaker
sakrah b0c9ee5
Enhance circuit breaker test to verify byte accounting
akrahdan 4876c61
Update CHANGELOG.md
sakrah 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
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
80 changes: 80 additions & 0 deletions
80
...t-grpc/src/main/java/org/opensearch/transport/grpc/util/CircuitBreakerStreamObserver.java
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,80 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
| package org.opensearch.transport.grpc.util; | ||
|
|
||
| import org.opensearch.core.common.breaker.CircuitBreaker; | ||
| import org.opensearch.core.indices.breaker.CircuitBreakerService; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import io.grpc.stub.StreamObserver; | ||
|
|
||
| /** | ||
| * Wrapper for StreamObserver that automatically releases circuit breaker bytes when a gRPC response completes. | ||
| * This ensures that in-flight request memory is properly tracked and released, preventing memory leaks. | ||
| * Bytes are released exactly once when either onCompleted() or onError() is called. | ||
| * | ||
| * @param <T> The type of message observed | ||
| */ | ||
| public class CircuitBreakerStreamObserver<T> implements StreamObserver<T> { | ||
| private final StreamObserver<T> delegate; | ||
| private final CircuitBreakerService circuitBreakerService; | ||
| private final int requestSize; | ||
| private final AtomicBoolean released = new AtomicBoolean(false); | ||
|
|
||
| /** | ||
| * Creates a new CircuitBreakerStreamObserver wrapper. | ||
| * | ||
| * @param delegate The underlying StreamObserver to delegate calls to | ||
| * @param circuitBreakerService The circuit breaker service for tracking in-flight requests | ||
| * @param requestSize The size of the request in bytes that was added to the circuit breaker | ||
| */ | ||
| public CircuitBreakerStreamObserver(StreamObserver<T> delegate, CircuitBreakerService circuitBreakerService, int requestSize) { | ||
| this.delegate = delegate; | ||
| this.circuitBreakerService = circuitBreakerService; | ||
| this.requestSize = requestSize; | ||
| } | ||
|
|
||
| /** | ||
| * Forwards the next value to the delegate observer. | ||
| * | ||
| * @param value The next value in the stream | ||
| */ | ||
| @Override | ||
| public void onNext(T value) { | ||
| delegate.onNext(value); | ||
| } | ||
|
|
||
| /** | ||
| * Releases circuit breaker bytes and forwards the error to the delegate observer. | ||
| * | ||
| * @param t The error that occurred | ||
| */ | ||
| @Override | ||
| public void onError(Throwable t) { | ||
| releaseBytes(); | ||
| delegate.onError(t); | ||
| } | ||
|
|
||
| /** | ||
| * Releases circuit breaker bytes and forwards the completion signal to the delegate observer. | ||
| */ | ||
| @Override | ||
| public void onCompleted() { | ||
| releaseBytes(); | ||
| delegate.onCompleted(); | ||
| } | ||
|
|
||
| private void releaseBytes() { | ||
| if (released.compareAndSet(false, true) == false) { | ||
| return; | ||
| } | ||
| CircuitBreaker breaker = circuitBreakerService.getBreaker(CircuitBreaker.IN_FLIGHT_REQUESTS); | ||
| breaker.addWithoutBreaking(-requestSize); | ||
| } | ||
| } |
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.
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.