-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
652ec78
commit 905e488
Showing
7 changed files
with
903 additions
and
594 deletions.
There are no files selected for viewing
614 changes: 614 additions & 0 deletions
614
servicetalk-loadbalancer/src/main/java/io/servicetalk/loadbalancer/DefaultHost.java
Large diffs are not rendered by default.
Oops, something went wrong.
613 changes: 50 additions & 563 deletions
613
servicetalk-loadbalancer/src/main/java/io/servicetalk/loadbalancer/Host.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
182 changes: 182 additions & 0 deletions
182
servicetalk-loadbalancer/src/test/java/io/servicetalk/loadbalancer/P2CSelectorTest.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,182 @@ | ||
/* | ||
* 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.loadbalancer; | ||
|
||
import io.servicetalk.client.api.NoActiveHostException; | ||
import io.servicetalk.concurrent.api.Single; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.RepeatedTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.function.Predicate; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.either; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.isA; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyBoolean; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class P2CSelectorTest { | ||
|
||
private static final Predicate<TestLoadBalancedConnection> PREDICATE = (ignored) -> true; | ||
private Random random; | ||
private P2CSelector<String, TestLoadBalancedConnection> selector; | ||
|
||
@BeforeEach | ||
void init() { | ||
init(5, random); | ||
} | ||
|
||
void init(int maxEffort, @Nullable Random random) { | ||
selector = new P2CSelector<>("testResource", maxEffort, random); | ||
} | ||
|
||
private Host mockHost(String addr, TestLoadBalancedConnection connection) { | ||
Host<String, TestLoadBalancedConnection> host = mock(Host.class); | ||
when(host.address()).thenReturn(addr); | ||
when(host.isUnhealthy()).thenReturn(true); | ||
when(host.isActiveAndHealthy()).thenReturn(true); | ||
when(host.pickConnection(any(), any())).thenReturn(connection); | ||
when(host.newConnection(any(), anyBoolean(), any())).thenReturn(Single.succeeded(connection)); | ||
return host; | ||
} | ||
|
||
private List<Host<String, TestLoadBalancedConnection>> connections(String... addresses) { | ||
final List<Host<String, TestLoadBalancedConnection>> results = new ArrayList<>(addresses.length); | ||
for (String addr : addresses) { | ||
results.add(mockHost(addr, TestLoadBalancedConnection.mockConnection(addr))); | ||
} | ||
return results; | ||
} | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}]: forceNewConnection={0}") | ||
@ValueSource(booleans = {false, true}) | ||
void singleHost(boolean forceNewConnection) throws Exception { | ||
List<Host<String, TestLoadBalancedConnection>> hosts = connections("addr-1"); | ||
TestLoadBalancedConnection connection = selector.selectConnection( | ||
hosts, PREDICATE, null, forceNewConnection).toFuture().get(); | ||
assertThat(connection.address(), equalTo("addr-1")); | ||
} | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}]: forceNewConnection={0}") | ||
@ValueSource(booleans = {false, true}) | ||
void singleUnhealthyHostWithConnection(boolean forceNewConnection) throws Exception { | ||
List<Host<String, TestLoadBalancedConnection>> hosts = connections("addr-1"); | ||
when(hosts.get(0).isActiveAndHealthy()).thenReturn(false); | ||
if (forceNewConnection) { | ||
Exception e = assertThrows(ExecutionException.class, () -> selector.selectConnection( | ||
hosts, PREDICATE, null, forceNewConnection).toFuture().get()); | ||
assertThat(e.getCause(), isA(NoActiveHostException.class)); | ||
} else { | ||
TestLoadBalancedConnection connection = selector.selectConnection( | ||
hosts, PREDICATE, null, forceNewConnection).toFuture().get(); | ||
assertThat(connection.address(), equalTo("addr-1")); | ||
} | ||
} | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}]: forceNewConnection={0}") | ||
@ValueSource(booleans = {false, true}) | ||
void singleUnhealthyHostWithoutConnection(boolean forceNewConnection) { | ||
List<Host<String, TestLoadBalancedConnection>> hosts = connections("addr-1"); | ||
when(hosts.get(0).isActiveAndHealthy()).thenReturn(false); | ||
when(hosts.get(0).pickConnection(any(), any())).thenReturn(null); | ||
Exception e = assertThrows(ExecutionException.class, () -> selector.selectConnection( | ||
hosts, PREDICATE, null, forceNewConnection).toFuture().get()); | ||
assertThat(e.getCause(), isA(NoActiveHostException.class)); | ||
} | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}]: forceNewConnection={0}") | ||
@ValueSource(booleans = {false, true}) | ||
void twoHealthyHosts(boolean forceNewConnection) throws Exception { | ||
List<Host<String, TestLoadBalancedConnection>> hosts = connections("addr-1", "addr-2"); | ||
TestLoadBalancedConnection connection = selector.selectConnection( | ||
hosts, PREDICATE, null, forceNewConnection).toFuture().get(); | ||
assertThat(connection.address(), either(equalTo("addr-1")).or(equalTo("addr-2"))); | ||
} | ||
|
||
@Test | ||
void twoUnHealthyHostsWithConnections() throws Exception { | ||
List<Host<String, TestLoadBalancedConnection>> hosts = connections("addr-1", "addr-2"); | ||
for (Host<String, TestLoadBalancedConnection> host : hosts) { | ||
when(host.isActiveAndHealthy()).thenReturn(false); | ||
} | ||
TestLoadBalancedConnection connection = selector.selectConnection( | ||
hosts, PREDICATE, null, false).toFuture().get(); | ||
assertThat(connection.address(), either(equalTo("addr-1")).or(equalTo("addr-2"))); | ||
} | ||
|
||
@Test | ||
void twoUnHealthyHostsWithoutConnections() { | ||
List<Host<String, TestLoadBalancedConnection>> hosts = connections("addr-1", "addr-2"); | ||
for (Host<String, TestLoadBalancedConnection> host : hosts) { | ||
when(host.isActiveAndHealthy()).thenReturn(false); | ||
when(host.pickConnection(any(), any())).thenReturn(null); | ||
} | ||
Exception e = assertThrows(ExecutionException.class, () -> selector.selectConnection( | ||
hosts, PREDICATE, null, false).toFuture().get()); | ||
assertThat(e.getCause(), isA(NoActiveHostException.class)); | ||
} | ||
|
||
@RepeatedTest(100) | ||
void doesntBiasTowardHostsWithConnections() throws Exception { | ||
List<Host<String, TestLoadBalancedConnection>> hosts = connections("addr-1", "addr-2"); | ||
// we setup the first host to always be preferred by score, but it also doesn't have any connections. | ||
when(hosts.get(0).pickConnection(any(), any())).thenReturn(null); | ||
when(hosts.get(0).score()).thenReturn(10); | ||
TestLoadBalancedConnection connection = selector.selectConnection( | ||
hosts, PREDICATE, null, false).toFuture().get(); | ||
assertThat(connection.address(), equalTo("addr-1")); | ||
} | ||
|
||
@RepeatedTest(100) | ||
void biasesTowardsActiveAndHealthyHostWhenNoConnections() throws Exception { | ||
List<Host<String, TestLoadBalancedConnection>> hosts = connections("addr-1", "addr-2"); | ||
when(hosts.get(0).isActiveAndHealthy()).thenReturn(false); | ||
TestLoadBalancedConnection connection = selector.selectConnection( | ||
hosts, PREDICATE, null, true).toFuture().get(); | ||
assertThat(connection.address(), equalTo("addr-2")); | ||
} | ||
|
||
@RepeatedTest(100) | ||
void biasesTowardTheHighestWeightHostForNewConnections() throws Exception { | ||
biasesTowardTheHighestWeightHost(true); | ||
} | ||
|
||
@RepeatedTest(100) | ||
void biasesTowardTheHighestWeightHostForExistingConnections() throws Exception { | ||
biasesTowardTheHighestWeightHost(false); | ||
} | ||
|
||
void biasesTowardTheHighestWeightHost(boolean forceNewConnection) throws Exception { | ||
List<Host<String, TestLoadBalancedConnection>> hosts = connections("addr-1", "addr-2"); | ||
// Host 0 has the highest score so it should always get the new connection. | ||
when(hosts.get(0).score()).thenReturn(10); | ||
TestLoadBalancedConnection connection = selector.selectConnection( | ||
hosts, PREDICATE, null, forceNewConnection).toFuture().get(); | ||
assertThat(connection.address(), equalTo("addr-1")); | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
...lk-loadbalancer/src/test/java/io/servicetalk/loadbalancer/TestLoadBalancedConnection.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,43 @@ | ||
/* | ||
* 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.loadbalancer; | ||
|
||
import io.servicetalk.client.api.LoadBalancedConnection; | ||
import io.servicetalk.concurrent.api.ListenableAsyncCloseable; | ||
|
||
import static io.servicetalk.concurrent.api.AsyncCloseables.emptyAsyncCloseable; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
interface TestLoadBalancedConnection extends LoadBalancedConnection { | ||
String address(); | ||
|
||
static TestLoadBalancedConnection mockConnection(final String address) { | ||
return mockConnection(address, emptyAsyncCloseable()); | ||
} | ||
|
||
static TestLoadBalancedConnection mockConnection(final String address, final ListenableAsyncCloseable closeable) { | ||
final TestLoadBalancedConnection cnx = mock(TestLoadBalancedConnection.class); | ||
when(cnx.closeAsync()).thenReturn(closeable.closeAsync()); | ||
when(cnx.closeAsyncGracefully()).thenReturn(closeable.closeAsyncGracefully()); | ||
when(cnx.onClose()).thenReturn(closeable.onClose()); | ||
when(cnx.onClosing()).thenReturn(closeable.onClosing()); | ||
when(cnx.address()).thenReturn(address); | ||
when(cnx.toString()).thenReturn(address + '@' + cnx.hashCode()); | ||
when(cnx.tryReserve()).thenReturn(true); | ||
return cnx; | ||
} | ||
} |