Skip to content

Commit

Permalink
graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
YifeiZhuang committed Mar 21, 2023
1 parent f6af33f commit f653807
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
21 changes: 11 additions & 10 deletions okhttp/src/main/java/io/grpc/okhttp/OkHttpServerTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ final class OkHttpServerTransport implements ServerTransport,
/** Non-{@code null} when waiting for forceful close GOAWAY to be sent. */
@GuardedBy("lock")
private ScheduledFuture<?> forcefulCloseTimer;
@GuardedBy("lock")
private Long gracefulShutdownPeriod = null;

public OkHttpServerTransport(Config config, Socket bareSocket) {
this.config = Preconditions.checkNotNull(config, "config");
Expand Down Expand Up @@ -233,8 +235,11 @@ public void data(boolean outFinished, int streamId, Buffer source, int byteCount
if (config.maxConnectionAgeInNanos != MAX_CONNECTION_AGE_NANOS_DISABLED) {
long maxConnectionAgeInNanos =
(long) ((.9D + Math.random() * .2D) * config.maxConnectionAgeInNanos);
synchronized (lock) {
gracefulShutdownPeriod = config.maxConnectionAgeGraceInNanos;
}
maxConnectionAgeMonitor = scheduledExecutorService.schedule(
new LogExceptionRunnable(() -> shutdown(config.maxConnectionAgeGraceInNanos)),
new LogExceptionRunnable(this::shutdown),
maxConnectionAgeInNanos,
TimeUnit.NANOSECONDS);
}
Expand All @@ -254,10 +259,6 @@ public void data(boolean outFinished, int streamId, Buffer source, int byteCount

@Override
public void shutdown() {
shutdown(null);
}

private void shutdown(@Nullable Long graceTimeInNanos) {
synchronized (lock) {
if (gracefulShutdown || abruptShutdown) {
return;
Expand All @@ -271,7 +272,7 @@ private void shutdown(@Nullable Long graceTimeInNanos) {
// we also set a timer to limit the upper bound in case the PING is excessively stalled or
// the client is malicious.
secondGoawayTimer = scheduledExecutorService.schedule(
() -> triggerGracefulSecondGoaway(graceTimeInNanos),
this::triggerGracefulSecondGoaway,
GRACEFUL_SHUTDOWN_PING_TIMEOUT_NANOS, TimeUnit.NANOSECONDS);
frameWriter.goAway(Integer.MAX_VALUE, ErrorCode.NO_ERROR, new byte[0]);
frameWriter.ping(false, 0, GRACEFUL_SHUTDOWN_PING);
Expand All @@ -280,7 +281,7 @@ private void shutdown(@Nullable Long graceTimeInNanos) {
}
}

private void triggerGracefulSecondGoaway(@Nullable Long gracePeriodNanos) {
private void triggerGracefulSecondGoaway() {
synchronized (lock) {
if (secondGoawayTimer == null) {
return;
Expand All @@ -294,9 +295,9 @@ private void triggerGracefulSecondGoaway(@Nullable Long gracePeriodNanos) {
} else {
frameWriter.flush();
}
if (gracePeriodNanos != null) {
if (gracefulShutdownPeriod != null) {
forcefulCloseTimer = scheduledExecutorService.schedule(
this::triggerForcefulClose, gracePeriodNanos, TimeUnit.NANOSECONDS);
this::triggerForcefulClose, gracefulShutdownPeriod, TimeUnit.NANOSECONDS);
}
}
}
Expand Down Expand Up @@ -935,7 +936,7 @@ public void ping(boolean ack, int payload1, int payload2) {
return;
}
if (GRACEFUL_SHUTDOWN_PING == payload) {
triggerGracefulSecondGoaway(null);
triggerGracefulSecondGoaway();
return;
}
log.log(Level.INFO, "Received unexpected ping ack: " + payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void startThenShutdown() throws Exception {
@Test
public void maxConnectionAge() throws Exception {
serverBuilder.maxConnectionAge(5, TimeUnit.SECONDS)
.maxConnectionAgeGrace(1, TimeUnit.SECONDS);
.maxConnectionAgeGrace(3, TimeUnit.SECONDS);
initTransport();
handshake();
clientFrameWriter.headers(1, Arrays.asList(
Expand All @@ -170,8 +170,7 @@ public void maxConnectionAge() throws Exception {
new Header("some-client-sent-trailer", "trailer-value")));
pingPong();
fakeClock.forwardNanos(TimeUnit.SECONDS.toNanos(6)); // > 1.1 * 5
fakeClock.forwardNanos(TimeUnit.SECONDS.toNanos(1));
verifyGracefulShutdown(1, TimeUnit.SECONDS.toNanos(1));
verifyGracefulShutdown(1, TimeUnit.SECONDS.toNanos(3));
}

@Test
Expand Down Expand Up @@ -1231,6 +1230,7 @@ private void verifyGracefulShutdown(int lastStreamId, @Nullable Long gracePeriod
assertThat(clientFrameReader.nextFrame(clientFramesRead)).isTrue();
verify(clientFramesRead).goAway(lastStreamId, ErrorCode.NO_ERROR, ByteString.EMPTY);
if (gracePeriodNanos != null) {
assertThat(fakeClock.getPendingTasks().size()).isEqualTo(1);
assertThat(fakeClock.forwardNanos(gracePeriodNanos)).isEqualTo(1);
assertThat(socket.isClosed()).isTrue();
}
Expand Down

0 comments on commit f653807

Please sign in to comment.