Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update setting API honors cluster's replica setting as default #14810 #14948

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update setting API honors cluster's replica setting as default #14810
Signed-off-by: Liyun Xiu <xiliyun@amazon.com>
  • Loading branch information
chishui committed Sep 23, 2024
commit e8acc370a87f230752d59b4db6c8a2143931412d
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,53 @@ private void runTestDefaultNumberOfReplicasTest(final boolean closeIndex) {
assertThat(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(response.getIndexToSettings().get("test")), equalTo(1));
}

public void testNullReplicaUpdate() {
internalCluster().ensureAtLeastNumDataNodes(2);
// cluster setting
String defaultNumberOfReplica = "3";
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("cluster.default_number_of_replicas", defaultNumberOfReplica))
.get()
);

assertAcked(
client().admin()
.indices()
.prepareCreate("test")
.setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, "2"))
);

String numberOfReplicas = client().admin()
.indices()
.prepareGetSettings("test")
.get()
.getSetting("test", IndexMetadata.SETTING_NUMBER_OF_REPLICAS);
assertEquals("2", numberOfReplicas);

assertAcked(
client().admin()
.indices()
.prepareUpdateSettings("test")
.setSettings(Settings.builder().putNull(IndexMetadata.SETTING_NUMBER_OF_REPLICAS))
);

numberOfReplicas = client().admin()
.indices()
.prepareGetSettings("test")
.get()
.getSetting("test", IndexMetadata.SETTING_NUMBER_OF_REPLICAS);
assertEquals(defaultNumberOfReplica, numberOfReplicas);
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().putNull("cluster.default_number_of_replicas"))
);
}

public void testNoopUpdate() {
internalCluster().ensureAtLeastNumDataNodes(2);
final ClusterService clusterService = internalCluster().getClusterManagerNodeInstance(ClusterService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public void updateSettings(

validateRefreshIntervalSettings(normalizedSettings, clusterService.getClusterSettings());
validateTranslogDurabilitySettings(normalizedSettings, clusterService.getClusterSettings(), clusterService.getSettings());
final int defaultReplicaCount = clusterService.getClusterSettings().get(Metadata.DEFAULT_REPLICA_COUNT_SETTING);

Settings.Builder settingsForClosedIndices = Settings.builder();
Settings.Builder settingsForOpenIndices = Settings.builder();
Expand Down Expand Up @@ -248,7 +249,10 @@ public ClusterState execute(ClusterState currentState) {
}

if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(openSettings)) {
final int updatedNumberOfReplicas = IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(openSettings);
final int updatedNumberOfReplicas = openSettings.getAsInt(
IndexMetadata.SETTING_NUMBER_OF_REPLICAS,
defaultReplicaCount
);
if (preserveExisting == false) {
for (Index index : request.indices()) {
if (index.getName().charAt(0) != '.') {
Expand Down Expand Up @@ -329,15 +333,13 @@ public ClusterState execute(ClusterState currentState) {
/*
* The setting index.number_of_replicas is special; we require that this setting has a value in the index. When
* creating the index, we ensure this by explicitly providing a value for the setting to the default (one) if
* there is a not value provided on the source of the index creation. A user can update this setting though,
* including updating it to null, indicating that they want to use the default value. In this case, we again
* have to provide an explicit value for the setting to the default (one).
* there is no value provided on the source of the index creation or "cluster.default_number_of_replicas" is
* not set. A user can update this setting though, including updating it to null, indicating that they want to
* use the value configured by cluster settings or a default value 1. In this case, we again have to provide
* an explicit value for the setting.
*/
if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(indexSettings) == false) {
indexSettings.put(
IndexMetadata.SETTING_NUMBER_OF_REPLICAS,
IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(Settings.EMPTY)
);
indexSettings.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, defaultReplicaCount);
}
Settings finalSettings = indexSettings.build();
indexScopedSettings.validate(
Expand Down