Source:
private void forceCloseJob(ClusterState currentState, CloseJobAction.Request request, List<String> jobIdsToForceClose, ActionListener<CloseJobAction.Response> listener) { PersistentTasksCustomMetadata tasks = currentState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); final int numberOfJobs = jobIdsToForceClose.size(); final AtomicInteger counter = new AtomicInteger(); final AtomicArray<Exception> failures = new AtomicArray<>(numberOfJobs); for (String jobId : jobIdsToForceClose) { PersistentTasksCustomMetadata.PersistentTask<?> jobTask = MlTasks.getJobTask(jobId, tasks); if (jobTask != null) { auditor.info(jobId, Messages.JOB_AUDIT_FORCE_CLOSING); persistentTasksService.sendRemoveRequest(jobTask.getId(), new ActionListener<PersistentTasksCustomMetadata.PersistentTask<?>>() { @Override public void onResponse(PersistentTasksCustomMetadata.PersistentTask<?> task) { if (counter.incrementAndGet() == numberOfJobs) { sendResponseOrFailure(request.getJobId(), listener, failures); } } @Override public void onFailure(Exception e) { final int slot = counter.incrementAndGet(); if ((ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException && Strings.isAllOrWildcard(new String[]{request.getJobId()})) == false) { failures.set(slot - 1, e); } if (slot == numberOfJobs) { sendResponseOrFailure(request.getJobId(), listener, failures); } } private void sendResponseOrFailure(String jobId, ActionListener<CloseJobAction.Response> listener, AtomicArray<Exception> failures) { List<Exception> caughtExceptions = failures.asList(); if (caughtExceptions.size() == 0) { closeRunningForecasts(jobId, listener); return; } String msg = "Failed to force close job [" + jobId + "] with [" + caughtExceptions.size() + "] failures, rethrowing last, all Exceptions: [" + caughtExceptions.stream().map(Exception::getMessage) .collect(Collectors.joining(", ")) + "]"; ElasticsearchException e = new ElasticsearchException(msg, caughtExceptions.get(0)); listener.onFailure(e); } }); } } }

Target:
private void forceCloseJob(ClusterState currentState, CloseJobAction.Request request, List<String> jobIdsToForceClose, ActionListener<CloseJobAction.Response> listener) { PersistentTasksCustomMetadata tasks = currentState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); final int numberOfJobs = jobIdsToForceClose.size(); final AtomicInteger counter = new AtomicInteger(); final AtomicArray<Exception> failures = new AtomicArray<>(numberOfJobs); for (String jobId : jobIdsToForceClose) { PersistentTasksCustomMetadata.PersistentTask<?> jobTask = MlTasks.getJobTask(jobId, tasks); if (jobTask != null) { auditor.info(jobId, Messages.JOB_AUDIT_FORCE_CLOSING); persistentTasksService.sendRemoveRequest(jobTask.getId(), new ActionListener<PersistentTasksCustomMetadata.PersistentTask<?>>() { @Override public void onResponse(PersistentTasksCustomMetadata.PersistentTask<?> task) { if (counter.incrementAndGet() == numberOfJobs) { sendResponseOrFailure(request.getJobId(), listener, failures); } } @Override public void onFailure(Exception e) { final int slot = counter.incrementAndGet(); if ((ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException && Strings.isAllOrWildcard(new String[]{request.getJobId()})) == false) { failures.set(slot - 1, e); } if (slot == numberOfJobs) { sendResponseOrFailure(request.getJobId(), listener, failures); } } private void sendResponseOrFailure(String jobId, ActionListener<CloseJobAction.Response> listener, AtomicArray<Exception> failures) { List<Exception> caughtExceptions = failures.asList(); if (caughtExceptions.size() == 0) { listener.onResponse(new CloseJobAction.Response(true)); return; } String msg = "Failed to force close job [" + jobId + "] with [" + caughtExceptions.size() + "] failures, rethrowing last, all Exceptions: [" + caughtExceptions.stream().map(Exception::getMessage) .collect(Collectors.joining(", ")) + "]"; ElasticsearchException e = new ElasticsearchException(msg, caughtExceptions.get(0)); listener.onFailure(e); } }); } } }