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 4 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
@@ -0,0 +1,112 @@
/*
* Copyright © 2021 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.loadbalancer;

import org.junit.jupiter.api.Test;

import static io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

abstract class EagerLoadBalancerTest extends LoadBalancerTest {

@Test
void duplicateEventsAreIgnored() {
assertThat(lb.usedAddresses(), is(empty()));

sendServiceDiscoveryEvents(upEvent("address-1"));
assertThat(lb.usedAddresses(), hasSize(1));
sendServiceDiscoveryEvents(upEvent("address-1"));
assertThat(lb.usedAddresses(), hasSize(1));

sendServiceDiscoveryEvents(downEvent("address-1"));
assertThat(lb.usedAddresses(), hasSize(0));
sendServiceDiscoveryEvents(downEvent("address-1"));
assertThat(lb.usedAddresses(), hasSize(0));
}

@Test
void handleDiscoveryEvents() {
assertAddresses(lb.usedAddresses(), EMPTY_ARRAY);

sendServiceDiscoveryEvents(upEvent("address-1"));
assertAddresses(lb.usedAddresses(), "address-1");

sendServiceDiscoveryEvents(downEvent("address-1"));
assertAddresses(lb.usedAddresses(), EMPTY_ARRAY);

sendServiceDiscoveryEvents(upEvent("address-2"));
assertAddresses(lb.usedAddresses(), "address-2");

sendServiceDiscoveryEvents(downEvent("address-3"));
assertAddresses(lb.usedAddresses(), "address-2");

sendServiceDiscoveryEvents(upEvent("address-1"));
assertAddresses(lb.usedAddresses(), "address-2", "address-1");

sendServiceDiscoveryEvents(downEvent("address-1"));
assertAddresses(lb.usedAddresses(), "address-2");

sendServiceDiscoveryEvents(downEvent("address-2"));
assertAddresses(lb.usedAddresses(), EMPTY_ARRAY);

sendServiceDiscoveryEvents(downEvent("address-3"));
assertAddresses(lb.usedAddresses(), EMPTY_ARRAY);

// Let's make sure that an SD failure doesn't compromise LB's internal state
sendServiceDiscoveryEvents(upEvent("address-1"));
assertAddresses(lb.usedAddresses(), "address-1");

serviceDiscoveryPublisher.onError(DELIBERATE_EXCEPTION);
assertAddresses(lb.usedAddresses(), "address-1");
}

@Test
void hostDownGracefullyClosesConnections() throws Exception {
sendServiceDiscoveryEvents(upEvent("address-1"));
TestLoadBalancedConnection host1Conn1 = lb.selectConnection(alwaysNewConnectionFilter(), null).toFuture().get();

sendServiceDiscoveryEvents(upEvent("address-2"));
TestLoadBalancedConnection host2Conn1 = newForHost("address-2");

// create another for address-1
TestLoadBalancedConnection host1Conn2 = newForHost("address-1");

sendServiceDiscoveryEvents(downEvent("address-1"));
validateConnectionClosedGracefully(host1Conn1);
validateConnectionClosedGracefully(host1Conn2);

// The remaining Host's connections should not be closed
assertConnectionCount(lb.usedAddresses(), connectionsCount("address-2", 1));
verify(host2Conn1, never()).closeAsync();
verify(host2Conn1, never()).closeAsyncGracefully();
}

private void validateConnectionClosedGracefully(final TestLoadBalancedConnection connection) throws Exception {
connection.onClose().toFuture().get();
verify(connection).closeAsyncGracefully();
verify(connection, never()).closeAsync();
}

@Override
protected boolean eagerConnectionShutdown() {
return true;
}
}
Loading
Loading