Search in sources :

Example 1 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult 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 2 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult 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 HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult 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 HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class ResultTxtVerboseSerializer method serialize.

public String serialize(final Result overallResult, final List<HealthCheckExecutionResult> executionResults, boolean includeDebug) {
    LOG.debug("Sending verbose txt response... ");
    StringBuilder resultStr = new StringBuilder();
    resultStr.append(StringUtils.repeat("-", totalWidth) + NEWLINE);
    resultStr.append(StringUtils.center("Overall Health Result: " + overallResult.getStatus().toString(), totalWidth) + NEWLINE);
    resultStr.append(StringUtils.repeat("-", totalWidth) + NEWLINE);
    resultStr.append(StringUtils.rightPad("Name", colWidthName));
    resultStr.append(StringUtils.rightPad("Result", colWidthResult));
    resultStr.append(StringUtils.rightPad("Timing", colWidthTiming));
    resultStr.append("Logs" + NEWLINE);
    resultStr.append(StringUtils.repeat("-", totalWidth) + NEWLINE);
    final DateFormat dfShort = new SimpleDateFormat("HH:mm:ss.SSS");
    for (HealthCheckExecutionResult healthCheckResult : executionResults) {
        appendVerboseTxtForResult(resultStr, healthCheckResult, includeDebug, dfShort);
    }
    resultStr.append(StringUtils.repeat("-", totalWidth) + NEWLINE);
    return resultStr.toString();
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 5 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class CompositeHealthCheckTest method testExecution.

@Test
public void testExecution() {
    doReturn((Result) null).when(compositeHealthCheck).checkForRecursion(Matchers.<ServiceReference>any(), Matchers.<Set<String>>any());
    String[] testTags = new String[] { "tag1" };
    compositeHealthCheck.setFilterTags(testTags);
    List<HealthCheckExecutionResult> executionResults = new LinkedList<HealthCheckExecutionResult>();
    executionResults.add(createExecutionResult("Check 1", testTags, new Result(Result.Status.INFO, "Good")));
    executionResults.add(createExecutionResult("Check 2", testTags, new Result(Result.Status.CRITICAL, "Bad")));
    when(healthCheckExecutor.execute(any(HealthCheckSelector.class))).thenReturn(executionResults);
    Result result = compositeHealthCheck.execute();
    verify(healthCheckExecutor, times(1)).execute(argThat(selectorWithTags(testTags)));
    assertEquals(Result.Status.CRITICAL, result.getStatus());
}
Also used : HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) LinkedList(java.util.LinkedList) 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) Test(org.junit.Test)

Aggregations

HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)34 Result (org.apache.sling.hc.api.Result)18 Test (org.junit.Test)16 Date (java.util.Date)9 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)9 HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)8 ExecutionResult (org.apache.sling.hc.core.impl.executor.ExecutionResult)7 ArrayList (java.util.ArrayList)6 TreeSet (java.util.TreeSet)6 LinkedList (java.util.LinkedList)5 Matchers.anyString (org.mockito.Matchers.anyString)5 SimpleDateFormat (java.text.SimpleDateFormat)4 DateFormat (java.text.DateFormat)3 HealthCheckSelector (org.apache.sling.hc.api.execution.HealthCheckSelector)3 ServiceReference (org.osgi.framework.ServiceReference)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 HashSet (java.util.HashSet)2 ResultLog (org.apache.sling.hc.api.ResultLog)2 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)2