Search in sources :

Example 1 with HealthCheckMetadata

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

the class AsyncHealthCheckExecutor method deactivate.

@Deactivate
protected final void deactivate(final ComponentContext componentContext) {
    this.bundleContext.removeServiceListener(this);
    this.bundleContext = null;
    LOG.debug("Unscheduling {} jobs for asynchronous health checks", registeredJobs.size());
    for (HealthCheckMetadata healthCheckDescriptor : new LinkedList<HealthCheckMetadata>(registeredJobs.keySet())) {
        unscheduleHealthCheck(healthCheckDescriptor);
    }
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) LinkedList(java.util.LinkedList) Deactivate(org.apache.felix.scr.annotations.Deactivate)

Example 2 with HealthCheckMetadata

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

the class AsyncHealthCheckExecutor method collectAsyncResults.

void collectAsyncResults(List<HealthCheckMetadata> healthCheckDescriptors, Collection<HealthCheckExecutionResult> results) {
    Iterator<HealthCheckMetadata> checksIt = healthCheckDescriptors.iterator();
    Set<ExecutionResult> asyncResults = new TreeSet<ExecutionResult>();
    while (checksIt.hasNext()) {
        HealthCheckMetadata healthCheckMetadata = checksIt.next();
        if (isAsync(healthCheckMetadata)) {
            ExecutionResult result = asyncResultsByDescriptor.get(healthCheckMetadata);
            if (result == null) {
                result = new ExecutionResult(healthCheckMetadata, new Result(Result.Status.INFO, "Async Health Check with cron expression '" + healthCheckMetadata.getAsyncCronExpression() + "' has not yet been executed."), 0L);
                asyncResults.add(result);
            }
            asyncResults.add(result);
            // remove from HC collection to not execute the check in HealthCheckExecutorImpl
            checksIt.remove();
        }
    }
    LOG.debug("Adding {} results from async results", asyncResults.size());
    results.addAll(asyncResults);
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) TreeSet(java.util.TreeSet) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 3 with HealthCheckMetadata

use of org.apache.sling.hc.util.HealthCheckMetadata 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 4 with HealthCheckMetadata

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

the class CompositeHealthCheck method checkForRecursion.

Result checkForRecursion(ServiceReference hcReference, Set<String> alreadyBannedTags) {
    HealthCheckMetadata thisCheckMetadata = new HealthCheckMetadata(hcReference);
    Set<String> bannedTagsForThisCompositeCheck = new HashSet<String>();
    bannedTagsForThisCompositeCheck.addAll(alreadyBannedTags);
    bannedTagsForThisCompositeCheck.addAll(thisCheckMetadata.getTags());
    String[] tagsForIncludedChecksArr = PropertiesUtil.toStringArray(hcReference.getProperty(PROP_FILTER_TAGS), new String[0]);
    Set<String> tagsForIncludedChecks = new HashSet<String>(Arrays.asList(tagsForIncludedChecksArr));
    log.debug("HC {} has banned tags {}", thisCheckMetadata.getName(), bannedTagsForThisCompositeCheck);
    log.debug("tagsForIncludedChecks {}", tagsForIncludedChecks);
    // is this HC ok?
    Set<String> intersection = new HashSet<String>();
    intersection.addAll(bannedTagsForThisCompositeCheck);
    intersection.retainAll(tagsForIncludedChecks);
    if (!intersection.isEmpty()) {
        return new Result(Status.HEALTH_CHECK_ERROR, "INVALID CONFIGURATION: Cycle detected in composite health check hierarchy. Health check '" + thisCheckMetadata.getName() + "' (" + hcReference.getProperty(Constants.SERVICE_ID) + ") must not have tag(s) " + intersection + " as a composite check in the hierarchy is itself already tagged alike (tags assigned to composite checks: " + bannedTagsForThisCompositeCheck + ")");
    }
    // check each sub composite check
    ServiceReference[] hcRefsOfCompositeCheck = healthCheckFilter.getHealthCheckServiceReferences(HealthCheckSelector.tags(tagsForIncludedChecksArr));
    for (ServiceReference hcRefOfCompositeCheck : hcRefsOfCompositeCheck) {
        if (CompositeHealthCheck.class.getName().equals(hcRefOfCompositeCheck.getProperty(ComponentConstants.COMPONENT_NAME))) {
            log.debug("Checking sub composite HC {}, {}", hcRefOfCompositeCheck, hcRefOfCompositeCheck.getProperty(ComponentConstants.COMPONENT_NAME));
            Result result = checkForRecursion(hcRefOfCompositeCheck, bannedTagsForThisCompositeCheck);
            if (result != null) {
                // found recursion
                return result;
            }
        }
    }
    // no recursion detected
    return null;
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) HashSet(java.util.HashSet) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with HealthCheckMetadata

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

the class HealthCheckExecutorServletTest method getExecutionResults.

private List<HealthCheckExecutionResult> getExecutionResults(Result.Status worstStatus) {
    List<HealthCheckExecutionResult> results = new ArrayList<HealthCheckExecutionResult>();
    results.add(new ExecutionResult(new HealthCheckMetadata(hcServiceRef), new Result(worstStatus, worstStatus.name()), 100));
    results.add(new ExecutionResult(new HealthCheckMetadata(hcServiceRef), new Result(Result.Status.OK, "OK"), 100));
    return results;
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) ArrayList(java.util.ArrayList) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Aggregations

HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)14 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)10 Result (org.apache.sling.hc.api.Result)8 ServiceReference (org.osgi.framework.ServiceReference)5 Date (java.util.Date)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)2 ProductInfo (com.adobe.granite.license.ProductInfo)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 List (java.util.List)1 ObjectName (javax.management.ObjectName)1 StopWatch (org.apache.commons.lang.time.StopWatch)1 Activate (org.apache.felix.scr.annotations.Activate)1 Deactivate (org.apache.felix.scr.annotations.Deactivate)1 HealthCheck (org.apache.sling.hc.api.HealthCheck)1