-
Notifications
You must be signed in to change notification settings - Fork 129
Add monitorType JMX #472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add monitorType JMX #472
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/ClusterStatsJmxMonitor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * 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.JsonNode; | ||
| import io.airlift.http.client.BasicAuthRequestFilter; | ||
| import io.airlift.http.client.HttpClient; | ||
| import io.airlift.http.client.HttpRequestFilter; | ||
| import io.airlift.http.client.JsonResponseHandler; | ||
| import io.airlift.http.client.Request; | ||
| import io.airlift.http.client.UnexpectedResponseException; | ||
| import io.airlift.log.Logger; | ||
| import io.trino.gateway.ha.config.BackendStateConfiguration; | ||
| import io.trino.gateway.ha.config.ProxyBackendConfiguration; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom; | ||
| import static io.airlift.http.client.JsonResponseHandler.createJsonResponseHandler; | ||
| import static io.airlift.http.client.Request.Builder.prepareGet; | ||
| import static io.airlift.json.JsonCodec.jsonCodec; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class ClusterStatsJmxMonitor | ||
| implements ClusterStatsMonitor | ||
| { | ||
| private static final Logger log = Logger.get(ClusterStatsJmxMonitor.class); | ||
| private static final JsonResponseHandler<JsonNode> JMX_JSON_RESPONSE_HANDLER = createJsonResponseHandler(jsonCodec(JsonNode.class)); | ||
| private static final String JMX_PATH = "/v1/jmx/mbean"; | ||
|
|
||
| private final String username; | ||
| private final String password; | ||
| private final HttpClient client; | ||
alaturqua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public ClusterStatsJmxMonitor(HttpClient client, BackendStateConfiguration backendStateConfiguration) | ||
| { | ||
| this.client = requireNonNull(client, "client is null"); | ||
| this.username = backendStateConfiguration.getUsername(); | ||
| this.password = backendStateConfiguration.getPassword(); | ||
| } | ||
|
|
||
| private static void updateClusterStatsFromDiscoveryNodeManagerResponse(JmxResponse response, ClusterStats.Builder clusterStats) | ||
| { | ||
| try { | ||
| response.attributes().stream() | ||
| .filter(attribute -> "ActiveNodeCount".equals(attribute.name())) | ||
| .findFirst() | ||
| .ifPresent(attribute -> { | ||
| int activeNodes = attribute.value(); | ||
| TrinoStatus trinoStatus = activeNodes > 0 ? TrinoStatus.HEALTHY : TrinoStatus.UNHEALTHY; | ||
| clusterStats.numWorkerNodes(activeNodes); | ||
| clusterStats.trinoStatus(trinoStatus); | ||
| log.debug("Processed DiscoveryNodeManager: ActiveNodeCount = %d, Health = %s", activeNodes, trinoStatus); | ||
| }); | ||
| } | ||
| catch (Exception e) { | ||
| log.error(e, "Error parsing DiscoveryNodeManager stats"); | ||
| clusterStats.trinoStatus(TrinoStatus.UNHEALTHY); | ||
| } | ||
| } | ||
|
|
||
| private static void updateClusterStatsFromQueryManagerResponse(JmxResponse response, ClusterStats.Builder clusterStats) | ||
| { | ||
| try { | ||
| Map<String, Integer> stats = response.attributes().stream() | ||
| .filter(attribute -> { | ||
| String attributeName = attribute.name(); | ||
| return "QueuedQueries".equals(attributeName) || "RunningQueries".equals(attributeName); | ||
| }) | ||
| .collect(Collectors.toMap(JmxAttribute::name, JmxAttribute::value)); | ||
|
|
||
| int queuedQueryCount = stats.getOrDefault("QueuedQueries", 0); | ||
| clusterStats.queuedQueryCount(queuedQueryCount); | ||
| int runningQueryCount = stats.getOrDefault("RunningQueries", 0); | ||
| clusterStats.runningQueryCount(runningQueryCount); | ||
|
|
||
| log.debug(String.format("Processed QueryManager: QueuedQueries = %d, RunningQueries = %d", queuedQueryCount, runningQueryCount)); | ||
alaturqua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| catch (Exception e) { | ||
| log.error(e, "Error parsing QueryManager stats"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public ClusterStats monitor(ProxyBackendConfiguration backend) | ||
| { | ||
| log.info("Monitoring cluster stats for backend: %s", backend.getProxyTo()); | ||
| ClusterStats.Builder clusterStatsBuilder = ClusterStatsMonitor.getClusterStatsBuilder(backend); | ||
|
|
||
| clusterStatsBuilder.proxyTo(backend.getProxyTo()) | ||
| .externalUrl(backend.getExternalUrl()) | ||
| .routingGroup(backend.getRoutingGroup()); | ||
|
|
||
| Optional<JmxResponse> discoveryResponse = queryJmx(backend, "trino.metadata:name=DiscoveryNodeManager"); | ||
| Optional<JmxResponse> queryResponse = queryJmx(backend, "trino.execution:name=QueryManager"); | ||
|
|
||
| if (discoveryResponse.isEmpty() || queryResponse.isEmpty()) { | ||
| clusterStatsBuilder.trinoStatus(TrinoStatus.UNHEALTHY); | ||
| return clusterStatsBuilder.build(); | ||
| } | ||
|
|
||
| discoveryResponse.ifPresent(response -> updateClusterStatsFromDiscoveryNodeManagerResponse(response, clusterStatsBuilder)); | ||
| queryResponse.ifPresent(response -> updateClusterStatsFromQueryManagerResponse(response, clusterStatsBuilder)); | ||
|
|
||
| return clusterStatsBuilder.build(); | ||
| } | ||
|
|
||
| private Optional<JmxResponse> queryJmx(ProxyBackendConfiguration backend, String mbeanName) | ||
| { | ||
| requireNonNull(backend, "backend is null"); | ||
| requireNonNull(mbeanName, "mbeanName is null"); | ||
|
|
||
| String jmxUrl = backend.getProxyTo(); | ||
| Request preparedRequest = prepareGet() | ||
| .setUri(uriBuilderFrom(URI.create(jmxUrl)) | ||
| .appendPath(JMX_PATH) | ||
| .appendPath(mbeanName) | ||
| .build()) | ||
| .addHeader("X-Trino-User", username) | ||
| .build(); | ||
|
|
||
| boolean isHttps = preparedRequest.getUri().getScheme().equalsIgnoreCase("https"); | ||
|
|
||
| if (isHttps) { | ||
| HttpRequestFilter filter = new BasicAuthRequestFilter(username, password); | ||
| preparedRequest = filter.filterRequest(preparedRequest); | ||
| } | ||
|
|
||
| log.debug("Querying JMX at %s for %s", preparedRequest.getUri(), mbeanName); | ||
|
|
||
| try { | ||
| JsonNode response = client.execute(preparedRequest, JMX_JSON_RESPONSE_HANDLER); | ||
| return Optional.ofNullable(response).map(JmxResponse::fromJson); | ||
|
Comment on lines
+146
to
+147
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
| } | ||
| catch (UnexpectedResponseException e) { | ||
| log.error(e, "Failed to fetch JMX data for %s, response code: %d", mbeanName, e.getStatusCode()); | ||
| return Optional.empty(); | ||
| } | ||
| catch (Exception e) { | ||
| log.error(e, "Exception while querying JMX at %s", jmxUrl); | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/JmxAttribute.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * 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.JsonNode; | ||
|
|
||
| public record JmxAttribute(String name, int value) | ||
| { | ||
| public static JmxAttribute fromJson(JsonNode json) | ||
| { | ||
| return new JmxAttribute( | ||
| json.get("name").asText(), | ||
| json.get("value").asInt()); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
gateway-ha/src/main/java/io/trino/gateway/ha/clustermonitor/JmxResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * 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.JsonNode; | ||
| import com.google.common.collect.ImmutableList; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.StreamSupport; | ||
|
|
||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
|
|
||
| public record JmxResponse(List<JmxAttribute> attributes) | ||
alaturqua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| public JmxResponse | ||
| { | ||
| attributes = ImmutableList.copyOf(attributes); | ||
| } | ||
|
|
||
| public static JmxResponse fromJson(JsonNode json) | ||
| { | ||
| List<JmxAttribute> attributes = StreamSupport.stream(json.get("attributes").spliterator(), false) | ||
| .map(JmxAttribute::fromJson) | ||
| .collect(toImmutableList()); | ||
| return new JmxResponse(attributes); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.