Skip to content

core: Don't pre-compute DEADLINE_EXCEEDED message for delayed calls #12208

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
46 changes: 22 additions & 24 deletions core/src/main/java/io/grpc/internal/DelayedClientCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,13 @@
private ScheduledFuture<?> scheduleDeadlineIfNeeded(
ScheduledExecutorService scheduler, @Nullable Deadline deadline) {
Deadline contextDeadline = context.getDeadline();
if (deadline == null && contextDeadline == null) {
return null;
}
long remainingNanos = Long.MAX_VALUE;
if (deadline != null) {
String deadlineName;
long remainingNanos;
if (deadline != null && isAbeforeB(deadline, contextDeadline)) {
deadlineName = "CallOptions";
remainingNanos = deadline.timeRemaining(NANOSECONDS);
}

if (contextDeadline != null && contextDeadline.timeRemaining(NANOSECONDS) < remainingNanos) {
} else if (contextDeadline != null) {
deadlineName = "Context";

Check warning on line 105 in core/src/main/java/io/grpc/internal/DelayedClientCall.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/DelayedClientCall.java#L105

Added line #L105 was not covered by tests
remainingNanos = contextDeadline.timeRemaining(NANOSECONDS);
if (logger.isLoggable(Level.FINE)) {
StringBuilder builder =
Expand All @@ -121,29 +119,29 @@
}
logger.fine(builder.toString());
}
}

long seconds = Math.abs(remainingNanos) / TimeUnit.SECONDS.toNanos(1);
long nanos = Math.abs(remainingNanos) % TimeUnit.SECONDS.toNanos(1);
final StringBuilder buf = new StringBuilder();
String deadlineName = isAbeforeB(contextDeadline, deadline) ? "Context" : "CallOptions";
if (remainingNanos < 0) {
buf.append("ClientCall started after ");
buf.append(deadlineName);
buf.append(" deadline was exceeded. Deadline has been exceeded for ");
} else {
buf.append("Deadline ");
buf.append(deadlineName);
buf.append(" will be exceeded in ");
return null;
}
buf.append(seconds);
buf.append(String.format(Locale.US, ".%09d", nanos));
buf.append("s. ");

/* Cancels the call if deadline exceeded prior to the real call being set. */
class DeadlineExceededRunnable implements Runnable {
@Override
public void run() {
long seconds = Math.abs(remainingNanos) / TimeUnit.SECONDS.toNanos(1);
long nanos = Math.abs(remainingNanos) % TimeUnit.SECONDS.toNanos(1);
StringBuilder buf = new StringBuilder();
if (remainingNanos < 0) {
buf.append("ClientCall started after ");
buf.append(deadlineName);
buf.append(" deadline was exceeded. Deadline has been exceeded for ");
} else {
buf.append("Deadline ");
buf.append(deadlineName);
buf.append(" was exceeded after ");
}
buf.append(seconds);
buf.append(String.format(Locale.US, ".%09d", nanos));
buf.append("s");
cancel(
Status.DEADLINE_EXCEEDED.withDescription(buf.toString()),
// We should not cancel the call if the realCall is set because there could be a
Expand Down
2 changes: 1 addition & 1 deletion xds/src/test/java/io/grpc/xds/XdsNameResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,7 @@ public long nanoTime() {
assertThat(testCall).isNull();
verifyRpcDelayedThenAborted(observer, 4000L, Status.DEADLINE_EXCEEDED.withDescription(
"Deadline exceeded after up to 5000 ns of fault-injected delay:"
+ " Deadline CallOptions will be exceeded in 0.000004000s. "));
+ " Deadline CallOptions was exceeded after 0.000004000s"));
}

@Test
Expand Down