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 ;
@@ -727,10 +729,7 @@ public void onFailure(Exception e) {
727729 }
728730 });
729731
730- TaskGroup taskGroupToRethrottle = findTaskToRethrottle ();
731- assertThat (taskGroupToRethrottle .getChildTasks (), empty ());
732- TaskId taskIdToRethrottle = taskGroupToRethrottle .getTaskInfo ().getTaskId ();
733-
732+ TaskId taskIdToRethrottle = findTaskToRethrottle (ReindexAction .NAME );
734733 float requestsPerSecond = 1000f ;
735734 ListTasksResponse response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
736735 highLevelClient ()::reindexRethrottle , highLevelClient ()::reindexRethrottleAsync );
@@ -752,10 +751,10 @@ public void onFailure(Exception e) {
752751 }
753752 }
754753
755- private TaskGroup findTaskToRethrottle () throws IOException {
754+ private TaskId findTaskToRethrottle (String actionName ) throws IOException {
756755 long start = System .nanoTime ();
757756 ListTasksRequest request = new ListTasksRequest ();
758- request .setActions (ReindexAction . NAME );
757+ request .setActions (actionName );
759758 request .setDetailed (true );
760759 do {
761760 ListTasksResponse list = highLevelClient ().tasks ().list (request , RequestOptions .DEFAULT );
@@ -766,13 +765,15 @@ private TaskGroup findTaskToRethrottle() throws IOException {
766765 // The parent task hasn't started yet
767766 continue ;
768767 }
769- return list .getTaskGroups ().get (0 );
768+ TaskGroup taskGroup = list .getTaskGroups ().get (0 );
769+ assertThat (taskGroup .getChildTasks (), empty ());
770+ return taskGroup .getTaskInfo ().getTaskId ();
770771 } while (System .nanoTime () - start < TimeUnit .SECONDS .toNanos (10 ));
771772 throw new AssertionError ("Couldn't find tasks to rethrottle. Here are the running tasks " +
772773 highLevelClient ().tasks ().list (request , RequestOptions .DEFAULT ));
773774 }
774775
775- public void testUpdateByQuery () throws IOException {
776+ public void testUpdateByQuery () throws Exception {
776777 final String sourceIndex = "source1" ;
777778 {
778779 // Prepare
@@ -836,9 +837,53 @@ public void testUpdateByQuery() throws IOException {
836837 .getSourceAsMap ().get ("foo" ))
837838 );
838839 }
840+ {
841+ // test update-by-query rethrottling
842+ UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest ();
843+ updateByQueryRequest .indices (sourceIndex );
844+ updateByQueryRequest .setQuery (new IdsQueryBuilder ().addIds ("1" ).types ("type" ));
845+ updateByQueryRequest .setRefresh (true );
846+
847+ // this following settings are supposed to halt reindexing after first document
848+ updateByQueryRequest .setBatchSize (1 );
849+ updateByQueryRequest .setRequestsPerSecond (0.00001f );
850+ final CountDownLatch taskFinished = new CountDownLatch (1 );
851+ highLevelClient ().updateByQueryAsync (updateByQueryRequest , RequestOptions .DEFAULT , new ActionListener <BulkByScrollResponse >() {
852+
853+ @ Override
854+ public void onResponse (BulkByScrollResponse response ) {
855+ taskFinished .countDown ();
856+ }
857+
858+ @ Override
859+ public void onFailure (Exception e ) {
860+ fail (e .toString ());
861+ }
862+ });
863+
864+ TaskId taskIdToRethrottle = findTaskToRethrottle (UpdateByQueryAction .NAME );
865+ float requestsPerSecond = 1000f ;
866+ ListTasksResponse response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
867+ highLevelClient ()::updateByQueryRethrottle , highLevelClient ()::updateByQueryRethrottleAsync );
868+ assertThat (response .getTasks (), hasSize (1 ));
869+ assertEquals (taskIdToRethrottle , response .getTasks ().get (0 ).getTaskId ());
870+ assertThat (response .getTasks ().get (0 ).getStatus (), instanceOf (RawTaskStatus .class ));
871+ assertEquals (Float .toString (requestsPerSecond ),
872+ ((RawTaskStatus ) response .getTasks ().get (0 ).getStatus ()).toMap ().get ("requests_per_second" ).toString ());
873+ taskFinished .await (2 , TimeUnit .SECONDS );
874+
875+ // any rethrottling after the update-by-query is done performed with the same taskId should result in a failure
876+ response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
877+ highLevelClient ()::updateByQueryRethrottle , highLevelClient ()::updateByQueryRethrottleAsync );
878+ assertTrue (response .getTasks ().isEmpty ());
879+ assertFalse (response .getNodeFailures ().isEmpty ());
880+ assertEquals (1 , response .getNodeFailures ().size ());
881+ assertEquals ("Elasticsearch exception [type=resource_not_found_exception, reason=task [" + taskIdToRethrottle + "] is missing]" ,
882+ response .getNodeFailures ().get (0 ).getCause ().getMessage ());
883+ }
839884 }
840885
841- public void testDeleteByQuery () throws IOException {
886+ public void testDeleteByQuery () throws Exception {
842887 final String sourceIndex = "source1" ;
843888 {
844889 // Prepare
@@ -855,6 +900,8 @@ public void testDeleteByQuery() throws IOException {
855900 .source (Collections .singletonMap ("foo" , 1 ), XContentType .JSON ))
856901 .add (new IndexRequest (sourceIndex , "type" , "2" )
857902 .source (Collections .singletonMap ("foo" , 2 ), XContentType .JSON ))
903+ .add (new IndexRequest (sourceIndex , "type" , "3" )
904+ .source (Collections .singletonMap ("foo" , 3 ), XContentType .JSON ))
858905 .setRefreshPolicy (RefreshPolicy .IMMEDIATE ),
859906 RequestOptions .DEFAULT
860907 ).status ()
@@ -878,10 +925,54 @@ public void testDeleteByQuery() throws IOException {
878925 assertEquals (0 , bulkResponse .getBulkFailures ().size ());
879926 assertEquals (0 , bulkResponse .getSearchFailures ().size ());
880927 assertEquals (
881- 1 ,
928+ 2 ,
882929 highLevelClient ().search (new SearchRequest (sourceIndex ), RequestOptions .DEFAULT ).getHits ().totalHits
883930 );
884931 }
932+ {
933+ // test delete-by-query rethrottling
934+ DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest ();
935+ deleteByQueryRequest .indices (sourceIndex );
936+ deleteByQueryRequest .setQuery (new IdsQueryBuilder ().addIds ("2" , "3" ).types ("type" ));
937+ deleteByQueryRequest .setRefresh (true );
938+
939+ // this following settings are supposed to halt reindexing after first document
940+ deleteByQueryRequest .setBatchSize (1 );
941+ deleteByQueryRequest .setRequestsPerSecond (0.00001f );
942+ final CountDownLatch taskFinished = new CountDownLatch (1 );
943+ highLevelClient ().deleteByQueryAsync (deleteByQueryRequest , RequestOptions .DEFAULT , new ActionListener <BulkByScrollResponse >() {
944+
945+ @ Override
946+ public void onResponse (BulkByScrollResponse response ) {
947+ taskFinished .countDown ();
948+ }
949+
950+ @ Override
951+ public void onFailure (Exception e ) {
952+ fail (e .toString ());
953+ }
954+ });
955+
956+ TaskId taskIdToRethrottle = findTaskToRethrottle (DeleteByQueryAction .NAME );
957+ float requestsPerSecond = 1000f ;
958+ ListTasksResponse response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
959+ highLevelClient ()::deleteByQueryRethrottle , highLevelClient ()::deleteByQueryRethrottleAsync );
960+ assertThat (response .getTasks (), hasSize (1 ));
961+ assertEquals (taskIdToRethrottle , response .getTasks ().get (0 ).getTaskId ());
962+ assertThat (response .getTasks ().get (0 ).getStatus (), instanceOf (RawTaskStatus .class ));
963+ assertEquals (Float .toString (requestsPerSecond ),
964+ ((RawTaskStatus ) response .getTasks ().get (0 ).getStatus ()).toMap ().get ("requests_per_second" ).toString ());
965+ taskFinished .await (2 , TimeUnit .SECONDS );
966+
967+ // any rethrottling after the delete-by-query is done performed with the same taskId should result in a failure
968+ response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
969+ highLevelClient ()::deleteByQueryRethrottle , highLevelClient ()::deleteByQueryRethrottleAsync );
970+ assertTrue (response .getTasks ().isEmpty ());
971+ assertFalse (response .getNodeFailures ().isEmpty ());
972+ assertEquals (1 , response .getNodeFailures ().size ());
973+ assertEquals ("Elasticsearch exception [type=resource_not_found_exception, reason=task [" + taskIdToRethrottle + "] is missing]" ,
974+ response .getNodeFailures ().get (0 ).getCause ().getMessage ());
975+ }
885976 }
886977
887978 public void testBulkProcessorIntegration () throws IOException {
0 commit comments