Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Endpoint selection is retried on selection timeout (#4269)
Motivation: - Differentiate the cases where "endpoint is set to `null`" vs. "endpoint selection timed out" for easier debugging. - `EmptyEndpointGroupException`: If an `EndpointGroup` sets the endpoint as `null` - `EndpointSelectionTimeoutException`: If the `Endpoint` selection timed out. - Add the capability to retry endpoint selection from an `EndpointGroup` if not selected yet. Modifications: - Define a helper class `ClientPendingThrowableUtil` which contains helper methods for setting a new attribute `CLIENT_PENDING_THROWABLE`. - This throwable is thrown after decorators are processed, but before the actual client execution starts. - This attribute should be removed if a user wants to re-use a derived context. - An example of this can be seen at `RetryingClient`. `RetryingClient` works by creating a derived context from the original context, and executing requests. If the original context contains `CLIENT_PENDING_THROWABLE`, then derived contexts would also contain the `CLIENT_PENDING_THROWABLE` and the retried requests would fail immediately. Hence, it is necessary that `CLIENT_PENDING_THROWABLE` be cleared in the derived context. - Set `CLIENT_PENDING_THROWABLE` to `EndpointSelectionTimeoutException` when an `Endpoint` selection times out. - Modify `RetryingClient`, `RetryingRpcClient`: - Check if the context has an `EndpointGroup` but doesn't have an `Endpoint` yet, which means previous endpoint selection has failed. If an endpoint hasn't been selected yet, call `initContextAndExecuteWithFallback` which tries another `EndpointSelection`. Result: - `EndpointSelectionTimeoutException` is thrown on selection timeout - Retry is done on selection timeout
- Loading branch information
Showing
14 changed files
with
687 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...src/main/java/com/linecorp/armeria/client/endpoint/EndpointSelectionTimeoutException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.client.endpoint; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.linecorp.armeria.client.Endpoint; | ||
import com.linecorp.armeria.common.Flags; | ||
import com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
||
/** | ||
* An {@link EndpointGroupException} raised when an {@link EndpointGroup} fails to resolve | ||
* an {@link Endpoint} within a configured selection timeout. | ||
*/ | ||
@UnstableApi | ||
public final class EndpointSelectionTimeoutException extends EndpointGroupException { | ||
|
||
private static final long serialVersionUID = -3079582212067997365L; | ||
|
||
private static final EndpointSelectionTimeoutException INSTANCE = | ||
new EndpointSelectionTimeoutException(); | ||
|
||
/** | ||
* Returns an {@link EndpointSelectionTimeoutException} which prints a message about | ||
* the {@link EndpointGroup} when thrown. | ||
*/ | ||
public static EndpointSelectionTimeoutException get(EndpointGroup endpointGroup, | ||
long selectionTimeoutMillis) { | ||
requireNonNull(endpointGroup, "endpointGroup"); | ||
checkArgument(selectionTimeoutMillis >= 0, "selectionTimeoutMillis: %s (expected: >= 0)", | ||
selectionTimeoutMillis); | ||
return Flags.verboseExceptionSampler().isSampled(EndpointSelectionTimeoutException.class) ? | ||
new EndpointSelectionTimeoutException(endpointGroup, selectionTimeoutMillis) : INSTANCE; | ||
} | ||
|
||
private EndpointSelectionTimeoutException() {} | ||
|
||
private EndpointSelectionTimeoutException(EndpointGroup endpointGroup, long selectionTimeoutMillis) { | ||
super("Failed to select within " + selectionTimeoutMillis + " ms an endpoint from: " + endpointGroup); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
core/src/main/java/com/linecorp/armeria/internal/client/ClientPendingThrowableUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.internal.client; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.linecorp.armeria.client.ClientRequestContext; | ||
import com.linecorp.armeria.common.annotation.Nullable; | ||
import com.linecorp.armeria.common.util.Exceptions; | ||
|
||
import io.netty.util.AttributeKey; | ||
|
||
/** | ||
* Sets a pending {@link Throwable} for the specified {@link ClientRequestContext}. | ||
* This throwable will be thrown after all decorators are executed, but before the | ||
* actual client execution starts. | ||
* | ||
* <p>For example:<pre>{@code | ||
* final RuntimeException e = new RuntimeException(); | ||
* final WebClient webClient = | ||
* WebClient.builder(SessionProtocol.HTTP, endpointGroup) | ||
* .contextCustomizer(ctx -> setPendingThrowable(ctx, e)) | ||
* .decorator(LoggingClient.newDecorator()) // the request is logged | ||
* .build(); | ||
* assertThatThrownBy(() -> webClient.blocking().get("/")) | ||
* .isInstanceOf(UnprocessedRequestException.class) | ||
* .hasCause(e); | ||
* }</pre> | ||
*/ | ||
public final class ClientPendingThrowableUtil { | ||
|
||
private static final AttributeKey<Throwable> CLIENT_PENDING_THROWABLE = | ||
AttributeKey.valueOf(ClientPendingThrowableUtil.class, "CLIENT_PENDING_THROWABLE"); | ||
|
||
/** | ||
* Sets a pending {@link Throwable} for the specified {@link ClientRequestContext}. | ||
* Note that the throwable will be peeled via {@link Exceptions#peel(Throwable)} | ||
* before being set. | ||
* | ||
* <p>For example:<pre>{@code | ||
* final RuntimeException e = new RuntimeException(); | ||
* final CompletionException wrapper = new CompletionException(e); | ||
* final ClientRequestContext ctx = ... | ||
* setPendingThrowable(ctx, wrapper); | ||
* final Throwable throwable = pendingThrowable(ctx); | ||
* assert throwable != null; | ||
* assertThat(throwable).isEqualTo(e); | ||
* }</pre> | ||
*/ | ||
public static void setPendingThrowable(ClientRequestContext ctx, Throwable cause) { | ||
requireNonNull(ctx, "ctx"); | ||
requireNonNull(cause, "cause"); | ||
cause = Exceptions.peel(cause); | ||
ctx.setAttr(CLIENT_PENDING_THROWABLE, cause); | ||
} | ||
|
||
/** | ||
* Retrieves the pending {@link Throwable} for the specified {@link ClientRequestContext}. | ||
* Note that the derived context will also contain this attribute by default, and can fail | ||
* requests immediately. The pending {@link Throwable} can be removed by | ||
* {@link #removePendingThrowable(ClientRequestContext)}. | ||
* | ||
* <p>For example:<pre>{@code | ||
* ClientRequestContext ctx = ...; | ||
* Throwable t = ...; | ||
* final Throwable t1 = pendingThrowable(ctx); | ||
* assert t1 == null; | ||
* setPendingThrowable(ctx, t); | ||
* final Throwable t2 = pendingThrowable(ctx); | ||
* assert t2 == t; | ||
* final ClientRequestContext derived = ctx.newDerivedContext(id, req, rpcReq, endpoint); | ||
* final Throwable t3 = pendingThrowable(derived); | ||
* assert t3 == t; | ||
* }</pre> | ||
*/ | ||
@Nullable | ||
public static Throwable pendingThrowable(ClientRequestContext ctx) { | ||
requireNonNull(ctx, "ctx"); | ||
return ctx.attr(CLIENT_PENDING_THROWABLE); | ||
} | ||
|
||
/** | ||
* Removes the pending throwable set by {@link #setPendingThrowable(ClientRequestContext, Throwable)}. | ||
* | ||
* <p>For example:<pre>{@code | ||
* final Throwable throwable = pendingThrowable(ctx); | ||
* assert throwable != null; | ||
* assertThat(throwable).isEqualTo(e); | ||
* removePendingThrowable(ctx); | ||
* assertThat(pendingThrowable(ctx)).isNull(); | ||
* }</pre> | ||
*/ | ||
public static void removePendingThrowable(ClientRequestContext ctx) { | ||
requireNonNull(ctx, "ctx"); | ||
if (!ctx.hasAttr(CLIENT_PENDING_THROWABLE)) { | ||
return; | ||
} | ||
ctx.setAttr(CLIENT_PENDING_THROWABLE, null); | ||
} | ||
|
||
private ClientPendingThrowableUtil() {} | ||
} |
Oops, something went wrong.