5555import org .elasticsearch .index .get .GetResult ;
5656import org .elasticsearch .index .query .IdsQueryBuilder ;
5757import org .elasticsearch .index .reindex .BulkByScrollResponse ;
58+ import org .elasticsearch .index .reindex .DeleteByQueryAction ;
5859import org .elasticsearch .index .reindex .DeleteByQueryRequest ;
5960import org .elasticsearch .index .reindex .ReindexAction ;
6061import org .elasticsearch .index .reindex .ReindexRequest ;
62+ import org .elasticsearch .index .reindex .UpdateByQueryAction ;
6163import org .elasticsearch .index .reindex .UpdateByQueryRequest ;
6264import org .elasticsearch .rest .RestStatus ;
6365import org .elasticsearch .script .Script ;
@@ -792,10 +794,7 @@ public void onFailure(Exception e) {
792794 }
793795 });
794796
795- TaskGroup taskGroupToRethrottle = findTaskToRethrottle ();
796- assertThat (taskGroupToRethrottle .getChildTasks (), empty ());
797- TaskId taskIdToRethrottle = taskGroupToRethrottle .getTaskInfo ().getTaskId ();
798-
797+ TaskId taskIdToRethrottle = findTaskToRethrottle (ReindexAction .NAME );
799798 float requestsPerSecond = 1000f ;
800799 ListTasksResponse response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
801800 highLevelClient ()::reindexRethrottle , highLevelClient ()::reindexRethrottleAsync );
@@ -817,10 +816,10 @@ public void onFailure(Exception e) {
817816 }
818817 }
819818
820- private TaskGroup findTaskToRethrottle () throws IOException {
819+ private TaskId findTaskToRethrottle (String actionName ) throws IOException {
821820 long start = System .nanoTime ();
822821 ListTasksRequest request = new ListTasksRequest ();
823- request .setActions (ReindexAction . NAME );
822+ request .setActions (actionName );
824823 request .setDetailed (true );
825824 do {
826825 ListTasksResponse list = highLevelClient ().tasks ().list (request , RequestOptions .DEFAULT );
@@ -831,13 +830,15 @@ private TaskGroup findTaskToRethrottle() throws IOException {
831830 // The parent task hasn't started yet
832831 continue ;
833832 }
834- return list .getTaskGroups ().get (0 );
833+ TaskGroup taskGroup = list .getTaskGroups ().get (0 );
834+ assertThat (taskGroup .getChildTasks (), empty ());
835+ return taskGroup .getTaskInfo ().getTaskId ();
835836 } while (System .nanoTime () - start < TimeUnit .SECONDS .toNanos (10 ));
836837 throw new AssertionError ("Couldn't find tasks to rethrottle. Here are the running tasks " +
837838 highLevelClient ().tasks ().list (request , RequestOptions .DEFAULT ));
838839 }
839840
840- public void testUpdateByQuery () throws IOException {
841+ public void testUpdateByQuery () throws Exception {
841842 final String sourceIndex = "source1" ;
842843 {
843844 // Prepare
@@ -901,9 +902,53 @@ public void testUpdateByQuery() throws IOException {
901902 .getSourceAsMap ().get ("foo" ))
902903 );
903904 }
905+ {
906+ // test update-by-query rethrottling
907+ UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest ();
908+ updateByQueryRequest .indices (sourceIndex );
909+ updateByQueryRequest .setQuery (new IdsQueryBuilder ().addIds ("1" ).types ("type" ));
910+ updateByQueryRequest .setRefresh (true );
911+
912+ // this following settings are supposed to halt reindexing after first document
913+ updateByQueryRequest .setBatchSize (1 );
914+ updateByQueryRequest .setRequestsPerSecond (0.00001f );
915+ final CountDownLatch taskFinished = new CountDownLatch (1 );
916+ highLevelClient ().updateByQueryAsync (updateByQueryRequest , RequestOptions .DEFAULT , new ActionListener <BulkByScrollResponse >() {
917+
918+ @ Override
919+ public void onResponse (BulkByScrollResponse response ) {
920+ taskFinished .countDown ();
921+ }
922+
923+ @ Override
924+ public void onFailure (Exception e ) {
925+ fail (e .toString ());
926+ }
927+ });
928+
929+ TaskId taskIdToRethrottle = findTaskToRethrottle (UpdateByQueryAction .NAME );
930+ float requestsPerSecond = 1000f ;
931+ ListTasksResponse response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
932+ highLevelClient ()::updateByQueryRethrottle , highLevelClient ()::updateByQueryRethrottleAsync );
933+ assertThat (response .getTasks (), hasSize (1 ));
934+ assertEquals (taskIdToRethrottle , response .getTasks ().get (0 ).getTaskId ());
935+ assertThat (response .getTasks ().get (0 ).getStatus (), instanceOf (RawTaskStatus .class ));
936+ assertEquals (Float .toString (requestsPerSecond ),
937+ ((RawTaskStatus ) response .getTasks ().get (0 ).getStatus ()).toMap ().get ("requests_per_second" ).toString ());
938+ taskFinished .await (2 , TimeUnit .SECONDS );
939+
940+ // any rethrottling after the update-by-query is done performed with the same taskId should result in a failure
941+ response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
942+ highLevelClient ()::updateByQueryRethrottle , highLevelClient ()::updateByQueryRethrottleAsync );
943+ assertTrue (response .getTasks ().isEmpty ());
944+ assertFalse (response .getNodeFailures ().isEmpty ());
945+ assertEquals (1 , response .getNodeFailures ().size ());
946+ assertEquals ("Elasticsearch exception [type=resource_not_found_exception, reason=task [" + taskIdToRethrottle + "] is missing]" ,
947+ response .getNodeFailures ().get (0 ).getCause ().getMessage ());
948+ }
904949 }
905950
906- public void testDeleteByQuery () throws IOException {
951+ public void testDeleteByQuery () throws Exception {
907952 final String sourceIndex = "source1" ;
908953 {
909954 // Prepare
@@ -920,6 +965,8 @@ public void testDeleteByQuery() throws IOException {
920965 .source (Collections .singletonMap ("foo" , 1 ), XContentType .JSON ))
921966 .add (new IndexRequest (sourceIndex , "type" , "2" )
922967 .source (Collections .singletonMap ("foo" , 2 ), XContentType .JSON ))
968+ .add (new IndexRequest (sourceIndex , "type" , "3" )
969+ .source (Collections .singletonMap ("foo" , 3 ), XContentType .JSON ))
923970 .setRefreshPolicy (RefreshPolicy .IMMEDIATE ),
924971 RequestOptions .DEFAULT
925972 ).status ()
@@ -943,10 +990,54 @@ public void testDeleteByQuery() throws IOException {
943990 assertEquals (0 , bulkResponse .getBulkFailures ().size ());
944991 assertEquals (0 , bulkResponse .getSearchFailures ().size ());
945992 assertEquals (
946- 1 ,
993+ 2 ,
947994 highLevelClient ().search (new SearchRequest (sourceIndex ), RequestOptions .DEFAULT ).getHits ().totalHits
948995 );
949996 }
997+ {
998+ // test delete-by-query rethrottling
999+ DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest ();
1000+ deleteByQueryRequest .indices (sourceIndex );
1001+ deleteByQueryRequest .setQuery (new IdsQueryBuilder ().addIds ("2" , "3" ).types ("type" ));
1002+ deleteByQueryRequest .setRefresh (true );
1003+
1004+ // this following settings are supposed to halt reindexing after first document
1005+ deleteByQueryRequest .setBatchSize (1 );
1006+ deleteByQueryRequest .setRequestsPerSecond (0.00001f );
1007+ final CountDownLatch taskFinished = new CountDownLatch (1 );
1008+ highLevelClient ().deleteByQueryAsync (deleteByQueryRequest , RequestOptions .DEFAULT , new ActionListener <BulkByScrollResponse >() {
1009+
1010+ @ Override
1011+ public void onResponse (BulkByScrollResponse response ) {
1012+ taskFinished .countDown ();
1013+ }
1014+
1015+ @ Override
1016+ public void onFailure (Exception e ) {
1017+ fail (e .toString ());
1018+ }
1019+ });
1020+
1021+ TaskId taskIdToRethrottle = findTaskToRethrottle (DeleteByQueryAction .NAME );
1022+ float requestsPerSecond = 1000f ;
1023+ ListTasksResponse response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
1024+ highLevelClient ()::deleteByQueryRethrottle , highLevelClient ()::deleteByQueryRethrottleAsync );
1025+ assertThat (response .getTasks (), hasSize (1 ));
1026+ assertEquals (taskIdToRethrottle , response .getTasks ().get (0 ).getTaskId ());
1027+ assertThat (response .getTasks ().get (0 ).getStatus (), instanceOf (RawTaskStatus .class ));
1028+ assertEquals (Float .toString (requestsPerSecond ),
1029+ ((RawTaskStatus ) response .getTasks ().get (0 ).getStatus ()).toMap ().get ("requests_per_second" ).toString ());
1030+ taskFinished .await (2 , TimeUnit .SECONDS );
1031+
1032+ // any rethrottling after the delete-by-query is done performed with the same taskId should result in a failure
1033+ response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
1034+ highLevelClient ()::deleteByQueryRethrottle , highLevelClient ()::deleteByQueryRethrottleAsync );
1035+ assertTrue (response .getTasks ().isEmpty ());
1036+ assertFalse (response .getNodeFailures ().isEmpty ());
1037+ assertEquals (1 , response .getNodeFailures ().size ());
1038+ assertEquals ("Elasticsearch exception [type=resource_not_found_exception, reason=task [" + taskIdToRethrottle + "] is missing]" ,
1039+ response .getNodeFailures ().get (0 ).getCause ().getMessage ());
1040+ }
9501041 }
9511042
9521043 public void testBulkProcessorIntegration () throws IOException {
0 commit comments