From 6da23d412adec545edf170ad9e93b69a547da759 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 27 Nov 2014 13:36:09 +0100 Subject: [PATCH] When stopping alert manager wait for ongoing operations to complete. Original commit: elastic/x-pack-elasticsearch@646e53462853995e7de002f05dc6bfc3576ad733 --- .../org/elasticsearch/alerts/AlertManager.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/org/elasticsearch/alerts/AlertManager.java b/src/main/java/org/elasticsearch/alerts/AlertManager.java index 39b837a4c5a..cffa856f91e 100644 --- a/src/main/java/org/elasticsearch/alerts/AlertManager.java +++ b/src/main/java/org/elasticsearch/alerts/AlertManager.java @@ -212,6 +212,20 @@ public class AlertManager extends AbstractComponent { private void internalStop() { if (state.compareAndSet(State.STARTED, State.STOPPING)) { + while (true) { + // It can happen we have still ongoing operations and we wait those operations to finish to avoid + // that AlertManager or any of its components end up in a illegal state after the state as been set to stopped. + // + // For example: An alert action entry may be added while we stopping alerting if we don't wait for + // ongoing operations to complete. Resulting in once the alert manager starts again that more than + // expected alert action entries are processed. + // + // Note: new operations will fail now because the state has been set to: stopping + if (!alertLock.hasLockedKeys()) { + break; + } + } + logger.info("Stopping alert manager..."); actionManager.stop(); scheduler.stop();