Skip to content

Commit bc16499

Browse files
authored
Enforce DOCUMENT Replication for AD Indices and Adjust Primary Shards (opensearch-project#948)
* Enforce DOCUMENT Replication for AD Indices and Adjust Primary Shards In this PR, we temporarily enforce DOCUMENT replication for AD indices. This change is necessary due to the current limitation of SegRep, which doesn't support Get/MultiGet by ID. This measure will be in place until SegRep adds support for these operations. This adjustment aligns with the modification made in the referenced PR: job-scheduler PR opensearch-project#417 Additionally, this PR increases the number of primary shards for forecasting indices from 10 to 20, recognizing the higher write intensity of these indices. For instance, when the horizon is 24, we are expected to save 25 documents: 24 for each forecast and 1 for the actual value. In contrast, for AD, we save one document per actual value. Testing done: 1. gradle build passed Signed-off-by: Kaituo Li <kaituo@amazon.com> * Fix comments Signed-off-by: Kaituo Li <kaituo@amazon.com> --------- Signed-off-by: Kaituo Li <kaituo@amazon.com>
1 parent a120382 commit bc16499

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

src/main/java/org/opensearch/ad/indices/ADIndexManagement.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.opensearch.ad.settings.AnomalyDetectorSettings.ANOMALY_DETECTION_STATE_INDEX_MAPPING_FILE;
2020
import static org.opensearch.ad.settings.AnomalyDetectorSettings.ANOMALY_RESULTS_INDEX_MAPPING_FILE;
2121
import static org.opensearch.ad.settings.AnomalyDetectorSettings.CHECKPOINT_INDEX_MAPPING_FILE;
22+
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE;
23+
import static org.opensearch.indices.replication.common.ReplicationType.DOCUMENT;
2224

2325
import java.io.IOException;
2426
import java.util.EnumMap;
@@ -64,7 +66,7 @@ public class ADIndexManagement extends IndexManagement<ADIndex> {
6466
* @param settings OS cluster setting
6567
* @param nodeFilter Used to filter eligible nodes to host AD indices
6668
* @param maxUpdateRunningTimes max number of retries to update index mapping and setting
67-
* @throws IOException
69+
* @throws IOException when failing to get mapping file
6870
*/
6971
public ADIndexManagement(
7072
Client client,
@@ -195,7 +197,10 @@ public void initDefaultResultIndexDirectly(ActionListener<CreateIndexResponse> a
195197
@Override
196198
public void initStateIndex(ActionListener<CreateIndexResponse> actionListener) {
197199
try {
198-
CreateIndexRequest request = new CreateIndexRequest(ADCommonName.DETECTION_STATE_INDEX)
200+
// AD indices need RAW (e.g., we want users to be able to consume AD results as soon as possible and send out an alert if
201+
// anomalies found).
202+
Settings replicationSettings = Settings.builder().put(SETTING_REPLICATION_TYPE, DOCUMENT.name()).build();
203+
CreateIndexRequest request = new CreateIndexRequest(ADCommonName.DETECTION_STATE_INDEX, replicationSettings)
199204
.mapping(getStateMappings(), XContentType.JSON)
200205
.settings(settings);
201206
adminClient.indices().create(request, markMappingUpToDate(ADIndex.STATE, actionListener));
@@ -219,7 +224,11 @@ public void initCheckpointIndex(ActionListener<CreateIndexResponse> actionListen
219224
} catch (IOException e) {
220225
throw new EndRunException("", "Cannot find checkpoint mapping file", true);
221226
}
222-
CreateIndexRequest request = new CreateIndexRequest(ADCommonName.CHECKPOINT_INDEX_NAME).mapping(mapping, XContentType.JSON);
227+
// AD indices need RAW (e.g., we want users to be able to consume AD results as soon as possible and send out an alert if anomalies
228+
// found).
229+
Settings replicationSettings = Settings.builder().put(SETTING_REPLICATION_TYPE, DOCUMENT.name()).build();
230+
CreateIndexRequest request = new CreateIndexRequest(ADCommonName.CHECKPOINT_INDEX_NAME, replicationSettings)
231+
.mapping(mapping, XContentType.JSON);
223232
choosePrimaryShards(request, true);
224233
adminClient.indices().create(request, markMappingUpToDate(ADIndex.CHECKPOINT, actionListener));
225234
}

src/main/java/org/opensearch/forecast/indices/ForecastIndexManagement.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
package org.opensearch.forecast.indices;
1313

14+
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE;
1415
import static org.opensearch.forecast.constant.ForecastCommonName.DUMMY_FORECAST_RESULT_ID;
1516
import static org.opensearch.forecast.settings.ForecastSettings.FORECAST_CHECKPOINT_INDEX_MAPPING_FILE;
1617
import static org.opensearch.forecast.settings.ForecastSettings.FORECAST_MAX_PRIMARY_SHARDS;
@@ -19,6 +20,7 @@
1920
import static org.opensearch.forecast.settings.ForecastSettings.FORECAST_RESULT_HISTORY_RETENTION_PERIOD;
2021
import static org.opensearch.forecast.settings.ForecastSettings.FORECAST_RESULT_HISTORY_ROLLOVER_PERIOD;
2122
import static org.opensearch.forecast.settings.ForecastSettings.FORECAST_STATE_INDEX_MAPPING_FILE;
23+
import static org.opensearch.indices.replication.common.ReplicationType.DOCUMENT;
2224

2325
import java.io.IOException;
2426
import java.util.EnumMap;
@@ -61,7 +63,7 @@ public class ForecastIndexManagement extends IndexManagement<ForecastIndex> {
6163
* @param settings OS cluster setting
6264
* @param nodeFilter Used to filter eligible nodes to host forecast indices
6365
* @param maxUpdateRunningTimes max number of retries to update index mapping and setting
64-
* @throws IOException
66+
* @throws IOException when failing to get mapping file
6567
*/
6668
public ForecastIndexManagement(
6769
Client client,
@@ -177,7 +179,8 @@ public boolean doesCheckpointIndexExist() {
177179
@Override
178180
public void initStateIndex(ActionListener<CreateIndexResponse> actionListener) {
179181
try {
180-
CreateIndexRequest request = new CreateIndexRequest(ForecastCommonName.FORECAST_STATE_INDEX)
182+
Settings replicationSettings = Settings.builder().put(SETTING_REPLICATION_TYPE, DOCUMENT.name()).build();
183+
CreateIndexRequest request = new CreateIndexRequest(ForecastCommonName.FORECAST_STATE_INDEX, replicationSettings)
181184
.mapping(getStateMappings(), XContentType.JSON)
182185
.settings(settings);
183186
adminClient.indices().create(request, markMappingUpToDate(ForecastIndex.STATE, actionListener));
@@ -201,7 +204,10 @@ public void initCheckpointIndex(ActionListener<CreateIndexResponse> actionListen
201204
} catch (IOException e) {
202205
throw new EndRunException("", "Cannot find checkpoint mapping file", true);
203206
}
204-
CreateIndexRequest request = new CreateIndexRequest(ForecastCommonName.FORECAST_CHECKPOINT_INDEX_NAME)
207+
// forecast indices need RAW (e.g., we want users to be able to consume forecast results as soon as
208+
// possible and send out an alert if a threshold is breached).
209+
Settings replicationSettings = Settings.builder().put(SETTING_REPLICATION_TYPE, DOCUMENT.name()).build();
210+
CreateIndexRequest request = new CreateIndexRequest(ForecastCommonName.FORECAST_CHECKPOINT_INDEX_NAME, replicationSettings)
205211
.mapping(mapping, XContentType.JSON);
206212
choosePrimaryShards(request, true);
207213
adminClient.indices().create(request, markMappingUpToDate(ForecastIndex.CHECKPOINT, actionListener));

src/main/java/org/opensearch/forecast/settings/ForecastSettings.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public final class ForecastSettings {
111111

112112
// max number of primary shards of a forecast index
113113
public static final Setting<Integer> FORECAST_MAX_PRIMARY_SHARDS = Setting
114-
.intSetting("plugins.forecast.max_primary_shards", 10, 0, 200, Setting.Property.NodeScope, Setting.Property.Dynamic);
114+
.intSetting("plugins.forecast.max_primary_shards", 20, 0, 200, Setting.Property.NodeScope, Setting.Property.Dynamic);
115115

116116
// ======================================
117117
// Security

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
package org.opensearch.timeseries.indices;
1313

14+
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE;
15+
import static org.opensearch.indices.replication.common.ReplicationType.DOCUMENT;
1416
import static org.opensearch.timeseries.constant.CommonMessages.CAN_NOT_FIND_RESULT_INDEX;
1517

1618
import java.io.IOException;
@@ -426,7 +428,10 @@ public void initConfigIndexIfAbsent(ActionListener<CreateIndexResponse> actionLi
426428
* @throws IOException IOException from {@link IndexManagement#getConfigMappings}
427429
*/
428430
public void initConfigIndex(ActionListener<CreateIndexResponse> actionListener) throws IOException {
429-
CreateIndexRequest request = new CreateIndexRequest(CommonName.CONFIG_INDEX)
431+
// time series indices need RAW (e.g., we want users to be able to consume AD results as soon as possible
432+
// and send out an alert if anomalies found).
433+
Settings replicationSettings = Settings.builder().put(SETTING_REPLICATION_TYPE, DOCUMENT.name()).build();
434+
CreateIndexRequest request = new CreateIndexRequest(CommonName.CONFIG_INDEX, replicationSettings)
430435
.mapping(getConfigMappings(), XContentType.JSON)
431436
.settings(settings);
432437
adminClient.indices().create(request, actionListener);
@@ -477,7 +482,11 @@ public static String getJobMappings() throws IOException {
477482
*/
478483
public void initJobIndex(ActionListener<CreateIndexResponse> actionListener) {
479484
try {
480-
CreateIndexRequest request = new CreateIndexRequest(CommonName.JOB_INDEX).mapping(getJobMappings(), XContentType.JSON);
485+
// time series indices need RAW (e.g., we want users to be able to consume AD results as soon as
486+
// possible and send out an alert if anomalies found).
487+
Settings replicationSettings = Settings.builder().put(SETTING_REPLICATION_TYPE, DOCUMENT.name()).build();
488+
CreateIndexRequest request = new CreateIndexRequest(CommonName.JOB_INDEX, replicationSettings)
489+
.mapping(getJobMappings(), XContentType.JSON);
481490
request
482491
.settings(
483492
Settings
@@ -928,7 +937,10 @@ protected void rolloverAndDeleteHistoryIndex(
928937

929938
CreateIndexRequest createRequest = rollOverRequest.getCreateIndexRequest();
930939

931-
createRequest.index(rolloverIndexPattern).mapping(resultMapping, XContentType.JSON);
940+
// time series indices need RAW (e.g., we want users to be able to consume AD results as soon as possible
941+
// and send out an alert if anomalies found).
942+
Settings replicationSettings = Settings.builder().put(SETTING_REPLICATION_TYPE, DOCUMENT.name()).build();
943+
createRequest.index(rolloverIndexPattern).settings(replicationSettings).mapping(resultMapping, XContentType.JSON);
932944

933945
choosePrimaryShards(createRequest, true);
934946

@@ -953,7 +965,10 @@ protected void initResultIndexDirectly(
953965
IndexType resultIndex,
954966
ActionListener<CreateIndexResponse> actionListener
955967
) {
956-
CreateIndexRequest request = new CreateIndexRequest(resultIndexName).mapping(resultMapping, XContentType.JSON);
968+
// time series indices need RAW (e.g., we want users to be able to consume AD results as soon as possible
969+
// and send out an alert if anomalies found).
970+
Settings replicationSettings = Settings.builder().put(SETTING_REPLICATION_TYPE, DOCUMENT.name()).build();
971+
CreateIndexRequest request = new CreateIndexRequest(resultIndexName, replicationSettings).mapping(resultMapping, XContentType.JSON);
957972
if (alias != null) {
958973
request.alias(new Alias(alias));
959974
}

0 commit comments

Comments
 (0)