Skip to content

Commit 9584b38

Browse files
committed
fix bug on local index reader with generateLocalIndexDateHash
Signed-off-by: Chenyang Ji <cyji@amazon.com>
1 parent cd14db4 commit 9584b38

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

src/main/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ public void close() {
207207
* @return A string representing the index name in the format "top_queries-YYYY.MM.dd-01234".
208208
*/
209209
String buildLocalIndexName() {
210-
return indexPattern.format(ZonedDateTime.now(ZoneOffset.UTC)) + "-" + generateLocalIndexDateHash();
210+
ZonedDateTime currentTime = ZonedDateTime.now(ZoneOffset.UTC);
211+
return indexPattern.format(currentTime) + "-" + generateLocalIndexDateHash(currentTime.toLocalDate());
211212
}
212213

213214
/**

src/main/java/org/opensearch/plugin/insights/core/reader/LocalIndexReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,6 @@ public void close() {
154154
}
155155

156156
private String buildLocalIndexName(ZonedDateTime current) {
157-
return current.format(indexPattern) + "-" + generateLocalIndexDateHash();
157+
return current.format(indexPattern) + "-" + generateLocalIndexDateHash(current.toLocalDate());
158158
}
159159
}

src/main/java/org/opensearch/plugin/insights/core/utils/ExporterReaderUtils.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
package org.opensearch.plugin.insights.core.utils;
1010

11-
import java.time.Instant;
12-
import java.time.ZoneOffset;
11+
import java.time.LocalDate;
1312
import java.time.format.DateTimeFormatter;
1413
import java.util.Locale;
1514

@@ -22,17 +21,16 @@ public class ExporterReaderUtils {
2221
private ExporterReaderUtils() {}
2322

2423
/**
25-
* Generates a consistent 5-digit numeric hash based on the current UTC date.
24+
* Generates a consistent 5-digit numeric hash based on the given UTC date.
2625
* The generated hash is deterministic, meaning it will return the same result for the same date.
2726
*
2827
* @return A 5-digit numeric string representation of the current date's hash.
2928
*/
30-
public static String generateLocalIndexDateHash() {
31-
// Get the current date in UTC (yyyy-MM-dd format)
32-
String currentDate = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ROOT)
33-
.format(Instant.now().atOffset(ZoneOffset.UTC).toLocalDate());
29+
public static String generateLocalIndexDateHash(LocalDate date) {
30+
// Get the date string in UTC (yyyy-MM-dd format)
31+
String dateString = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ROOT).format(date);
3432

3533
// Generate a 5-digit numeric hash from the date's hashCode
36-
return String.format(Locale.ROOT, "%05d", (currentDate.hashCode() % 100000 + 100000) % 100000);
34+
return String.format(Locale.ROOT, "%05d", (dateString.hashCode() % 100000 + 100000) % 100000);
3735
}
3836
}

src/test/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporterTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public class LocalIndexExporterTests extends OpenSearchTestCase {
6767

6868
@Before
6969
public void setup() {
70-
indexName = format.format(ZonedDateTime.now(ZoneOffset.UTC)) + "-" + generateLocalIndexDateHash();
70+
indexName = format.format(ZonedDateTime.now(ZoneOffset.UTC))
71+
+ "-"
72+
+ generateLocalIndexDateHash(ZonedDateTime.now(ZoneOffset.UTC).toLocalDate());
7173
Settings.Builder settingsBuilder = Settings.builder();
7274
Settings settings = settingsBuilder.build();
7375
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import static org.opensearch.plugin.insights.core.utils.ExporterReaderUtils.generateLocalIndexDateHash;
2626

2727
import java.time.Instant;
28+
import java.time.ZoneId;
29+
import java.time.ZonedDateTime;
2830
import java.time.format.DateTimeFormatter;
2931
import java.time.temporal.ChronoUnit;
3032
import java.util.HashMap;
@@ -257,7 +259,10 @@ public void testDeleteAllTopNIndices() {
257259
// Create 9 top_queries-* indices
258260
Map<String, IndexMetadata> indexMetadataMap = new HashMap<>();
259261
for (int i = 1; i < 10; i++) {
260-
String indexName = "top_queries-2024.01.0" + i + "-" + generateLocalIndexDateHash();
262+
String indexName = "top_queries-2024.01.0"
263+
+ i
264+
+ "-"
265+
+ generateLocalIndexDateHash(ZonedDateTime.of(2024, 1, i, 0, 0, 0, 0, ZoneId.of("UTC")).toLocalDate());
261266
long creationTime = Instant.now().minus(i, ChronoUnit.DAYS).toEpochMilli();
262267

263268
IndexMetadata indexMetadata = IndexMetadata.builder(indexName)

0 commit comments

Comments
 (0)