Skip to content

Commit 6bf8f6d

Browse files
committed
Fix test cases for PENDING health state
1 parent 2202ea3 commit 6bf8f6d

File tree

5 files changed

+82
-22
lines changed

5 files changed

+82
-22
lines changed

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

Lines changed: 26 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,29 @@ 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+
// pull cluster health states for 10 seconds
148+
// It should be enough as the healthcheck is run every second
149+
int timeout = 10 * 1000;
150+
while (newClusterHealthState != TrinoHealthStateType.HEALTHY && (System.currentTimeMillis() - startTime) < timeout) {
151+
// check the state of newly added cluster every second
152+
request = new Request.Builder()
153+
.url(String.format("http://localhost:%s/api/public/backends/%s/state", routerPort, name))
154+
.get()
155+
.build();
156+
response = httpClient.newCall(request).execute();
157+
if (response.isSuccessful()) {
158+
JsonCodec<ClusterStats> responseCodec = JsonCodec.jsonCodec(ClusterStats.class);
159+
ClusterStats clusterStats = responseCodec.fromJson(response.body().string());
160+
newClusterHealthState = clusterStats.healthState();
161+
log.info("health state for trino cluster %s is %s", name, newClusterHealthState);
162+
}
163+
else {
164+
Thread.sleep(1000);
165+
}
166+
}
167+
assertThat(newClusterHealthState).isEqualTo(TrinoHealthStateType.HEALTHY);
162168
}
163169

164170
public record TestConfig(String configFilePath, String h2DbFilePath)

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

Lines changed: 23 additions & 2 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(
93+
Map<String, String> pathResponseMap = ImmutableMap.of(
8894
oauthInitiatePath, oauthInitialResponse,
8995
oauthCallbackPath, oauthCallbackResponse,
9096
CUSTOM_PATH, CUSTOM_RESPONSE,
91-
CUSTOM_LOGOUT, ""));
97+
CUSTOM_LOGOUT, "");
98+
customBackend.setDispatcher(new Dispatcher() {
99+
@Override
100+
public MockResponse dispatch(RecordedRequest request)
101+
{
102+
if (pathResponseMap.containsKey(request.getPath())) {
103+
return new MockResponse().setResponseCode(200).setBody(pathResponseMap.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)