Skip to content

Commit 0e6cbbd

Browse files
authored
Watcher: Ensure trigger service pauses execution (#30363)
When the watcher service pauses execution due to a cluster state update, the trigger service and its engines also need to pause properly instead of keeping going. This is also important when the .watches index is deleted, so that watches don't stay in a triggered mode.
1 parent 137ce70 commit 0e6cbbd

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherService.java

+2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ void shutDown() {
167167
void stopExecutor() {
168168
ThreadPool.terminate(executor, 10L, TimeUnit.SECONDS);
169169
}
170+
170171
/**
171172
* Reload the watcher service, does not switch the state from stopped to started, just keep going
172173
* @param state cluster state, which is needed to find out about local shards
@@ -231,6 +232,7 @@ private synchronized void reloadInner(ClusterState state, String reason, boolean
231232
* manual watch execution, i.e. via the execute watch API
232233
*/
233234
public void pauseExecution(String reason) {
235+
triggerService.pauseExecution();
234236
int cancelledTaskCount = executionService.pause();
235237
logger.info("paused watch execution, reason [{}], cancelled [{}] queued tasks", reason, cancelledTaskCount);
236238
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherServiceTests.java

+34
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@
4343
import org.elasticsearch.test.ESTestCase;
4444
import org.elasticsearch.threadpool.ThreadPool;
4545
import org.elasticsearch.xpack.core.XPackSettings;
46+
import org.elasticsearch.xpack.core.watcher.trigger.Trigger;
4647
import org.elasticsearch.xpack.core.watcher.watch.Watch;
4748
import org.elasticsearch.xpack.core.watcher.watch.WatchStatus;
49+
import org.elasticsearch.xpack.watcher.condition.InternalAlwaysCondition;
4850
import org.elasticsearch.xpack.watcher.execution.ExecutionService;
4951
import org.elasticsearch.xpack.watcher.execution.TriggeredWatchStore;
52+
import org.elasticsearch.xpack.watcher.input.none.ExecutableNoneInput;
53+
import org.elasticsearch.xpack.watcher.trigger.TriggerEngine;
5054
import org.elasticsearch.xpack.watcher.trigger.TriggerService;
5155
import org.elasticsearch.xpack.watcher.watch.WatchParser;
5256
import org.joda.time.DateTime;
@@ -204,6 +208,36 @@ void stopExecutor() {
204208
assertThat(watches, hasSize(activeWatchCount));
205209
}
206210

211+
public void testPausingWatcherServiceAlsoPausesTriggerService() {
212+
String engineType = "foo";
213+
TriggerEngine triggerEngine = mock(TriggerEngine.class);
214+
when(triggerEngine.type()).thenReturn(engineType);
215+
TriggerService triggerService = new TriggerService(Settings.EMPTY, Collections.singleton(triggerEngine));
216+
217+
Trigger trigger = mock(Trigger.class);
218+
when(trigger.type()).thenReturn(engineType);
219+
220+
Watch watch = mock(Watch.class);
221+
when(watch.trigger()).thenReturn(trigger);
222+
when(watch.condition()).thenReturn(InternalAlwaysCondition.INSTANCE);
223+
ExecutableNoneInput noneInput = new ExecutableNoneInput(logger);
224+
when(watch.input()).thenReturn(noneInput);
225+
226+
triggerService.add(watch);
227+
assertThat(triggerService.count(), is(1L));
228+
229+
WatcherService service = new WatcherService(Settings.EMPTY, triggerService, mock(TriggeredWatchStore.class),
230+
mock(ExecutionService.class), mock(WatchParser.class), mock(Client.class), executorService) {
231+
@Override
232+
void stopExecutor() {
233+
}
234+
};
235+
236+
service.pauseExecution("pausing");
237+
assertThat(triggerService.count(), is(0L));
238+
verify(triggerEngine).pauseExecution();
239+
}
240+
207241
private static DiscoveryNode newNode() {
208242
return new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(),
209243
new HashSet<>(asList(DiscoveryNode.Role.values())), Version.CURRENT);

0 commit comments

Comments
 (0)