Skip to content

Commit a573991

Browse files
committed
util: exclude client cancellations and deadline exceeded from outlier detection call counter
1 parent 20682d9 commit a573991

3 files changed

Lines changed: 122 additions & 74 deletions

File tree

core/src/main/java/io/grpc/internal/AbstractClientStream.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ public final void halfClose() {
197197
@Override
198198
public final void cancel(Status reason) {
199199
Preconditions.checkArgument(!reason.isOk(), "Should not cancel with OK status");
200+
if (cancelled || transportState().isListenerClosed()) {
201+
return;
202+
}
200203
cancelled = true;
201204
abstractClientStreamSink().cancel(reason);
202205
}
@@ -251,6 +254,10 @@ protected TransportState(
251254
}
252255
}
253256

257+
protected final boolean isListenerClosed() {
258+
return listenerClosed;
259+
}
260+
254261
private void setFullStreamDecompression(boolean fullStreamDecompression) {
255262
this.fullStreamDecompression = fullStreamDecompression;
256263
}

util/src/main/java/io/grpc/util/OutlierDetectionLoadBalancer.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -477,41 +477,22 @@ public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata header
477477
if (delegateFactory != null) {
478478
ClientStreamTracer delegateTracer = delegateFactory.newClientStreamTracer(info, headers);
479479
return new ForwardingClientStreamTracer() {
480-
private volatile boolean cancelled;
481-
482480
@Override
483481
protected ClientStreamTracer delegate() {
484482
return delegateTracer;
485483
}
486484

487-
@Override
488-
public void cancelled(Status status) {
489-
cancelled = true;
490-
delegate().cancelled(status);
491-
}
492-
493485
@Override
494486
public void streamClosed(Status status) {
495-
if (!cancelled) {
496-
tracker.incrementCallCount(status.isOk());
497-
}
487+
tracker.incrementCallCount(status);
498488
delegate().streamClosed(status);
499489
}
500490
};
501491
} else {
502492
return new ClientStreamTracer() {
503-
private volatile boolean cancelled;
504-
505-
@Override
506-
public void cancelled(Status status) {
507-
cancelled = true;
508-
}
509-
510493
@Override
511494
public void streamClosed(Status status) {
512-
if (!cancelled) {
513-
tracker.incrementCallCount(status.isOk());
514-
}
495+
tracker.incrementCallCount(status);
515496
}
516497
};
517498
}
@@ -571,13 +552,18 @@ Set<OutlierDetectionSubchannel> getSubchannels() {
571552
return ImmutableSet.copyOf(subchannels);
572553
}
573554

574-
void incrementCallCount(boolean success) {
555+
void incrementCallCount(Status status) {
575556
// If neither algorithm is configured, no point in incrementing counters.
576557
if (config.successRateEjection == null && config.failurePercentageEjection == null) {
577558
return;
578559
}
579560

580-
if (success) {
561+
if (status.getCode() == Status.Code.CANCELLED
562+
|| status.getCode() == Status.Code.DEADLINE_EXCEEDED) {
563+
return;
564+
}
565+
566+
if (status.isOk()) {
581567
activeCallCounter.successCount.getAndIncrement();
582568
} else {
583569
activeCallCounter.failureCount.getAndIncrement();

0 commit comments

Comments
 (0)