Skip to content

Commit d5ba7f5

Browse files
committed
Test8
Signed-off-by: Jackie Han <jkhanjob@gmail.com>
1 parent 2eb8a82 commit d5ba7f5

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/main/java/org/opensearch/timeseries/indices/IndexManagement.java

+19-18
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.opensearch.core.xcontent.NamedXContentRegistry;
7575
import org.opensearch.core.xcontent.XContentParser;
7676
import org.opensearch.core.xcontent.XContentParser.Token;
77+
import org.opensearch.forecast.constant.ForecastCommonName;
7778
import org.opensearch.forecast.indices.ForecastIndex;
7879
import org.opensearch.index.IndexNotFoundException;
7980
import org.opensearch.index.query.BoolQueryBuilder;
@@ -289,7 +290,7 @@ protected void choosePrimaryShards(CreateIndexRequest request, boolean hiddenInd
289290
);
290291
}
291292

292-
protected void deleteOldHistoryIndices(String indexPattern, TimeValue historyRetentionPeriod, Integer customResultIndexTtl) {
293+
protected void deleteOldHistoryIndices(String indexPattern, TimeValue historyRetentionPeriod) {
293294
Set<String> candidates = new HashSet<String>();
294295

295296
ClusterStateRequest clusterStateRequest = new ClusterStateRequest()
@@ -302,12 +303,12 @@ protected void deleteOldHistoryIndices(String indexPattern, TimeValue historyRet
302303
adminClient.cluster().state(clusterStateRequest, ActionListener.wrap(clusterStateResponse -> {
303304
String latestToDelete = null;
304305
long latest = Long.MIN_VALUE;
305-
long customTtlMillis = (customResultIndexTtl != null) ? customResultIndexTtl * 24 * 60 * 60 * 1000L : Long.MAX_VALUE;
306306
for (IndexMetadata indexMetaData : clusterStateResponse.getState().metadata().indices().values()) {
307307
long creationTime = indexMetaData.getCreationDate();
308308
long indexAgeMillis = Instant.now().toEpochMilli() - creationTime;
309-
if (indexAgeMillis > historyRetentionPeriod.millis() || indexAgeMillis > customTtlMillis) {
309+
if (indexAgeMillis > historyRetentionPeriod.millis()) {
310310
String indexName = indexMetaData.getIndex().getName();
311+
System.out.println("indexName: " + indexName);
311312
candidates.add(indexName);
312313
if (latest < creationTime) {
313314
latest = creationTime;
@@ -317,7 +318,7 @@ protected void deleteOldHistoryIndices(String indexPattern, TimeValue historyRet
317318
}
318319
if (candidates.size() > 1) {
319320
// delete all indices except the last one because the last one may contain docs newer than the retention period
320-
candidates.remove(latestToDelete);
321+
//candidates.remove(latestToDelete);
321322
String[] toDelete = candidates.toArray(Strings.EMPTY_ARRAY);
322323
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(toDelete);
323324
adminClient.indices().delete(deleteIndexRequest, ActionListener.wrap(deleteIndexResponse -> {
@@ -1085,7 +1086,7 @@ public void onClusterManager() {
10851086

10861087
// schedule the next rollover for approx MAX_AGE later
10871088
scheduledRollover = threadPool
1088-
.scheduleWithFixedDelay(() -> rolloverAndDeleteHistoryIndex(), TimeValue.timeValueMinutes(5), executorName());
1089+
.scheduleWithFixedDelay(() -> rolloverAndDeleteHistoryIndex(), TimeValue.timeValueMinutes(1), executorName());
10891090
} catch (Exception e) {
10901091
// This should be run on cluster startup
10911092
logger.error("Error rollover result indices. " + "Can't rollover result until clusterManager node is restarted.", e);
@@ -1108,7 +1109,6 @@ protected void rescheduleRollover() {
11081109
if (scheduledRollover != null) {
11091110
scheduledRollover.cancel();
11101111
}
1111-
System.out.println(5);
11121112

11131113
scheduledRollover = threadPool
11141114
.scheduleWithFixedDelay(() -> rolloverAndDeleteHistoryIndex(), historyRolloverPeriod, executorName());
@@ -1244,13 +1244,9 @@ protected void rolloverAndDeleteHistoryIndex(
12441244
String rolloverIndexPattern,
12451245
IndexType resultIndex
12461246
) {
1247-
System.out.println("resultIndexAlias: " + resultIndexAlias);
1248-
System.out.println("allResultIndicesPattern: " + allResultIndicesPattern);
1249-
System.out.println("rolloverIndexPattern: " + rolloverIndexPattern);
1250-
System.out.println("resultIndex: " + resultIndex.getIndexName());
1251-
12521247
// build rollover request for default result index
12531248
RolloverRequest defaultResultIndexRolloverRequest = buildRolloverRequest(resultIndexAlias, rolloverIndexPattern);
1249+
defaultResultIndexRolloverRequest.addMaxIndexDocsCondition(historyMaxDocs * getNumberOfPrimaryShards());
12541250

12551251
// get config files that have custom result index alias to perform rollover on
12561252
getConfigsWithCustomResultIndexAlias(ActionListener.wrap(candidateResultAliases -> {
@@ -1271,8 +1267,6 @@ protected void rolloverAndDeleteHistoryIndex(
12711267
return;
12721268
}
12731269

1274-
System.out.println("size: " + candidateResultAliases.size());
1275-
12761270
// perform rollover and delete on found custom result index alias
12771271
candidateResultAliases.forEach(config -> handleCustomResultIndex(config, resultIndex));
12781272

@@ -1284,16 +1278,14 @@ protected void rolloverAndDeleteHistoryIndex(
12841278
}
12851279

12861280
private void handleCustomResultIndex(Config config, IndexType resultIndex) {
1287-
System.out.println("detector name: " + config.getName());
1288-
System.out.println("custom index name: " + config.getCustomResultIndexOrAlias());
12891281
RolloverRequest rolloverRequest = buildRolloverRequest(
12901282
config.getCustomResultIndexOrAlias(),
12911283
getCustomResultIndexPattern(config.getCustomResultIndexOrAlias())
12921284
);
12931285

12941286
// add rollover conditions if found in config
12951287
if (config.getCustomResultIndexMinAge() != null) {
1296-
rolloverRequest.addMaxIndexAgeCondition(TimeValue.timeValueMinutes(10));
1288+
rolloverRequest.addMaxIndexAgeCondition(TimeValue.timeValueMinutes(1));
12971289

12981290
// rolloverRequest.addMaxIndexAgeCondition(TimeValue.timeValueDays(config.getCustomResultIndexMinAge()));
12991291
}
@@ -1326,7 +1318,6 @@ private RolloverRequest buildRolloverRequest(String resultIndexAlias, String rol
13261318

13271319
createRequest.index(rolloverIndexPattern).mapping(resultMapping, XContentType.JSON);
13281320
choosePrimaryShards(createRequest, true);
1329-
rollOverRequest.addMaxIndexDocsCondition(historyMaxDocs * getNumberOfPrimaryShards());
13301321

13311322
return rollOverRequest;
13321323
}
@@ -1345,7 +1336,17 @@ private void proceedWithRolloverAndDelete(
13451336
IndexState indexState = indexStates.computeIfAbsent(resultIndex, k -> new IndexState(k.getMapping()));
13461337
indexState.mappingUpToDate = true;
13471338
logger.info("{} rolled over. Conditions were: {}", resultIndexAlias, response.getConditionStatus());
1348-
deleteOldHistoryIndices(allResultIndicesPattern, historyRetentionPeriod, customResultIndexTtl);
1339+
if (resultIndexAlias.startsWith(ADCommonName.CUSTOM_RESULT_INDEX_PREFIX) || resultIndexAlias.startsWith(CUSTOM_RESULT_INDEX_PREFIX)) {
1340+
// handle custom result index deletion
1341+
if (customResultIndexTtl != null) {
1342+
// deleteOldHistoryIndices(allResultIndicesPattern, TimeValue.timeValueHours(customResultIndexTtl * 24));
1343+
deleteOldHistoryIndices(allResultIndicesPattern, TimeValue.timeValueMinutes(1));
1344+
1345+
}
1346+
} else {
1347+
// handle default result index deletion
1348+
deleteOldHistoryIndices(allResultIndicesPattern, historyRetentionPeriod);
1349+
}
13491350
}
13501351
}, exception -> { logger.error("Fail to roll over result index", exception); }));
13511352
}

0 commit comments

Comments
 (0)