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

[Feature/extensions] Adding support to register settings dynamically #3753

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
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ protected AbstractScopedSettings(
keySettings.putIfAbsent(setting.getKey(), setting);
}
}
this.complexMatchers = Collections.unmodifiableMap(complexMatchers);
this.keySettings = Collections.unmodifiableMap(keySettings);
this.complexMatchers = complexMatchers;
this.keySettings = keySettings;
saratvemulapalli marked this conversation as resolved.
Show resolved Hide resolved
}

protected void validateSettingKey(Setting<?> setting) {
Expand All @@ -144,6 +144,15 @@ protected AbstractScopedSettings(Settings nodeSettings, Settings scopeSettings,
settingUpdaters.addAll(other.settingUpdaters);
}

protected boolean registerSetting(Setting<?> setting) {
Copy link
Member

Choose a reason for hiding this comment

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

Given that ClusterSettings and IndexScopedSettings are the only two subclasses of this class, why not simply make this method public and avoid the super calls in two places?

validateSettingKey(setting);
if (setting.hasComplexMatcher()) {
return setting != complexMatchers.putIfAbsent(setting.getKey(), setting);
} else {
return setting != keySettings.putIfAbsent(setting.getKey(), setting);
}
}

/**
* Returns <code>true</code> iff the given key is a valid settings key otherwise <code>false</code>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public ClusterSettings(final Settings nodeSettings, final Set<Setting<?>> settin
addSettingsUpdater(new LoggingSettingUpdater(nodeSettings));
}

public boolean registerSetting(Setting<?> setting) {
return super.registerSetting(setting);
}

private static final class LoggingSettingUpdater implements SettingUpdater<Settings> {
final Predicate<String> loggerPredicate = Loggers.LOG_LEVEL_SETTING::match;
private final Settings settings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public IndexScopedSettings copy(Settings settings, IndexMetadata metadata) {
return new IndexScopedSettings(settings, this, metadata);
}

public boolean registerSetting(Setting<?> setting) {
validateSettingKey(setting);
return super.registerSetting(setting);
}

@Override
protected void validateSettingKey(Setting setting) {
if (setting.getKey().startsWith("index.") == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,29 @@ public void configure(Binder binder) {
binder.bind(IndexScopedSettings.class).toInstance(indexScopedSettings);
}

/**
* Dynamically registers a new Setting at Runtime. This method is mostly used by plugins/extensions
* to register new settings at runtime. Settings can be of Node Scope or Index Scope.
* @param setting which is being registered in the cluster.
* @return boolean value is set to true when successfully registered, else returns false
*/
public boolean registerDynamicSetting(Setting<?> setting) {
try {
registerSetting(setting);
if (setting.hasNodeScope()) {
return clusterSettings.registerSetting(setting);
}
if (setting.hasIndexScope()) {
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick - else if here, unless it's possible for a setting to be both NodeScope and IndexScope

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

return indexScopedSettings.registerSetting(setting);
}
logger.info("Registered new Setting: " + setting.getKey() + " successfully ");
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick - This log line will be printed inconsistently since there are return statements above. Consider moving this to the caller, or before the return statements

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for this. The return came later after the feedback.
Sure will send out a follow up.

} catch (Exception e) {
logger.error("Could not register setting " + setting.getKey());
saratvemulapalli marked this conversation as resolved.
Show resolved Hide resolved
throw new SettingsException("Could not register setting:" + setting.getKey());
}
return false;
}

/**
* Registers a new setting. This method should be used by plugins in order to expose any custom settings the plugin defines.
* Unless a setting is registered the setting is unusable. If a setting is never the less specified the node will reject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,46 @@ public void testOldMaxClauseCountSetting() {
ex.getMessage()
);
}

public void testDynamicNodeSettingsRegistration() {
Settings settings = Settings.builder().put("some.custom.setting", "2.0").build();
SettingsModule module = new SettingsModule(settings, Setting.floatSetting("some.custom.setting", 1.0f, Property.NodeScope));
assertNotNull(module.getClusterSettings().get("some.custom.setting"));
// For unregistered setting the value is expected to be null
assertNull(module.getClusterSettings().get("some.custom.setting2"));
assertInstanceBinding(module, Settings.class, (s) -> s == settings);

assertTrue(module.registerDynamicSetting(Setting.floatSetting("some.custom.setting2", 1.0f, Property.NodeScope)));
assertNotNull(module.getClusterSettings().get("some.custom.setting2"));
saratvemulapalli marked this conversation as resolved.
Show resolved Hide resolved

// verify if some.custom.setting still exists
assertNotNull(module.getClusterSettings().get("some.custom.setting"));

// verify exception is thrown when setting registration fails
expectThrows(
SettingsException.class,
() -> module.registerDynamicSetting(Setting.floatSetting("some.custom.setting", 1.0f, Property.NodeScope))
);
}

public void testDynamicIndexSettingsRegistration() {
Settings settings = Settings.builder().put("some.custom.setting", "2.0").build();
SettingsModule module = new SettingsModule(settings, Setting.floatSetting("some.custom.setting", 1.0f, Property.NodeScope));
saratvemulapalli marked this conversation as resolved.
Show resolved Hide resolved
assertNotNull(module.getClusterSettings().get("some.custom.setting"));
// For unregistered setting the value is expected to be null
assertNull(module.getIndexScopedSettings().get("index.custom.setting2"));
assertInstanceBinding(module, Settings.class, (s) -> s == settings);

assertTrue(module.registerDynamicSetting(Setting.floatSetting("index.custom.setting2", 1.0f, Property.IndexScope)));
assertNotNull(module.getIndexScopedSettings().get("index.custom.setting2"));

// verify if some.custom.setting still exists
assertNotNull(module.getClusterSettings().get("some.custom.setting"));

// verify exception is thrown when setting registration fails
expectThrows(
SettingsException.class,
() -> module.registerDynamicSetting(Setting.floatSetting("index.custom.setting2", 1.0f, Property.IndexScope))
);
}
}