Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Change AD indices to be hidden indices instead of system indices (#398)
Browse files Browse the repository at this point in the history
This is a forward-port of PR 394, 395.

Testing done:
1. Manually verified AD indices are hidden.
  • Loading branch information
kaituo authored Mar 11, 2021
1 parent 0515a35 commit 09eebb5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ on:
branches:
- main
- opendistro-*
- 7.10.2-no-workbench
pull_request:
branches:
- main
- opendistro-*
- 7.10.2-no-workbench

jobs:
Build-ad:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.time.Clock;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
Expand Down Expand Up @@ -51,12 +50,10 @@
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.indices.SystemIndexDescriptor;
import org.elasticsearch.monitor.jvm.JvmService;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.plugins.SystemIndexPlugin;
import org.elasticsearch.repositories.RepositoriesService;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
Expand Down Expand Up @@ -189,7 +186,7 @@
/**
* Entry point of AD plugin.
*/
public class AnomalyDetectorPlugin extends Plugin implements ActionPlugin, ScriptPlugin, JobSchedulerExtension, SystemIndexPlugin {
public class AnomalyDetectorPlugin extends Plugin implements ActionPlugin, ScriptPlugin, JobSchedulerExtension {

private static final Logger LOG = LogManager.getLogger(AnomalyDetectorPlugin.class);

Expand Down Expand Up @@ -726,19 +723,4 @@ public ScheduledJobParser getJobParser() {
return AnomalyDetectorJob.parse(parser);
};
}

@Override
public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) {
return Collections
.unmodifiableList(
Arrays
.asList(
new SystemIndexDescriptor(AnomalyDetectionIndices.ALL_AD_RESULTS_INDEX_PATTERN, "anomaly result"),
new SystemIndexDescriptor(AnomalyDetector.ANOMALY_DETECTORS_INDEX, "detector definition"),
new SystemIndexDescriptor(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX, "detector job"),
new SystemIndexDescriptor(CommonName.CHECKPOINT_INDEX_NAME, "model checkpoint"),
new SystemIndexDescriptor(CommonName.DETECTION_STATE_INDEX, "detector information like total rcf updates")
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public class AnomalyDetectionIndices implements LocalNodeMasterListener {
private boolean allUpdated;
// we only want one update at a time
private final AtomicBoolean updateRunning;
// AD index settings
private final Settings setting;

class IndexState {
// keep track of whether the mapping version is up-to-date
Expand Down Expand Up @@ -169,6 +171,8 @@ public AnomalyDetectionIndices(
.addSettingsUpdateConsumer(AD_RESULT_HISTORY_RETENTION_PERIOD, it -> { historyRetentionPeriod = it; });

this.clusterService.getClusterSettings().addSettingsUpdateConsumer(MAX_PRIMARY_SHARDS, it -> maxPrimaryShards = it);

this.setting = Settings.builder().put("index.hidden", true).build();
}

/**
Expand Down Expand Up @@ -328,7 +332,8 @@ public void initAnomalyDetectorIndexIfAbsent(ActionListener<CreateIndexResponse>
*/
public void initAnomalyDetectorIndex(ActionListener<CreateIndexResponse> actionListener) throws IOException {
CreateIndexRequest request = new CreateIndexRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX)
.mapping(AnomalyDetector.TYPE, getAnomalyDetectorMappings(), XContentType.JSON);
.mapping(AnomalyDetector.TYPE, getAnomalyDetectorMappings(), XContentType.JSON)
.settings(setting);
adminClient.indices().create(request, markMappingUpToDate(ADIndex.CONFIG, actionListener));
}

Expand Down Expand Up @@ -357,6 +362,7 @@ private void choosePrimaryShards(CreateIndexRequest request) {
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, Math.min(nodeFilter.getNumberOfEligibleDataNodes(), maxPrimaryShards))
// 1 replica for better search performance and fail-over
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
.put("index.hidden", true)
);
}

Expand Down Expand Up @@ -400,7 +406,8 @@ public void initAnomalyDetectorJobIndex(ActionListener<CreateIndexResponse> acti
public void initDetectionStateIndex(ActionListener<CreateIndexResponse> actionListener) {
try {
CreateIndexRequest request = new CreateIndexRequest(CommonName.DETECTION_STATE_INDEX)
.mapping(AnomalyDetector.TYPE, getDetectionStateMappings(), XContentType.JSON);
.mapping(AnomalyDetector.TYPE, getDetectionStateMappings(), XContentType.JSON)
.settings(setting);
adminClient.indices().create(request, markMappingUpToDate(ADIndex.STATE, actionListener));
} catch (IOException e) {
logger.error("Fail to init AD detection state index", e);
Expand Down Expand Up @@ -469,20 +476,22 @@ void rolloverAndDeleteHistoryIndex() {
}

// We have to pass null for newIndexName in order to get Elastic to increment the index count.
RolloverRequest request = new RolloverRequest(CommonName.ANOMALY_RESULT_INDEX_ALIAS, null);
RolloverRequest rollOverRequest = new RolloverRequest(CommonName.ANOMALY_RESULT_INDEX_ALIAS, null);
String adResultMapping = null;
try {
adResultMapping = getAnomalyResultMappings();
} catch (IOException e) {
logger.error("Fail to roll over AD result index, as can't get AD result index mapping");
return;
}
request
.getCreateIndexRequest()
.index(AD_RESULT_HISTORY_INDEX_PATTERN)
.mapping(CommonName.MAPPING_TYPE, adResultMapping, XContentType.JSON);
request.addMaxIndexDocsCondition(historyMaxDocs);
adminClient.indices().rolloverIndex(request, ActionListener.wrap(response -> {
CreateIndexRequest createRequest = rollOverRequest.getCreateIndexRequest();

createRequest.index(AD_RESULT_HISTORY_INDEX_PATTERN).mapping(CommonName.MAPPING_TYPE, adResultMapping, XContentType.JSON);

choosePrimaryShards(createRequest);

rollOverRequest.addMaxIndexDocsCondition(historyMaxDocs);
adminClient.indices().rolloverIndex(rollOverRequest, ActionListener.wrap(response -> {
if (!response.isRolledOver()) {
logger
.warn("{} not rolled over. Conditions were: {}", CommonName.ANOMALY_RESULT_INDEX_ALIAS, response.getConditionStatus());
Expand Down

0 comments on commit 09eebb5

Please sign in to comment.