29
29
import org .elasticsearch .index .engine .VersionConflictEngineException ;
30
30
import org .elasticsearch .search .SearchService ;
31
31
import org .elasticsearch .search .aggregations .InternalAggregation ;
32
- import org .elasticsearch .tasks .CancellableTask ;
33
32
import org .elasticsearch .tasks .Task ;
34
- import org .elasticsearch .tasks .TaskCancelledException ;
35
33
import org .elasticsearch .tasks .TaskId ;
36
34
import org .elasticsearch .transport .TransportService ;
37
35
import org .elasticsearch .xpack .core .async .AsyncExecutionId ;
@@ -74,8 +72,7 @@ public TransportSubmitAsyncSearchAction(ClusterService clusterService,
74
72
}
75
73
76
74
@ Override
77
- protected void doExecute (Task task , SubmitAsyncSearchRequest request , ActionListener <AsyncSearchResponse > submitListener ) {
78
- CancellableTask submitTask = (CancellableTask ) task ;
75
+ protected void doExecute (Task submitTask , SubmitAsyncSearchRequest request , ActionListener <AsyncSearchResponse > submitListener ) {
79
76
final SearchRequest searchRequest = createSearchRequest (request , submitTask , request .getKeepAlive ());
80
77
AsyncSearchTask searchTask = (AsyncSearchTask ) taskManager .register ("transport" , SearchAction .INSTANCE .name (), searchRequest );
81
78
searchAction .execute (searchTask , searchRequest , searchTask .getSearchProgressActionListener ());
@@ -87,42 +84,34 @@ public void onResponse(AsyncSearchResponse searchResponse) {
87
84
// the task is still running and the user cannot wait more so we create
88
85
// a document for further retrieval
89
86
try {
90
- if (submitTask .isCancelled ()) {
91
- // the user cancelled the submit so we don't store anything
92
- // and propagate the failure
93
- Exception cause = new TaskCancelledException (submitTask .getReasonCancelled ());
94
- onFatalFailure (searchTask , cause , searchResponse .isRunning (),
95
- "submit task is cancelled" , submitListener );
96
- } else {
97
- final String docId = searchTask .getExecutionId ().getDocId ();
98
- // creates the fallback response if the node crashes/restarts in the middle of the request
99
- // TODO: store intermediate results ?
100
- AsyncSearchResponse initialResp = searchResponse .clone (searchResponse .getId ());
101
- store .storeInitialResponse (docId , searchTask .getOriginHeaders (), initialResp ,
102
- new ActionListener <IndexResponse >() {
103
- @ Override
104
- public void onResponse (IndexResponse r ) {
105
- if (searchResponse .isRunning ()) {
106
- try {
107
- // store the final response on completion unless the submit is cancelled
108
- searchTask .addCompletionListener (finalResponse ->
109
- onFinalResponse (submitTask , searchTask , finalResponse , () -> {}));
110
- } finally {
111
- submitListener .onResponse (searchResponse );
112
- }
113
- } else {
114
- onFinalResponse (submitTask , searchTask , searchResponse ,
115
- () -> submitListener .onResponse (searchResponse ));
87
+ final String docId = searchTask .getExecutionId ().getDocId ();
88
+ // creates the fallback response if the node crashes/restarts in the middle of the request
89
+ // TODO: store intermediate results ?
90
+ AsyncSearchResponse initialResp = searchResponse .clone (searchResponse .getId ());
91
+ store .storeInitialResponse (docId , searchTask .getOriginHeaders (), initialResp ,
92
+ new ActionListener <IndexResponse >() {
93
+ @ Override
94
+ public void onResponse (IndexResponse r ) {
95
+ if (searchResponse .isRunning ()) {
96
+ try {
97
+ // store the final response on completion unless the submit is cancelled
98
+ searchTask .addCompletionListener (finalResponse ->
99
+ onFinalResponse (searchTask , finalResponse , () -> {
100
+ }));
101
+ } finally {
102
+ submitListener .onResponse (searchResponse );
116
103
}
104
+ } else {
105
+ onFinalResponse (searchTask , searchResponse , () -> submitListener .onResponse (searchResponse ));
117
106
}
107
+ }
118
108
119
- @ Override
120
- public void onFailure (Exception exc ) {
121
- onFatalFailure (searchTask , exc , searchResponse .isRunning (),
122
- "unable to store initial response" , submitListener );
123
- }
124
- });
125
- }
109
+ @ Override
110
+ public void onFailure (Exception exc ) {
111
+ onFatalFailure (searchTask , exc , searchResponse .isRunning (),
112
+ "unable to store initial response" , submitListener );
113
+ }
114
+ });
126
115
} catch (Exception exc ) {
127
116
onFatalFailure (searchTask , exc , searchResponse .isRunning (), "generic error" , submitListener );
128
117
}
@@ -141,7 +130,7 @@ public void onFailure(Exception exc) {
141
130
}, request .getWaitForCompletionTimeout ());
142
131
}
143
132
144
- private SearchRequest createSearchRequest (SubmitAsyncSearchRequest request , CancellableTask submitTask , TimeValue keepAlive ) {
133
+ private SearchRequest createSearchRequest (SubmitAsyncSearchRequest request , Task submitTask , TimeValue keepAlive ) {
145
134
String docID = UUIDs .randomBase64UUID ();
146
135
Map <String , String > originHeaders = nodeClient .threadPool ().getThreadContext ().getHeaders ();
147
136
SearchRequest searchRequest = new SearchRequest (request .getSearchRequest ()) {
@@ -150,9 +139,8 @@ public AsyncSearchTask createTask(long id, String type, String action, TaskId pa
150
139
AsyncExecutionId searchId = new AsyncExecutionId (docID , new TaskId (nodeClient .getLocalNodeId (), id ));
151
140
Supplier <InternalAggregation .ReduceContext > aggReduceContextSupplier =
152
141
() -> requestToAggReduceContextBuilder .apply (request .getSearchRequest ());
153
- return new AsyncSearchTask (id , type , action , parentTaskId ,
154
- submitTask ::isCancelled , keepAlive , originHeaders , taskHeaders , searchId , store .getClient (),
155
- nodeClient .threadPool (), aggReduceContextSupplier );
142
+ return new AsyncSearchTask (id , type , action , parentTaskId , keepAlive ,
143
+ originHeaders , taskHeaders , searchId , store .getClient (), nodeClient .threadPool (), aggReduceContextSupplier );
156
144
}
157
145
};
158
146
searchRequest .setParentTask (new TaskId (nodeClient .getLocalNodeId (), submitTask .getId ()));
@@ -178,11 +166,10 @@ private void onFatalFailure(AsyncSearchTask task, Exception error, boolean shoul
178
166
}
179
167
}
180
168
181
- private void onFinalResponse (CancellableTask submitTask ,
182
- AsyncSearchTask searchTask ,
169
+ private void onFinalResponse (AsyncSearchTask searchTask ,
183
170
AsyncSearchResponse response ,
184
171
Runnable nextAction ) {
185
- if (submitTask . isCancelled () || searchTask .isCancelled ()) {
172
+ if (searchTask .isCancelled ()) {
186
173
// the task was cancelled so we ensure that there is nothing stored in the response index.
187
174
store .deleteResponse (searchTask .getExecutionId (), ActionListener .wrap (
188
175
resp -> unregisterTaskAndMoveOn (searchTask , nextAction ),
0 commit comments