Skip to content

Commit 7671694

Browse files
committed
add monitorType JMX
1 parent a11e5a4 commit 7671694

File tree

6 files changed

+198
-1
lines changed

6 files changed

+198
-1
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
}

gateway-ha/src/main/java/io/trino/gateway/ha/config/ClusterStatsMonitorType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public enum ClusterStatsMonitorType
1818
NOOP,
1919
INFO_API,
2020
UI_API,
21-
JDBC
21+
JDBC,
22+
JMX
2223
}

gateway-ha/src/main/java/io/trino/gateway/ha/module/ClusterStatsMonitorModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.trino.gateway.ha.clustermonitor.ClusterStatsHttpMonitor;
2121
import io.trino.gateway.ha.clustermonitor.ClusterStatsInfoApiMonitor;
2222
import io.trino.gateway.ha.clustermonitor.ClusterStatsJdbcMonitor;
23+
import io.trino.gateway.ha.clustermonitor.ClusterStatsJmxMonitor;
2324
import io.trino.gateway.ha.clustermonitor.ClusterStatsMonitor;
2425
import io.trino.gateway.ha.clustermonitor.ForMonitor;
2526
import io.trino.gateway.ha.clustermonitor.NoopClusterStatsMonitor;
@@ -50,6 +51,7 @@ public ClusterStatsMonitor getClusterStatsMonitor(@ForMonitor HttpClient httpCli
5051
case INFO_API -> new ClusterStatsInfoApiMonitor(httpClient, config.getMonitor());
5152
case UI_API -> new ClusterStatsHttpMonitor(config.getBackendState());
5253
case JDBC -> new ClusterStatsJdbcMonitor(config.getBackendState(), config.getMonitor());
54+
case JMX -> new ClusterStatsJmxMonitor(httpClient, config.getBackendState());
5355
case NOOP -> new NoopClusterStatsMonitor();
5456
};
5557
}

gateway-ha/src/test/java/io/trino/gateway/ha/clustermonitor/TestClusterStatsMonitor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void setUp()
4040
{
4141
trino = new TrinoContainer("trinodb/trino");
4242
trino.withCopyFileToContainer(forClasspathResource("trino-config.properties"), "/etc/trino/config.properties");
43+
trino.withCopyFileToContainer(forClasspathResource("jvm.config"), "/etc/trino/jvm.config");
4344
trino.start();
4445
}
4546

@@ -61,6 +62,12 @@ void testJdbcMonitor()
6162
testClusterStatsMonitor(backendStateConfiguration -> new ClusterStatsJdbcMonitor(backendStateConfiguration, new MonitorConfiguration()));
6263
}
6364

65+
@Test
66+
void testJmxMonitor()
67+
{
68+
testClusterStatsMonitor(backendStateConfiguration -> new ClusterStatsJmxMonitor(new JettyHttpClient(new HttpClientConfig()), backendStateConfiguration));
69+
}
70+
6471
@Test
6572
void testInfoApiMonitor()
6673
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-server
2+
-XX:InitialRAMPercentage=80
3+
-XX:MaxRAMPercentage=80
4+
-XX:G1HeapRegionSize=32M
5+
-XX:+ExplicitGCInvokesConcurrent
6+
-XX:+ExitOnOutOfMemoryError
7+
-XX:+HeapDumpOnOutOfMemoryError
8+
-XX:-OmitStackTraceInFastThrow
9+
-XX:ReservedCodeCacheSize=512M
10+
-XX:PerMethodRecompilationCutoff=10000
11+
-XX:PerBytecodeRecompilationCutoff=10000
12+
-Djdk.attach.allowAttachSelf=true
13+
-Djdk.nio.maxCachedBufferSize=2000000
14+
-Dfile.encoding=UTF-8
15+
# Allow loading dynamic agent used by JOL
16+
-XX:+EnableDynamicAgentLoading
17+
-Dcom.sun.management.jmxremote.rmi.port=9081

gateway-ha/src/test/resources/trino-config.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ catalog.management=${ENV:CATALOG_MANAGEMENT}
77

88
# Customize
99
http-server.process-forwarded=true
10+
jmx.rmiregistry.port=9080
11+
jmx.rmiserver.port=9081

0 commit comments

Comments
 (0)