|
| 1 | +package resilience.loadbalance; |
| 2 | + |
| 3 | +import com.arangodb.ArangoDB; |
| 4 | +import com.arangodb.ArangoDBAsync; |
| 5 | +import com.arangodb.ArangoDBException; |
| 6 | +import com.arangodb.Protocol; |
| 7 | +import com.arangodb.entity.LoadBalancingStrategy; |
| 8 | +import eu.rekawek.toxiproxy.model.ToxicDirection; |
| 9 | +import eu.rekawek.toxiproxy.model.toxic.Latency; |
| 10 | +import org.junit.jupiter.api.Disabled; |
| 11 | +import org.junit.jupiter.params.ParameterizedTest; |
| 12 | +import org.junit.jupiter.params.provider.MethodSource; |
| 13 | +import resilience.ClusterTest; |
| 14 | +import resilience.Endpoint; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.Executors; |
| 19 | +import java.util.concurrent.ScheduledExecutorService; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | +import static org.assertj.core.api.Assertions.catchThrowable; |
| 25 | + |
| 26 | +public class LoadBalanceNoneClusterTest extends ClusterTest { |
| 27 | + |
| 28 | + static Stream<ArangoDB> arangoProvider() { |
| 29 | + return Stream.of( |
| 30 | + dbBuilder().loadBalancingStrategy(LoadBalancingStrategy.NONE).protocol(Protocol.VST).build(), |
| 31 | + dbBuilder().loadBalancingStrategy(LoadBalancingStrategy.NONE).protocol(Protocol.HTTP_VPACK).build(), |
| 32 | + dbBuilder().loadBalancingStrategy(LoadBalancingStrategy.NONE).protocol(Protocol.HTTP2_JSON).build() |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + static Stream<ArangoDBAsync> asyncArangoProvider() { |
| 37 | + return arangoProvider().map(ArangoDB::async); |
| 38 | + } |
| 39 | + |
| 40 | + @ParameterizedTest(name = "{index}") |
| 41 | + @MethodSource("arangoProvider") |
| 42 | + void loadBalancing(ArangoDB arangoDB) { |
| 43 | + List<Endpoint> endpoints = getEndpoints(); |
| 44 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 45 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 46 | + } |
| 47 | + |
| 48 | + @ParameterizedTest(name = "{index}") |
| 49 | + @MethodSource("asyncArangoProvider") |
| 50 | + void loadBalancingAsync(ArangoDBAsync arangoDB) { |
| 51 | + List<Endpoint> endpoints = getEndpoints(); |
| 52 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 53 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 54 | + } |
| 55 | + |
| 56 | + @ParameterizedTest(name = "{index}") |
| 57 | + @MethodSource("arangoProvider") |
| 58 | + void failover(ArangoDB arangoDB) throws IOException { |
| 59 | + List<Endpoint> endpoints = getEndpoints(); |
| 60 | + |
| 61 | + endpoints.get(0).getProxy().disable(); |
| 62 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(1).getServerId()); |
| 63 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(1).getServerId()); |
| 64 | + enableAllEndpoints(); |
| 65 | + |
| 66 | + endpoints.get(1).getProxy().disable(); |
| 67 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(2).getServerId()); |
| 68 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(2).getServerId()); |
| 69 | + enableAllEndpoints(); |
| 70 | + |
| 71 | + endpoints.get(2).getProxy().disable(); |
| 72 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 73 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 74 | + enableAllEndpoints(); |
| 75 | + } |
| 76 | + |
| 77 | + @ParameterizedTest(name = "{index}") |
| 78 | + @MethodSource("asyncArangoProvider") |
| 79 | + void failoverAsymc(ArangoDBAsync arangoDB) throws IOException { |
| 80 | + List<Endpoint> endpoints = getEndpoints(); |
| 81 | + |
| 82 | + endpoints.get(0).getProxy().disable(); |
| 83 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(1).getServerId()); |
| 84 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(1).getServerId()); |
| 85 | + enableAllEndpoints(); |
| 86 | + |
| 87 | + endpoints.get(1).getProxy().disable(); |
| 88 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(2).getServerId()); |
| 89 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(2).getServerId()); |
| 90 | + enableAllEndpoints(); |
| 91 | + |
| 92 | + endpoints.get(2).getProxy().disable(); |
| 93 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 94 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 95 | + enableAllEndpoints(); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + // FIXME: this fails for VST |
| 100 | + @Disabled |
| 101 | + @ParameterizedTest(name = "{index}") |
| 102 | + @MethodSource("arangoProvider") |
| 103 | + void retryGET(ArangoDB arangoDB) throws IOException, InterruptedException { |
| 104 | + List<Endpoint> endpoints = getEndpoints(); |
| 105 | + |
| 106 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 107 | + |
| 108 | + // slow down the driver connection |
| 109 | + Latency toxic = getEndpoints().get(0).getProxy().toxics().latency("latency", ToxicDirection.DOWNSTREAM, 10_000); |
| 110 | + Thread.sleep(100); |
| 111 | + |
| 112 | + ScheduledExecutorService es = Executors.newSingleThreadScheduledExecutor(); |
| 113 | + es.schedule(() -> getEndpoints().get(0).disable(), 300, TimeUnit.MILLISECONDS); |
| 114 | + |
| 115 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(1).getServerId()); |
| 116 | + assertThat(serverIdGET(arangoDB)).isEqualTo(endpoints.get(1).getServerId()); |
| 117 | + |
| 118 | + toxic.remove(); |
| 119 | + enableAllEndpoints(); |
| 120 | + es.shutdown(); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + @ParameterizedTest(name = "{index}") |
| 125 | + @MethodSource("arangoProvider") |
| 126 | + void retryPOST(ArangoDB arangoDB) throws IOException, InterruptedException { |
| 127 | + List<Endpoint> endpoints = getEndpoints(); |
| 128 | + |
| 129 | + assertThat(serverIdPOST(arangoDB)).isEqualTo(endpoints.get(0).getServerId()); |
| 130 | + |
| 131 | + // slow down the driver connection |
| 132 | + Latency toxic = getEndpoints().get(0).getProxy().toxics().latency("latency", ToxicDirection.DOWNSTREAM, 10_000); |
| 133 | + Thread.sleep(100); |
| 134 | + |
| 135 | + ScheduledExecutorService es = Executors.newSingleThreadScheduledExecutor(); |
| 136 | + es.schedule(() -> getEndpoints().get(0).disable(), 300, TimeUnit.MILLISECONDS); |
| 137 | + |
| 138 | + Throwable thrown = catchThrowable(() -> serverIdPOST(arangoDB)); |
| 139 | + assertThat(thrown).isInstanceOf(ArangoDBException.class); |
| 140 | + assertThat(thrown.getCause()).isInstanceOf(IOException.class); |
| 141 | + |
| 142 | + assertThat(serverIdPOST(arangoDB)).isEqualTo(endpoints.get(1).getServerId()); |
| 143 | + assertThat(serverIdPOST(arangoDB)).isEqualTo(endpoints.get(1).getServerId()); |
| 144 | + |
| 145 | + toxic.remove(); |
| 146 | + enableAllEndpoints(); |
| 147 | + es.shutdown(); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments