QueryLimitsHealthCheck.java 4.85 KB
/*
 * 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();
    }
}