diff --git a/docs/quickstart-config.yaml b/docs/quickstart-config.yaml index 43182e5ff..e8254df76 100644 --- a/docs/quickstart-config.yaml +++ b/docs/quickstart-config.yaml @@ -22,11 +22,7 @@ dataStore: driver: org.postgresql.Driver clusterStatsConfiguration: - useApi: true - -backendState: - username: gateway - ssl: false + clusterStatsMonitorType: COORDINATOR_INFO modules: - io.trino.gateway.ha.module.HaGatewayProviderModule diff --git a/gateway-ha/gateway-ha-config-docker.yml b/gateway-ha/gateway-ha-config-docker.yml index 4c7e8f587..2198cbd34 100644 --- a/gateway-ha/gateway-ha-config-docker.yml +++ b/gateway-ha/gateway-ha-config-docker.yml @@ -15,12 +15,8 @@ dataStore: driver: org.postgresql.Driver queryHistoryHoursRetention: 24 -backendState: - username: lb_query - ssl: false - clusterStatsConfiguration: - useApi: true + clusterStatsMonitorType: COORDINATOR_INFO server: applicationConnectors: diff --git a/gateway-ha/gateway-ha-config.yml b/gateway-ha/gateway-ha-config.yml index d60d4ec4d..2d470a7b7 100644 --- a/gateway-ha/gateway-ha-config.yml +++ b/gateway-ha/gateway-ha-config.yml @@ -15,12 +15,8 @@ dataStore: driver: org.postgresql.Driver queryHistoryHoursRetention: 24 -backendState: - username: lb_query - ssl: false - clusterStatsConfiguration: - useApi: true + clusterStatsMonitorType: COORDINATOR_INFO server: applicationConnectors: diff --git a/gateway-ha/pom.xml b/gateway-ha/pom.xml index 18f11084e..26bf73656 100644 --- a/gateway-ha/pom.xml +++ b/gateway-ha/pom.xml @@ -223,6 +223,20 @@ metrics-healthchecks + + io.trino + trino-client + 411 + + + + com.squareup.okhttp3 + * + + + + jakarta.annotation jakarta.annotation-api diff --git a/gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/ClusterStatsCoordinatorInfoMonitor.java b/gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/ClusterStatsCoordinatorInfoMonitor.java new file mode 100644 index 000000000..896664972 --- /dev/null +++ b/gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/ClusterStatsCoordinatorInfoMonitor.java @@ -0,0 +1,74 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.trino.gateway.ha.clustermonitor; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.trino.client.ServerInfo; +import io.trino.gateway.ha.config.ProxyBackendConfiguration; +import okhttp3.Call; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.eclipse.jetty.http.HttpStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +public class ClusterStatsCoordinatorInfoMonitor + implements ClusterStatsMonitor +{ + private static OkHttpClient client = new OkHttpClient(); + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final Logger log = LoggerFactory.getLogger(ClusterStatsCoordinatorInfoMonitor.class); + + @Override + public ClusterStats monitor(ProxyBackendConfiguration backend) + { + ClusterStats.Builder clusterStats = ClusterStats.builder(backend.getName()); + + clusterStats.healthy(isReadyStatus(backend.getProxyTo())) + .proxyTo(backend.getProxyTo()) + .externalUrl(backend.getExternalUrl()) + .routingGroup(backend.getRoutingGroup()); + + return clusterStats.build(); + } + + private boolean isReadyStatus(String baseUrl) + { + String statusUrl = baseUrl + "/v1/status"; + + Request request = new Request.Builder() + .url(HttpUrl.parse(statusUrl)) + .get() + .build(); + + Call call = client.newCall(request); + + try (Response res = call.execute()) { + if (res.code() != HttpStatus.OK_200) { + return false; + } + ServerInfo info = objectMapper.readValue(res.body().string(), ServerInfo.class); + return !info.isStarting(); + } + catch (IOException e) { + log.warn("Error checking health of backend {}. Error: {}", baseUrl, e.getMessage()); + } + return false; + } +} diff --git a/gateway-ha/src/main/java/io/trino/gateway/ha/config/ClusterStatsConfiguration.java b/gateway-ha/src/main/java/io/trino/gateway/ha/config/ClusterStatsConfiguration.java index 3b84e6e5e..f3454b813 100644 --- a/gateway-ha/src/main/java/io/trino/gateway/ha/config/ClusterStatsConfiguration.java +++ b/gateway-ha/src/main/java/io/trino/gateway/ha/config/ClusterStatsConfiguration.java @@ -15,17 +15,17 @@ public class ClusterStatsConfiguration { - private boolean useApi; + private ClusterStatsMonitorType clusterStatsMonitorType; public ClusterStatsConfiguration() {} - public boolean isUseApi() + public ClusterStatsMonitorType getClusterStatsMonitorType() { - return this.useApi; + return clusterStatsMonitorType; } - public void setUseApi(boolean useApi) + public void setClusterStatsMonitorType(ClusterStatsMonitorType clusterStatsMonitorType) { - this.useApi = useApi; + this.clusterStatsMonitorType = clusterStatsMonitorType; } } diff --git a/gateway-ha/src/main/java/io/trino/gateway/ha/config/ClusterStatsMonitorType.java b/gateway-ha/src/main/java/io/trino/gateway/ha/config/ClusterStatsMonitorType.java new file mode 100644 index 000000000..1c278e561 --- /dev/null +++ b/gateway-ha/src/main/java/io/trino/gateway/ha/config/ClusterStatsMonitorType.java @@ -0,0 +1,22 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.trino.gateway.ha.config; + +public enum ClusterStatsMonitorType +{ + NOOP, + COORDINATOR_INFO, + UI_API, + JDBC +} diff --git a/gateway-ha/src/main/java/io/trino/gateway/ha/module/ClusterStatsMonitorModule.java b/gateway-ha/src/main/java/io/trino/gateway/ha/module/ClusterStatsMonitorModule.java index dd11be260..eeea5c448 100644 --- a/gateway-ha/src/main/java/io/trino/gateway/ha/module/ClusterStatsMonitorModule.java +++ b/gateway-ha/src/main/java/io/trino/gateway/ha/module/ClusterStatsMonitorModule.java @@ -17,6 +17,7 @@ import com.google.inject.Singleton; import io.dropwizard.core.setup.Environment; import io.trino.gateway.baseapp.AppModule; +import io.trino.gateway.ha.clustermonitor.ClusterStatsCoordinatorInfoMonitor; import io.trino.gateway.ha.clustermonitor.ClusterStatsHttpMonitor; import io.trino.gateway.ha.clustermonitor.ClusterStatsJdbcMonitor; import io.trino.gateway.ha.clustermonitor.ClusterStatsMonitor; @@ -41,13 +42,18 @@ public ClusterStatsMonitor getClusterStatsMonitor() { ClusterStatsConfiguration clusterStatsConfig = config.getClusterStatsConfiguration(); if (config.getBackendState() == null) { - return new NoopClusterStatsMonitor(); + return new ClusterStatsCoordinatorInfoMonitor(); } - if (clusterStatsConfig.isUseApi()) { - return new ClusterStatsHttpMonitor(config.getBackendState()); - } - else { - return new ClusterStatsJdbcMonitor(config.getBackendState()); + switch (clusterStatsConfig.getClusterStatsMonitorType()) { + case COORDINATOR_INFO: + return new ClusterStatsCoordinatorInfoMonitor(); + case UI_API: + return new ClusterStatsHttpMonitor(config.getBackendState()); + case JDBC: + return new ClusterStatsJdbcMonitor(config.getBackendState()); + case NOOP: + default: + return new NoopClusterStatsMonitor(); } } } diff --git a/gateway-ha/src/test/java/io/trino/gateway/ha/clustermonitor/TestClusterStatsMonitor.java b/gateway-ha/src/test/java/io/trino/gateway/ha/clustermonitor/TestClusterStatsMonitor.java index bfbde8f47..d944ee726 100644 --- a/gateway-ha/src/test/java/io/trino/gateway/ha/clustermonitor/TestClusterStatsMonitor.java +++ b/gateway-ha/src/test/java/io/trino/gateway/ha/clustermonitor/TestClusterStatsMonitor.java @@ -56,12 +56,21 @@ public void testJdbcMonitor() testClusterStatsMonitor(ClusterStatsJdbcMonitor::new); } + public void testCoordinatorInfoMonitor() + { + testClusterStatsMonitor(new ClusterStatsCoordinatorInfoMonitor()); + } + private void testClusterStatsMonitor(Function monitorFactory) { BackendStateConfiguration backendStateConfiguration = new BackendStateConfiguration(); backendStateConfiguration.setUsername("test_user"); ClusterStatsMonitor monitor = monitorFactory.apply(backendStateConfiguration); + testClusterStatsMonitor(monitor); + } + private void testClusterStatsMonitor(ClusterStatsMonitor monitor) + { ProxyBackendConfiguration proxyBackend = new ProxyBackendConfiguration(); proxyBackend.setProxyTo("http://localhost:" + trino.getMappedPort(8080)); proxyBackend.setName("test_cluster");