Skip to content

Commit e34bc57

Browse files
committed
xds: implement upstream flow control queuing and draining in ext_proc client interceptor
- Queue mutated request body messages received from ext_proc in a new `pendingUpstreamBodyMessages` queue if the upstream transport is not ready (`isReady() == false`). - Drain the queued request body messages and forward them to the transport when the transport becomes ready (triggered in `onReady()`). - Defer request half-close if there are still body messages in the queue, executing the half-close once the queue is fully drained. - Remove the `isReady()` check when merging or sending accumulated client window updates (`accumulatedWindowUpdateSidestreamToUpstream`), ensuring window updates are always propagated. - Add a new unit test `testSidestreamToUpstreamFlowControl_QueuingAndDraining` in `ExternalProcessorClientInterceptorTest` to verify flow control queuing, transport transitions, and draining behavior.
1 parent ceef8df commit e34bc57

2 files changed

Lines changed: 526 additions & 612 deletions

File tree

‎xds/src/main/java/io/grpc/xds/ExternalProcessorClientInterceptor.java‎

Lines changed: 119 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
197197
MethodDescriptor<ReqT, RespT> method,
198198
CallOptions callOptions,
199199
Channel next) {
200-
java.util.concurrent.Executor callExecutor = callOptions.getExecutor();
201-
if (callExecutor == null) {
202-
callExecutor = new java.util.concurrent.Executor() {
203-
@Override
204-
public void execute(Runnable command) {
205-
command.run();
206-
}
207-
};
208-
}
200+
Executor callExecutor = callOptions.getExecutor();
209201
SerializingExecutor serializingExecutor = new SerializingExecutor(callExecutor);
210202

211203
ExternalProcessorGrpc.ExternalProcessorStub extProcStub = ExternalProcessorGrpc.newStub(
@@ -300,6 +292,7 @@ private static class DataPlaneClientCall
300292
private final AtomicInteger pendingRequests = new AtomicInteger(0);
301293
private final ProcessingMode currentProcessingMode;
302294

295+
// Default initial window size
303296
private static final long DEFAULT_INITIAL_WINDOW_SIZE = 65536;
304297

305298
// Outbound (sending) windows
@@ -310,23 +303,30 @@ private static class DataPlaneClientCall
310303
private long sidestreamToUpstreamWindow = DEFAULT_INITIAL_WINDOW_SIZE;
311304
private long sidestreamToDownstreamWindow = DEFAULT_INITIAL_WINDOW_SIZE;
312305

313-
// Buffered request body messages from downstream
314-
private final Queue<ByteString> pendingRequestBodyMessages =
315-
new java.util.concurrent.ConcurrentLinkedQueue<>();
306+
// Threshold to trigger standalone client window updates
307+
private static final long WINDOW_UPDATE_THRESHOLD = DEFAULT_INITIAL_WINDOW_SIZE / 2;
308+
309+
// Path 1: Pending/buffered request body messages from downstream
310+
private final Queue<ByteString> pendingRequestBodyMessages = new ConcurrentLinkedQueue<>();
316311

317-
// Buffered mutated response bodies from sidecar
312+
// Path 2: Buffered request body messages from ext_proc server to forward upstream
313+
private final Queue<ByteString> pendingUpstreamBodyMessages =
314+
new java.util.concurrent.ConcurrentLinkedQueue<>();
315+
// Path 4: Outstanding requests from downstream for pulling responses
318316
private int downstreamRequestsPending = 0;
317+
// Buffered mutated response bodies from ext_proc server
319318
private final Queue<ByteString> pendingMutatedResponseBodies =
320319
new java.util.concurrent.ConcurrentLinkedQueue<>();
320+
// Deferred half-close flag for upstream direction
321+
private final AtomicBoolean pendingUpstreamHalfClose = new AtomicBoolean(false);
321322

322-
private static final long WINDOW_UPDATE_THRESHOLD = DEFAULT_INITIAL_WINDOW_SIZE / 2;
323-
324-
// Accumulated client window updates for external processor
323+
// Accumulated client window updates to send to ext_proc
325324
private long accumulatedWindowUpdateSidestreamToUpstream = 0;
326325
private long accumulatedWindowUpdateSidestreamToDownstream = 0;
327326

328-
// Track whether flow_control_init has been sent on the stream
327+
// Flag to track if FlowControlInit was sent in the initial message
329328
private boolean flowControlInitSent = false;
329+
330330
private final MethodDescriptor<?, ?> method;
331331
private final Channel channel;
332332
private final MetricRecorder metricsRecorder;
@@ -488,10 +488,12 @@ public void onNext(ProcessingResponse response) {
488488
upstreamToSidestreamWindow += update.getWindowIncrementUpstreamToSidestream();
489489
drainPendingRequestBodyMessages();
490490
drainPendingRequests();
491+
if (wrappedListener != null) {
492+
wrappedListener.drainSavedMessages();
493+
}
491494
}
492-
if (wrappedListener != null) {
493-
wrappedListener.drainSavedMessages();
494-
}
495+
// If isReady() becomes true (depends on updated downstreamToSidestreamWindow),
496+
// notify the client application via onReadyNotify() (runs unlocked).
495497
if (!wasReady && isReady()) {
496498
onReadyNotify();
497499
}
@@ -671,7 +673,7 @@ public void onCompleted() {
671673
== ProcessingMode.HeaderSendMode.DEFAULT;
672674

673675
if (sendRequestHeaders) {
674-
sendToExtProcDirect(ProcessingRequest.newBuilder()
676+
sendToExtProc(ProcessingRequest.newBuilder()
675677
.setRequestHeaders(HttpHeaders.newBuilder()
676678
.setHeaders(toHeaderMap(headers, config.getForwardRulesConfig()))
677679
.setEndOfStream(false)
@@ -684,7 +686,7 @@ public void onCompleted() {
684686
}
685687
}
686688

687-
private void sendToExtProcDirect(ProcessingRequest request) {
689+
private void sendToExtProc(ProcessingRequest request) {
688690
synchronized (streamLock) {
689691
if (extProcStreamState.get().isCompleted()) {
690692
return;
@@ -743,8 +745,7 @@ private void sendToExtProcDirect(ProcessingRequest request) {
743745

744746
void mergeAccumulatedWindowUpdates(ProcessingRequest.Builder requestBuilder) {
745747
synchronized (streamLock) {
746-
long incrementUpstream = super.isReady()
747-
? accumulatedWindowUpdateSidestreamToUpstream : 0;
748+
long incrementUpstream = accumulatedWindowUpdateSidestreamToUpstream;
748749
long incrementDownstream = accumulatedWindowUpdateSidestreamToDownstream;
749750

750751
if (incrementUpstream > 0 || incrementDownstream > 0) {
@@ -766,8 +767,7 @@ private void trySendAccumulatedWindowUpdates() {
766767
if (extProcStreamState.get().isCompleted()) {
767768
return;
768769
}
769-
long incrementUpstream = super.isReady()
770-
? accumulatedWindowUpdateSidestreamToUpstream : 0;
770+
long incrementUpstream = accumulatedWindowUpdateSidestreamToUpstream;
771771
long incrementDownstream = accumulatedWindowUpdateSidestreamToDownstream;
772772

773773
boolean shouldSend = (incrementUpstream > 0 || incrementDownstream > 0) && (
@@ -784,7 +784,7 @@ private void trySendAccumulatedWindowUpdates() {
784784
sidestreamToUpstreamWindow += incrementUpstream;
785785
sidestreamToDownstreamWindow += incrementDownstream;
786786

787-
sendToExtProcDirect(ProcessingRequest.newBuilder()
787+
sendToExtProc(ProcessingRequest.newBuilder()
788788
.setClientWindowUpdate(ProcessingRequest.ClientWindowUpdate.newBuilder()
789789
.setWindowIncrementSidestreamToUpstream(incrementUpstream)
790790
.setWindowIncrementSidestreamToDownstream(incrementDownstream)
@@ -811,7 +811,7 @@ void drainPendingRequests() {
811811
}
812812

813813
// Normal mode flow control: pull 1 message at a time
814-
if (isSidecarReady() && upstreamToSidestreamWindow > 0 && pendingRequests.get() > 0) {
814+
if (isExtProcReady() && upstreamToSidestreamWindow > 0 && pendingRequests.get() > 0) {
815815
super.request(1);
816816
pendingRequests.decrementAndGet();
817817
}
@@ -863,7 +863,7 @@ private void onReadyNotify() {
863863
wrappedListener.onReadyNotify();
864864
}
865865

866-
boolean isSidecarReady() {
866+
boolean isExtProcReady() {
867867
ExtProcStreamState state = extProcStreamState.get();
868868
if (state.isCompleted()) {
869869
return true;
@@ -889,11 +889,11 @@ public boolean isReady() {
889889
return false;
890890
}
891891
synchronized (streamLock) {
892-
boolean sidecarReady = isSidecarReady();
892+
boolean extProcReady = isExtProcReady();
893893
if (config.getObservabilityMode()) {
894-
return super.isReady() && sidecarReady;
894+
return super.isReady() && extProcReady;
895895
}
896-
return downstreamToSidestreamWindow > 0 && sidecarReady
896+
return downstreamToSidestreamWindow > 0 && extProcReady
897897
&& pendingRequestBodyMessages.isEmpty();
898898
}
899899
}
@@ -904,22 +904,34 @@ public void request(int numMessages) {
904904
super.request(numMessages);
905905
return;
906906
}
907-
if (!config.getObservabilityMode()
908-
&& currentProcessingMode.getResponseBodyMode() != ProcessingMode.BodySendMode.GRPC) {
909-
super.request(numMessages);
910-
return;
911-
}
912-
if (config.getObservabilityMode()
913-
|| currentProcessingMode.getResponseBodyMode() != ProcessingMode.BodySendMode.GRPC) {
914-
super.request(numMessages);
915-
return;
916-
}
917907
synchronized (streamLock) {
918-
pendingRequests.addAndGet(numMessages);
919-
downstreamRequestsPending += numMessages;
920-
if (isSidecarReady()) {
921-
drainPendingMutatedResponseBodies();
922-
drainPendingRequests();
908+
boolean sendResponseBodiesToExtProc = config.getObservabilityMode()
909+
|| currentProcessingMode.getResponseBodyMode() == ProcessingMode.BodySendMode.GRPC;
910+
911+
if (!sendResponseBodiesToExtProc) {
912+
// We do not send response bodies to ext_proc server at all. Bypassed.
913+
super.request(numMessages);
914+
return;
915+
}
916+
917+
// We send response bodies to ext_proc server (either in normal GRPC mode or observability mode).
918+
// Gated by ext_proc server readiness.
919+
boolean normalFlowControl = !config.getObservabilityMode(); // i.e. normal GRPC response body mode
920+
921+
if (normalFlowControl) {
922+
pendingRequests.addAndGet(numMessages);
923+
downstreamRequestsPending += numMessages;
924+
if (isExtProcReady()) {
925+
drainPendingMutatedResponseBodies();
926+
drainPendingRequests();
927+
}
928+
} else {
929+
// Observability mode: gate on readiness but pull all at once
930+
if (isExtProcReady()) {
931+
super.request(numMessages);
932+
} else {
933+
pendingRequests.addAndGet(numMessages);
934+
}
923935
}
924936
}
925937
}
@@ -966,7 +978,7 @@ public void sendMessage(InputStream message) {
966978
try {
967979
ByteString bodyByteString = outboundStreamToByteString(message);
968980
if (config.getObservabilityMode()) {
969-
sendToExtProcDirect(ProcessingRequest.newBuilder()
981+
sendToExtProc(ProcessingRequest.newBuilder()
970982
.setRequestBody(HttpBody.newBuilder()
971983
.setBody(bodyByteString)
972984
.setEndOfStream(false)
@@ -996,7 +1008,7 @@ private void sendRequestBodyToExtProc(ByteString body) {
9961008
.setEndOfStream(false)
9971009
.build());
9981010
mergeAccumulatedWindowUpdates(builder);
999-
sendToExtProcDirect(builder.build());
1011+
sendToExtProc(builder.build());
10001012
bodyMessageSentToExtProc.set(true);
10011013
}
10021014
}
@@ -1060,7 +1072,7 @@ public void halfClose() {
10601072
.setEndOfStreamWithoutMessage(true)
10611073
.build());
10621074
mergeAccumulatedWindowUpdates(builder);
1063-
sendToExtProcDirect(builder.build());
1075+
sendToExtProc(builder.build());
10641076
}
10651077
}
10661078

@@ -1087,6 +1099,7 @@ private void handleRequestBodyResponse(BodyResponse bodyResponse) {
10871099
com.google.protobuf.ByteString body = streamed.getBody();
10881100
if (!config.getObservabilityMode()
10891101
&& currentProcessingMode.getRequestBodyMode() == ProcessingMode.BodySendMode.GRPC) {
1102+
boolean sendImmediately = false;
10901103
synchronized (streamLock) {
10911104
if (sidestreamToUpstreamWindow <= 0) {
10921105
internalOnError(Status.INTERNAL
@@ -1097,18 +1110,30 @@ private void handleRequestBodyResponse(BodyResponse bodyResponse) {
10971110
return;
10981111
}
10991112
sidestreamToUpstreamWindow -= body.size();
1100-
accumulatedWindowUpdateSidestreamToUpstream += body.size();
1113+
if (super.isReady() && pendingUpstreamBodyMessages.isEmpty()) {
1114+
sendImmediately = true;
1115+
accumulatedWindowUpdateSidestreamToUpstream += body.size();
1116+
} else {
1117+
pendingUpstreamBodyMessages.add(body);
1118+
}
11011119
}
1102-
}
1103-
super.sendMessage(new KnownLengthInputStream(body));
1104-
if (!config.getObservabilityMode()
1105-
&& currentProcessingMode.getRequestBodyMode() == ProcessingMode.BodySendMode.GRPC) {
1106-
trySendAccumulatedWindowUpdates();
1120+
if (sendImmediately) {
1121+
super.sendMessage(new KnownLengthInputStream(body));
1122+
trySendAccumulatedWindowUpdates();
1123+
}
1124+
} else {
1125+
super.sendMessage(new KnownLengthInputStream(body));
11071126
}
11081127
}
11091128
if (streamed.getEndOfStream() || streamed.getEndOfStreamWithoutMessage()) {
1110-
if (requestSideClosed.compareAndSet(false, true)) {
1111-
proceedWithHalfClose();
1129+
synchronized (streamLock) {
1130+
if (pendingUpstreamBodyMessages.isEmpty()) {
1131+
if (requestSideClosed.compareAndSet(false, true)) {
1132+
proceedWithHalfClose();
1133+
}
1134+
} else {
1135+
pendingUpstreamHalfClose.set(true);
1136+
}
11121137
}
11131138
}
11141139
}
@@ -1195,6 +1220,34 @@ private void drainPendingMutatedResponseBodies() {
11951220
}
11961221
}
11971222

1223+
void drainPendingUpstreamBodyMessages() {
1224+
while (true) {
1225+
ByteString body = null;
1226+
boolean triggerHalfClose = false;
1227+
synchronized (streamLock) {
1228+
if (super.isReady() && !pendingUpstreamBodyMessages.isEmpty()) {
1229+
body = pendingUpstreamBodyMessages.poll();
1230+
if (body != null) {
1231+
accumulatedWindowUpdateSidestreamToUpstream += body.size();
1232+
}
1233+
if (pendingUpstreamBodyMessages.isEmpty() && pendingUpstreamHalfClose.compareAndSet(true, false)) {
1234+
triggerHalfClose = true;
1235+
}
1236+
}
1237+
}
1238+
if (body == null) {
1239+
break;
1240+
}
1241+
super.sendMessage(new KnownLengthInputStream(body));
1242+
trySendAccumulatedWindowUpdates();
1243+
if (triggerHalfClose) {
1244+
if (requestSideClosed.compareAndSet(false, true)) {
1245+
proceedWithHalfClose();
1246+
}
1247+
}
1248+
}
1249+
}
1250+
11981251
private void handleImmediateResponse(ImmediateResponse immediate, DataPlaneListener listener)
11991252
throws HeaderMutationDisallowedException {
12001253
Status status = Status.fromCodeValue(immediate.getGrpcStatus().getStatus());
@@ -1307,6 +1360,8 @@ AtomicBoolean getIsProcessingTrailers() {
13071360
private static class DataPlaneListener extends SimpleForwardingClientCallListener<InputStream> {
13081361
private final ClientCall<?, ?> rawCall;
13091362
private final DataPlaneClientCall dataPlaneClientCall;
1363+
// Path 3: Upstream response bodies queued because upstream to sidestream window not available,
1364+
// response headers not cleared by ext_proc or ext_proc stream draining
13101365
private final Queue<InputStream> savedMessages = new ConcurrentLinkedQueue<>();
13111366
private boolean inboundPassThrough = false;
13121367
@Nullable private volatile Metadata savedHeaders;
@@ -1344,6 +1399,7 @@ void setImmediateResponse(Status status, Metadata trailers) {
13441399

13451400
@Override
13461401
public void onReady() {
1402+
dataPlaneClientCall.drainPendingUpstreamBodyMessages();
13471403
dataPlaneClientCall.trySendAccumulatedWindowUpdates();
13481404
dataPlaneClientCall.drainPendingRequests();
13491405
onReadyNotify();
@@ -1372,7 +1428,7 @@ public void onHeaders(Metadata headers) {
13721428
}
13731429

13741430
this.savedHeaders = headers;
1375-
dataPlaneClientCall.sendToExtProcDirect(ProcessingRequest.newBuilder()
1431+
dataPlaneClientCall.sendToExtProc(ProcessingRequest.newBuilder()
13761432
.setResponseHeaders(HttpHeaders.newBuilder()
13771433
.setHeaders(
13781434
toHeaderMap(headers, dataPlaneClientCall.getConfig().getForwardRulesConfig()))
@@ -1443,7 +1499,7 @@ public void onMessage(InputStream message) {
14431499

14441500
void drainSavedMessages() {
14451501
synchronized (dataPlaneClientCall.getStreamLock()) {
1446-
while (dataPlaneClientCall.isSidecarReady()
1502+
while (dataPlaneClientCall.isExtProcReady()
14471503
&& dataPlaneClientCall.upstreamToSidestreamWindow > 0
14481504
&& !savedMessages.isEmpty()) {
14491505
InputStream msg = savedMessages.poll();
@@ -1605,7 +1661,7 @@ private void triggerCloseHandshake() {
16051661

16061662
if (trailersOnly.get()) {
16071663
if (sendResponseHeaders) {
1608-
dataPlaneClientCall.sendToExtProcDirect(ProcessingRequest.newBuilder()
1664+
dataPlaneClientCall.sendToExtProc(ProcessingRequest.newBuilder()
16091665
.setResponseHeaders(HttpHeaders.newBuilder()
16101666
.setHeaders(
16111667
toHeaderMap(
@@ -1622,7 +1678,7 @@ private void triggerCloseHandshake() {
16221678
}
16231679
} else if (sendResponseTrailers) {
16241680
dataPlaneClientCall.getIsProcessingTrailers().set(true);
1625-
dataPlaneClientCall.sendToExtProcDirect(ProcessingRequest.newBuilder()
1681+
dataPlaneClientCall.sendToExtProc(ProcessingRequest.newBuilder()
16261682
.setResponseTrailers(HttpTrailers.newBuilder()
16271683
.setTrailers(
16281684
toHeaderMap(
@@ -1665,7 +1721,7 @@ private void sendResponseBodyToExtProc(
16651721
ProcessingRequest.Builder builder = ProcessingRequest.newBuilder()
16661722
.setResponseBody(bodyBuilder.build());
16671723
dataPlaneClientCall.mergeAccumulatedWindowUpdates(builder);
1668-
dataPlaneClientCall.sendToExtProcDirect(builder.build());
1724+
dataPlaneClientCall.sendToExtProc(builder.build());
16691725
}
16701726
}
16711727
}

0 commit comments

Comments
 (0)