forked from apple/servicetalk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RetryingHttpRequesterFilter
: don't wait for LB if it's unhealthy (a…
…pple#2648) Motivation: Today, `NoAvailableHostException` can be thrown when either LB is not ready yet or when it's already ready, but all hosts turned into unhealthy state. In this case, there is no need to wait for "ready" event, because the onHostsAvailable completable is already completed. There is a risk to never exit from the retry loop if `RetryingHttpRequesterFilter` is configured with `maxTotalRetries(Integer.MAX_VALUE)`. Modifications: - Introduce `NoActiveHostException` to avoid interacting with `LoadBalancerReadySubscriber` when LB turns into unhealthy state; - Add tests to verify expected behavior; - Adjust `RoundRobinLoadBalancerTest`; Result: `RetryingHttpRequesterFilter` doesn't involve `LoadBalancerReadySubscriber` when LB is unhealthy.
- Loading branch information
1 parent
140abce
commit c30e076
Showing
7 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
servicetalk-client-api/src/main/java/io/servicetalk/client/api/NoActiveHostException.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,37 @@ | ||
/* | ||
* Copyright © 2023 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed 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 | ||
* | ||
* http://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 io.servicetalk.client.api; | ||
|
||
import io.servicetalk.transport.api.RetryableException; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Thrown when no host is active to establish a new connection. | ||
*/ | ||
public class NoActiveHostException extends IOException implements RetryableException { | ||
|
||
private static final long serialVersionUID = -4764627055167224323L; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param message the detail message. | ||
*/ | ||
public NoActiveHostException(final String message) { | ||
super(message); | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
...icetalk-http-netty/src/test/java/io/servicetalk/http/netty/LoadBalancerUnhealthyTest.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,78 @@ | ||
/* | ||
* Copyright © 2023 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed 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 | ||
* | ||
* http://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 io.servicetalk.http.netty; | ||
|
||
import io.servicetalk.client.api.DelegatingConnectionFactory; | ||
import io.servicetalk.client.api.NoActiveHostException; | ||
import io.servicetalk.concurrent.api.Single; | ||
import io.servicetalk.concurrent.internal.DeliberateException; | ||
import io.servicetalk.context.api.ContextMap; | ||
import io.servicetalk.http.api.BlockingHttpClient; | ||
import io.servicetalk.http.api.FilterableStreamingHttpConnection; | ||
import io.servicetalk.http.api.HttpServerContext; | ||
import io.servicetalk.transport.api.TransportObserver; | ||
import io.servicetalk.transport.netty.internal.ExecutionContextExtension; | ||
|
||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
import java.net.InetSocketAddress; | ||
import javax.annotation.Nullable; | ||
|
||
import static io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION; | ||
import static io.servicetalk.http.netty.BuilderUtils.newClientBuilder; | ||
import static io.servicetalk.http.netty.BuilderUtils.newServerBuilder; | ||
import static java.lang.Integer.MAX_VALUE; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class LoadBalancerUnhealthyTest { | ||
|
||
@RegisterExtension | ||
static final ExecutionContextExtension SERVER_CTX = | ||
ExecutionContextExtension.cached("server-io", "server-executor") | ||
.setClassLevel(true); | ||
@RegisterExtension | ||
static final ExecutionContextExtension CLIENT_CTX = | ||
ExecutionContextExtension.cached("client-io", "client-executor") | ||
.setClassLevel(true); | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}] protocol={0}") | ||
@EnumSource(HttpProtocol.class) | ||
void doesNotRetryIndefinitely(HttpProtocol protocol) throws Exception { | ||
try (HttpServerContext serverContext = newServerBuilder(SERVER_CTX, protocol) | ||
.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok()); | ||
BlockingHttpClient client = newClientBuilder(serverContext, CLIENT_CTX, protocol) | ||
// Intentionally set maxTotalRetries to MAX_VALUE | ||
.appendClientFilter(new RetryingHttpRequesterFilter.Builder().maxTotalRetries(MAX_VALUE).build()) | ||
.appendConnectionFactoryFilter(factory -> new DelegatingConnectionFactory<InetSocketAddress, | ||
FilterableStreamingHttpConnection>(factory) { | ||
@Override | ||
public Single<FilterableStreamingHttpConnection> newConnection( | ||
InetSocketAddress address, @Nullable ContextMap context, | ||
@Nullable TransportObserver observer) { | ||
return Single.failed(DELIBERATE_EXCEPTION); | ||
} | ||
}) | ||
.buildBlocking()) { | ||
// Turn LoadBalancer into "unhealthy" state: | ||
for (int i = 0; i < 5; i++) { | ||
assertThrows(DeliberateException.class, () -> client.request(client.get("/"))); | ||
} | ||
assertThrows(NoActiveHostException.class, () -> client.request(client.get("/"))); | ||
} | ||
} | ||
} |
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
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