11
11
import org .apache .logging .log4j .Logger ;
12
12
import org .elasticsearch .ElasticsearchException ;
13
13
import org .elasticsearch .action .ActionListener ;
14
+ import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
15
+ import org .elasticsearch .action .admin .indices .delete .TransportDeleteIndexAction ;
14
16
import org .elasticsearch .action .admin .indices .rollover .RolloverAction ;
15
17
import org .elasticsearch .action .admin .indices .rollover .RolloverRequest ;
16
18
import org .elasticsearch .action .datastreams .GetDataStreamAction ;
17
19
import org .elasticsearch .action .datastreams .ModifyDataStreamsAction ;
18
20
import org .elasticsearch .action .support .CountDownActionListener ;
21
+ import org .elasticsearch .action .support .SubscribableListener ;
19
22
import org .elasticsearch .action .support .master .AcknowledgedResponse ;
20
23
import org .elasticsearch .client .internal .Client ;
21
24
import org .elasticsearch .cluster .metadata .DataStream ;
@@ -210,26 +213,29 @@ private void maybeProcessNextIndex(
210
213
reindexDataStreamTask .incrementInProgressIndicesCount (index .getName ());
211
214
ReindexDataStreamIndexAction .Request reindexDataStreamIndexRequest = new ReindexDataStreamIndexAction .Request (index .getName ());
212
215
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 -> {
215
225
reindexDataStreamTask .reindexSucceeded (index .getName ());
216
226
listener .onResponse (null );
217
227
maybeProcessNextIndex (indicesRemaining , reindexDataStreamTask , reindexClient , sourceDataStream , listener , parentTaskId );
218
- }, exception -> {
219
- reindexDataStreamTask .reindexFailed (index .getName (), exception );
228
+ }, e -> {
229
+ reindexDataStreamTask .reindexFailed (index .getName (), e );
220
230
listener .onResponse (null );
221
- }), reindexClient , parentTaskId );
222
- }, exception -> {
223
- reindexDataStreamTask .reindexFailed (index .getName (), exception );
224
- listener .onResponse (null );
225
- }));
231
+ }));
226
232
}
227
233
228
234
private void updateDataStream (
229
235
String dataStream ,
230
236
String oldIndex ,
231
237
String newIndex ,
232
- ActionListener <Void > listener ,
238
+ ActionListener <AcknowledgedResponse > listener ,
233
239
ExecuteWithHeadersClient reindexClient ,
234
240
TaskId parentTaskId
235
241
) {
@@ -239,17 +245,18 @@ private void updateDataStream(
239
245
List .of (DataStreamAction .removeBackingIndex (dataStream , oldIndex ), DataStreamAction .addBackingIndex (dataStream , newIndex ))
240
246
);
241
247
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
+ }
247
250
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 );
253
260
}
254
261
255
262
private void completeSuccessfulPersistentTask (
0 commit comments