Skip to content

Commit

Permalink
only parse number value for custom result index conditions fields
Browse files Browse the repository at this point in the history
Signed-off-by: Jackie Han <[email protected]>
  • Loading branch information
jackiehanyang committed Jun 10, 2024
1 parent b6faf90 commit ce85d43
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public ADIndexManagement(
ADIndex.RESULT.getMapping(),
xContentRegistry,
AnomalyDetector::parse,
ADCommonName.CUSTOM_RESULT_INDEX_PREFIX + "*"
ADCommonName.CUSTOM_RESULT_INDEX_PREFIX
);
this.clusterService.addLocalNodeClusterManagerListener(this);

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/opensearch/ad/model/AnomalyDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,13 @@ public static AnomalyDetector parse(
}
break;
case RESULT_INDEX_FIELD_MIN_SIZE:
customResultIndexMinSize = parser.intValue();
customResultIndexMinSize = onlyParseNumberValue(parser);
break;
case RESULT_INDEX_FIELD_MIN_AGE:
customResultIndexMinAge = parser.intValue();
customResultIndexMinAge = onlyParseNumberValue(parser);
break;
case RESULT_INDEX_FIELD_TTL:
customResultIndexTTL = parser.intValue();
customResultIndexTTL = onlyParseNumberValue(parser);
break;
default:
parser.skipChildren();
Expand Down Expand Up @@ -685,4 +685,12 @@ private List<Rule> getDefaultRule() {
}
return rules;
}

private static Integer onlyParseNumberValue(XContentParser parser) throws IOException {
XContentParser.Token currentToken = parser.currentToken();
if (currentToken == XContentParser.Token.VALUE_NUMBER) {
return parser.intValue();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public ForecastIndexManagement(
ForecastIndex.RESULT.getMapping(),
xContentRegistry,
Forecaster::parse,
ForecastCommonName.CUSTOM_RESULT_INDEX_PREFIX + "*"
ForecastCommonName.CUSTOM_RESULT_INDEX_PREFIX
);
this.indexStates = new EnumMap<ForecastIndex, IndexState>(ForecastIndex.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
package org.opensearch.timeseries.indices;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.forecast.constant.ForecastCommonName.CUSTOM_RESULT_INDEX_PREFIX;
import static org.opensearch.timeseries.util.RestHandlerUtils.createXContentParserFromRegistry;

import java.io.IOException;
Expand Down Expand Up @@ -52,7 +51,6 @@
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.support.GroupedActionListener;
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.ad.constant.ADCommonName;
import org.opensearch.client.AdminClient;
import org.opensearch.client.Client;
import org.opensearch.cluster.LocalNodeClusterManagerListener;
Expand Down Expand Up @@ -137,7 +135,7 @@ public abstract class IndexManagement<IndexType extends Enum<IndexType> & TimeSe
private String resultMapping;
private NamedXContentRegistry xContentRegistry;
protected BiCheckedFunction<XContentParser, String, ? extends Config, IOException> configParser;
protected String customResultIndexRegex;
protected String customResultIndexPrefix;

protected class IndexState {
// keep track of whether the mapping version is up-to-date
Expand Down Expand Up @@ -169,7 +167,7 @@ protected IndexManagement(
String resultMapping,
NamedXContentRegistry xContentRegistry,
BiCheckedFunction<XContentParser, String, ? extends Config, IOException> configParser,
String customResultIndexRegex
String customResultIndexPrefix
)
throws IOException {
this.client = client;
Expand All @@ -193,7 +191,7 @@ protected IndexManagement(
this.resultMapping = resultMapping;
this.xContentRegistry = xContentRegistry;
this.configParser = configParser;
this.customResultIndexRegex = customResultIndexRegex;
this.customResultIndexPrefix = customResultIndexPrefix;
}

/**
Expand Down Expand Up @@ -794,7 +792,7 @@ private void getConfigsWithCustomResultIndexAlias(ActionListener<List<Config>> l
}
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
BoolQueryBuilder shouldQueries = new BoolQueryBuilder();
shouldQueries.should(QueryBuilders.wildcardQuery(Config.RESULT_INDEX_FIELD, customResultIndexRegex));
shouldQueries.should(QueryBuilders.wildcardQuery(Config.RESULT_INDEX_FIELD, customResultIndexPrefix + "*"));
if (shouldQueries.should().isEmpty() == false) {
boolQuery.filter(shouldQueries);
}
Expand Down Expand Up @@ -1308,19 +1306,20 @@ private void proceedWithRolloverAndDelete(
IndexType resultIndex,
Integer customResultIndexTtl
) {
if (rollOverRequest.getConditions().size() == 0) {
return;
}
adminClient.indices().rolloverIndex(rollOverRequest, ActionListener.wrap(response -> {
if (!response.isRolledOver()) {
logger.warn("{} not rolled over. Conditions were: {}", resultIndexAlias, response.getConditionStatus());
} else {
IndexState indexState = indexStates.computeIfAbsent(resultIndex, k -> new IndexState(k.getMapping()));
indexState.mappingUpToDate = true;
logger.info("{} rolled over. Conditions were: {}", resultIndexAlias, response.getConditionStatus());
if (resultIndexAlias.startsWith(ADCommonName.CUSTOM_RESULT_INDEX_PREFIX)
|| resultIndexAlias.startsWith(CUSTOM_RESULT_INDEX_PREFIX)) {
if (resultIndexAlias.startsWith(customResultIndexPrefix)) {
// handle custom result index deletion
if (customResultIndexTtl != null) {
deleteOldHistoryIndices(allResultIndicesPattern, TimeValue.timeValueHours(customResultIndexTtl * 24));

}
} else {
// handle default result index deletion
Expand Down

0 comments on commit ce85d43

Please sign in to comment.