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

Correctly determine defaults of settings which depend on other settings #65989

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,11 @@ private T get(Settings settings, boolean validate) {
* @param defaultSettings the default settings object to diff against
*/
public void diff(Settings.Builder builder, Settings source, Settings defaultSettings) {
if (exists(source) == false) {
if (exists(source) == false && exists(defaultSettings) == false) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this might read better if the outer "doesn't exist in source" stays the same and then have two inner conditions for "if (exists in defaults) X else Y"

// If the setting is in neither source nor default, get the value from source as it may depend on the value of other settings
builder.put(getKey(), getRaw(source));
} else if (exists(source) == false && exists(defaultSettings)) {
// If the setting is only in the defaults, use the value from the defaults
builder.put(getKey(), getRaw(defaultSettings));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,28 @@ public void testDiff() throws IOException {
assertThat(diff.getAsInt("foo.bar", null), equalTo(1));
}

public void testDiffWithDependentSettings() {
final String dependedSettingName = "this.setting.is.depended.on";
Setting<Integer> dependedSetting = Setting.intSetting(dependedSettingName, 1, Property.Dynamic, Property.NodeScope);

final String dependentSettingName = "this.setting.depends.on.another";
Setting<Integer> dependentSetting = new Setting<>(dependentSettingName,
(s) -> Integer.toString(dependedSetting.get(s) + 10),
(s) -> Setting.parseInt(s, 1, dependentSettingName),
Property.Dynamic, Property.NodeScope);

ClusterSettings settings = new ClusterSettings(Settings.EMPTY, new HashSet<>(Arrays.asList(dependedSetting, dependentSetting)));

// Ensure that the value of the dependent setting is correctly calculated based on the "source" settings
Settings diff = settings.diff(Settings.builder().put(dependedSettingName, 2).build(), Settings.EMPTY);
assertThat(diff.getAsInt(dependentSettingName, null), equalTo(12));

// Ensure that the value is correctly calculated if neither is set
diff = settings.diff(Settings.EMPTY, Settings.EMPTY);
assertThat(diff.getAsInt(dependedSettingName, null), equalTo(1));
assertThat(diff.getAsInt(dependentSettingName, null), equalTo(11));
}

public void testDiffWithAffixAndComplexMatcher() {
Setting<Integer> fooBarBaz = Setting.intSetting("foo.bar.baz", 1, Property.NodeScope);
Setting<Integer> fooBar = Setting.intSetting("foo.bar", 1, Property.Dynamic, Property.NodeScope);
Expand Down