Skip to content

Commit 1f61b2d

Browse files
committed
Fix test cases for PENDING health state
1 parent 79caae9 commit 1f61b2d

File tree

5 files changed

+91
-25
lines changed

5 files changed

+91
-25
lines changed

gateway-ha/src/test/java/io/trino/gateway/ha/HaGatewayTestUtils.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
*/
1414
package io.trino.gateway.ha;
1515

16+
import com.google.common.base.Stopwatch;
17+
import io.airlift.json.JsonCodec;
1618
import io.airlift.log.Logger;
19+
import io.trino.gateway.ha.clustermonitor.ClusterStats;
20+
import io.trino.gateway.ha.clustermonitor.TrinoStatus;
1721
import okhttp3.MediaType;
1822
import okhttp3.OkHttpClient;
1923
import okhttp3.Request;
2024
import okhttp3.RequestBody;
2125
import okhttp3.Response;
22-
import okhttp3.mockwebserver.Dispatcher;
26+
import okhttp3.ResponseBody;
2327
import okhttp3.mockwebserver.MockResponse;
2428
import okhttp3.mockwebserver.MockWebServer;
25-
import okhttp3.mockwebserver.RecordedRequest;
2629
import org.jdbi.v3.core.Handle;
2730
import org.jdbi.v3.core.Jdbi;
2831

@@ -32,12 +35,13 @@
3235
import java.io.InputStream;
3336
import java.net.URL;
3437
import java.nio.file.Paths;
35-
import java.util.Map;
38+
import java.time.Duration;
3639
import java.util.Random;
3740
import java.util.Scanner;
3841

3942
import static com.google.common.net.HttpHeaders.CONTENT_ENCODING;
4043
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
44+
import static java.lang.String.format;
4145
import static java.nio.charset.StandardCharsets.UTF_8;
4246
import static java.util.Objects.requireNonNull;
4347
import static org.assertj.core.api.Assertions.assertThat;
@@ -71,23 +75,6 @@ public static void prepareMockBackend(
7175
.setResponseCode(200));
7276
}
7377

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-
9178
public static TestConfig buildGatewayConfigAndSeedDb(int routerPort, String configFile)
9279
throws Exception
9380
{
@@ -159,6 +146,31 @@ public static void setUpBackend(
159146
.build();
160147
Response response = httpClient.newCall(request).execute();
161148
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);
162174
}
163175

164176
public record TestConfig(String configFilePath, String h2DbFilePath)

gateway-ha/src/test/java/io/trino/gateway/ha/TestGatewayHaMultipleBackend.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
import okhttp3.Request;
2727
import okhttp3.RequestBody;
2828
import okhttp3.Response;
29+
import okhttp3.mockwebserver.Dispatcher;
30+
import okhttp3.mockwebserver.MockResponse;
2931
import okhttp3.mockwebserver.MockWebServer;
32+
import okhttp3.mockwebserver.RecordedRequest;
3033
import org.junit.jupiter.api.AfterAll;
3134
import org.junit.jupiter.api.BeforeAll;
3235
import org.junit.jupiter.api.Test;
@@ -37,10 +40,13 @@
3740
import java.io.IOException;
3841
import java.util.Base64;
3942
import java.util.List;
43+
import java.util.Map;
4044
import java.util.Optional;
4145
import java.util.concurrent.TimeUnit;
4246

4347
import static com.google.common.collect.MoreCollectors.onlyElement;
48+
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
49+
import static com.google.common.net.MediaType.JSON_UTF_8;
4450
import static org.assertj.core.api.Assertions.assertThat;
4551
import static org.testcontainers.utility.MountableFile.forClasspathResource;
4652

@@ -84,11 +90,26 @@ void setup()
8490
int backend2Port = scheduledTrino.getMappedPort(8080);
8591

8692
HaGatewayTestUtils.prepareMockBackend(customBackend, customBackendPort, "default custom response");
87-
HaGatewayTestUtils.setPathSpecificResponses(customBackend, ImmutableMap.of(
88-
oauthInitiatePath, oauthInitialResponse,
89-
oauthCallbackPath, oauthCallbackResponse,
90-
CUSTOM_PATH, CUSTOM_RESPONSE,
91-
CUSTOM_LOGOUT, ""));
93+
customBackend.setDispatcher(new Dispatcher() {
94+
@Override
95+
public MockResponse dispatch(RecordedRequest request)
96+
{
97+
Map<String, String> pathResponse = ImmutableMap.of(
98+
oauthInitiatePath, oauthInitialResponse,
99+
oauthCallbackPath, oauthCallbackResponse,
100+
CUSTOM_PATH, CUSTOM_RESPONSE,
101+
CUSTOM_LOGOUT, "");
102+
if (pathResponse.containsKey(request.getPath())) {
103+
return new MockResponse().setResponseCode(200).setBody(pathResponse.get(request.getPath()));
104+
}
105+
if (request.getPath().equals("/v1/info")) {
106+
return new MockResponse().setResponseCode(200)
107+
.setHeader(CONTENT_TYPE, JSON_UTF_8)
108+
.setBody("{\"starting\": false}");
109+
}
110+
return new MockResponse().setResponseCode(404);
111+
}
112+
});
92113

93114
// seed database
94115
HaGatewayTestUtils.TestConfig testConfig =

gateway-ha/src/test/resources/test-config-template.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ dataStore:
1111

1212
modules:
1313
- io.trino.gateway.ha.module.HaGatewayProviderModule
14+
- io.trino.gateway.ha.module.ClusterStateListenerModule
15+
- io.trino.gateway.ha.module.ClusterStatsMonitorModule
16+
17+
managedApps:
18+
- io.trino.gateway.ha.clustermonitor.ActiveClusterMonitor
19+
20+
clusterStatsConfiguration:
21+
monitorType: INFO_API
22+
23+
monitor:
24+
taskDelaySeconds: 1
1425

1526
extraWhitelistPaths:
1627
- '/v1/custom.*'

gateway-ha/src/test/resources/test-config-with-routing-template.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ dataStore:
1010

1111
modules:
1212
- io.trino.gateway.ha.module.HaGatewayProviderModule
13+
- io.trino.gateway.ha.module.ClusterStateListenerModule
14+
- io.trino.gateway.ha.module.ClusterStatsMonitorModule
15+
16+
managedApps:
17+
- io.trino.gateway.ha.clustermonitor.ActiveClusterMonitor
18+
19+
clusterStatsConfiguration:
20+
monitorType: INFO_API
21+
22+
monitor:
23+
taskDelaySeconds: 1
1324

1425
extraWhitelistPaths:
1526
- '/v1/custom.*'

gateway-ha/src/test/resources/test-config-without-x-forwarded-template.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ dataStore:
1010

1111
modules:
1212
- io.trino.gateway.ha.module.HaGatewayProviderModule
13+
- io.trino.gateway.ha.module.ClusterStateListenerModule
14+
- io.trino.gateway.ha.module.ClusterStatsMonitorModule
15+
16+
managedApps:
17+
- io.trino.gateway.ha.clustermonitor.ActiveClusterMonitor
18+
19+
clusterStatsConfiguration:
20+
monitorType: INFO_API
21+
22+
monitor:
23+
taskDelaySeconds: 1
1324

1425
extraWhitelistPaths:
1526
- '/v1/custom.*'

0 commit comments

Comments
 (0)