Skip to content

Commit 954c147

Browse files
loadbalancer: move DefaultLoadBalancer to its own experimental module (#2819)
Motivation: We want to start integrating with other packages but we can't without making some interfaces and types public. However, that means we're committing to the API and we don't want to do that yet. Modifications: - Move the new load balancer code to its own experimental module. This is a strong signal to users that the API is in flux and should not be depended upon yet. - Move the `LoadBalancerTests` to the experimental module as well to simplify things: they'll get moved back once we drop the experimental status.
1 parent ed7df84 commit 954c147

File tree

60 files changed

+105
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+105
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright © 2018-2019 Apple Inc. and the ServiceTalk project authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
apply plugin: "io.servicetalk.servicetalk-gradle-plugin-internal-library"
18+
19+
dependencies {
20+
implementation platform(project(":servicetalk-dependencies"))
21+
testImplementation enforcedPlatform("org.junit:junit-bom:$junit5Version")
22+
23+
api project(":servicetalk-client-api")
24+
api project(":servicetalk-concurrent-api")
25+
26+
implementation project(":servicetalk-annotations")
27+
implementation project(":servicetalk-concurrent-api-internal")
28+
implementation project(":servicetalk-concurrent-internal")
29+
implementation project(":servicetalk-loadbalancer")
30+
implementation project(":servicetalk-utils-internal")
31+
implementation "com.google.code.findbugs:jsr305"
32+
implementation "org.slf4j:slf4j-api"
33+
34+
testImplementation testFixtures(project(":servicetalk-concurrent-api"))
35+
testImplementation testFixtures(project(":servicetalk-concurrent-internal"))
36+
testImplementation project(":servicetalk-concurrent-test-internal")
37+
testImplementation project(":servicetalk-test-resources")
38+
testImplementation "org.junit.jupiter:junit-jupiter-api"
39+
testImplementation "org.junit.jupiter:junit-jupiter-params"
40+
testImplementation "org.apache.logging.log4j:log4j-core"
41+
testImplementation "org.hamcrest:hamcrest:$hamcrestVersion"
42+
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright © 2019 Apple Inc. and the ServiceTalk project authors
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<FindBugsFilter>
18+
<!-- Fields are usually initialized in @BeforeClass/@Before methods instead of constructors for tests -->
19+
<Match>
20+
<Source name="~.*Test\.java"/>
21+
<Bug pattern="NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"/>
22+
</Match>
23+
<Match>
24+
<Source name="~.*Test\.java"/>
25+
<Bug pattern="THROWS_METHOD_THROWS_RUNTIMEEXCEPTION"/>
26+
</Match>
27+
<Match>
28+
<Source name="~.*Test\.java"/>
29+
<Bug pattern="THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION"/>
30+
</Match>
31+
<Match>
32+
<Source name="~.*Test\.java"/>
33+
<Bug pattern="THROWS_METHOD_THROWS_CLAUSE_THROWABLE"/>
34+
</Match>
35+
</FindBugsFilter>

servicetalk-loadbalancer/src/main/java/io/servicetalk/loadbalancer/ErrorClass.java servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/ErrorClass.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Enumeration of the main failure classes.
2020
*/
21-
enum ErrorClass {
21+
public enum ErrorClass {
2222

2323
/**
2424
* Failures related to locally enforced timeouts that prevent session establishment with the peer.

servicetalk-loadbalancer/src/main/java/io/servicetalk/loadbalancer/RequestTracker.java servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/RequestTracker.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
* - At initiation of an action for which a request is must call {@link RequestTracker#beforeStart()} and save the
2525
* timestamp much like would be done when using a stamped lock.
2626
* - Once the request event is complete only one of the {@link RequestTracker#onSuccess(long)} or
27-
* {@link RequestTracker#onError(long, ErrorClass, Throwable)} methods must be called and called exactly once.
27+
* {@link RequestTracker#onError(long, ErrorClass)} methods must be called and called exactly once.
2828
* In other words, every call to {@link RequestTracker#beforeStart()} must be followed by exactly one call to either of
2929
* the completion methods {@link RequestTracker#onSuccess(long)} or
30-
* {@link RequestTracker#onError(long, ErrorClass, Throwable)}. Failure to do so can cause state corruption in the
30+
* {@link RequestTracker#onError(long, ErrorClass)}. Failure to do so can cause state corruption in the
3131
* {@link RequestTracker} implementations which may track not just latency but also the outstanding requests.
3232
*/
33-
interface RequestTracker {
33+
public interface RequestTracker {
3434

3535
ContextMap.Key<RequestTracker> REQUEST_TRACKER_KEY =
3636
ContextMap.Key.newKey("request_tracker", RequestTracker.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
@ElementsAreNonnullByDefault
17+
package io.servicetalk.loadbalancer;
18+
19+
import io.servicetalk.annotations.ElementsAreNonnullByDefault;

servicetalk-loadbalancer/src/main/java/io/servicetalk/loadbalancer/RoundRobinLoadBalancerFactory.java

+3-35
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import java.time.Duration;
3030
import java.util.Collection;
31-
import java.util.Collections;
3231
import java.util.concurrent.LinkedBlockingQueue;
3332
import java.util.concurrent.ThreadPoolExecutor;
3433
import javax.annotation.Nullable;
@@ -58,17 +57,14 @@ public final class RoundRobinLoadBalancerFactory<ResolvedAddress, C extends Load
5857

5958
private final String id;
6059
private final int linearSearchSpace;
61-
private final boolean useNewRoundRobin;
6260
@Nullable
6361
private final HealthCheckConfig healthCheckConfig;
6462

6563
private RoundRobinLoadBalancerFactory(final String id,
6664
final int linearSearchSpace,
67-
final boolean useNewRoundRobin,
6865
@Nullable final HealthCheckConfig healthCheckConfig) {
6966
this.id = id;
7067
this.linearSearchSpace = linearSearchSpace;
71-
this.useNewRoundRobin = useNewRoundRobin;
7268
this.healthCheckConfig = healthCheckConfig;
7369
}
7470

@@ -78,30 +74,10 @@ public <T extends C> LoadBalancer<T> newLoadBalancer(
7874
final String targetResource,
7975
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
8076
final ConnectionFactory<ResolvedAddress, T> connectionFactory) {
81-
return useNewRoundRobin ? useNewRoundRobinLoadBalancer(targetResource, eventPublisher, connectionFactory)
82-
: new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
77+
return new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
8378
linearSearchSpace, healthCheckConfig);
8479
}
8580

86-
@Override
87-
public LoadBalancer<C> newLoadBalancer(
88-
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
89-
final ConnectionFactory<ResolvedAddress, C> connectionFactory,
90-
final String targetResource) {
91-
return useNewRoundRobin ? useNewRoundRobinLoadBalancer(targetResource, eventPublisher, connectionFactory)
92-
: new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
93-
linearSearchSpace, healthCheckConfig);
94-
}
95-
96-
private <T extends C> LoadBalancer<T> useNewRoundRobinLoadBalancer(
97-
final String targetResource,
98-
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
99-
final ConnectionFactory<ResolvedAddress, T> connectionFactory) {
100-
return new DefaultLoadBalancer<>(id, targetResource, eventPublisher,
101-
new RoundRobinSelector<>(Collections.emptyList(), targetResource, false), connectionFactory,
102-
linearSearchSpace, NoopLoadBalancerObserver.instance(), healthCheckConfig, null);
103-
}
104-
10581
@Override
10682
public ExecutionStrategy requiredOffloads() {
10783
// We do not block
@@ -121,7 +97,6 @@ public static final class Builder<ResolvedAddress, C extends LoadBalancedConnect
12197
implements RoundRobinLoadBalancerBuilder<ResolvedAddress, C> {
12298
private final String id;
12399
private int linearSearchSpace = 16;
124-
private boolean useNewRoundRobin;
125100
@Nullable
126101
private Executor backgroundExecutor;
127102
private Duration healthCheckInterval = DEFAULT_HEALTH_CHECK_INTERVAL;
@@ -153,13 +128,6 @@ public RoundRobinLoadBalancerFactory.Builder<ResolvedAddress, C> linearSearchSpa
153128
return this;
154129
}
155130

156-
// In the future we may elevate this to the RoundRobinLoadBalancerBuilder interface or pick another
157-
// route to transition to the new load balancer structure.
158-
RoundRobinLoadBalancerBuilder<ResolvedAddress, C> useNewRoundRobin(boolean useNewRoundRobin) {
159-
this.useNewRoundRobin = useNewRoundRobin;
160-
return this;
161-
}
162-
163131
@Override
164132
public RoundRobinLoadBalancerFactory.Builder<ResolvedAddress, C> backgroundExecutor(
165133
Executor backgroundExecutor) {
@@ -217,14 +185,14 @@ public RoundRobinLoadBalancerFactory.Builder<ResolvedAddress, C> healthCheckFail
217185
@Override
218186
public RoundRobinLoadBalancerFactory<ResolvedAddress, C> build() {
219187
if (this.healthCheckFailedConnectionsThreshold < 0) {
220-
return new RoundRobinLoadBalancerFactory<>(id, linearSearchSpace, useNewRoundRobin, null);
188+
return new RoundRobinLoadBalancerFactory<>(id, linearSearchSpace, null);
221189
}
222190

223191
HealthCheckConfig healthCheckConfig = new HealthCheckConfig(
224192
this.backgroundExecutor == null ? SharedExecutor.getInstance() : this.backgroundExecutor,
225193
healthCheckInterval, healthCheckJitter, healthCheckFailedConnectionsThreshold,
226194
healthCheckResubscribeInterval, healthCheckResubscribeJitter);
227-
return new RoundRobinLoadBalancerFactory<>(id, linearSearchSpace, useNewRoundRobin, healthCheckConfig);
195+
return new RoundRobinLoadBalancerFactory<>(id, linearSearchSpace, healthCheckConfig);
228196
}
229197
}
230198

settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ include "servicetalk-annotations",
8686
"servicetalk-http-security-jersey",
8787
"servicetalk-http-utils",
8888
"servicetalk-loadbalancer",
89+
"servicetalk-loadbalancer-experimental",
8990
"servicetalk-log4j2-mdc",
9091
"servicetalk-log4j2-mdc-utils",
9192
"servicetalk-logging-api",

0 commit comments

Comments
 (0)