Skip to content
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

loadbalancer: move DefaultLoadBalancer to its own experimental module #2819

Merged
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
43 changes: 43 additions & 0 deletions servicetalk-loadbalancer-experimental/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright © 2018-2019 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.
*/

apply plugin: "io.servicetalk.servicetalk-gradle-plugin-internal-library"

dependencies {
implementation platform(project(":servicetalk-dependencies"))
testImplementation enforcedPlatform("org.junit:junit-bom:$junit5Version")

api project(":servicetalk-client-api")
api project(":servicetalk-concurrent-api")

implementation project(":servicetalk-annotations")
implementation project(":servicetalk-concurrent-api-internal")
implementation project(":servicetalk-concurrent-internal")
implementation project(":servicetalk-loadbalancer")
implementation project(":servicetalk-utils-internal")
implementation "com.google.code.findbugs:jsr305"
implementation "org.slf4j:slf4j-api"

testImplementation testFixtures(project(":servicetalk-concurrent-api"))
testImplementation testFixtures(project(":servicetalk-concurrent-internal"))
testImplementation project(":servicetalk-concurrent-test-internal")
testImplementation project(":servicetalk-test-resources")
testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation "org.junit.jupiter:junit-jupiter-params"
testImplementation "org.apache.logging.log4j:log4j-core"
testImplementation "org.hamcrest:hamcrest:$hamcrestVersion"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright © 2019 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.
-->
<FindBugsFilter>
<!-- Fields are usually initialized in @BeforeClass/@Before methods instead of constructors for tests -->
<Match>
<Source name="~.*Test\.java"/>
<Bug pattern="NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR"/>
</Match>
<Match>
<Source name="~.*Test\.java"/>
<Bug pattern="THROWS_METHOD_THROWS_RUNTIMEEXCEPTION"/>
</Match>
<Match>
<Source name="~.*Test\.java"/>
<Bug pattern="THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION"/>
</Match>
<Match>
<Source name="~.*Test\.java"/>
<Bug pattern="THROWS_METHOD_THROWS_CLAUSE_THROWABLE"/>
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Enumeration of the main failure classes.
*/
enum ErrorClass {
public enum ErrorClass {

/**
* Failures related to locally enforced timeouts that prevent session establishment with the peer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
* - At initiation of an action for which a request is must call {@link RequestTracker#beforeStart()} and save the
* timestamp much like would be done when using a stamped lock.
* - Once the request event is complete only one of the {@link RequestTracker#onSuccess(long)} or
* {@link RequestTracker#onError(long, ErrorClass, Throwable)} methods must be called and called exactly once.
* {@link RequestTracker#onError(long, ErrorClass)} methods must be called and called exactly once.
* In other words, every call to {@link RequestTracker#beforeStart()} must be followed by exactly one call to either of
* the completion methods {@link RequestTracker#onSuccess(long)} or
* {@link RequestTracker#onError(long, ErrorClass, Throwable)}. Failure to do so can cause state corruption in the
* {@link RequestTracker#onError(long, ErrorClass)}. Failure to do so can cause state corruption in the
* {@link RequestTracker} implementations which may track not just latency but also the outstanding requests.
*/
interface RequestTracker {
public interface RequestTracker {

ContextMap.Key<RequestTracker> REQUEST_TRACKER_KEY =
ContextMap.Key.newKey("request_tracker", RequestTracker.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright © 2018 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.
*/
@ElementsAreNonnullByDefault
package io.servicetalk.loadbalancer;

import io.servicetalk.annotations.ElementsAreNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -58,17 +57,14 @@ public final class RoundRobinLoadBalancerFactory<ResolvedAddress, C extends Load

private final String id;
private final int linearSearchSpace;
private final boolean useNewRoundRobin;
@Nullable
private final HealthCheckConfig healthCheckConfig;

private RoundRobinLoadBalancerFactory(final String id,
final int linearSearchSpace,
final boolean useNewRoundRobin,
@Nullable final HealthCheckConfig healthCheckConfig) {
this.id = id;
this.linearSearchSpace = linearSearchSpace;
this.useNewRoundRobin = useNewRoundRobin;
this.healthCheckConfig = healthCheckConfig;
}

Expand All @@ -78,30 +74,10 @@ public <T extends C> LoadBalancer<T> newLoadBalancer(
final String targetResource,
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
final ConnectionFactory<ResolvedAddress, T> connectionFactory) {
return useNewRoundRobin ? useNewRoundRobinLoadBalancer(targetResource, eventPublisher, connectionFactory)
: new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
return new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
linearSearchSpace, healthCheckConfig);
}

@Override
public LoadBalancer<C> newLoadBalancer(
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
final ConnectionFactory<ResolvedAddress, C> connectionFactory,
final String targetResource) {
return useNewRoundRobin ? useNewRoundRobinLoadBalancer(targetResource, eventPublisher, connectionFactory)
: new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
linearSearchSpace, healthCheckConfig);
}

private <T extends C> LoadBalancer<T> useNewRoundRobinLoadBalancer(
final String targetResource,
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
final ConnectionFactory<ResolvedAddress, T> connectionFactory) {
return new DefaultLoadBalancer<>(id, targetResource, eventPublisher,
new RoundRobinSelector<>(Collections.emptyList(), targetResource, false), connectionFactory,
linearSearchSpace, NoopLoadBalancerObserver.instance(), healthCheckConfig, null);
}

@Override
public ExecutionStrategy requiredOffloads() {
// We do not block
Expand All @@ -121,7 +97,6 @@ public static final class Builder<ResolvedAddress, C extends LoadBalancedConnect
implements RoundRobinLoadBalancerBuilder<ResolvedAddress, C> {
private final String id;
private int linearSearchSpace = 16;
private boolean useNewRoundRobin;
@Nullable
private Executor backgroundExecutor;
private Duration healthCheckInterval = DEFAULT_HEALTH_CHECK_INTERVAL;
Expand Down Expand Up @@ -153,13 +128,6 @@ public RoundRobinLoadBalancerFactory.Builder<ResolvedAddress, C> linearSearchSpa
return this;
}

// In the future we may elevate this to the RoundRobinLoadBalancerBuilder interface or pick another
// route to transition to the new load balancer structure.
RoundRobinLoadBalancerBuilder<ResolvedAddress, C> useNewRoundRobin(boolean useNewRoundRobin) {
this.useNewRoundRobin = useNewRoundRobin;
return this;
}

@Override
public RoundRobinLoadBalancerFactory.Builder<ResolvedAddress, C> backgroundExecutor(
Executor backgroundExecutor) {
Expand Down Expand Up @@ -217,14 +185,14 @@ public RoundRobinLoadBalancerFactory.Builder<ResolvedAddress, C> healthCheckFail
@Override
public RoundRobinLoadBalancerFactory<ResolvedAddress, C> build() {
if (this.healthCheckFailedConnectionsThreshold < 0) {
return new RoundRobinLoadBalancerFactory<>(id, linearSearchSpace, useNewRoundRobin, null);
return new RoundRobinLoadBalancerFactory<>(id, linearSearchSpace, null);
}

HealthCheckConfig healthCheckConfig = new HealthCheckConfig(
this.backgroundExecutor == null ? SharedExecutor.getInstance() : this.backgroundExecutor,
healthCheckInterval, healthCheckJitter, healthCheckFailedConnectionsThreshold,
healthCheckResubscribeInterval, healthCheckResubscribeJitter);
return new RoundRobinLoadBalancerFactory<>(id, linearSearchSpace, useNewRoundRobin, healthCheckConfig);
return new RoundRobinLoadBalancerFactory<>(id, linearSearchSpace, healthCheckConfig);
}
}

Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ include "servicetalk-annotations",
"servicetalk-http-security-jersey",
"servicetalk-http-utils",
"servicetalk-loadbalancer",
"servicetalk-loadbalancer-experimental",
"servicetalk-log4j2-mdc",
"servicetalk-log4j2-mdc-utils",
"servicetalk-logging-api",
Expand Down
Loading