QueryLimitsHealthCheck.java
4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.PropertyUnbounded
* org.apache.felix.scr.annotations.Service
* org.apache.sling.hc.api.HealthCheck
* org.apache.sling.hc.api.Result
* org.apache.sling.hc.api.ResultLog
* org.apache.sling.hc.util.FormattingResultLog
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.queries.impl.hc;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.Iterator;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.OperationsException;
import javax.management.QueryExp;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.hc.api.HealthCheck;
import org.apache.sling.hc.api.Result;
import org.apache.sling.hc.api.ResultLog;
import org.apache.sling.hc.util.FormattingResultLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe Granite Query Traversal Limits Health Check", description="This health check checks if the query traversal limits are set to a value above Integer.MAX_VALUE.")
@Properties(value={@Property(name="hc.name", value={"Query Traversal Limits"}, propertyPrivate=1), @Property(name="hc.tags", unbounded=PropertyUnbounded.ARRAY, value={"queries"}, label="Tags", description="Tags for this check to be used by composite health checks."), @Property(name="hc.mbean.name", value={"queryTraversalLimitsBundle"}, propertyPrivate=1)})
@Service(value={HealthCheck.class})
public class QueryLimitsHealthCheck
implements HealthCheck {
Logger log;
public QueryLimitsHealthCheck() {
this.log = LoggerFactory.getLogger(this.getClass());
}
public Result execute() {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
FormattingResultLog resultLog = new FormattingResultLog();
try {
ObjectName mbean = this.getQueryMBean(server, "QueryEngineSettings");
if (mbean != null) {
resultLog.debug("[The in-memory query limit tells the query engine how many nodes a query may read into memory at most, for \"order by\" and \"distinct\" queries.]( )", new Object[0]);
resultLog.debug("[The query read limit tells the query engine how many nodes a query may read at most - raw read operations, including skipped nodes.]( )", new Object[0]);
resultLog.debug("[The limits can be configured via the QueryEngineSettings in the JMX console.](/system/console/jmx)", new Object[0]);
Long limit = (Long)server.getAttribute(mbean, "LimitInMemory");
if (Integer.MAX_VALUE > limit) {
resultLog.warn("The in-memory query limit ( {} ) is lower than Integer.MAX_VALUE ( {} ).", new Object[]{limit, Integer.MAX_VALUE});
} else {
resultLog.info("The in-memory query limit ( {} ) is greater than or equal to Integer.MAX_VALUE ( {} ).", new Object[]{limit, Integer.MAX_VALUE});
}
limit = (Long)server.getAttribute(mbean, "LimitReads");
if (Integer.MAX_VALUE > limit) {
resultLog.warn("The query read limit ( {} ) is lower than Integer.MAX_VALUE ( {} ).", new Object[]{limit, Integer.MAX_VALUE});
} else {
resultLog.info("The query read limit ( {} ) is greater than or equal to Integer.MAX_VALUE ( {} ).", new Object[]{limit, Integer.MAX_VALUE});
}
} else {
this.log.warn("Could not retrieve QueryEngineSettings MBean.");
resultLog.critical("Could not retrieve QueryEngineSettings MBean.", new Object[0]);
}
}
catch (Exception exc) {
this.log.warn("Error while retrieving query traversal limits: ", (Throwable)exc);
resultLog.critical("Could not retrieve query traversal limits.", new Object[0]);
}
return new Result((ResultLog)resultLog);
}
private ObjectName getQueryMBean(MBeanServerConnection server, String beanName) throws OperationsException, IOException {
Set<ObjectName> names = server.queryNames(new ObjectName("org.apache.jackrabbit.oak:type=" + beanName + ",*"), null);
if (names.isEmpty() && (names = server.queryNames(new ObjectName("org.apache.jackrabbit.oak:type=\"" + beanName + "\",*"), null)).isEmpty()) {
return null;
}
return names.iterator().next();
}
}