Skip to content

Commit 2d6291b

Browse files
author
Andy Su (Apps)
committed
Fix test cases for PENDING health state
1 parent 2a00b88 commit 2d6291b

File tree

5 files changed

+85
-22
lines changed

5 files changed

+85
-22
lines changed

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

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

16+
import io.airlift.json.JsonCodec;
1617
import io.airlift.log.Logger;
18+
import io.trino.gateway.ha.clustermonitor.ClusterStats;
19+
import io.trino.gateway.ha.clustermonitor.TrinoHealthStateType;
1720
import okhttp3.MediaType;
1821
import okhttp3.OkHttpClient;
1922
import okhttp3.Request;
2023
import okhttp3.RequestBody;
2124
import okhttp3.Response;
22-
import okhttp3.mockwebserver.Dispatcher;
2325
import okhttp3.mockwebserver.MockResponse;
2426
import okhttp3.mockwebserver.MockWebServer;
25-
import okhttp3.mockwebserver.RecordedRequest;
2627
import org.jdbi.v3.core.Handle;
2728
import org.jdbi.v3.core.Jdbi;
2829

@@ -32,7 +33,6 @@
3233
import java.io.InputStream;
3334
import java.net.URL;
3435
import java.nio.file.Paths;
35-
import java.util.Map;
3636
import java.util.Random;
3737
import java.util.Scanner;
3838

@@ -71,23 +71,6 @@ public static void prepareMockBackend(
7171
.setResponseCode(200));
7272
}
7373

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-
9174
public static TestConfig buildGatewayConfigAndSeedDb(int routerPort, String configFile)
9275
throws Exception
9376
{
@@ -159,6 +142,31 @@ public static void setUpBackend(
159142
.build();
160143
Response response = httpClient.newCall(request).execute();
161144
assertThat(response.isSuccessful()).isTrue();
145+
TrinoHealthStateType newClusterHealthState = TrinoHealthStateType.PENDING;
146+
long startTime = System.currentTimeMillis();
147+
long lastExecutionTime = System.currentTimeMillis();
148+
// pull cluster health states for 10 seconds
149+
// It should be enough as the healthcheck is run every second
150+
int timeout = 10 * 1000;
151+
while (newClusterHealthState != TrinoHealthStateType.HEALTHY && (lastExecutionTime - startTime) < timeout) {
152+
// check the state of newly added cluster every second
153+
if (System.currentTimeMillis() - lastExecutionTime <= 1000) {
154+
continue;
155+
}
156+
lastExecutionTime = System.currentTimeMillis();
157+
request = new Request.Builder()
158+
.url(String.format("http://localhost:%s/api/public/backends/%s/state", routerPort, name))
159+
.get()
160+
.build();
161+
response = httpClient.newCall(request).execute();
162+
if (response.isSuccessful()) {
163+
JsonCodec<ClusterStats> responseCodec = JsonCodec.jsonCodec(ClusterStats.class);
164+
ClusterStats clusterStats = responseCodec.fromJson(response.body().string());
165+
newClusterHealthState = clusterStats.healthState();
166+
log.info("health state for trino cluster %s is %s", name, newClusterHealthState);
167+
}
168+
}
169+
assertThat(newClusterHealthState).isEqualTo(TrinoHealthStateType.HEALTHY);
162170
}
163171

164172
public record TestConfig(String configFilePath, String h2DbFilePath)

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
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;
33+
import org.jetbrains.annotations.NotNull;
3034
import org.junit.jupiter.api.AfterAll;
3135
import org.junit.jupiter.api.BeforeAll;
3236
import org.junit.jupiter.api.Test;
@@ -37,9 +41,12 @@
3741
import java.io.IOException;
3842
import java.util.Base64;
3943
import java.util.List;
44+
import java.util.Map;
4045
import java.util.Optional;
4146
import java.util.concurrent.TimeUnit;
4247

48+
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
49+
import static com.google.common.net.MediaType.JSON_UTF_8;
4350
import static org.assertj.core.api.Assertions.assertThat;
4451
import static org.testcontainers.utility.MountableFile.forClasspathResource;
4552

@@ -81,11 +88,26 @@ public void setup()
8188
int backend2Port = scheduledTrino.getMappedPort(8080);
8289

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

90112
// seed database
91113
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
@@ -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-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)