Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc.xds;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static io.grpc.xds.internal.extproc.ExternalProcessorUtil.applyHeaderMutations;
import static io.grpc.xds.internal.extproc.ExternalProcessorUtil.collectAttributes;
import static io.grpc.xds.internal.extproc.ExternalProcessorUtil.markDataPlaneCallClosed;
Expand Down Expand Up @@ -366,6 +367,8 @@ private static class DataPlaneClientCall<ReqT, RespT>
final AtomicBoolean isProcessingTrailers = new AtomicBoolean(false);
final AtomicBoolean pendingHalfClose = new AtomicBoolean(false);
final AtomicBoolean bodyMessageSentToExtProc = new AtomicBoolean(false);
final AtomicBoolean aborted = new AtomicBoolean(false);
final AtomicBoolean cancelCalled = new AtomicBoolean(false);

protected DataPlaneClientCall(
DataPlaneDelayedCall<ReqT, RespT> delayedCall,
Expand Down Expand Up @@ -462,6 +465,7 @@ private boolean validateCompressionSupport(BodyResponse bodyResponse) {

@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
checkState(!cancelCalled.get(), "call was cancelled");
this.callContext = Context.current();
clientHeadersStartNanos = System.nanoTime();
this.requestHeaders = headers;
Expand Down Expand Up @@ -994,26 +998,30 @@ public void request(int numMessages) {
@SuppressWarnings("unchecked")
@Override
public void sendMessage(ReqT message) {
checkState(!cancelCalled.get(), "call was cancelled");
Comment thread
sauravzg marked this conversation as resolved.
Resolved
if (aborted.get()) {
return;
}
if (requestSideClosed.get()) {
// External processor already closed the request stream. Discard further messages.
return;
}

if (passThroughMode.get()) {
super.sendMessage(message);
proceedWithSendMessage(message);
return;
}

synchronized (streamLock) {
if (passThroughMode.get()) {
super.sendMessage(message);
proceedWithSendMessage(message);
return;
}

ExtProcStreamState state = extProcStreamState.get();
if (state.isDraining() || state.isCompleted()) {
if (currentProcessingMode.getRequestBodyMode() == ProcessingMode.BodySendMode.NONE) {
super.sendMessage(message);
proceedWithSendMessage(message);
return;
}
try {
Expand All @@ -1026,7 +1034,7 @@ public void sendMessage(ReqT message) {
}

if (currentProcessingMode.getRequestBodyMode() == ProcessingMode.BodySendMode.NONE) {
super.sendMessage(message);
proceedWithSendMessage(message);
return;
}

Expand All @@ -1041,7 +1049,7 @@ public void sendMessage(ReqT message) {
.build())
.build());
bodyMessageSentToExtProc.set(true);
super.sendMessage((ReqT) new KnownLengthInputStream(bodyByteString));
proceedWithSendMessage((ReqT) new KnownLengthInputStream(bodyByteString));
} else {
if (downstreamToSidestreamWindow <= 0 || !pendingRequestBodyMessages.isEmpty()) {
pendingRequestBodyMessages.add(bodyByteString);
Expand Down Expand Up @@ -1070,16 +1078,33 @@ private void sendRequestBodyToExtProc(ByteString body) {

@GuardedBy("streamLock")
private void drainPendingRequestBodyMessages() {
if (isCancelledOrAborted()) {
return;
}
while (downstreamToSidestreamWindow > 0 && !pendingRequestBodyMessages.isEmpty()) {
ByteString body = pendingRequestBodyMessages.poll();
sendRequestBodyToExtProc(body);
}
if (pendingRequestBodyMessages.isEmpty() && pendingHalfClose.compareAndSet(true, false)) {
halfClose();
internalHalfClose();
}
}

private boolean isCancelledOrAborted() {
return cancelCalled.get() || aborted.get();
}

private void proceedWithSendMessage(ReqT message) {
if (isCancelledOrAborted()) {
return;
}
super.sendMessage(message);
}

private void proceedWithHalfClose() {
if (isCancelledOrAborted()) {
return;
}
if (clientHalfCloseStartNanos > 0) {
long durationNanos = System.nanoTime() - clientHalfCloseStartNanos;
recordDuration(clientHalfCloseDuration, durationNanos);
Expand All @@ -1090,6 +1115,14 @@ private void proceedWithHalfClose() {

@Override
public void halfClose() {
checkState(!cancelCalled.get(), "call was cancelled");
internalHalfClose();
}

private void internalHalfClose() {
if (aborted.get()) {
return;
}
if (appHalfClosed.compareAndSet(false, true)) {
clientHalfCloseStartNanos = System.nanoTime();
}
Expand Down Expand Up @@ -1159,12 +1192,14 @@ public void halfClose() {
}

void cancelDownstream(@Nullable String message, @Nullable Throwable cause) {
Comment thread
sauravzg marked this conversation as resolved.
Resolved
aborted.set(true);
delayedCall.cancel(message, cause);
}

@Override
public void cancel(@Nullable String message, @Nullable Throwable cause) {
synchronized (streamLock) {
cancelCalled.set(true);
if (markExtProcStreamFailed(extProcStreamState)) {
if (extProcClientCallRequestObserver != null) {
extProcClientCallRequestObserver.onError(
Expand All @@ -1176,7 +1211,7 @@ public void cancel(@Nullable String message, @Nullable Throwable cause) {
}
}
}
cancelDownstream(message, cause);
delayedCall.cancel(message, cause);
}

@SuppressWarnings("unchecked")
Expand All @@ -1201,7 +1236,7 @@ private void handleRequestBodyResponse(BodyResponse bodyResponse) {
}
}
if (sendImmediately) {
super.sendMessage((ReqT) new KnownLengthInputStream(body));
proceedWithSendMessage((ReqT) new KnownLengthInputStream(body));
trySendAccumulatedWindowUpdates();
}
}
Expand Down Expand Up @@ -1319,7 +1354,7 @@ void drainPendingUpstreamBodyMessages() {
if (body == null) {
break;
}
super.sendMessage((ReqT) new KnownLengthInputStream(body));
proceedWithSendMessage((ReqT) new KnownLengthInputStream(body));
trySendAccumulatedWindowUpdates();
if (triggerHalfClose) {
if (requestSideClosed.compareAndSet(false, true)) {
Expand Down Expand Up @@ -1402,9 +1437,9 @@ private void drainPendingDrainingMessages() {
}

if (isMutated) {
super.sendMessage((ReqT) new KnownLengthInputStream((ByteString) msg));
proceedWithSendMessage((ReqT) new KnownLengthInputStream((ByteString) msg));
} else {
super.sendMessage((ReqT) msg);
proceedWithSendMessage((ReqT) msg);
}
}
}
Expand Down
70 changes: 46 additions & 24 deletions xds/src/main/java/io/grpc/xds/XdsNameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static io.grpc.xds.client.Bootstrapper.XDSTP_SCHEME;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -1201,6 +1202,7 @@ private static <ReqT, RespT> ClientCall<ReqT, RespT> interceptBoth(

@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
checkNotCancelled();
rawCall.start(createRawListener(method, responseListener), headers);
}

Expand All @@ -1224,6 +1226,7 @@ private static <ReqT, RespT> ClientCall<ReqT, RespT> interceptRequestOnly(

@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
checkNotCancelled();
rawCall.start(responseListener, headers);
}

Expand All @@ -1247,11 +1250,13 @@ private static <ReqT, RespT> ClientCall<ReqT, RespT> interceptResponseOnly(

@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
checkNotCancelled();
rawCall.start(createRawListener(method, responseListener), headers);
}

@Override
public void sendMessage(ReqT message) {
checkNotCancelled();
rawCall.sendMessage(message);
}
};
Expand Down Expand Up @@ -1283,35 +1288,47 @@ public void onReady() {
};
}

private static <ReqT> void sendSerializedMessage(
ClientCall<InputStream, ?> rawCall,
MethodDescriptor<ReqT, ?> method,
ReqT message) {
ByteString byteString;
try {
InputStream stream = method.getRequestMarshaller().stream(message);
private abstract static class PartialForwardingClientCall<ReqT, RespT>
extends ClientCall<ReqT, RespT> {
private boolean aborted;
private volatile boolean cancelCalled;

protected abstract ClientCall<?, ?> delegate();

protected void checkNotCancelled() {
checkState(!cancelCalled, "call was cancelled");
}

protected void sendSerializedMessage(
ClientCall<InputStream, ?> rawCall,
MethodDescriptor<ReqT, ?> method,
ReqT message) {
checkNotCancelled();
if (aborted) {
return;
}
ByteString byteString;
try {
byteString = ExternalProcessorUtil.outboundStreamToByteString(stream);
} finally {
InputStream stream = method.getRequestMarshaller().stream(message);
try {
stream.close();
} catch (IOException ignored) {
// ignore
byteString = ExternalProcessorUtil.outboundStreamToByteString(stream);
} finally {
try {
stream.close();
} catch (IOException ignored) {
// ignore
}
}
} catch (IOException | RuntimeException e) {
aborted = true;
rawCall.cancel("Failed to read message for raw message interceptor", e);
return;
} catch (Error e) {
rawCall.cancel("Client sendMessage() failed with Error", e);
throw e;
}
} catch (IOException | RuntimeException e) {
rawCall.cancel("Failed to read message for raw message interceptor", e);
return;
} catch (Error e) {
rawCall.cancel("Client sendMessage() failed with Error", e);
throw e;
rawCall.sendMessage(new KnownLengthInputStream(byteString));
}
rawCall.sendMessage(new KnownLengthInputStream(byteString));
}

private abstract static class PartialForwardingClientCall<ReqT, RespT>
extends ClientCall<ReqT, RespT> {
protected abstract ClientCall<?, ?> delegate();

@Override
public void request(int numMessages) {
Expand All @@ -1320,11 +1337,16 @@ public void request(int numMessages) {

@Override
public void cancel(@Nullable String message, @Nullable Throwable cause) {
cancelCalled = true;
delegate().cancel(message, cause);
}

@Override
public void halfClose() {
checkNotCancelled();
if (aborted) {
return;
}
delegate().halfClose();
}

Expand Down
Loading
Loading