Skip to content
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

fix: Fix watchdog to start with WAITING state #2468

Merged
merged 7 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion gax-java/gax/src/main/java/com/google/api/gax/rpc/Watchdog.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class WatchdogStream<ResponseT> extends StateCheckingResponseObserver<ResponseT>
private volatile StreamController innerController;

@GuardedBy("lock")
private State state = State.IDLE;
mutianf marked this conversation as resolved.
Show resolved Hide resolved
private State state = State.WAITING;

@GuardedBy("lock")
private int pendingCount = 0;
Expand All @@ -220,6 +220,16 @@ public void onStartImpl(StreamController controller) {
public void disableAutoInboundFlowControl() {
Preconditions.checkState(
!hasStarted, "Can't disable automatic flow control after the stream has started");

synchronized (lock) {
mutianf marked this conversation as resolved.
Show resolved Hide resolved
// WatchdogStream is created with waiting state. If auto flow control is disabled,
// set the state to be IDLE, which will be updated to WAITING when onRequest() is
// called.
if (state == State.WAITING) {
state = State.IDLE;
}
mutianf marked this conversation as resolved.
Show resolved Hide resolved
}

autoAutoFlowControl = false;
innerController.disableAutoInboundFlowControl();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,31 @@ public void testTimedOutBeforeStart() throws InterruptedException {
assertThat(error).isInstanceOf(WatchdogTimeoutException.class);
}

@Test
public void testTimedOutBeforeResponse() throws InterruptedException {
MockServerStreamingCallable<String, String> autoFlowControlCallable =
new MockServerStreamingCallable<>();
AutoFlowControlObserver<String> downstreamObserver = new AutoFlowControlObserver<>();

autoFlowControlCallable.call("request", watchdog.watch(downstreamObserver, waitTime, idleTime));
MockServerStreamingCall<String, String> call1 = autoFlowControlCallable.popLastCall();

clock.incrementNanoTime(idleTime.toNanos() + 1);
watchdog.run();
assertThat(downstreamObserver.done.isDone()).isFalse();
assertThat(call1.getController().isCancelled()).isTrue();
call1.getController().getObserver().onError(new CancellationException("cancelled"));
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved

Throwable actualError = null;
try {
downstreamObserver.done.get();
} catch (ExecutionException e) {
actualError = e.getCause();
}
assertThat(actualError).isInstanceOf(WatchdogTimeoutException.class);
assertThat(actualError.getMessage()).contains("waiting for next response");
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void testMultiple() throws Exception {
// Start stream1
Expand Down Expand Up @@ -310,4 +335,30 @@ public void onComplete() {
done.set(null);
}
}

static class AutoFlowControlObserver<T> implements ResponseObserver<T> {
SettableApiFuture<StreamController> controller = SettableApiFuture.create();
Queue<T> responses = Queues.newLinkedBlockingDeque();
SettableApiFuture<Void> done = SettableApiFuture.create();

@Override
public void onStart(StreamController controller) {
this.controller.set(controller);
}

@Override
public void onResponse(T response) {
responses.add(response);
}

@Override
public void onError(Throwable t) {
done.setException(t);
}

@Override
public void onComplete() {
done.set(null);
}
}
}
Loading