|
13 | 13 | */ |
14 | 14 | package io.trino.gateway.ha; |
15 | 15 |
|
| 16 | +import com.google.common.base.Stopwatch; |
| 17 | +import io.airlift.json.JsonCodec; |
16 | 18 | import io.airlift.log.Logger; |
| 19 | +import io.trino.gateway.ha.clustermonitor.ClusterStats; |
| 20 | +import io.trino.gateway.ha.clustermonitor.TrinoStatus; |
17 | 21 | import okhttp3.MediaType; |
18 | 22 | import okhttp3.OkHttpClient; |
19 | 23 | import okhttp3.Request; |
20 | 24 | import okhttp3.RequestBody; |
21 | 25 | import okhttp3.Response; |
22 | | -import okhttp3.mockwebserver.Dispatcher; |
| 26 | +import okhttp3.ResponseBody; |
23 | 27 | import okhttp3.mockwebserver.MockResponse; |
24 | 28 | import okhttp3.mockwebserver.MockWebServer; |
25 | | -import okhttp3.mockwebserver.RecordedRequest; |
26 | 29 | import org.jdbi.v3.core.Handle; |
27 | 30 | import org.jdbi.v3.core.Jdbi; |
28 | 31 |
|
|
32 | 35 | import java.io.InputStream; |
33 | 36 | import java.net.URL; |
34 | 37 | import java.nio.file.Paths; |
35 | | -import java.util.Map; |
| 38 | +import java.time.Duration; |
36 | 39 | import java.util.Random; |
37 | 40 | import java.util.Scanner; |
38 | 41 |
|
39 | 42 | import static com.google.common.net.HttpHeaders.CONTENT_ENCODING; |
40 | 43 | import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8; |
| 44 | +import static java.lang.String.format; |
41 | 45 | import static java.nio.charset.StandardCharsets.UTF_8; |
42 | 46 | import static java.util.Objects.requireNonNull; |
43 | 47 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -71,23 +75,6 @@ public static void prepareMockBackend( |
71 | 75 | .setResponseCode(200)); |
72 | 76 | } |
73 | 77 |
|
74 | | - public static void setPathSpecificResponses( |
75 | | - MockWebServer backend, Map<String, String> pathResponseMap) |
76 | | - { |
77 | | - Dispatcher dispatcher = new Dispatcher() |
78 | | - { |
79 | | - @Override |
80 | | - public MockResponse dispatch(RecordedRequest request) |
81 | | - { |
82 | | - if (pathResponseMap.containsKey(request.getPath())) { |
83 | | - return new MockResponse().setResponseCode(200).setBody(pathResponseMap.get(request.getPath())); |
84 | | - } |
85 | | - return new MockResponse().setResponseCode(404); |
86 | | - } |
87 | | - }; |
88 | | - backend.setDispatcher(dispatcher); |
89 | | - } |
90 | | - |
91 | 78 | public static TestConfig buildGatewayConfigAndSeedDb(int routerPort, String configFile) |
92 | 79 | throws Exception |
93 | 80 | { |
@@ -159,6 +146,31 @@ public static void setUpBackend( |
159 | 146 | .build(); |
160 | 147 | Response response = httpClient.newCall(request).execute(); |
161 | 148 | assertThat(response.isSuccessful()).isTrue(); |
| 149 | + TrinoStatus newClusterHealthState = TrinoStatus.PENDING; |
| 150 | + Stopwatch stopwatch = Stopwatch.createStarted(); |
| 151 | + // pull cluster health states for 10 seconds |
| 152 | + // It should be enough as the healthcheck is run every second |
| 153 | + Duration timeout = Duration.ofSeconds(10); |
| 154 | + while (newClusterHealthState != TrinoStatus.HEALTHY && stopwatch.elapsed().compareTo(timeout) < 0) { |
| 155 | + // check the state of newly added cluster every second |
| 156 | + Request getBackendStateRequest = new Request.Builder() |
| 157 | + .url(format("http://localhost:%s/api/public/backends/%s/state", routerPort, name)) |
| 158 | + .get() |
| 159 | + .build(); |
| 160 | + try (Response getBackendStateResponse = httpClient.newCall(getBackendStateRequest).execute()) { |
| 161 | + if (getBackendStateResponse.isSuccessful()) { |
| 162 | + JsonCodec<ClusterStats> responseCodec = JsonCodec.jsonCodec(ClusterStats.class); |
| 163 | + ResponseBody getBackendStateResponseBody = getBackendStateResponse.body(); |
| 164 | + if (getBackendStateResponseBody != null) { |
| 165 | + ClusterStats clusterStats = responseCodec.fromJson(getBackendStateResponseBody.string()); |
| 166 | + newClusterHealthState = clusterStats.healthState(); |
| 167 | + log.debug("health state for cluster %s is %s", name, newClusterHealthState); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + Thread.sleep(1000); |
| 172 | + } |
| 173 | + assertThat(newClusterHealthState).isEqualTo(TrinoStatus.HEALTHY); |
162 | 174 | } |
163 | 175 |
|
164 | 176 | public record TestConfig(String configFilePath, String h2DbFilePath) |
|
0 commit comments