|
| 1 | +package concurrency; |
| 2 | + |
| 3 | +import com.arangodb.*; |
| 4 | +import com.arangodb.config.ArangoConfigProperties; |
| 5 | +import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.Arguments; |
| 7 | +import org.junit.jupiter.params.provider.MethodSource; |
| 8 | +import util.TestUtils; |
| 9 | + |
| 10 | +import java.time.Duration; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.CompletableFuture; |
| 13 | +import java.util.stream.IntStream; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +import static org.awaitility.Awaitility.await; |
| 17 | + |
| 18 | +public class ConnectionLoadBalanceTest { |
| 19 | + |
| 20 | + public static Stream<Arguments> configs() { |
| 21 | + return Stream.of( |
| 22 | + // FIXME: DE-1017 |
| 23 | + // new Config(Protocol.VST, 1), |
| 24 | + // new Config(Protocol.VST, 2), |
| 25 | + new Config(Protocol.HTTP_JSON, 10), |
| 26 | + new Config(Protocol.HTTP_JSON, 20), |
| 27 | + new Config(Protocol.HTTP2_JSON, 1), |
| 28 | + new Config(Protocol.HTTP2_JSON, 2) |
| 29 | + ).map(Arguments::of); |
| 30 | + } |
| 31 | + |
| 32 | + // Test the requests load balancing across different connections, when all the slots except 1 are busy |
| 33 | + @MethodSource("configs") |
| 34 | + @ParameterizedTest |
| 35 | + void loadBalanceToFreeConnection(Config cfg) throws InterruptedException { |
| 36 | + doTestLoadBalance(cfg, 1); |
| 37 | + } |
| 38 | + |
| 39 | + // Test the requests load balancing across different connections, when all the slots are busy |
| 40 | + @MethodSource("configs") |
| 41 | + @ParameterizedTest |
| 42 | + void loadBalanceAllBusy(Config cfg) throws InterruptedException { |
| 43 | + doTestLoadBalance(cfg, 2); |
| 44 | + } |
| 45 | + |
| 46 | + void doTestLoadBalance(Config cfg, int sleepCycles) throws InterruptedException { |
| 47 | + int longTasksCount = cfg.maxStreams() * cfg.maxConnections * sleepCycles - 1; |
| 48 | + int shortTasksCount = 10; |
| 49 | + long sleepDuration = 2; |
| 50 | + |
| 51 | + ArangoDatabaseAsync db = new ArangoDB.Builder() |
| 52 | + .loadProperties(ArangoConfigProperties.fromFile()) |
| 53 | + .protocol(cfg.protocol) |
| 54 | + .serde(TestUtils.createSerde(cfg.protocol)) |
| 55 | + .maxConnections(cfg.maxConnections) |
| 56 | + .build().async().db(); |
| 57 | + |
| 58 | + CompletableFuture<Void> longRunningTasks = CompletableFuture.allOf( |
| 59 | + IntStream.range(0, longTasksCount) |
| 60 | + .mapToObj(__ -> |
| 61 | + db.query("RETURN SLEEP(@duration)", Void.class, Map.of("duration", sleepDuration))) |
| 62 | + .toArray(CompletableFuture[]::new) |
| 63 | + ); |
| 64 | + |
| 65 | + Thread.sleep(100); |
| 66 | +<
D7AE
/span> |
| 67 | + CompletableFuture<Void> shortRunningTasks = CompletableFuture.allOf( |
| 68 | + IntStream.range(0, shortTasksCount) |
| 69 | + .mapToObj(__ -> db.query("RETURN 1", Integer.class)) |
| 70 | + .toArray(CompletableFuture[]::new) |
| 71 | + ); |
| 72 | + |
| 73 | + await() |
| 74 | + .timeout(Duration.ofSeconds(sleepDuration * sleepCycles - 1L)) |
| 75 | + .until(shortRunningTasks::isDone); |
| 76 | + |
| 77 | + await() |
| 78 | + .timeout(Duration.ofSeconds(sleepDuration * sleepCycles + 1L)) |
| 79 | + .until(longRunningTasks::isDone); |
| 80 | + |
| 81 | + shortRunningTasks.join(); |
| 82 | + longRunningTasks.join(); |
| 83 | + db.arango().shutdown(); |
| 84 | + } |
| 85 | + |
| 86 | + private record Config( |
| 87 | + Protocol protocol, |
| 88 | + int maxConnections |
| 89 | + ) { |
| 90 | + int maxStreams() { |
| 91 | + return switch (protocol) { |
| 92 | + case HTTP_JSON, HTTP_VPACK -> 1; |
| 93 | + default -> 32; |
| 94 | + }; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments