2929import org .elasticsearch .threadpool .ThreadPool ;
3030import org .elasticsearch .transport .TransportService ;
3131import org .elasticsearch .xpack .core .watcher .actions .ActionWrapper ;
32- import org .elasticsearch .xpack .core .watcher .execution .WatchExecutionSnapshot ;
3332import org .elasticsearch .xpack .core .watcher .transport .actions .ack .AckWatchAction ;
3433import org .elasticsearch .xpack .core .watcher .transport .actions .ack .AckWatchRequest ;
3534import org .elasticsearch .xpack .core .watcher .transport .actions .ack .AckWatchResponse ;
35+ import org .elasticsearch .xpack .core .watcher .transport .actions .stats .WatcherStatsAction ;
36+ import org .elasticsearch .xpack .core .watcher .transport .actions .stats .WatcherStatsRequest ;
3637import org .elasticsearch .xpack .core .watcher .watch .Watch ;
3738import org .elasticsearch .xpack .core .watcher .watch .WatchField ;
38- import org .elasticsearch .xpack .watcher .execution .ExecutionService ;
3939import org .elasticsearch .xpack .watcher .transport .actions .WatcherTransportAction ;
4040import org .elasticsearch .xpack .watcher .watch .WatchParser ;
4141import org .joda .time .DateTime ;
@@ -53,85 +53,88 @@ public class TransportAckWatchAction extends WatcherTransportAction<AckWatchRequ
5353
5454 private final Clock clock ;
5555 private final WatchParser parser ;
56- private ExecutionService executionService ;
5756 private final Client client ;
5857
5958 @ Inject
6059 public TransportAckWatchAction (Settings settings , TransportService transportService , ThreadPool threadPool , ActionFilters actionFilters ,
6160 IndexNameExpressionResolver indexNameExpressionResolver , Clock clock , XPackLicenseState licenseState ,
62- WatchParser parser , ExecutionService executionService , Client client , ClusterService clusterService ) {
61+ WatchParser parser , Client client , ClusterService clusterService ) {
6362 super (settings , AckWatchAction .NAME , transportService , threadPool , actionFilters , indexNameExpressionResolver ,
6463 licenseState , clusterService , AckWatchRequest ::new , AckWatchResponse ::new );
6564 this .clock = clock ;
6665 this .parser = parser ;
67- this .executionService = executionService ;
6866 this .client = client ;
6967 }
7068
7169 @ Override
7270 protected void masterOperation (AckWatchRequest request , ClusterState state ,
7371 ActionListener <AckWatchResponse > listener ) throws Exception {
74- // if the watch to be acked is running currently, reject this request
75- List <WatchExecutionSnapshot > snapshots = executionService .currentExecutions ();
76- boolean isWatchRunning = snapshots .stream ().anyMatch (s -> s .watchId ().equals (request .getWatchId ()));
77- if (isWatchRunning ) {
78- listener .onFailure (new ElasticsearchStatusException ("watch[{}] is running currently, cannot ack until finished" ,
72+ WatcherStatsRequest watcherStatsRequest = new WatcherStatsRequest ();
73+ watcherStatsRequest .includeCurrentWatches (true );
74+
75+ executeAsyncWithOrigin (client , WATCHER_ORIGIN , WatcherStatsAction .INSTANCE , watcherStatsRequest , ActionListener .wrap (response -> {
76+ boolean isWatchRunning = response .getNodes ().stream ()
77+ .anyMatch (node -> node .getSnapshots ().stream ().anyMatch (snapshot -> snapshot .watchId ().equals (request .getWatchId ())));
78+ if (isWatchRunning ) {
79+ listener .onFailure (new ElasticsearchStatusException ("watch[{}] is running currently, cannot ack until finished" ,
7980 RestStatus .CONFLICT , request .getWatchId ()));
80- return ;
81- }
82-
83- GetRequest getRequest = new GetRequest (Watch .INDEX , Watch .DOC_TYPE , request .getWatchId ())
84- .preference (Preference .LOCAL .type ()).realtime (true );
85-
86- executeAsyncWithOrigin (client .threadPool ().getThreadContext (), WATCHER_ORIGIN , getRequest ,
87- ActionListener .<GetResponse >wrap ((response ) -> {
88- if (response .isExists () == false ) {
89- listener .onFailure (new ResourceNotFoundException ("Watch with id [{}] does not exist" , request .getWatchId ()));
90- } else {
91- DateTime now = new DateTime (clock .millis (), UTC );
92- Watch watch = parser .parseWithSecrets (request .getWatchId (), true , response .getSourceAsBytesRef (),
81+ } else {
82+ GetRequest getRequest = new GetRequest (Watch .INDEX , Watch .DOC_TYPE , request .getWatchId ())
83+ .preference (Preference .LOCAL .type ()).realtime (true );
84+
85+ executeAsyncWithOrigin (client .threadPool ().getThreadContext (), WATCHER_ORIGIN , getRequest ,
86+ ActionListener .<GetResponse >wrap (getResponse -> {
87+ if (getResponse .isExists () == false ) {
88+ listener .onFailure (new ResourceNotFoundException ("Watch with id [{}] does not exist" , request .getWatchId ()));
89+ } else {
90+ DateTime now = new DateTime (clock .millis (), UTC );
91+ Watch watch = parser .parseWithSecrets (request .getWatchId (), true , getResponse .getSourceAsBytesRef (),
9392 now , XContentType .JSON );
94- watch .version (response .getVersion ());
95- watch .status ().version (response .getVersion ());
96- String [] actionIds = request .getActionIds ();
97- if (actionIds == null || actionIds .length == 0 ) {
98- actionIds = new String []{WatchField .ALL_ACTIONS_ID };
99- }
93+ watch .version (getResponse .getVersion ());
94+ watch .status ().version (getResponse .getVersion ());
95+ String [] actionIds = request .getActionIds ();
96+ if (actionIds == null || actionIds .length == 0 ) {
97+ actionIds = new String []{WatchField .ALL_ACTIONS_ID };
98+ }
10099
101- // exit early in case nothing changes
102- boolean isChanged = watch .ack (now , actionIds );
103- if (isChanged == false ) {
104- listener .onResponse (new AckWatchResponse (watch .status ()));
105- return ;
106- }
100+ // exit early in case nothing changes
101+ boolean isChanged = watch .ack (now , actionIds );
102+ if (isChanged == false ) {
103+ listener .onResponse (new AckWatchResponse (watch .status ()));
104+ return ;
105+ }
107106
108- UpdateRequest updateRequest = new UpdateRequest (Watch .INDEX , Watch .DOC_TYPE , request .getWatchId ());
109- // this may reject this action, but prevents concurrent updates from a watch execution
110- updateRequest .version (response .getVersion ());
111- updateRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
112- XContentBuilder builder = jsonBuilder ();
113- builder .startObject ()
107+ UpdateRequest updateRequest = new UpdateRequest (Watch .INDEX , Watch .DOC_TYPE , request .getWatchId ());
108+ // this may reject this action, but prevents concurrent updates from a watch execution
109+ updateRequest .version (getResponse .getVersion ());
110+ updateRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
111+ XContentBuilder builder = jsonBuilder ();
112+ builder .startObject ()
114113 .startObject (WatchField .STATUS .getPreferredName ())
115114 .startObject ("actions" );
116115
117- List <String > actionIdsAsList = Arrays .asList (actionIds );
118- boolean updateAll = actionIdsAsList .contains ("_all" );
119- for (ActionWrapper actionWrapper : watch .actions ()) {
120- if (updateAll || actionIdsAsList .contains (actionWrapper .id ())) {
121- builder .startObject (actionWrapper .id ())
116+ List <String > actionIdsAsList = Arrays .asList (actionIds );
117+ boolean updateAll = actionIdsAsList .contains ("_all" );
118+ for (ActionWrapper actionWrapper : watch .actions ()) {
119+ if (updateAll || actionIdsAsList .contains (actionWrapper .id ())) {
120+ builder .startObject (actionWrapper .id ())
122121 .field ("ack" , watch .status ().actionStatus (actionWrapper .id ()).ackStatus (), ToXContent .EMPTY_PARAMS )
123122 .endObject ();
123+ }
124124 }
125- }
126125
127- builder .endObject ().endObject ().endObject ();
128- updateRequest .doc (builder );
126+ builder .endObject ().endObject ().endObject ();
127+ updateRequest .doc (builder );
129128
130- executeAsyncWithOrigin (client .threadPool ().getThreadContext (), WATCHER_ORIGIN , updateRequest ,
129+ executeAsyncWithOrigin (client .threadPool ().getThreadContext (), WATCHER_ORIGIN , updateRequest ,
131130 ActionListener .<UpdateResponse >wrap (
132- (updateResponse ) -> listener .onResponse (new AckWatchResponse (watch .status ())),
133- listener ::onFailure ), client ::update );
134- }
135- }, listener ::onFailure ), client ::get );
131+ (updateResponse ) -> listener .onResponse (new AckWatchResponse (watch .status ())),
132+ listener ::onFailure ), client ::update );
133+ }
134+ }, listener ::onFailure ), client ::get );
135+
136+ }
137+
138+ }, listener ::onFailure ));
136139 }
137140}
0 commit comments