Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions docs/changelog/134091.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 134091
summary: Use latest setting value when initializing setting watch
area: Infra/Settings
type: bug
issues:
- 133701
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,11 @@ public synchronized <T> void initializeAndWatch(Setting<T> setting, Consumer<T>
assert setting.getProperties().contains(Setting.Property.Dynamic)
|| setting.getProperties().contains(Setting.Property.OperatorDynamic) : "Can only watch dynamic settings";
assert setting.getProperties().contains(Setting.Property.NodeScope) : "Can only watch node settings";
consumer.accept(setting.get(settings));

// this mimics the combined settings of last applied and node settings, without building a new settings object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this comment I understand you are trying to avoid doing
Settings.builder().put(this.settings).put(this.lastSettingsApplied).build() like in other functions, correct?

Is there an advantage? Is it correct? (is lastSettingsApplied always a superset of settings?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building a combined Settings object is heavyweight. It requires combing the internal maps that each Settings object contains.

If the latter settings object contains a setting we are looking for, it would have overridden the value when combined, so looking at the latter first should mimic the combined behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building a combined Settings object is heavyweight. It requires combing the internal maps that each Settings object contains.

I understand that, but I was wondering if it really matters in this case - I might be mistaken, but this is an initialization method, so probably it's not on a hot path.

As long as it's correct I like the optimization though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an initialization method

While true, it's also called for every setting that is watched, currently 43 uses. Seems silly to reconstruct the same settings object 43 times (but also not worth the complexity to cache it in a threadsafe way).

Settings settingsWithValue = setting.exists(lastSettingsApplied) ? lastSettingsApplied : settings;

consumer.accept(setting.get(settingsWithValue));
addSettingsUpdateConsumer(setting, consumer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.action.admin.cluster.settings;

import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESTestCase;

import java.util.Set;

import static org.hamcrest.Matchers.equalTo;

public class ClusterSettingsTests extends ESTestCase {

public void testWatchAfterApply() {
Setting<String> clusterSetting = Setting.simpleString("cluster.setting", Setting.Property.NodeScope, Setting.Property.Dynamic);
Settings nodeSettings = Settings.builder().put("cluster.setting", "initial_value").build();

ClusterSettings clusterSettings = new ClusterSettings(nodeSettings, Set.of(clusterSetting));
Settings newSettings = Settings.builder().put("cluster.setting", "updated_value").build();
clusterSettings.applySettings(newSettings);

// the value should be current when initializing the consumer
clusterSettings.initializeAndWatch(clusterSetting, value -> { assertThat(value, equalTo("updated_value")); });
}
}