use of org.apache.sling.hc.api.Result in project sling by apache.
the class ThreadUsageHealthCheck method execute.
@Override
public Result execute() {
FormattingResultLog log = new FormattingResultLog();
log.debug("Checking threads for exceeding {}% CPU time within time period of {}ms", cpuTimeThresholdWarn, samplePeriodInMs);
try {
ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
List<ThreadTimeInfo> threadTimeInfos = collectThreadTimeInfos(log, threadMxBean);
Collections.sort(threadTimeInfos);
float totalCpuTimePercentage = 0;
for (int i = 0; i < threadTimeInfos.size(); i++) {
ThreadTimeInfo threadInfo = threadTimeInfos.get(i);
float cpuTimePercentage = ((float) threadInfo.getCpuTime() / ((float) samplePeriodInMs * 1000000)) * 100f;
totalCpuTimePercentage += cpuTimePercentage;
String msg = String.format("%4.1f", cpuTimePercentage) + "% used by thread \"" + threadInfo.name + "\"";
if (i < 3 || (i < 10 && cpuTimePercentage > 15)) {
log.info(msg);
} else {
log.debug(msg);
}
}
log.info(threadTimeInfos.size() + " threads using " + String.format("%4.1f", totalCpuTimePercentage) + "% CPU Time");
boolean isHighCpuTime = totalCpuTimePercentage > cpuTimeThresholdWarn;
if (isHighCpuTime) {
log.warn("Threads are consuming significant CPU time " + (cpuTimeThresholdWarnIsConfigured ? "(more than configured " + cpuTimeThresholdWarn + "% within " + samplePeriodInMs + "ms)" : "(more than " + availableProcessors + " cores * 90% = " + cpuTimeThresholdWarn + "%)"));
}
checkForDeadlock(log, threadMxBean);
} catch (Exception e) {
LOG.error("Could not analyse thread usage " + e, e);
log.healthCheckError("Could not analyse thread usage", e);
}
return new Result(log);
}
use of org.apache.sling.hc.api.Result in project sling by apache.
the class HealthCheckExecutorServletTest method testDoGetNameAndTagInPath.
@Test
public void testDoGetNameAndTagInPath() throws ServletException, IOException {
final String testTag = "testTag";
final String testName = "test name";
doReturn(testTag + "," + testName).when(request).getPathInfo();
doReturn("false").when(request).getParameter(HealthCheckExecutorServlet.PARAM_COMBINE_TAGS_WITH_OR.name);
final List<HealthCheckExecutionResult> executionResults = getExecutionResults(Result.Status.CRITICAL);
doReturn(executionResults).when(healthCheckExecutor).execute(selector(new String[] { testTag }, new String[] { testName }), eq(new HealthCheckExecutionOptions()));
healthCheckExecutorServlet.doGet(request, response);
verify(request, never()).getParameter(HealthCheckExecutorServlet.PARAM_TAGS.name);
verify(request, never()).getParameter(HealthCheckExecutorServlet.PARAM_NAMES.name);
verifyZeroInteractions(jsonSerializer);
verifyZeroInteractions(txtSerializer);
verifyZeroInteractions(verboseTxtSerializer);
verify(htmlSerializer).serialize(resultEquals(new Result(Result.Status.CRITICAL, "Overall Status CRITICAL")), eq(executionResults), contains("Supported URL parameters"), eq(false));
}
use of org.apache.sling.hc.api.Result 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;
}
use of org.apache.sling.hc.api.Result in project sling by apache.
the class HealthCheckExecutorServletTest method testDoGetJson.
@Test
public void testDoGetJson() throws ServletException, IOException {
final String testTag = "testTag";
doReturn("true").when(request).getParameter(HealthCheckExecutorServlet.PARAM_COMBINE_TAGS_WITH_OR.name);
int timeout = 5000;
doReturn(timeout + "").when(request).getParameter(HealthCheckExecutorServlet.PARAM_OVERRIDE_GLOBAL_TIMEOUT.name);
doReturn("/" + testTag + ".json").when(request).getPathInfo();
final List<HealthCheckExecutionResult> executionResults = getExecutionResults(Result.Status.WARN);
HealthCheckExecutionOptions options = new HealthCheckExecutionOptions();
options.setCombineTagsWithOr(true);
options.setOverrideGlobalTimeout(timeout);
doReturn(executionResults).when(healthCheckExecutor).execute(selector(new String[] { testTag }, new String[0]), eq(options));
healthCheckExecutorServlet.doGet(request, response);
verifyZeroInteractions(htmlSerializer);
verifyZeroInteractions(txtSerializer);
verifyZeroInteractions(verboseTxtSerializer);
verify(jsonSerializer).serialize(resultEquals(new Result(Result.Status.WARN, "Overall Status WARN")), eq(executionResults), anyString(), eq(false));
}
use of org.apache.sling.hc.api.Result in project sling by apache.
the class ExtendedHealthCheckExecutorIT method registerHC.
private void registerHC(final String... tags) {
final HealthCheck hc = new HealthCheck() {
@Override
public Result execute() {
return new Result(testResult, "Returning " + testResult + " for " + tags[0]);
}
};
final Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put(HealthCheck.NAME, "name_" + tags[0]);
props.put(HealthCheck.TAGS, tags);
regs.add(bundleContext.registerService(HealthCheck.class.getName(), hc, props));
}
Aggregations