AbstractHealthCheck: cancel tasks which raised an exception To save compute resources we should cancel tasks which ended up causing an exception. We can cancel them since the healthcheck assumes they failed so there is no point in letting them continue to run. If we don't cancel tasks which failed due to a TimeoutException they may cause subsequent tasks to again fail with a TimeoutException if they don't get an execution thread since failed tasks still run and occupy threads. Change-Id: I73e15d1a1c9bd4c7eb9bfc249843ae069782557c
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java index 0234e8f..e65d269 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java +++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
@@ -88,10 +88,12 @@ checkStatusSummary = resultFuture.get(timeout, TimeUnit.MILLISECONDS); } catch (TimeoutException e) { checkStatusSummary = - handleError(ts, e, String.format("Check %s timed out", name), Result.TIMEOUT); + handleError( + resultFuture, ts, e, String.format("Check %s timed out", name), Result.TIMEOUT); } catch (InterruptedException | ExecutionException e) { checkStatusSummary = handleError( + resultFuture, ts, e, String.format("Check %s failed while waiting for its future result", name), @@ -100,7 +102,9 @@ return checkStatusSummary; } - private StatusSummary handleError(long ts, Exception e, String message, Result result) { + private StatusSummary handleError( + ListenableFuture<StatusSummary> future, long ts, Exception e, String message, Result result) { + future.cancel(true); Long elapsed = System.currentTimeMillis() - ts; log.warn(message, e); StatusSummary checkStatusSummary =