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

core: change serverimpl,servercallimpl's internalclose to cancel stream #4038

Merged
merged 5 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion core/src/main/java/io/grpc/internal/ServerCallImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
* on.
*/
private void internalClose(Status internalError) {
stream.close(internalError, new Metadata());
stream.cancel(internalError);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This status is stored internally in gRPC, but won't actually be exposed anywhere on server-side. We should probably go ahead and log it here.

Copy link
Contributor Author

@ramaraochavali ramaraochavali Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. which level? Is SEVERE the correct level? I have added with WARN. Let me know if needs to be changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING seems good. SEVERE seems a bit too much, because we were able to recover.

}

/**
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/io/grpc/internal/ServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,7 @@ void setListener(ServerStreamListener listener) {
* Like {@link ServerCall#close(Status, Metadata)}, but thread-safe for internal use.
*/
private void internalClose() {
// TODO(ejona86): this is not thread-safe :)
stream.close(Status.UNKNOWN, new Metadata());
stream.cancel(Status.INTERNAL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix doc comments if you change? edit: Please don't care.

}

@Override
Expand Down
9 changes: 3 additions & 6 deletions core/src/test/java/io/grpc/internal/ServerCallImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,9 @@ private void sendMessage_serverSendsOne_closeOnSecondCall(
verify(stream, times(1)).writeMessage(any(InputStream.class));
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
verify(stream, times(1)).close(statusCaptor.capture(), metadataCaptor.capture());
verify(stream, times(1)).cancel(statusCaptor.capture());
assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
assertEquals(ServerCallImpl.TOO_MANY_RESPONSES, statusCaptor.getValue().getDescription());
assertTrue(metadataCaptor.getValue().keys().isEmpty());
}

@Test
Expand All @@ -221,7 +220,7 @@ private void sendMessage_serverSendsOne_closeOnSecondCall_appRunToCompletion(
serverCall.sendMessage(1L);
serverCall.sendMessage(1L);
verify(stream, times(1)).writeMessage(any(InputStream.class));
verify(stream, times(1)).close(any(Status.class), any(Metadata.class));
verify(stream, times(1)).cancel(any(Status.class));

// App runs to completion but everything is ignored
serverCall.sendMessage(1L);
Expand Down Expand Up @@ -255,11 +254,9 @@ private void serverSendsOne_okFailsOnMissingResponse(
CompressorRegistry.getDefaultInstance());
serverCall.close(Status.OK, new Metadata());
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
verify(stream, times(1)).close(statusCaptor.capture(), metadataCaptor.capture());
verify(stream, times(1)).cancel(statusCaptor.capture());
assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
assertEquals(ServerCallImpl.MISSING_RESPONSE, statusCaptor.getValue().getDescription());
assertTrue(metadataCaptor.getValue().keys().isEmpty());
}

@Test
Expand Down
18 changes: 12 additions & 6 deletions core/src/test/java/io/grpc/internal/ServerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ public void messageRead_errorCancelsCall() throws Exception {
fail("Expected exception");
} catch (TestError t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
ensureServerStateIsCancelled();
}
}

Expand All @@ -1120,7 +1120,7 @@ public void messageRead_runtimeExceptionCancelsCall() throws Exception {
fail("Expected exception");
} catch (RuntimeException t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
ensureServerStateIsCancelled();
}
}

Expand All @@ -1143,7 +1143,7 @@ public void halfClosed_errorCancelsCall() {
fail("Expected exception");
} catch (TestError t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
ensureServerStateIsCancelled();
}
}

Expand All @@ -1166,7 +1166,7 @@ public void halfClosed_runtimeExceptionCancelsCall() {
fail("Expected exception");
} catch (RuntimeException t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
ensureServerStateIsCancelled();
}
}

Expand All @@ -1189,7 +1189,7 @@ public void onReady_errorCancelsCall() {
fail("Expected exception");
} catch (TestError t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
ensureServerStateIsCancelled();
}
}

Expand All @@ -1212,7 +1212,7 @@ public void onReady_runtimeExceptionCancelsCall() {
fail("Expected exception");
} catch (RuntimeException t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
ensureServerStateIsCancelled();
}
}

Expand Down Expand Up @@ -1250,6 +1250,12 @@ private void ensureServerStateNotLeaked() {
assertTrue(metadataCaptor.getValue().keys().isEmpty());
}

private void ensureServerStateIsCancelled() {
verify(stream).cancel(statusCaptor.capture());
assertEquals(Status.INTERNAL, statusCaptor.getValue());
assertNull(statusCaptor.getValue().getCause());
}

private static class SimpleServer implements io.grpc.internal.InternalServer {
ServerListener listener;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void onCompleted() {
.onNext(StreamingInputCallRequest.getDefaultInstance());

assertTrue(finishLatch.await(900, TimeUnit.MILLISECONDS));
assertEquals(Status.UNKNOWN, Status.fromThrowable(throwableRef.get()));
assertEquals(Status.CANCELLED.getCode(), Status.fromThrowable(throwableRef.get()).getCode());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now calling serverStream.cancel() will propagate a CANCEL HTTP/2 error to the remote. It'd probably be better if it was INTERNAL_ERROR, which will get mapped to a INTERNAL grpc status code. That way the client would get a signal that it shouldn't retry.

I think it'd be fair to map gRPC's DEADLINE_EXCEEDED and CANCELLED error codes to HTTP/2's CANCEL, and everything else map to HTTP/2's INTERNAL_ERROR. Right now we use DEADLINE_EXCEEDED for when deadline expires. That mapping could be in NettyServerHandler.cancelStream().

assertNull(responseRef.get());
}
}