1111import org .elasticsearch .action .search .TransportSearchAction ;
1212import org .elasticsearch .client .internal .Client ;
1313import org .elasticsearch .client .internal .OriginSettingClient ;
14- import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
15- import org .elasticsearch .cluster .service .ClusterService ;
1614import org .elasticsearch .index .reindex .BulkByScrollResponse ;
1715import org .elasticsearch .index .reindex .DeleteByQueryAction ;
1816import org .elasticsearch .index .reindex .DeleteByQueryRequest ;
19- import org .elasticsearch .indices .TestIndexNameExpressionResolver ;
2017import org .elasticsearch .tasks .TaskId ;
2118import org .elasticsearch .test .ESTestCase ;
2219import org .elasticsearch .threadpool .ThreadPool ;
@@ -54,8 +51,6 @@ public class ExpiredAnnotationsRemoverTests extends ESTestCase {
5451 private OriginSettingClient originSettingClient ;
5552 private List <DeleteByQueryRequest > capturedDeleteByQueryRequests ;
5653 private ActionListener <Boolean > listener ;
57- private ClusterService clusterService ;
58- private IndexNameExpressionResolver indexNameExpressionResolver = TestIndexNameExpressionResolver .newInstance ();
5954
6055 @ Before
6156 @ SuppressWarnings ("unchecked" )
@@ -65,7 +60,6 @@ public void setUpTests() {
6560 client = mock (Client .class );
6661 originSettingClient = MockOriginSettingClient .mockOriginSettingClient (client , ClientHelper .ML_ORIGIN );
6762 listener = mock (ActionListener .class );
68- clusterService = mock (ClusterService .class );
6963 when (listener .delegateFailureAndWrap (any ())).thenCallRealMethod ();
7064 }
7165
@@ -158,6 +152,21 @@ public void testCalcCutoffEpochMs() {
158152 verify (cutoffListener ).onResponse (eq (new AbstractExpiredJobDataRemover .CutoffDetails (latest .getTime (), expectedCutoffTime )));
159153 }
160154
155+ public void testRemove_GivenIndexNotWritable_ShouldHandleGracefully () {
156+ givenBucket (new Bucket ("id_not_important" , new Date (), 60 ));
157+ List <Job > jobs = Arrays .asList (
158+ JobTests .buildJobBuilder ("annotations-1" ).setResultsRetentionDays (10L ).build (),
159+ JobTests .buildJobBuilder ("annotations-2" ).setResultsRetentionDays (20L ).build ()
160+ );
161+
162+ // annotationIndexWritable = false
163+ createExpiredAnnotationsRemover (jobs .iterator (), false ).remove (1.0f , listener , () -> false );
164+
165+ // No DBQ requests should be made, but listener should still be called with true
166+ assertThat (capturedDeleteByQueryRequests .size (), equalTo (0 ));
167+ verify (listener ).onResponse (true );
168+ }
169+
161170 private void givenDBQRequestsSucceed () {
162171 givenDBQRequest (true );
163172 }
@@ -191,7 +200,7 @@ private void givenBucket(Bucket bucket) {
191200 }).when (client ).execute (eq (TransportSearchAction .TYPE ), any (), any ());
192201 }
193202
194- private ExpiredAnnotationsRemover createExpiredAnnotationsRemover (Iterator <Job > jobIterator ) {
203+ private ExpiredAnnotationsRemover createExpiredAnnotationsRemover (Iterator <Job > jobIterator , boolean annotationIndexWritable ) {
195204 ThreadPool threadPool = mock (ThreadPool .class );
196205 ExecutorService executor = mock (ExecutorService .class );
197206
@@ -203,13 +212,27 @@ private ExpiredAnnotationsRemover createExpiredAnnotationsRemover(Iterator<Job>
203212 return null ;
204213 }).when (executor ).execute (any ());
205214
215+ WritableIndexExpander writableIndexExpander = mock (WritableIndexExpander .class );
216+ if (annotationIndexWritable ) {
217+ when (writableIndexExpander .getWritableIndices (AnnotationIndex .READ_ALIAS_NAME )).thenReturn (
218+ new ArrayList <>(Collections .singletonList (AnnotationIndex .READ_ALIAS_NAME ))
219+ );
220+ } else {
221+ when (writableIndexExpander .getWritableIndices (AnnotationIndex .READ_ALIAS_NAME )).thenReturn (
222+ new ArrayList <>(Collections .emptyList ())
223+ );
224+ }
206225 return new ExpiredAnnotationsRemover (
207226 originSettingClient ,
208227 jobIterator ,
209228 new TaskId ("test" , 0L ),
210- new WritableIndexExpander ( clusterService , indexNameExpressionResolver ) ,
229+ writableIndexExpander ,
211230 mock (AnomalyDetectionAuditor .class ),
212231 threadPool
213232 );
214233 }
234+
235+ private ExpiredAnnotationsRemover createExpiredAnnotationsRemover (Iterator <Job > jobIterator ) {
236+ return createExpiredAnnotationsRemover (jobIterator , true );
237+ }
215238}
0 commit comments