Search in sources :

Example 1 with FormattingResultLog

use of org.apache.sling.hc.util.FormattingResultLog in project sling by apache.

the class JmxAttributeHealthCheck method execute.

@Override
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();
    resultLog.debug("Checking {} / {} with constraint {}", mbeanName, attributeName, constraint);
    try {
        final MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
        final ObjectName objectName = new ObjectName(mbeanName);
        if (jmxServer.queryNames(objectName, null).size() == 0) {
            resultLog.warn("MBean not found: {}", objectName);
        } else {
            final Object value = jmxServer.getAttribute(objectName, attributeName);
            resultLog.debug("{} {} returns {}", mbeanName, attributeName, value);
            new SimpleConstraintChecker().check(value, constraint, resultLog);
        }
    } catch (final Exception e) {
        log.warn("JMX attribute {}/{} check failed: {}", new Object[] { mbeanName, attributeName, e });
        resultLog.healthCheckError("JMX attribute check failed: {}", e);
    }
    return new Result(resultLog);
}
Also used : SimpleConstraintChecker(org.apache.sling.hc.util.SimpleConstraintChecker) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Result(org.apache.sling.hc.api.Result)

Example 2 with FormattingResultLog

use of org.apache.sling.hc.util.FormattingResultLog in project sling by apache.

the class HealthCheckExecutorImpl method collectResultFromFuture.

/**
     * Collect the result from a single future
     * @param future The future
     * @return The execution result or a result for a reached timeout
     */
HealthCheckExecutionResult collectResultFromFuture(final HealthCheckFuture future) {
    HealthCheckExecutionResult result;
    HealthCheckMetadata hcMetadata = future.getHealthCheckMetadata();
    if (future.isDone()) {
        logger.debug("Health Check is done: {}", hcMetadata);
        try {
            result = future.get();
        } catch (final Exception e) {
            logger.warn("Unexpected Exception during future.get(): " + e, e);
            long futureElapsedTimeMs = new Date().getTime() - future.getCreatedTime().getTime();
            result = new ExecutionResult(hcMetadata, Result.Status.HEALTH_CHECK_ERROR, "Unexpected Exception during future.get(): " + e, futureElapsedTimeMs, false);
        }
    } else {
        logger.debug("Health Check timed out: {}", hcMetadata);
        // Futures must not be cancelled as interrupting a health check might leave the system in invalid state 
        // (worst case could be a corrupted repository index if using write operations)
        // normally we turn the check into WARN (normal timeout), but if the threshold time for CRITICAL is reached for a certain
        // future we turn the result CRITICAL
        long futureElapsedTimeMs = new Date().getTime() - future.getCreatedTime().getTime();
        FormattingResultLog resultLog = new FormattingResultLog();
        if (futureElapsedTimeMs < this.longRunningFutureThresholdForRedMs) {
            resultLog.warn("Timeout: Check still running after " + msHumanReadable(futureElapsedTimeMs));
        } else {
            resultLog.critical("Timeout: Check still running after " + msHumanReadable(futureElapsedTimeMs) + " (exceeding the configured threshold for CRITICAL: " + msHumanReadable(this.longRunningFutureThresholdForRedMs) + ")");
        }
        // add logs from previous, cached result if exists (using a 1 year TTL)
        HealthCheckExecutionResult lastCachedResult = healthCheckResultCache.getValidCacheResult(hcMetadata, 1000 * 60 * 60 * 24 * 365);
        if (lastCachedResult != null) {
            DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
            resultLog.info("*** Result log of last execution finished at {} after {} ***", df.format(lastCachedResult.getFinishedAt()), FormattingResultLog.msHumanReadable(lastCachedResult.getElapsedTimeInMs()));
            for (ResultLog.Entry entry : lastCachedResult.getHealthCheckResult()) {
                resultLog.add(entry);
            }
        }
        result = new ExecutionResult(hcMetadata, new Result(resultLog), futureElapsedTimeMs, true);
    }
    return result;
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) ResultLog(org.apache.sling.hc.api.ResultLog) SimpleDateFormat(java.text.SimpleDateFormat) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Date(java.util.Date) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 3 with FormattingResultLog

use of org.apache.sling.hc.util.FormattingResultLog in project sling by apache.

the class CompositeHealthCheck method execute.

@Override
public Result execute() {
    final ComponentContext localCtx = this.componentContext;
    final ServiceReference referenceToThis = localCtx == null ? null : localCtx.getServiceReference();
    Result result = referenceToThis == null ? null : checkForRecursion(referenceToThis, new HashSet<String>());
    if (result != null) {
        // return recursion error
        return result;
    }
    FormattingResultLog resultLog = new FormattingResultLog();
    List<HealthCheckExecutionResult> executionResults = healthCheckExecutor.execute(HealthCheckSelector.tags(filterTags));
    resultLog.debug("Executing {} HealthChecks selected by tags {}", executionResults.size(), Arrays.asList(filterTags));
    result = new CompositeResult(resultLog, executionResults);
    return result;
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ServiceReference(org.osgi.framework.ServiceReference) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) HashSet(java.util.HashSet)

Example 4 with FormattingResultLog

use of org.apache.sling.hc.util.FormattingResultLog in project sling by apache.

the class AnnotatedHealthCheckSample method execute.

@Override
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();
    resultLog.info("All good at {}", new Date());
    return new Result(resultLog);
}
Also used : FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Date(java.util.Date) Result(org.apache.sling.hc.api.Result)

Example 5 with FormattingResultLog

use of org.apache.sling.hc.util.FormattingResultLog in project sling by apache.

the class SlowHealthCheckSample method execute.

@Override
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();
    try {
        final long randomDelay = (long) (Math.random() * (maxExecutionTime - minExecutionTime));
        final long toSleep = minExecutionTime + randomDelay;
        resultLog.debug("Executing {} will last {} msec", this, toSleep);
        Thread.sleep(toSleep);
    } catch (InterruptedException iex) {
        resultLog.warn("{} during execution", iex.getClass().getSimpleName());
    }
    final String execMsg = "Done executing, execution counter=" + counter.incrementAndGet();
    resultLog.debug("{}:{}", this, execMsg);
    log.debug("{}:{}", this, execMsg);
    return new Result(resultLog);
}
Also used : FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Result(org.apache.sling.hc.api.Result)

Aggregations

FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)17 Result (org.apache.sling.hc.api.Result)15 Date (java.util.Date)3 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)2 Test (org.junit.Test)2 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Credentials (javax.jcr.Credentials)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1