|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.gateway.ha.clustermonitor; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableMap; |
| 17 | +import com.google.common.collect.ImmutableSet; |
| 18 | +import io.airlift.http.client.HttpClient; |
| 19 | +import io.airlift.http.client.HttpUriBuilder; |
| 20 | +import io.airlift.http.client.Request; |
| 21 | +import io.airlift.http.client.Response; |
| 22 | +import io.airlift.http.client.ResponseHandler; |
| 23 | +import io.airlift.http.client.UnexpectedResponseException; |
| 24 | +import io.airlift.log.Logger; |
| 25 | +import io.trino.gateway.ha.config.BackendStateConfiguration; |
| 26 | +import io.trino.gateway.ha.config.MonitorConfiguration; |
| 27 | +import io.trino.gateway.ha.config.ProxyBackendConfiguration; |
| 28 | +import io.trino.gateway.ha.security.util.BasicCredentials; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.net.URI; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
| 35 | + |
| 36 | +import static com.google.common.base.Strings.isNullOrEmpty; |
| 37 | +import static com.google.common.collect.ImmutableMap.toImmutableMap; |
| 38 | +import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom; |
| 39 | +import static io.airlift.http.client.Request.Builder.prepareGet; |
| 40 | +import static io.airlift.http.client.ResponseHandlerUtils.propagate; |
| 41 | +import static io.trino.gateway.ha.clustermonitor.MonitorUtils.shouldRetry; |
| 42 | +import static java.lang.String.format; |
| 43 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 44 | +import static java.util.Objects.requireNonNull; |
| 45 | + |
| 46 | +public class ClusterStatsMetricsMonitor |
| 47 | + implements ClusterStatsMonitor |
| 48 | +{ |
| 49 | + public static final String RUNNING_QUERIES_METRIC = "trino_execution_name_QueryManager_RunningQueries"; |
| 50 | + public static final String QUEUED_QUERIES_METRIC = "trino_execution_name_QueryManager_QueuedQueries"; |
| 51 | + private static final Logger log = Logger.get(ClusterStatsMetricsMonitor.class); |
| 52 | + |
| 53 | + private final HttpClient httpClient; |
| 54 | + private final int retries; |
| 55 | + private final MetricsResponseHandler metricsResponseHandler; |
| 56 | + private final Header identityHeader; |
| 57 | + private final String metricsEndpoint; |
| 58 | + private final ImmutableSet<String> metricNames; |
| 59 | + private final Map<String, Float> metricMinimumValues; |
| 60 | + private final Map<String, Float> metricMaximumValues; |
| 61 | + |
| 62 | + public ClusterStatsMetricsMonitor(HttpClient httpClient, BackendStateConfiguration backendStateConfiguration, MonitorConfiguration monitorConfiguration) |
| 63 | + { |
| 64 | + this.httpClient = requireNonNull(httpClient, "httpClient is null"); |
| 65 | + retries = monitorConfiguration.getRetries(); |
| 66 | + if (!isNullOrEmpty(backendStateConfiguration.getPassword())) { |
| 67 | + identityHeader = new Header("Authorization", |
| 68 | + new BasicCredentials(backendStateConfiguration.getUsername(), backendStateConfiguration.getPassword()).getBasicAuthHeader()); |
| 69 | + } |
| 70 | + else { |
| 71 | + identityHeader = new Header("X-Trino-User", backendStateConfiguration.getUsername()); |
| 72 | + } |
| 73 | + metricsEndpoint = monitorConfiguration.getMetricsEndpoint(); |
| 74 | + metricMinimumValues = ImmutableMap.copyOf(monitorConfiguration.getMetricMinimumValues()); |
| 75 | + metricMaximumValues = ImmutableMap.copyOf(monitorConfiguration.getMetricMaximumValues()); |
| 76 | + metricNames = ImmutableSet.<String>builder() |
| 77 | + .add(RUNNING_QUERIES_METRIC, QUEUED_QUERIES_METRIC) |
| 78 | + .addAll(metricMinimumValues.keySet()) |
| 79 | + .addAll(metricMaximumValues.keySet()) |
| 80 | + .build(); |
| 81 | + metricsResponseHandler = new MetricsResponseHandler(metricNames); |
| 82 | + } |
| 83 | + |
| 84 | + private static ClusterStats getUnhealthyStats(ProxyBackendConfiguration backend) |
| 85 | + { |
| 86 | + return ClusterStats.builder(backend.getName()) |
| 87 | + .trinoStatus(TrinoStatus.UNHEALTHY) |
| 88 | + .proxyTo(backend.getProxyTo()) |
| 89 | + .externalUrl(backend.getExternalUrl()) |
| 90 | + .routingGroup(backend.getRoutingGroup()) |
| 91 | + .build(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public ClusterStats monitor(ProxyBackendConfiguration backend) |
| 96 | + { |
| 97 | + Map<String, String> metrics = getMetrics(backend.getProxyTo(), retries); |
| 98 | + if (metrics.isEmpty()) { |
| 99 | + log.error("No metrics available for %s!", backend.getName()); |
| 100 | + return getUnhealthyStats(backend); |
| 101 | + } |
| 102 | + |
| 103 | + for (Map.Entry<String, Float> entry : metricMinimumValues.entrySet()) { |
| 104 | + if (!metrics.containsKey(entry.getKey()) |
| 105 | + || Float.parseFloat(metrics.get(entry.getKey())) < entry.getValue()) { |
| 106 | + log.warn("Health metric value below min for cluster %s: %s=%s", backend.getName(), entry.getKey(), metrics.get(entry.getKey())); |
| 107 | + return getUnhealthyStats(backend); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + for (Map.Entry<String, Float> entry : metricMaximumValues.entrySet()) { |
| 112 | + if (!metrics.containsKey(entry.getKey()) |
| 113 | + || Float.parseFloat(metrics.get(entry.getKey())) > entry.getValue()) { |
| 114 | + log.warn("Health metric value over max for cluster %s: %s=%s", backend.getName(), entry.getKey(), metrics.get(entry.getKey())); |
| 115 | + return getUnhealthyStats(backend); |
| 116 | + } |
| 117 | + } |
| 118 | + return ClusterStats.builder(backend.getName()) |
| 119 | + .trinoStatus(TrinoStatus.HEALTHY) |
| 120 | + .runningQueryCount((int) Float.parseFloat(metrics.get(RUNNING_QUERIES_METRIC))) |
| 121 | + .queuedQueryCount((int) Float.parseFloat(metrics.get(QUEUED_QUERIES_METRIC))) |
| 122 | + .proxyTo(backend.getProxyTo()) |
| 123 | + .externalUrl(backend.getExternalUrl()) |
| 124 | + .routingGroup(backend.getRoutingGroup()) |
| 125 | + .build(); |
| 126 | + } |
| 127 | + |
| 128 | + private Map<String, String> getMetrics(String baseUrl, int retriesRemaining) |
| 129 | + { |
| 130 | + HttpUriBuilder uri = uriBuilderFrom(URI.create(baseUrl)).appendPath(metricsEndpoint); |
| 131 | + for (String metric : metricNames) { |
| 132 | + uri.addParameter("name[]", metric); |
| 133 | + } |
| 134 | + |
| 135 | + Request request = prepareGet() |
| 136 | + .setUri(uri.build()) |
| 137 | + .addHeader(identityHeader.name, identityHeader.value) |
| 138 | + .addHeader("Content-Type", "application/openmetrics-text; version=1.0.0; charset=utf-8") |
| 139 | + .build(); |
| 140 | + try { |
| 141 | + return httpClient.execute(request, metricsResponseHandler); |
| 142 | + } |
| 143 | + catch (UnexpectedResponseException e) { |
| 144 | + if (shouldRetry(e.getStatusCode())) { |
| 145 | + if (retriesRemaining > 0) { |
| 146 | + log.warn("Retrying health check on error: %s, ", e.toString()); |
| 147 | + return getMetrics(baseUrl, retriesRemaining - 1); |
| 148 | + } |
| 149 | + log.error("Encountered error %s, no retries remaining", e.toString()); |
| 150 | + } |
| 151 | + log.error(e, "Health check failed with non-retryable response.\n%s", e.toString()); |
| 152 | + } |
| 153 | + catch (Exception e) { |
| 154 | + log.error(e, "Exception checking %s for health", request.getUri()); |
| 155 | + } |
| 156 | + return ImmutableMap.of(); |
| 157 | + } |
| 158 | + |
| 159 | + private static class MetricsResponseHandler |
| 160 | + implements ResponseHandler<Map<String, String>, RuntimeException> |
| 161 | + { |
| 162 | + private final ImmutableSet<String> requiredKeys; |
| 163 | + |
| 164 | + public MetricsResponseHandler(Set<String> requiredKeys) |
| 165 | + { |
| 166 | + this.requiredKeys = ImmutableSet.copyOf(requiredKeys); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public Map<String, String> handleException(Request request, Exception exception) |
| 171 | + throws RuntimeException |
| 172 | + { |
| 173 | + throw propagate(request, exception); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public Map<String, String> handle(Request request, Response response) |
| 178 | + throws RuntimeException |
| 179 | + { |
| 180 | + try { |
| 181 | + String responseBody = new String(response.getInputStream().readAllBytes(), UTF_8); |
| 182 | + Map<String, String> metrics = Arrays.stream(responseBody.split("\n")) |
| 183 | + .filter(line -> !line.startsWith("#")) |
| 184 | + .collect(toImmutableMap(s -> s.split(" ")[0], s -> s.split(" ")[1])); |
| 185 | + if (!metrics.keySet().containsAll(requiredKeys)) { |
| 186 | + throw new UnexpectedResponseException( |
| 187 | + format("Request is missing required keys: \n%s\nin response: '%s'", String.join("\n", requiredKeys), responseBody), |
| 188 | + request, |
| 189 | + response); |
| 190 | + } |
| 191 | + return metrics; |
| 192 | + } |
| 193 | + catch (IOException e) { |
| 194 | + throw new UnexpectedResponseException(request, response); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private record Header(String name, String value) {} |
| 200 | +} |
0 commit comments