|
8 | 8 |
|
9 | 9 | package org.opensearch.plugin.insights.core.exporter;
|
10 | 10 |
|
11 |
| -import static org.opensearch.plugin.insights.core.service.TopQueriesService.isTopQueriesIndex; |
| 11 | +import static org.opensearch.plugin.insights.core.utils.ExporterReaderUtils.generateLocalIndexDateHash; |
12 | 12 | import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.DEFAULT_DELETE_AFTER_VALUE;
|
13 | 13 |
|
14 | 14 | import java.io.IOException;
|
15 | 15 | import java.nio.charset.Charset;
|
16 |
| -import java.time.Instant; |
17 | 16 | import java.time.ZoneOffset;
|
18 | 17 | import java.time.ZonedDateTime;
|
19 | 18 | import java.time.format.DateTimeFormatter;
|
20 | 19 | import java.util.List;
|
21 |
| -import java.util.Locale; |
22 |
| -import java.util.Map; |
23 | 20 | import java.util.Objects;
|
24 |
| -import java.util.concurrent.TimeUnit; |
25 | 21 | import org.apache.logging.log4j.LogManager;
|
26 | 22 | import org.apache.logging.log4j.Logger;
|
| 23 | +import org.opensearch.ExceptionsHelper; |
27 | 24 | import org.opensearch.ResourceAlreadyExistsException;
|
| 25 | +import org.opensearch.index.IndexNotFoundException; |
28 | 26 | import org.opensearch.action.admin.indices.create.CreateIndexRequest;
|
29 | 27 | import org.opensearch.action.admin.indices.create.CreateIndexResponse;
|
| 28 | +import org.opensearch.action.admin.indices.delete.DeleteIndexRequest; |
30 | 29 | import org.opensearch.action.bulk.BulkRequestBuilder;
|
31 | 30 | import org.opensearch.action.bulk.BulkResponse;
|
32 | 31 | import org.opensearch.action.index.IndexRequest;
|
33 | 32 | import org.opensearch.client.Client;
|
34 | 33 | import org.opensearch.cluster.ClusterState;
|
35 |
| -import org.opensearch.cluster.metadata.IndexMetadata; |
36 | 34 | import org.opensearch.cluster.service.ClusterService;
|
37 | 35 | import org.opensearch.common.settings.Settings;
|
38 | 36 | import org.opensearch.common.unit.TimeValue;
|
|
41 | 39 | import org.opensearch.core.xcontent.ToXContent;
|
42 | 40 | import org.opensearch.plugin.insights.core.metrics.OperationalMetric;
|
43 | 41 | import org.opensearch.plugin.insights.core.metrics.OperationalMetricsCounter;
|
44 |
| -import org.opensearch.plugin.insights.core.service.TopQueriesService; |
45 | 42 | import org.opensearch.plugin.insights.rules.model.SearchQueryRecord;
|
46 | 43 |
|
47 | 44 | /**
|
48 | 45 | * Local index exporter for exporting query insights data to local OpenSearch indices.
|
49 | 46 | */
|
50 |
| -public final class LocalIndexExporter implements QueryInsightsExporter { |
| 47 | +public class LocalIndexExporter implements QueryInsightsExporter { |
51 | 48 | /**
|
52 | 49 | * Logger of the local index exporter
|
53 | 50 | */
|
@@ -110,7 +107,7 @@ public DateTimeFormatter getIndexPattern() {
|
110 | 107 | *
|
111 | 108 | * @param indexPattern index pattern
|
112 | 109 | */
|
113 |
| - void setIndexPattern(DateTimeFormatter indexPattern) { |
| 110 | + public void setIndexPattern(DateTimeFormatter indexPattern) { |
114 | 111 | this.indexPattern = indexPattern;
|
115 | 112 | }
|
116 | 113 |
|
@@ -153,7 +150,8 @@ public void onResponse(CreateIndexResponse createIndexResponse) {
|
153 | 150 |
|
154 | 151 | @Override
|
155 | 152 | public void onFailure(Exception e) {
|
156 |
| - if (e instanceof ResourceAlreadyExistsException) { |
| 153 | + Throwable cause = ExceptionsHelper.unwrapCause(e); |
| 154 | + if (cause instanceof ResourceAlreadyExistsException) { |
157 | 155 | try {
|
158 | 156 | bulk(indexName, records);
|
159 | 157 | } catch (IOException ex) {
|
@@ -222,34 +220,37 @@ public void setDeleteAfter(final int deleteAfter) {
|
222 | 220 | }
|
223 | 221 |
|
224 | 222 | /**
|
225 |
| - * Delete Top N local indices older than the configured data retention period |
| 223 | + * Get local index exporter data retention period |
226 | 224 | *
|
227 |
| - * @param indexMetadataMap Map of index name {@link String} to {@link IndexMetadata} |
| 225 | + * @return the number of days after which Top N local indices should be deleted |
228 | 226 | */
|
229 |
| - public void deleteExpiredTopNIndices(final Map<String, IndexMetadata> indexMetadataMap) { |
230 |
| - long expirationMillisLong = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(deleteAfter); |
231 |
| - for (Map.Entry<String, IndexMetadata> entry : indexMetadataMap.entrySet()) { |
232 |
| - String indexName = entry.getKey(); |
233 |
| - if (isTopQueriesIndex(indexName, entry.getValue()) && entry.getValue().getCreationDate() <= expirationMillisLong) { |
234 |
| - // delete this index |
235 |
| - TopQueriesService.deleteSingleIndex(indexName, client); |
236 |
| - } |
237 |
| - } |
| 227 | + public int getDeleteAfter() { |
| 228 | + return deleteAfter; |
238 | 229 | }
|
239 | 230 |
|
240 | 231 | /**
|
241 |
| - * Generates a consistent 5-digit numeric hash based on the current UTC date. |
242 |
| - * The generated hash is deterministic, meaning it will return the same result for the same date. |
| 232 | + * Deletes the specified index and logs any failure that occurs during the operation. |
243 | 233 | *
|
244 |
| - * @return A 5-digit numeric string representation of the current date's hash. |
| 234 | + * @param indexName The name of the index to delete. |
| 235 | + * @param client The OpenSearch client used to perform the deletion. |
245 | 236 | */
|
246 |
| - public static String generateLocalIndexDateHash() { |
247 |
| - // Get the current date in UTC (yyyy-MM-dd format) |
248 |
| - String currentDate = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ROOT) |
249 |
| - .format(Instant.now().atOffset(ZoneOffset.UTC).toLocalDate()); |
| 237 | + public void deleteSingleIndex(String indexName, Client client) { |
| 238 | + Logger logger = LogManager.getLogger(); |
| 239 | + client.admin().indices().delete(new DeleteIndexRequest(indexName), new ActionListener<>() { |
| 240 | + @Override |
| 241 | + // CS-SUPPRESS-SINGLE: RegexpSingleline It is not possible to use phrase "cluster manager" instead of master here |
| 242 | + public void onResponse(org.opensearch.action.support.master.AcknowledgedResponse acknowledgedResponse) {} |
250 | 243 |
|
251 |
| - // Generate a 5-digit numeric hash from the date's hashCode |
252 |
| - return String.format(Locale.ROOT, "%05d", (currentDate.hashCode() % 100000 + 100000) % 100000); |
| 244 | + @Override |
| 245 | + public void onFailure(Exception e) { |
| 246 | + Throwable cause = ExceptionsHelper.unwrapCause(e); |
| 247 | + if (cause instanceof IndexNotFoundException) { |
| 248 | + return; |
| 249 | + } |
| 250 | + OperationalMetricsCounter.getInstance().incrementCounter(OperationalMetric.LOCAL_INDEX_EXPORTER_DELETE_FAILURES); |
| 251 | + logger.error("Failed to delete index '{}': ", indexName, e); |
| 252 | + } |
| 253 | + }); |
253 | 254 | }
|
254 | 255 |
|
255 | 256 | /**
|
|
0 commit comments