1111import org .apache .logging .log4j .Logger ;
1212import org .elasticsearch .ElasticsearchException ;
1313import org .elasticsearch .action .ActionListener ;
14+ import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
15+ import org .elasticsearch .action .admin .indices .delete .TransportDeleteIndexAction ;
1416import org .elasticsearch .action .admin .indices .rollover .RolloverAction ;
1517import org .elasticsearch .action .admin .indices .rollover .RolloverRequest ;
1618import org .elasticsearch .action .datastreams .GetDataStreamAction ;
1719import org .elasticsearch .action .datastreams .ModifyDataStreamsAction ;
1820import org .elasticsearch .action .support .CountDownActionListener ;
21+ import org .elasticsearch .action .support .SubscribableListener ;
1922import org .elasticsearch .action .support .master .AcknowledgedResponse ;
2023import org .elasticsearch .client .internal .Client ;
2124import org .elasticsearch .cluster .metadata .DataStream ;
@@ -210,26 +213,29 @@ private void maybeProcessNextIndex(
210213 reindexDataStreamTask .incrementInProgressIndicesCount (index .getName ());
211214 ReindexDataStreamIndexAction .Request reindexDataStreamIndexRequest = new ReindexDataStreamIndexAction .Request (index .getName ());
212215 reindexDataStreamIndexRequest .setParentTask (parentTaskId );
213- reindexClient .execute (ReindexDataStreamIndexAction .INSTANCE , reindexDataStreamIndexRequest , ActionListener .wrap (response1 -> {
214- updateDataStream (sourceDataStream , index .getName (), response1 .getDestIndex (), ActionListener .wrap (unused -> {
216+
217+ SubscribableListener .<ReindexDataStreamIndexAction .Response >newForked (
218+ l -> reindexClient .execute (ReindexDataStreamIndexAction .INSTANCE , reindexDataStreamIndexRequest , l )
219+ )
220+ .<AcknowledgedResponse >andThen (
221+ (l , result ) -> updateDataStream (sourceDataStream , index .getName (), result .getDestIndex (), l , reindexClient , parentTaskId )
222+ )
223+ .<AcknowledgedResponse >andThen (l -> deleteIndex (index .getName (), reindexClient , parentTaskId , l ))
224+ .addListener (ActionListener .wrap (unused -> {
215225 reindexDataStreamTask .reindexSucceeded (index .getName ());
216226 listener .onResponse (null );
217227 maybeProcessNextIndex (indicesRemaining , reindexDataStreamTask , reindexClient , sourceDataStream , listener , parentTaskId );
218- }, exception -> {
219- reindexDataStreamTask .reindexFailed (index .getName (), exception );
228+ }, e -> {
229+ reindexDataStreamTask .reindexFailed (index .getName (), e );
220230 listener .onResponse (null );
221- }), reindexClient , parentTaskId );
222- }, exception -> {
223- reindexDataStreamTask .reindexFailed (index .getName (), exception );
224- listener .onResponse (null );
225- }));
231+ }));
226232 }
227233
228234 private void updateDataStream (
229235 String dataStream ,
230236 String oldIndex ,
231237 String newIndex ,
232- ActionListener <Void > listener ,
238+ ActionListener <AcknowledgedResponse > listener ,
233239 ExecuteWithHeadersClient reindexClient ,
234240 TaskId parentTaskId
235241 ) {
@@ -239,17 +245,18 @@ private void updateDataStream(
239245 List .of (DataStreamAction .removeBackingIndex (dataStream , oldIndex ), DataStreamAction .addBackingIndex (dataStream , newIndex ))
240246 );
241247 modifyDataStreamRequest .setParentTask (parentTaskId );
242- reindexClient .execute (ModifyDataStreamsAction .INSTANCE , modifyDataStreamRequest , new ActionListener <>() {
243- @ Override
244- public void onResponse (AcknowledgedResponse response ) {
245- listener .onResponse (null );
246- }
248+ reindexClient .execute (ModifyDataStreamsAction .INSTANCE , modifyDataStreamRequest , listener );
249+ }
247250
248- @ Override
249- public void onFailure (Exception e ) {
250- listener .onFailure (e );
251- }
252- });
251+ private void deleteIndex (
252+ String indexName ,
253+ ExecuteWithHeadersClient reindexClient ,
254+ TaskId parentTaskId ,
255+ ActionListener <AcknowledgedResponse > listener
256+ ) {
257+ DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest (indexName );
258+ deleteIndexRequest .setParentTask (parentTaskId );
259+ reindexClient .execute (TransportDeleteIndexAction .TYPE , deleteIndexRequest , listener );
253260 }
254261
255262 private void completeSuccessfulPersistentTask (
0 commit comments