Skip to content

Commit deadf3e

Browse files
committed
Update deleteAllTopNIndices
Signed-off-by: David Zane <davizane@amazon.com>
1 parent 8d2c76b commit deadf3e

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -632,13 +632,22 @@ void deleteExpiredTopNIndices() {
632632
* @param localIndexExporter the exporter to handle the local index operations
633633
*/
634634
void deleteAllTopNIndices(final Client client, final LocalIndexExporter localIndexExporter) {
635-
clusterService.state()
636-
.metadata()
637-
.indices()
638-
.entrySet()
639-
.stream()
640-
.filter(entry -> isTopQueriesIndex(entry.getKey(), entry.getValue()))
641-
.forEach(entry -> localIndexExporter.deleteSingleIndex(entry.getKey(), client));
635+
final ClusterStateRequest clusterStateRequest = new ClusterStateRequest().clear()
636+
.indices(TOP_QUERIES_INDEX_PATTERN_GLOB)
637+
.metadata(true)
638+
.local(true)
639+
.indicesOptions(IndicesOptions.strictExpand());
640+
641+
client.admin().cluster().state(clusterStateRequest, ActionListener.wrap(clusterStateResponse -> {
642+
logger.error(clusterStateResponse.getState().metadata().indices());
643+
clusterStateResponse.getState()
644+
.metadata()
645+
.indices()
646+
.entrySet()
647+
.stream()
648+
.filter(entry -> isTopQueriesIndex(entry.getKey(), entry.getValue()))
649+
.forEach(entry -> localIndexExporter.deleteSingleIndex(entry.getKey(), client));
650+
}, exception -> { logger.error("Error while deleting expired top_queries-* indices: ", exception); }));
642651
}
643652

644653
/**

src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333

3434
import java.io.IOException;
3535
import java.time.Instant;
36+
import java.time.LocalDate;
3637
import java.time.ZoneId;
37-
import java.time.ZoneOffset;
3838
import java.time.ZonedDateTime;
3939
import java.time.format.DateTimeFormatter;
4040
import java.time.temporal.ChronoUnit;
@@ -358,10 +358,8 @@ public void testDeleteExpiredTopNIndices() throws InterruptedException, IOExcept
358358
// Create 9 top_queries-* indices with creation dates older than the retention period
359359
Map<String, IndexMetadata> indexMetadataMap = new HashMap<>();
360360
for (int i = 1; i < 10; i++) {
361-
String indexName = "top_queries-2023.01.0"
362-
+ i
363-
+ "-"
364-
+ generateLocalIndexDateHash(ZonedDateTime.now(ZoneOffset.UTC).toLocalDate());
361+
LocalDate date = LocalDate.of(2023, 1, i);
362+
String indexName = "top_queries-" + date.format(format) + "-" + generateLocalIndexDateHash(date);
365363
long creationTime = Instant.now().minus(i + 100, ChronoUnit.DAYS).toEpochMilli(); // Ensure indices are expired
366364
IndexMetadata indexMetadata = IndexMetadata.builder(indexName)
367365
.settings(

0 commit comments

Comments
 (0)