This repository was archived by the owner on May 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
fix: catch all throwables so version mismatch won't hang the client #1402
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5775dfa
fix: catch all throwables so version mismatch won't hang the client
mutianf cd06807
create a SafeResponseObserver
mutianf 6281745
format
mutianf e833e9d
extend SafeResponseObserver
mutianf c2664a5
catch stream cancellation
mutianf e866c6b
update error log
mutianf 4517183
update
mutianf 4720481
throw on onStart
mutianf 4a61bbb
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] a61671d
fix version
mutianf 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
123 changes: 123 additions & 0 deletions
123
...d-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/SafeResponseObserver.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,123 @@ | ||
| /* | ||
| * Copyright 2022 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.google.cloud.bigtable.data.v2.stub; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import com.google.api.gax.rpc.ResponseObserver; | ||
| import com.google.api.gax.rpc.StreamController; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| /** | ||
| * Base implementation of {@link ResponseObserver} that checks the state and catches all the | ||
| * throwables. | ||
| */ | ||
| @InternalApi | ||
| public abstract class SafeResponseObserver<ResponseT> implements ResponseObserver<ResponseT> { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(SafeResponseObserver.class.getName()); | ||
| private AtomicBoolean isStarted = new AtomicBoolean(false); | ||
| private AtomicBoolean isClosed = new AtomicBoolean(false); | ||
| private StreamController streamController; | ||
| private ResponseObserver outerObserver; | ||
|
|
||
| public SafeResponseObserver(ResponseObserver outerObserver) { | ||
| this.outerObserver = outerObserver; | ||
| } | ||
|
|
||
| @Override | ||
| public final void onStart(StreamController streamController) { | ||
| if (!isStarted.compareAndSet(false, true)) { | ||
| throw new IllegalStateException("A stream is already started"); | ||
| } | ||
|
|
||
| this.streamController = streamController; | ||
| try { | ||
| onStartImpl(streamController); | ||
| } catch (Throwable t) { | ||
| if (!isClosed.compareAndSet(false, true)) { | ||
| logException("Tried to cancel a closed stream"); | ||
| return; | ||
| } | ||
| streamController.cancel(); | ||
|
mutianf marked this conversation as resolved.
|
||
| outerObserver.onError(t); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public final void onResponse(ResponseT response) { | ||
| if (isClosed.get()) { | ||
| logException("Received a response after the stream is closed"); | ||
| return; | ||
| } | ||
| try { | ||
| onResponseImpl(response); | ||
| } catch (Throwable t1) { | ||
| try { | ||
| if (!isClosed.compareAndSet(false, true)) { | ||
|
igorbernstein2 marked this conversation as resolved.
|
||
| logException("Tried to cancel a closed stream"); | ||
| return; | ||
| } | ||
| streamController.cancel(); | ||
| } catch (Throwable t2) { | ||
| t1.addSuppressed(t2); | ||
| } | ||
| outerObserver.onError(t1); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public final void onError(Throwable throwable) { | ||
| if (!isClosed.compareAndSet(false, true)) { | ||
| logException("Received error after the stream is closed"); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| onErrorImpl(throwable); | ||
| } catch (Throwable t) { | ||
| throwable.addSuppressed(t); | ||
|
igorbernstein2 marked this conversation as resolved.
|
||
| outerObserver.onError(throwable); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public final void onComplete() { | ||
| if (!isClosed.compareAndSet(false, true)) { | ||
| logException("Tried to double close the stream"); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| onCompleteImpl(); | ||
| } catch (Throwable t) { | ||
| outerObserver.onError(t); | ||
| } | ||
| } | ||
|
|
||
| private void logException(String message) { | ||
| LOGGER.log(Level.WARNING, message, new IllegalStateException(message)); | ||
| } | ||
|
|
||
| protected abstract void onStartImpl(StreamController streamController); | ||
|
|
||
| protected abstract void onResponseImpl(ResponseT response); | ||
|
|
||
| protected abstract void onErrorImpl(Throwable throwable); | ||
|
|
||
| protected abstract void onCompleteImpl(); | ||
| } | ||
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
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.