-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mitigate gRPC stream connection issues (#1038)
Mitigates hanging gRPC streams by detecting idle streams and reconnecting after a timeout (default 10min for partition assignment streams, 2min for all others). The StreamIdleTimer is restarted when the client receives a response on the stream. The stream is reinitialized when the timeout expires. For publish and commit streams, the timeout will still expire even if there is no user activity.
- Loading branch information
Showing
10 changed files
with
346 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- see http://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html --> | ||
<differences> | ||
<difference> | ||
<differenceType>7004</differenceType> | ||
<className>com/google/cloud/pubsublite/internal/**</className> | ||
<method>*</method> | ||
</difference> | ||
</differences> |
This file contains 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 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 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 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 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
85 changes: 85 additions & 0 deletions
85
...d-pubsublite/src/main/java/com/google/cloud/pubsublite/internal/wire/StreamIdleTimer.java
This file contains 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,85 @@ | ||
/* | ||
* 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 | ||
* | ||
* http://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.pubsublite.internal.wire; | ||
|
||
import static com.google.common.collect.Comparators.min; | ||
|
||
import com.google.cloud.pubsublite.internal.AlarmFactory; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Stopwatch; | ||
import com.google.common.base.Ticker; | ||
import java.time.Duration; | ||
import java.util.concurrent.Future; | ||
import javax.annotation.concurrent.GuardedBy; | ||
|
||
/** An approximate timer used to detect idle streams. */ | ||
class StreamIdleTimer implements AutoCloseable { | ||
/** Handles a timeout. */ | ||
interface Handler { | ||
void onTimeout(); | ||
} | ||
|
||
private static final long POLL_DIVISOR = 4; | ||
private static final Duration MAX_POLL_INTERVAL = Duration.ofMinutes(1); | ||
|
||
@VisibleForTesting | ||
static Duration getDelay(Duration timeout) { | ||
return min(MAX_POLL_INTERVAL, timeout.dividedBy(POLL_DIVISOR)); | ||
} | ||
|
||
private final Duration timeout; | ||
private final Handler handler; | ||
private final Future<?> task; | ||
|
||
@GuardedBy("this") | ||
private final Stopwatch stopwatch; | ||
|
||
/** | ||
* Creates a started timer. | ||
* | ||
* @param timeout Call the handler after this duration has elapsed. The call may be delayed up to | ||
* (timeout / POLL_DIVISOR) after the timeout duration. | ||
* @param handler Called after the timeout has expired and the timer is running. | ||
*/ | ||
StreamIdleTimer(Duration timeout, Handler handler) { | ||
this(timeout, handler, Ticker.systemTicker(), AlarmFactory.create(getDelay(timeout))); | ||
} | ||
|
||
@VisibleForTesting | ||
StreamIdleTimer(Duration timeout, Handler handler, Ticker ticker, AlarmFactory alarmFactory) { | ||
this.timeout = timeout; | ||
this.handler = handler; | ||
this.stopwatch = Stopwatch.createStarted(ticker); | ||
this.task = alarmFactory.newAlarm(this::onPoll); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
task.cancel(false); | ||
} | ||
|
||
/** Restart the timer from zero. */ | ||
public synchronized void restart() { | ||
stopwatch.reset().start(); | ||
} | ||
|
||
private synchronized void onPoll() { | ||
if (stopwatch.elapsed().compareTo(timeout) > 0) { | ||
SystemExecutors.getFuturesExecutor().execute(handler::onTimeout); | ||
} | ||
} | ||
} |
This file contains 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.