-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
368 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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; | ||
|
||
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)); | ||
} | ||
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); | ||
} | ||
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 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 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) | ||
{ | ||
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 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 |
---|---|---|
|
@@ -18,5 +18,6 @@ public enum ClusterStatsMonitorType | |
NOOP, | ||
INFO_API, | ||
UI_API, | ||
JDBC | ||
JDBC, | ||
JMX | ||
} |
This file contains 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 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.