46
46
import java .util .concurrent .TimeUnit ;
47
47
import org .junit .Before ;
48
48
import org .opensearch .Version ;
49
+ import org .opensearch .action .admin .cluster .state .ClusterStateRequest ;
50
+ import org .opensearch .action .admin .cluster .state .ClusterStateResponse ;
49
51
import org .opensearch .action .support .replication .ClusterStateCreationUtils ;
50
52
import org .opensearch .cluster .ClusterState ;
51
53
import org .opensearch .cluster .metadata .IndexMetadata ;
58
60
import org .opensearch .common .settings .Settings ;
59
61
import org .opensearch .common .unit .TimeValue ;
60
62
import org .opensearch .common .util .io .IOUtils ;
63
+ import org .opensearch .core .action .ActionListener ;
61
64
import org .opensearch .core .xcontent .NamedXContentRegistry ;
62
65
import org .opensearch .plugin .insights .QueryInsightsTestUtils ;
63
66
import org .opensearch .plugin .insights .core .exporter .DebugExporter ;
84
87
import org .opensearch .threadpool .ThreadPool ;
85
88
import org .opensearch .transport .client .AdminClient ;
86
89
import org .opensearch .transport .client .Client ;
90
+ import org .opensearch .transport .client .ClusterAdminClient ;
87
91
import org .opensearch .transport .client .IndicesAdminClient ;
88
92
89
93
/**
@@ -98,6 +102,7 @@ public class QueryInsightsServiceTests extends OpenSearchTestCase {
98
102
private QueryInsightsService queryInsightsServiceSpy ;
99
103
private final AdminClient adminClient = mock (AdminClient .class );
100
104
private final IndicesAdminClient indicesAdminClient = mock (IndicesAdminClient .class );
105
+ private final ClusterAdminClient clusterAdminClient = mock (ClusterAdminClient .class );
101
106
private ClusterService clusterService ;
102
107
private LocalIndexExporter mockLocalIndexExporter ;
103
108
private DebugExporter mockDebugExporter ;
@@ -144,6 +149,7 @@ public void setup() {
144
149
145
150
when (client .admin ()).thenReturn (adminClient );
146
151
when (adminClient .indices ()).thenReturn (indicesAdminClient );
152
+ when (adminClient .cluster ()).thenReturn (clusterAdminClient );
147
153
}
148
154
149
155
@ Override
@@ -371,6 +377,20 @@ public void testDeleteExpiredTopNIndices() throws InterruptedException, IOExcept
371
377
.build ();
372
378
indexMetadataMap .put (indexName , indexMetadata );
373
379
}
380
+ // Create some non Query Insights indices
381
+ for (String indexName : List .of ("logs-1" , "logs-2" , "top_queries-2023.01.01-12345" , "top_queries-2023.01.02-12345" )) {
382
+ IndexMetadata indexMetadata = IndexMetadata .builder (indexName )
383
+ .settings (
384
+ Settings .builder ()
385
+ .put ("index.version.created" , Version .CURRENT .id )
386
+ .put ("index.number_of_shards" , 1 )
387
+ .put ("index.number_of_replicas" , 1 )
388
+ .put (SETTING_CREATION_DATE , Instant .now ().minus (100 , ChronoUnit .DAYS ).toEpochMilli ())
389
+ )
390
+ .build ();
391
+ indexMetadataMap .put (indexName , indexMetadata );
392
+ }
393
+
374
394
List <AbstractLifecycleComponent > updatedService = createQueryInsightsServiceWithIndexState (indexMetadataMap );
375
395
QueryInsightsService updatedQueryInsightsService = (QueryInsightsService ) updatedService .get (0 );
376
396
ClusterService updatedClusterService = (ClusterService ) updatedService .get (1 );
@@ -385,7 +405,7 @@ public void testDeleteExpiredTopNIndices() throws InterruptedException, IOExcept
385
405
assertTrue (latch .await (10 , TimeUnit .SECONDS ));
386
406
// Verify that the correct number of indices are deleted
387
407
// Default retention is 7 days, so all 9 indices should be deleted
388
- verify (client , times (9 )).admin ();
408
+ verify (client , times (1 + 9 )).admin (); // one extra to get list of local indices
389
409
verify (adminClient , times (9 )).indices ();
390
410
verify (indicesAdminClient , times (9 )).delete (any (), any ());
391
411
@@ -563,6 +583,16 @@ private List<AbstractLifecycleComponent> createQueryInsightsServiceWithIndexStat
563
583
clusterSettings
564
584
);
565
585
ClusterServiceUtils .setState (updatedClusterService , updatedState );
586
+
587
+ ClusterStateResponse mockClusterStateResponse = mock (ClusterStateResponse .class );
588
+ when (mockClusterStateResponse .getState ()).thenReturn (updatedState );
589
+
590
+ doAnswer (invocation -> {
591
+ ActionListener <ClusterStateResponse > actionListener = invocation .getArgument (1 );
592
+ actionListener .onResponse (mockClusterStateResponse );
593
+ return null ;
594
+ }).when (clusterAdminClient ).state (any (ClusterStateRequest .class ), any (ActionListener .class ));
595
+
566
596
// Initialize the QueryInsightsService with the new cluster service
567
597
QueryInsightsService updatedQueryInsightsService = new QueryInsightsService (
568
598
updatedClusterService ,
0 commit comments