@@ -106,7 +106,7 @@ public class ExecutionService {
106
106
private final WatchExecutor executor ;
107
107
private final ExecutorService genericExecutor ;
108
108
109
- private CurrentExecutions currentExecutions ;
109
+ private AtomicReference < CurrentExecutions > currentExecutions = new AtomicReference <>() ;
110
110
private final AtomicBoolean paused = new AtomicBoolean (false );
111
111
112
112
public ExecutionService (Settings settings , HistoryStore historyStore , TriggeredWatchStore triggeredWatchStore , WatchExecutor executor ,
@@ -123,7 +123,7 @@ public ExecutionService(Settings settings, HistoryStore historyStore, TriggeredW
123
123
this .client = client ;
124
124
this .genericExecutor = genericExecutor ;
125
125
this .indexDefaultTimeout = settings .getAsTime ("xpack.watcher.internal.ops.index.default_timeout" , TimeValue .timeValueSeconds (30 ));
126
- this .currentExecutions = new CurrentExecutions ();
126
+ this .currentExecutions . set ( new CurrentExecutions () );
127
127
}
128
128
129
129
public void unPause () {
@@ -169,12 +169,12 @@ public long executionThreadPoolMaxSize() {
169
169
170
170
// for testing only
171
171
CurrentExecutions getCurrentExecutions () {
172
- return currentExecutions ;
172
+ return currentExecutions . get () ;
173
173
}
174
174
175
175
public List <WatchExecutionSnapshot > currentExecutions () {
176
176
List <WatchExecutionSnapshot > currentExecutions = new ArrayList <>();
177
- for (WatchExecution watchExecution : this .currentExecutions ) {
177
+ for (WatchExecution watchExecution : this .currentExecutions . get () ) {
178
178
currentExecutions .add (watchExecution .createSnapshot ());
179
179
}
180
180
// Lets show the longest running watch first:
@@ -279,7 +279,7 @@ public WatchRecord execute(WatchExecutionContext ctx) {
279
279
WatchRecord record = null ;
280
280
final String watchId = ctx .id ().watchId ();
281
281
try {
282
- boolean executionAlreadyExists = currentExecutions .put (watchId , new WatchExecution (ctx , Thread .currentThread ()));
282
+ boolean executionAlreadyExists = currentExecutions .get (). put (watchId , new WatchExecution (ctx , Thread .currentThread ()));
283
283
if (executionAlreadyExists ) {
284
284
logger .trace ("not executing watch [{}] because it is already queued" , watchId );
285
285
record = ctx .abortBeforeExecution (ExecutionState .NOT_EXECUTED_ALREADY_QUEUED , "Watch is already queued in thread pool" );
@@ -334,7 +334,7 @@ record = createWatchRecord(record, ctx, e);
334
334
335
335
triggeredWatchStore .delete (ctx .id ());
336
336
}
337
- currentExecutions .remove (watchId );
337
+ currentExecutions .get (). remove (watchId );
338
338
logger .debug ("finished [{}]/[{}]" , watchId , ctx .id ());
339
339
}
340
340
return record ;
@@ -578,9 +578,10 @@ public Counters executionTimes() {
578
578
* This clears out the current executions and sets new empty current executions
579
579
* This is needed, because when this method is called, watcher keeps running, so sealing executions would be a bad idea
580
580
*/
581
- private synchronized void clearExecutions () {
582
- currentExecutions .sealAndAwaitEmpty (maxStopTimeout );
583
- currentExecutions = new CurrentExecutions ();
581
+ private void clearExecutions () {
582
+ final CurrentExecutions currentExecutionsBeforeSetting = currentExecutions .getAndSet (new CurrentExecutions ());
583
+ // clear old executions in background, no need to wait
584
+ genericExecutor .execute (() -> currentExecutionsBeforeSetting .sealAndAwaitEmpty (maxStopTimeout ));
584
585
}
585
586
586
587
// the watch execution task takes another runnable as parameter
0 commit comments