25
25
import org .elasticsearch .rest .RestStatus ;
26
26
import org .elasticsearch .transport .TransportService ;
27
27
import org .elasticsearch .xpack .core .watcher .actions .ActionWrapper ;
28
- import org .elasticsearch .xpack .core .watcher .execution .WatchExecutionSnapshot ;
29
28
import org .elasticsearch .xpack .core .watcher .transport .actions .ack .AckWatchAction ;
30
29
import org .elasticsearch .xpack .core .watcher .transport .actions .ack .AckWatchRequest ;
31
30
import org .elasticsearch .xpack .core .watcher .transport .actions .ack .AckWatchResponse ;
31
+ import org .elasticsearch .xpack .core .watcher .transport .actions .stats .WatcherStatsAction ;
32
+ import org .elasticsearch .xpack .core .watcher .transport .actions .stats .WatcherStatsRequest ;
32
33
import org .elasticsearch .xpack .core .watcher .watch .Watch ;
33
34
import org .elasticsearch .xpack .core .watcher .watch .WatchField ;
34
- import org .elasticsearch .xpack .watcher .execution .ExecutionService ;
35
35
import org .elasticsearch .xpack .watcher .transport .actions .WatcherTransportAction ;
36
36
import org .elasticsearch .xpack .watcher .watch .WatchParser ;
37
37
import org .joda .time .DateTime ;
@@ -49,83 +49,86 @@ public class TransportAckWatchAction extends WatcherTransportAction<AckWatchRequ
49
49
50
50
private final Clock clock ;
51
51
private final WatchParser parser ;
52
- private ExecutionService executionService ;
53
52
private final Client client ;
54
53
55
54
@ Inject
56
55
public TransportAckWatchAction (Settings settings , TransportService transportService , ActionFilters actionFilters ,
57
- Clock clock , XPackLicenseState licenseState , WatchParser parser , ExecutionService executionService ,
56
+ Clock clock , XPackLicenseState licenseState , WatchParser parser ,
58
57
Client client ) {
59
58
super (settings , AckWatchAction .NAME , transportService , actionFilters , licenseState , AckWatchRequest ::new );
60
59
this .clock = clock ;
61
60
this .parser = parser ;
62
- this .executionService = executionService ;
63
61
this .client = client ;
64
62
}
65
63
66
64
@ Override
67
65
protected void doExecute (AckWatchRequest request , ActionListener <AckWatchResponse > listener ) {
68
- // if the watch to be acked is running currently, reject this request
69
- List <WatchExecutionSnapshot > snapshots = executionService .currentExecutions ();
70
- boolean isWatchRunning = snapshots .stream ().anyMatch (s -> s .watchId ().equals (request .getWatchId ()));
71
- if (isWatchRunning ) {
72
- listener .onFailure (new ElasticsearchStatusException ("watch[{}] is running currently, cannot ack until finished" ,
66
+ WatcherStatsRequest watcherStatsRequest = new WatcherStatsRequest ();
67
+ watcherStatsRequest .includeCurrentWatches (true );
68
+
69
+ executeAsyncWithOrigin (client , WATCHER_ORIGIN , WatcherStatsAction .INSTANCE , watcherStatsRequest , ActionListener .wrap (response -> {
70
+ boolean isWatchRunning = response .getNodes ().stream ()
71
+ .anyMatch (node -> node .getSnapshots ().stream ().anyMatch (snapshot -> snapshot .watchId ().equals (request .getWatchId ())));
72
+ if (isWatchRunning ) {
73
+ listener .onFailure (new ElasticsearchStatusException ("watch[{}] is running currently, cannot ack until finished" ,
73
74
RestStatus .CONFLICT , request .getWatchId ()));
74
- return ;
75
- }
76
-
77
- GetRequest getRequest = new GetRequest (Watch .INDEX , Watch .DOC_TYPE , request .getWatchId ())
78
- .preference (Preference .LOCAL .type ()).realtime (true );
79
-
80
- executeAsyncWithOrigin (client .threadPool ().getThreadContext (), WATCHER_ORIGIN , getRequest ,
81
- ActionListener .<GetResponse >wrap ((response ) -> {
82
- if (response .isExists () == false ) {
83
- listener .onFailure (new ResourceNotFoundException ("Watch with id [{}] does not exist" , request .getWatchId ()));
84
- } else {
85
- DateTime now = new DateTime (clock .millis (), UTC );
86
- Watch watch = parser .parseWithSecrets (request .getWatchId (), true , response .getSourceAsBytesRef (),
75
+ } else {
76
+ GetRequest getRequest = new GetRequest (Watch .INDEX , Watch .DOC_TYPE , request .getWatchId ())
77
+ .preference (Preference .LOCAL .type ()).realtime (true );
78
+
79
+ executeAsyncWithOrigin (client .threadPool ().getThreadContext (), WATCHER_ORIGIN , getRequest ,
80
+ ActionListener .<GetResponse >wrap (getResponse -> {
81
+ if (getResponse .isExists () == false ) {
82
+ listener .onFailure (new ResourceNotFoundException ("Watch with id [{}] does not exist" , request .getWatchId ()));
83
+ } else {
84
+ DateTime now = new DateTime (clock .millis (), UTC );
85
+ Watch watch = parser .parseWithSecrets (request .getWatchId (), true , getResponse .getSourceAsBytesRef (),
87
86
now , XContentType .JSON );
88
- watch .version (response .getVersion ());
89
- watch .status ().version (response .getVersion ());
90
- String [] actionIds = request .getActionIds ();
91
- if (actionIds == null || actionIds .length == 0 ) {
92
- actionIds = new String []{WatchField .ALL_ACTIONS_ID };
93
- }
87
+ watch .version (getResponse .getVersion ());
88
+ watch .status ().version (getResponse .getVersion ());
89
+ String [] actionIds = request .getActionIds ();
90
+ if (actionIds == null || actionIds .length == 0 ) {
91
+ actionIds = new String []{WatchField .ALL_ACTIONS_ID };
92
+ }
94
93
95
- // exit early in case nothing changes
96
- boolean isChanged = watch .ack (now , actionIds );
97
- if (isChanged == false ) {
98
- listener .onResponse (new AckWatchResponse (watch .status ()));
99
- return ;
100
- }
94
+ // exit early in case nothing changes
95
+ boolean isChanged = watch .ack (now , actionIds );
96
+ if (isChanged == false ) {
97
+ listener .onResponse (new AckWatchResponse (watch .status ()));
98
+ return ;
99
+ }
101
100
102
- UpdateRequest updateRequest = new UpdateRequest (Watch .INDEX , Watch .DOC_TYPE , request .getWatchId ());
103
- // this may reject this action, but prevents concurrent updates from a watch execution
104
- updateRequest .version (response .getVersion ());
105
- updateRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
106
- XContentBuilder builder = jsonBuilder ();
107
- builder .startObject ()
101
+ UpdateRequest updateRequest = new UpdateRequest (Watch .INDEX , Watch .DOC_TYPE , request .getWatchId ());
102
+ // this may reject this action, but prevents concurrent updates from a watch execution
103
+ updateRequest .version (getResponse .getVersion ());
104
+ updateRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
105
+ XContentBuilder builder = jsonBuilder ();
106
+ builder .startObject ()
108
107
.startObject (WatchField .STATUS .getPreferredName ())
109
108
.startObject ("actions" );
110
109
111
- List <String > actionIdsAsList = Arrays .asList (actionIds );
112
- boolean updateAll = actionIdsAsList .contains ("_all" );
113
- for (ActionWrapper actionWrapper : watch .actions ()) {
114
- if (updateAll || actionIdsAsList .contains (actionWrapper .id ())) {
115
- builder .startObject (actionWrapper .id ())
110
+ List <String > actionIdsAsList = Arrays .asList (actionIds );
111
+ boolean updateAll = actionIdsAsList .contains ("_all" );
112
+ for (ActionWrapper actionWrapper : watch .actions ()) {
113
+ if (updateAll || actionIdsAsList .contains (actionWrapper .id ())) {
114
+ builder .startObject (actionWrapper .id ())
116
115
.field ("ack" , watch .status ().actionStatus (actionWrapper .id ()).ackStatus (), ToXContent .EMPTY_PARAMS )
117
116
.endObject ();
117
+ }
118
118
}
119
- }
120
119
121
- builder .endObject ().endObject ().endObject ();
122
- updateRequest .doc (builder );
120
+ builder .endObject ().endObject ().endObject ();
121
+ updateRequest .doc (builder );
123
122
124
- executeAsyncWithOrigin (client .threadPool ().getThreadContext (), WATCHER_ORIGIN , updateRequest ,
123
+ executeAsyncWithOrigin (client .threadPool ().getThreadContext (), WATCHER_ORIGIN , updateRequest ,
125
124
ActionListener .<UpdateResponse >wrap (
126
- (updateResponse ) -> listener .onResponse (new AckWatchResponse (watch .status ())),
127
- listener ::onFailure ), client ::update );
128
- }
129
- }, listener ::onFailure ), client ::get );
125
+ (updateResponse ) -> listener .onResponse (new AckWatchResponse (watch .status ())),
126
+ listener ::onFailure ), client ::update );
127
+ }
128
+ }, listener ::onFailure ), client ::get );
129
+
130
+ }
131
+
132
+ }, listener ::onFailure ));
130
133
}
131
134
}
0 commit comments