55
55
import org .elasticsearch .index .get .GetResult ;
56
56
import org .elasticsearch .index .query .IdsQueryBuilder ;
57
57
import org .elasticsearch .index .reindex .BulkByScrollResponse ;
58
+ import org .elasticsearch .index .reindex .DeleteByQueryAction ;
58
59
import org .elasticsearch .index .reindex .DeleteByQueryRequest ;
59
60
import org .elasticsearch .index .reindex .ReindexAction ;
60
61
import org .elasticsearch .index .reindex .ReindexRequest ;
62
+ import org .elasticsearch .index .reindex .UpdateByQueryAction ;
61
63
import org .elasticsearch .index .reindex .UpdateByQueryRequest ;
62
64
import org .elasticsearch .rest .RestStatus ;
63
65
import org .elasticsearch .script .Script ;
@@ -727,10 +729,7 @@ public void onFailure(Exception e) {
727
729
}
728
730
});
729
731
730
- TaskGroup taskGroupToRethrottle = findTaskToRethrottle ();
731
- assertThat (taskGroupToRethrottle .getChildTasks (), empty ());
732
- TaskId taskIdToRethrottle = taskGroupToRethrottle .getTaskInfo ().getTaskId ();
733
-
732
+ TaskId taskIdToRethrottle = findTaskToRethrottle (ReindexAction .NAME );
734
733
float requestsPerSecond = 1000f ;
735
734
ListTasksResponse response = execute (new RethrottleRequest (taskIdToRethrottle , requestsPerSecond ),
736
735
highLevelClient ()::reindexRethrottle , highLevelClient ()::reindexRethrottleAsync );
@@ -752,10 +751,10 @@ public void onFailure(Exception e) {
752
751
}
753
752
}
754
753
755
- private TaskGroup findTaskToRethrottle () throws IOException {
754
+ private TaskId findTaskToRethrottle (String actionName ) throws IOException {
756
755
long start = System .nanoTime ();
757
756
ListTasksRequest request = new ListTasksRequest ();
758
- request .setActions (ReindexAction . NAME );
757
+ request .setActions (actionName );
759
758
request .setDetailed (true );
760
759
do {
761
760
ListTasksResponse list = highLevelClient ().tasks ().list (request , RequestOptions .DEFAULT );
@@ -766,13 +765,15 @@ private TaskGroup findTaskToRethrottle() throws IOException {
766
765
// The parent task hasn't started yet
767
766
continue ;
768
767
}
769
- return list .getTaskGroups ().get (0 );
768
+ TaskGroup taskGroup = list .getTaskGroups ().get (0 );
769
+ assertThat (taskGroup .getChildTasks (), empty ());
770
+ return taskGroup .getTaskInfo ().getTaskId ();
770
771
} while (System .nanoTime () - start < TimeUnit .SECONDS .toNanos (10 ));
771
772
throw new AssertionError ("Couldn't find tasks to rethrottle. Here are the running tasks " +
772
773
highLevelClient ().tasks ().list (request , RequestOptions .DEFAULT ));
773
774
}
774
775
775
- public void testUpdateByQuery () throws IOException {
776
+ public void testUpdateByQuery () throws Exception {
776
777
final String sourceIndex = "source1" ;
777
778
{
778
779
// Prepare
@@ -836,9 +837,53 @@ public void testUpdateByQuery() throws IOException {
836
837
.getSourceAsMap ().get ("foo" ))
837
838
);
838
839
}
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
+ }
839
884
}
840
885
841
- public void testDeleteByQuery () throws IOException {
886
+ public void testDeleteByQuery () throws Exception {
842
887
final String sourceIndex = "source1" ;
843
888
{
844
889
// Prepare
@@ -855,6 +900,8 @@ public void testDeleteByQuery() throws IOException {
855
900
.source (Collections .singletonMap ("foo" , 1 ), XContentType .JSON ))
856
901
.add (new IndexRequest (sourceIndex , "type" , "2" )
857
902
.source (Collections .singletonMap ("foo" , 2 ), XContentType .JSON ))
903
+ .add (new IndexRequest (sourceIndex , "type" , "3" )
904
+ .source (Collections .singletonMap ("foo" , 3 ), XContentType .JSON ))
858
905
.setRefreshPolicy (RefreshPolicy .IMMEDIATE ),
859
906
RequestOptions .DEFAULT
860
907
).status ()
@@ -878,10 +925,54 @@ public void testDeleteByQuery() throws IOException {
878
925
assertEquals (0 , bulkResponse .getBulkFailures ().size ());
879
926
assertEquals (0 , bulkResponse .getSearchFailures ().size ());
880
927
assertEquals (
881
- 1 ,
928
+ 2 ,
882
929
highLevelClient ().search (new SearchRequest (sourceIndex ), RequestOptions .DEFAULT ).getHits ().totalHits
883
930
);
884
931
}
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
+ }
885
976
}
886
977
887
978
public void testBulkProcessorIntegration () throws IOException {
0 commit comments