33
33
34
34
import java .io .IOException ;
35
35
import java .time .Instant ;
36
+ import java .time .LocalDate ;
36
37
import java .time .ZoneId ;
37
- import java .time .ZoneOffset ;
38
38
import java .time .ZonedDateTime ;
39
39
import java .time .format .DateTimeFormatter ;
40
40
import java .time .temporal .ChronoUnit ;
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 .client .AdminClient ;
51
53
import org .opensearch .client .Client ;
54
+ import org .opensearch .client .ClusterAdminClient ;
52
55
import org .opensearch .client .IndicesAdminClient ;
53
56
import org .opensearch .cluster .ClusterState ;
54
57
import org .opensearch .cluster .metadata .IndexMetadata ;
61
64
import org .opensearch .common .settings .Settings ;
62
65
import org .opensearch .common .unit .TimeValue ;
63
66
import org .opensearch .common .util .io .IOUtils ;
67
+ import org .opensearch .core .action .ActionListener ;
64
68
import org .opensearch .core .xcontent .NamedXContentRegistry ;
65
69
import org .opensearch .plugin .insights .QueryInsightsTestUtils ;
66
70
import org .opensearch .plugin .insights .core .exporter .DebugExporter ;
@@ -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
@@ -352,10 +358,8 @@ public void testDeleteExpiredTopNIndices() throws InterruptedException, IOExcept
352
358
// Create 9 top_queries-* indices with creation dates older than the retention period
353
359
Map <String , IndexMetadata > indexMetadataMap = new HashMap <>();
354
360
for (int i = 1 ; i < 10 ; i ++) {
355
- String indexName = "top_queries-2023.01.0"
356
- + i
357
- + "-"
358
- + generateLocalIndexDateHash (ZonedDateTime .now (ZoneOffset .UTC ).toLocalDate ());
361
+ LocalDate date = LocalDate .of (2023 , 1 , i );
362
+ String indexName = "top_queries-" + date .format (format ) + "-" + generateLocalIndexDateHash (date );
359
363
long creationTime = Instant .now ().minus (i + 100 , ChronoUnit .DAYS ).toEpochMilli (); // Ensure indices are expired
360
364
IndexMetadata indexMetadata = IndexMetadata .builder (indexName )
361
365
.settings (
@@ -371,6 +375,20 @@ public void testDeleteExpiredTopNIndices() throws InterruptedException, IOExcept
371
375
.build ();
372
376
indexMetadataMap .put (indexName , indexMetadata );
373
377
}
378
+ // Create some non Query Insights indices
379
+ for (String indexName : List .of ("logs-1" , "logs-2" , "top_queries-2023.01.01-12345" , "top_queries-2023.01.02-12345" )) {
380
+ IndexMetadata indexMetadata = IndexMetadata .builder (indexName )
381
+ .settings (
382
+ Settings .builder ()
383
+ .put ("index.version.created" , Version .CURRENT .id )
384
+ .put ("index.number_of_shards" , 1 )
385
+ .put ("index.number_of_replicas" , 1 )
386
+ .put (SETTING_CREATION_DATE , Instant .now ().minus (100 , ChronoUnit .DAYS ).toEpochMilli ())
387
+ )
388
+ .build ();
389
+ indexMetadataMap .put (indexName , indexMetadata );
390
+ }
391
+
374
392
List <AbstractLifecycleComponent > updatedService = createQueryInsightsServiceWithIndexState (indexMetadataMap );
375
393
QueryInsightsService updatedQueryInsightsService = (QueryInsightsService ) updatedService .get (0 );
376
394
ClusterService updatedClusterService = (ClusterService ) updatedService .get (1 );
@@ -385,7 +403,7 @@ public void testDeleteExpiredTopNIndices() throws InterruptedException, IOExcept
385
403
assertTrue (latch .await (10 , TimeUnit .SECONDS ));
386
404
// Verify that the correct number of indices are deleted
387
405
// Default retention is 7 days, so all 9 indices should be deleted
388
- verify (client , times (9 )).admin ();
406
+ verify (client , times (1 + 9 )).admin (); // one extra to get list of local indices
389
407
verify (adminClient , times (9 )).indices ();
390
408
verify (indicesAdminClient , times (9 )).delete (any (), any ());
391
409
@@ -563,6 +581,16 @@ private List<AbstractLifecycleComponent> createQueryInsightsServiceWithIndexStat
563
581
clusterSettings
564
582
);
565
583
ClusterServiceUtils .setState (updatedClusterService , updatedState );
584
+
585
+ ClusterStateResponse mockClusterStateResponse = mock (ClusterStateResponse .class );
586
+ when (mockClusterStateResponse .getState ()).thenReturn (updatedState );
587
+
588
+ doAnswer (invocation -> {
589
+ ActionListener <ClusterStateResponse > actionListener = invocation .getArgument (1 );
590
+ actionListener .onResponse (mockClusterStateResponse );
591
+ return null ;
592
+ }).when (clusterAdminClient ).state (any (ClusterStateRequest .class ), any (ActionListener .class ));
593
+
566
594
// Initialize the QueryInsightsService with the new cluster service
567
595
QueryInsightsService updatedQueryInsightsService = new QueryInsightsService (
568
596
updatedClusterService ,
0 commit comments