|
| 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.fasterxml.jackson.databind.JsonNode; |
| 17 | +import io.airlift.http.client.BasicAuthRequestFilter; |
| 18 | +import io.airlift.http.client.HttpClient; |
| 19 | +import io.airlift.http.client.HttpRequestFilter; |
| 20 | +import io.airlift.http.client.JsonResponseHandler; |
| 21 | +import io.airlift.http.client.Request; |
| 22 | +import io.airlift.http.client.UnexpectedResponseException; |
| 23 | +import io.airlift.log.Logger; |
| 24 | +import io.trino.gateway.ha.config.BackendStateConfiguration; |
| 25 | +import io.trino.gateway.ha.config.ProxyBackendConfiguration; |
| 26 | + |
| 27 | +import java.net.URI; |
| 28 | + |
| 29 | +import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom; |
| 30 | +import static io.airlift.http.client.JsonResponseHandler.createJsonResponseHandler; |
| 31 | +import static io.airlift.http.client.Request.Builder.prepareGet; |
| 32 | +import static io.airlift.json.JsonCodec.jsonCodec; |
| 33 | +import static java.util.Objects.requireNonNull; |
| 34 | + |
| 35 | +public class ClusterStatsJmxMonitor |
| 36 | + implements ClusterStatsMonitor |
| 37 | +{ |
| 38 | + private static final Logger log = Logger.get(ClusterStatsJmxMonitor.class); |
| 39 | + private static final JsonResponseHandler<JsonNode> JMX_JSON_RESPONSE_HANDLER = |
| 40 | + createJsonResponseHandler(jsonCodec(JsonNode.class)); |
| 41 | + private static final String JMX_PATH = "/v1/jmx/mbean"; |
| 42 | + |
| 43 | + private final String username; |
| 44 | + private final String password; |
| 45 | + private final HttpClient client; |
| 46 | + |
| 47 | + public ClusterStatsJmxMonitor(HttpClient client, BackendStateConfiguration backendStateConfiguration) |
| 48 | + { |
| 49 | + this.client = requireNonNull(client, "client is null"); |
| 50 | + this.username = backendStateConfiguration.getUsername(); |
| 51 | + this.password = backendStateConfiguration.getPassword(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public ClusterStats monitor(ProxyBackendConfiguration backend) |
| 56 | + { |
| 57 | + log.info("Monitoring cluster stats for backend: %s", backend.getProxyTo()); |
| 58 | + ClusterStats.Builder clusterStats = ClusterStatsMonitor.getClusterStatsBuilder(backend); |
| 59 | + |
| 60 | + processJmxStats(backend, "trino.metadata:name=DiscoveryNodeManager", this::processDiscoveryNodeManagerStats, clusterStats); |
| 61 | + processJmxStats(backend, "trino.execution:name=QueryManager", this::processQueryManagerStats, clusterStats); |
| 62 | + |
| 63 | + clusterStats.proxyTo(backend.getProxyTo()) |
| 64 | + .externalUrl(backend.getExternalUrl()) |
| 65 | + .routingGroup(backend.getRoutingGroup()); |
| 66 | + |
| 67 | + ClusterStats stats = clusterStats.build(); |
| 68 | + log.debug("Completed monitoring for backend: %s. Stats: %s", backend.getProxyTo(), stats); |
| 69 | + return stats; |
| 70 | + } |
| 71 | + |
| 72 | + private void processJmxStats(ProxyBackendConfiguration backend, String mbeanName, |
| 73 | + JmxStatProcessor processor, ClusterStats.Builder clusterStats) |
| 74 | + { |
| 75 | + JsonNode response = queryJmx(backend, mbeanName); |
| 76 | + if (response != null) { |
| 77 | + processor.process(response, clusterStats); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void processDiscoveryNodeManagerStats(JsonNode response, ClusterStats.Builder clusterStats) |
| 82 | + { |
| 83 | + try { |
| 84 | + JsonNode attributes = response.get("attributes"); |
| 85 | + for (JsonNode attribute : attributes) { |
| 86 | + if ("ActiveNodeCount".equals(attribute.get("name").asText())) { |
| 87 | + int activeNodes = attribute.get("value").asInt(); |
| 88 | + TrinoStatus trinoStatus = activeNodes > 0 ? TrinoStatus.HEALTHY : TrinoStatus.UNHEALTHY; |
| 89 | + clusterStats.numWorkerNodes(activeNodes) |
| 90 | + .trinoStatus(trinoStatus); |
| 91 | + log.debug("Processed DiscoveryNodeManager: ActiveNodeCount = %d, Health = %s", |
| 92 | + activeNodes, trinoStatus); |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + catch (Exception e) { |
| 98 | + log.error(e, "Error parsing DiscoveryNodeManager stats"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void processQueryManagerStats(JsonNode response, ClusterStats.Builder clusterStats) |
| 103 | + { |
| 104 | + try { |
| 105 | + JsonNode attributes = response.get("attributes"); |
| 106 | + int queuedQueries = 0; |
| 107 | + int runningQueries = 0; |
| 108 | + for (JsonNode attribute : attributes) { |
| 109 | + String name = attribute.get("name").asText(); |
| 110 | + if ("QueuedQueries".equals(name)) { |
| 111 | + queuedQueries = attribute.get("value").asInt(); |
| 112 | + } |
| 113 | + else if ("RunningQueries".equals(name)) { |
| 114 | + runningQueries = attribute.get("value").asInt(); |
| 115 | + } |
| 116 | + } |
| 117 | + clusterStats.queuedQueryCount(queuedQueries).runningQueryCount(runningQueries); |
| 118 | + log.debug("Processed QueryManager: QueuedQueries = %d, RunningQueries = %d", queuedQueries, runningQueries); |
| 119 | + } |
| 120 | + catch (Exception e) { |
| 121 | + log.error(e, "Error parsing QueryManager stats"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private JsonNode queryJmx(ProxyBackendConfiguration backend, String mbeanName) |
| 126 | + { |
| 127 | + String jmxUrl = backend.getProxyTo(); |
| 128 | + Request request; |
| 129 | + |
| 130 | + Request preparedRequest = prepareGet() |
| 131 | + .setUri(uriBuilderFrom(URI.create(jmxUrl)) |
| 132 | + .appendPath(JMX_PATH) |
| 133 | + .appendPath(mbeanName) |
| 134 | + .build() |
| 135 | + ).addHeader("X-Trino-User", username) |
| 136 | + .build(); |
| 137 | + |
| 138 | + boolean isHttps = preparedRequest.getUri().getScheme().equalsIgnoreCase("https"); |
| 139 | + |
| 140 | + if (isHttps) { |
| 141 | + HttpRequestFilter filter = new BasicAuthRequestFilter(username, password); |
| 142 | + request = filter.filterRequest(preparedRequest); |
| 143 | + } |
| 144 | + else { |
| 145 | + request = preparedRequest; |
| 146 | + } |
| 147 | + |
| 148 | + log.debug("Querying JMX at %s for %s", request.getUri(), mbeanName); |
| 149 | + |
| 150 | + try { |
| 151 | + return client.execute(request, JMX_JSON_RESPONSE_HANDLER); |
| 152 | + } |
| 153 | + catch (UnexpectedResponseException e) { |
| 154 | + log.error(e, "Failed to fetch JMX data for %s, response code: %d", mbeanName, e.getStatusCode()); |
| 155 | + return null; |
| 156 | + } |
| 157 | + catch (Exception e) { |
| 158 | + log.error(e, "Exception while querying JMX at %s", jmxUrl); |
| 159 | + return null; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @FunctionalInterface |
| 164 | + private interface JmxStatProcessor |
| 165 | + { |
| 166 | + void process(JsonNode response, ClusterStats.Builder clusterStats); |
| 167 | + } |
| 168 | +} |
0 commit comments