2929import org .elasticsearch .index .engine .VersionConflictEngineException ;
3030import org .elasticsearch .search .SearchService ;
3131import org .elasticsearch .search .aggregations .InternalAggregation ;
32- import org .elasticsearch .tasks .CancellableTask ;
3332import org .elasticsearch .tasks .Task ;
34- import org .elasticsearch .tasks .TaskCancelledException ;
3533import org .elasticsearch .tasks .TaskId ;
3634import org .elasticsearch .transport .TransportService ;
3735import org .elasticsearch .xpack .core .XPackPlugin ;
@@ -75,8 +73,7 @@ public TransportSubmitAsyncSearchAction(ClusterService clusterService,
7573 }
7674
7775 @ Override
78- protected void doExecute (Task task , SubmitAsyncSearchRequest request , ActionListener <AsyncSearchResponse > submitListener ) {
79- CancellableTask submitTask = (CancellableTask ) task ;
76+ protected void doExecute (Task submitTask , SubmitAsyncSearchRequest request , ActionListener <AsyncSearchResponse > submitListener ) {
8077 final SearchRequest searchRequest = createSearchRequest (request , submitTask , request .getKeepAlive ());
8178 AsyncSearchTask searchTask = (AsyncSearchTask ) taskManager .register ("transport" , SearchAction .INSTANCE .name (), searchRequest );
8279 searchAction .execute (searchTask , searchRequest , searchTask .getSearchProgressActionListener ());
@@ -88,42 +85,34 @@ public void onResponse(AsyncSearchResponse searchResponse) {
8885 // the task is still running and the user cannot wait more so we create
8986 // a document for further retrieval
9087 try {
91- if (submitTask .isCancelled ()) {
92- // the user cancelled the submit so we don't store anything
93- // and propagate the failure
94- Exception cause = new TaskCancelledException (submitTask .getReasonCancelled ());
95- onFatalFailure (searchTask , cause , searchResponse .isRunning (),
96- "submit task is cancelled" , submitListener );
97- } else {
98- final String docId = searchTask .getExecutionId ().getDocId ();
99- // creates the fallback response if the node crashes/restarts in the middle of the request
100- // TODO: store intermediate results ?
101- AsyncSearchResponse initialResp = searchResponse .clone (searchResponse .getId ());
102- store .createResponse (docId , searchTask .getOriginHeaders (), initialResp ,
103- new ActionListener <>() {
104- @ Override
105- public void onResponse (IndexResponse r ) {
106- if (searchResponse .isRunning ()) {
107- try {
108- // store the final response on completion unless the submit is cancelled
109- searchTask .addCompletionListener (finalResponse ->
110- onFinalResponse (submitTask , searchTask , finalResponse , () -> {}));
111- } finally {
112- submitListener .onResponse (searchResponse );
113- }
114- } else {
115- onFinalResponse (submitTask , searchTask , searchResponse ,
116- () -> submitListener .onResponse (searchResponse ));
88+ final String docId = searchTask .getExecutionId ().getDocId ();
89+ // creates the fallback response if the node crashes/restarts in the middle of the request
90+ // TODO: store intermediate results ?
91+ AsyncSearchResponse initialResp = searchResponse .clone (searchResponse .getId ());
92+ store .createResponse (docId , searchTask .getOriginHeaders (), initialResp ,
93+ new ActionListener <>() {
94+ @ Override
95+ public void onResponse (IndexResponse r ) {
96+ if (searchResponse .isRunning ()) {
97+ try {
98+ // store the final response on completion unless the submit is cancelled
99+ searchTask .addCompletionListener (finalResponse ->
100+ onFinalResponse (searchTask , finalResponse , () -> {
101+ }));
102+ } finally {
103+ submitListener .onResponse (searchResponse );
117104 }
105+ } else {
106+ onFinalResponse (searchTask , searchResponse , () -> submitListener .onResponse (searchResponse ));
118107 }
108+ }
119109
120- @ Override
121- public void onFailure (Exception exc ) {
122- onFatalFailure (searchTask , exc , searchResponse .isRunning (),
123- "unable to store initial response" , submitListener );
124- }
125- });
126- }
110+ @ Override
111+ public void onFailure (Exception exc ) {
112+ onFatalFailure (searchTask , exc , searchResponse .isRunning (),
113+ "unable to store initial response" , submitListener );
114+ }
115+ });
127116 } catch (Exception exc ) {
128117 onFatalFailure (searchTask , exc , searchResponse .isRunning (), "generic error" , submitListener );
129118 }
@@ -142,7 +131,7 @@ public void onFailure(Exception exc) {
142131 }, request .getWaitForCompletionTimeout ());
143132 }
144133
145- private SearchRequest createSearchRequest (SubmitAsyncSearchRequest request , CancellableTask submitTask , TimeValue keepAlive ) {
134+ private SearchRequest createSearchRequest (SubmitAsyncSearchRequest request , Task submitTask , TimeValue keepAlive ) {
146135 String docID = UUIDs .randomBase64UUID ();
147136 Map <String , String > originHeaders = nodeClient .threadPool ().getThreadContext ().getHeaders ();
148137 SearchRequest searchRequest = new SearchRequest (request .getSearchRequest ()) {
@@ -151,9 +140,8 @@ public AsyncSearchTask createTask(long id, String type, String action, TaskId pa
151140 AsyncExecutionId searchId = new AsyncExecutionId (docID , new TaskId (nodeClient .getLocalNodeId (), id ));
152141 Supplier <InternalAggregation .ReduceContext > aggReduceContextSupplier =
153142 () -> requestToAggReduceContextBuilder .apply (request .getSearchRequest ());
154- return new AsyncSearchTask (id , type , action , parentTaskId ,
155- submitTask ::isCancelled , keepAlive , originHeaders , taskHeaders , searchId , store .getClient (),
156- nodeClient .threadPool (), aggReduceContextSupplier );
143+ return new AsyncSearchTask (id , type , action , parentTaskId , keepAlive ,
144+ originHeaders , taskHeaders , searchId , store .getClient (), nodeClient .threadPool (), aggReduceContextSupplier );
157145 }
158146 };
159147 searchRequest .setParentTask (new TaskId (nodeClient .getLocalNodeId (), submitTask .getId ()));
@@ -179,11 +167,10 @@ private void onFatalFailure(AsyncSearchTask task, Exception error, boolean shoul
179167 }
180168 }
181169
182- private void onFinalResponse (CancellableTask submitTask ,
183- AsyncSearchTask searchTask ,
170+ private void onFinalResponse (AsyncSearchTask searchTask ,
184171 AsyncSearchResponse response ,
185172 Runnable nextAction ) {
186- if (submitTask . isCancelled () || searchTask .isCancelled ()) {
173+ if (searchTask .isCancelled ()) {
187174 // the task was cancelled so we ensure that there is nothing stored in the response index.
188175 store .deleteResponse (searchTask .getExecutionId (), ActionListener .wrap (
189176 resp -> unregisterTaskAndMoveOn (searchTask , nextAction ),
0 commit comments