Skip to content

Commit

Permalink
Fix template setting override for replication type
Browse files Browse the repository at this point in the history
Signed-off-by: Kunal Kotwani <kkotwani@amazon.com>
  • Loading branch information
kotwanikunal committed Dec 1, 2023
1 parent edf7861 commit 5569cd8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix for stuck update action in a bulk with `retry_on_conflict` property ([#11152](https://github.com/opensearch-project/OpenSearch/issues/11152))
- Remove shadowJar from `lang-painless` module publication ([#11369](https://github.com/opensearch-project/OpenSearch/issues/11369))
- Fix remote shards balancer and remove unused variables ([#11167](https://github.com/opensearch-project/OpenSearch/pull/11167))
- Fix template setting override for replication type ([#11417](https://github.com/opensearch-project/OpenSearch/pull/11417))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ static Settings aggregateIndexSettings(
indexSettingsBuilder.put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, request.getProvidedName());
indexSettingsBuilder.put(SETTING_INDEX_UUID, UUIDs.randomBase64UUID());

updateReplicationStrategy(indexSettingsBuilder, request.settings(), settings);
updateReplicationStrategy(indexSettingsBuilder, request.settings(), settings, combinedTemplateSettings, sourceMetadata);
updateRemoteStoreSettings(indexSettingsBuilder, settings);

if (sourceMetadata != null) {
Expand Down Expand Up @@ -934,17 +934,38 @@ static Settings aggregateIndexSettings(
* @param settingsBuilder index settings builder to be updated with relevant settings
* @param requestSettings settings passed in during index create request
* @param clusterSettings cluster level settings
* @param combinedTemplateSettings combined template settings which satisfy the index
*
*/
private static void updateReplicationStrategy(Settings.Builder settingsBuilder, Settings requestSettings, Settings clusterSettings) {
if (INDEX_REPLICATION_TYPE_SETTING.exists(requestSettings)) {
settingsBuilder.put(SETTING_REPLICATION_TYPE, INDEX_REPLICATION_TYPE_SETTING.get(requestSettings));
} else if (CLUSTER_REPLICATION_TYPE_SETTING.exists(clusterSettings)) {
settingsBuilder.put(SETTING_REPLICATION_TYPE, CLUSTER_REPLICATION_TYPE_SETTING.get(clusterSettings));
} else if (isRemoteStoreAttributePresent(clusterSettings)) {
private static void updateReplicationStrategy(
Settings.Builder settingsBuilder,
Settings requestSettings,
Settings clusterSettings,
Settings combinedTemplateSettings,
IndexMetadata sourceMetadata
) {
// The replication setting is applied in the following order:
// 1. For remote store clusters, SegRep is the only replication type supported
// 2. For non-remote store clusters, the order is as follows -
// i. Explicit index creation request parameter
// ii. Source index metadata for replication type property
// iii. Template property for replication type
// iv. Default cluster level setting

if (isRemoteStoreAttributePresent(clusterSettings)) {
settingsBuilder.put(SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT);
} else {
settingsBuilder.put(SETTING_REPLICATION_TYPE, CLUSTER_REPLICATION_TYPE_SETTING.getDefault(clusterSettings));
return;
}

ReplicationType indexReplicationType = CLUSTER_REPLICATION_TYPE_SETTING.get(clusterSettings);
if (INDEX_REPLICATION_TYPE_SETTING.exists(requestSettings)) {
indexReplicationType = INDEX_REPLICATION_TYPE_SETTING.get(requestSettings);
} else if (sourceMetadata != null && INDEX_REPLICATION_TYPE_SETTING.exists(sourceMetadata.getSettings())) {
indexReplicationType = INDEX_REPLICATION_TYPE_SETTING.get(sourceMetadata.getSettings());
} else if (INDEX_REPLICATION_TYPE_SETTING.exists(combinedTemplateSettings)) {
indexReplicationType = INDEX_REPLICATION_TYPE_SETTING.get(combinedTemplateSettings);
}
settingsBuilder.put(SETTING_REPLICATION_TYPE, indexReplicationType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING;
import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING;
import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK;
import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_REPLICATION_TYPE_SETTING;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_READ_ONLY;
Expand Down Expand Up @@ -1217,6 +1218,27 @@ public void testvalidateIndexSettings() {
threadPool.shutdown();
}

public void testIndexTemplateReplicationType() {
Settings templateSettings = Settings.builder().put(INDEX_REPLICATION_TYPE_SETTING.getKey(), ReplicationType.SEGMENT).build();

request = new CreateIndexClusterStateUpdateRequest("create index", "test", "test");
final Settings.Builder requestSettings = Settings.builder();
request.settings(requestSettings.build());
Settings indexSettings = aggregateIndexSettings(
ClusterState.EMPTY_STATE,
request,
templateSettings,
null,
Settings.EMPTY,
IndexScopedSettings.DEFAULT_SCOPED_SETTINGS,
randomShardLimitService(),
Collections.emptySet(),
clusterSettings
);
assertNotEquals(ReplicationType.SEGMENT, clusterSettings.get(CLUSTER_REPLICATION_TYPE_SETTING));
assertEquals(ReplicationType.SEGMENT.toString(), indexSettings.get(INDEX_REPLICATION_TYPE_SETTING.getKey()));
}

public void testRemoteStoreNoUserOverrideExceptReplicationTypeSegmentIndexSettings() {
Settings settings = Settings.builder()
.put(CLUSTER_REPLICATION_TYPE_SETTING.getKey(), ReplicationType.DOCUMENT)
Expand Down

0 comments on commit 5569cd8

Please sign in to comment.