Remove some raw warnings from settings
Also sneaks in a couple of javadocs and one early return that I think makes the method easier to read.
This commit is contained in:
parent
69b21feb3b
commit
b14371bd22
|
@ -36,7 +36,7 @@ import java.util.function.Consumer;
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractScopedSettings extends AbstractComponent {
|
public abstract class AbstractScopedSettings extends AbstractComponent {
|
||||||
private Settings lastSettingsApplied = Settings.EMPTY;
|
private Settings lastSettingsApplied = Settings.EMPTY;
|
||||||
private final List<SettingUpdater> settingUpdaters = new ArrayList<>();
|
private final List<SettingUpdater<?>> settingUpdaters = new ArrayList<>();
|
||||||
private final Map<String, Setting<?>> complexMatchers = new HashMap<>();
|
private final Map<String, Setting<?>> complexMatchers = new HashMap<>();
|
||||||
private final Map<String, Setting<?>> keySettings = new HashMap<>();
|
private final Map<String, Setting<?>> keySettings = new HashMap<>();
|
||||||
private final Setting.Scope scope;
|
private final Setting.Scope scope;
|
||||||
|
@ -68,7 +68,7 @@ public abstract class AbstractScopedSettings extends AbstractComponent {
|
||||||
final Settings current = Settings.builder().put(this.settings).put(settings).build();
|
final Settings current = Settings.builder().put(this.settings).put(settings).build();
|
||||||
final Settings previous = Settings.builder().put(this.settings).put(this.lastSettingsApplied).build();
|
final Settings previous = Settings.builder().put(this.settings).put(this.lastSettingsApplied).build();
|
||||||
List<RuntimeException> exceptions = new ArrayList<>();
|
List<RuntimeException> exceptions = new ArrayList<>();
|
||||||
for (SettingUpdater settingUpdater : settingUpdaters) {
|
for (SettingUpdater<?> settingUpdater : settingUpdaters) {
|
||||||
try {
|
try {
|
||||||
if (settingUpdater.hasChanged(current, previous)) {
|
if (settingUpdater.hasChanged(current, previous)) {
|
||||||
settingUpdater.getValue(current, previous);
|
settingUpdater.getValue(current, previous);
|
||||||
|
@ -99,7 +99,7 @@ public abstract class AbstractScopedSettings extends AbstractComponent {
|
||||||
final Settings previous = Settings.builder().put(this.settings).put(this.lastSettingsApplied).build();
|
final Settings previous = Settings.builder().put(this.settings).put(this.lastSettingsApplied).build();
|
||||||
try {
|
try {
|
||||||
List<Runnable> applyRunnables = new ArrayList<>();
|
List<Runnable> applyRunnables = new ArrayList<>();
|
||||||
for (SettingUpdater settingUpdater : settingUpdaters) {
|
for (SettingUpdater<?> settingUpdater : settingUpdaters) {
|
||||||
try {
|
try {
|
||||||
applyRunnables.add(settingUpdater.updater(current, previous));
|
applyRunnables.add(settingUpdater.updater(current, previous));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
@ -164,6 +164,7 @@ public abstract class AbstractScopedSettings extends AbstractComponent {
|
||||||
/**
|
/**
|
||||||
* Transactional interface to update settings.
|
* Transactional interface to update settings.
|
||||||
* @see Setting
|
* @see Setting
|
||||||
|
* @param <T> the type of the value of the setting
|
||||||
*/
|
*/
|
||||||
public interface SettingUpdater<T> {
|
public interface SettingUpdater<T> {
|
||||||
|
|
||||||
|
@ -216,17 +217,16 @@ public abstract class AbstractScopedSettings extends AbstractComponent {
|
||||||
/**
|
/**
|
||||||
* Returns the {@link Setting} for the given key or <code>null</code> if the setting can not be found.
|
* Returns the {@link Setting} for the given key or <code>null</code> if the setting can not be found.
|
||||||
*/
|
*/
|
||||||
public Setting get(String key) {
|
public Setting<?> get(String key) {
|
||||||
Setting<?> setting = keySettings.get(key);
|
Setting<?> setting = keySettings.get(key);
|
||||||
if (setting == null) {
|
if (setting != null) {
|
||||||
for (Map.Entry<String, Setting<?>> entry : complexMatchers.entrySet()) {
|
|
||||||
if (entry.getValue().match(key)) {
|
|
||||||
return entry.getValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return setting;
|
return setting;
|
||||||
}
|
}
|
||||||
|
for (Map.Entry<String, Setting<?>> entry : complexMatchers.entrySet()) {
|
||||||
|
if (entry.getValue().match(key)) {
|
||||||
|
return entry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,7 +234,7 @@ public abstract class AbstractScopedSettings extends AbstractComponent {
|
||||||
* Returns <code>true</code> if the setting for the given key is dynamically updateable. Otherwise <code>false</code>.
|
* Returns <code>true</code> if the setting for the given key is dynamically updateable. Otherwise <code>false</code>.
|
||||||
*/
|
*/
|
||||||
public boolean hasDynamicSetting(String key) {
|
public boolean hasDynamicSetting(String key) {
|
||||||
final Setting setting = get(key);
|
final Setting<?> setting = get(key);
|
||||||
return setting != null && setting.isDynamic();
|
return setting != null && setting.isDynamic();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,8 @@ import java.util.function.Function;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* A setting. Encapsulates typical stuff like default value, parsing, and scope.
|
||||||
|
* Some (dynamic=true) can by modified at run time using the API.
|
||||||
*/
|
*/
|
||||||
public class Setting<T> extends ToXContentToBytes {
|
public class Setting<T> extends ToXContentToBytes {
|
||||||
private final String key;
|
private final String key;
|
||||||
|
@ -173,11 +175,18 @@ public class Setting<T> extends ToXContentToBytes {
|
||||||
INDEX;
|
INDEX;
|
||||||
}
|
}
|
||||||
|
|
||||||
final AbstractScopedSettings.SettingUpdater newUpdater(Consumer<T> consumer, ESLogger logger) {
|
/**
|
||||||
|
* Build a new updater with a noop validator.
|
||||||
|
*/
|
||||||
|
final AbstractScopedSettings.SettingUpdater<T> newUpdater(Consumer<T> consumer, ESLogger logger) {
|
||||||
return newUpdater(consumer, logger, (s) -> {});
|
return newUpdater(consumer, logger, (s) -> {});
|
||||||
}
|
}
|
||||||
|
|
||||||
AbstractScopedSettings.SettingUpdater newUpdater(Consumer<T> consumer, ESLogger logger, Consumer<T> validator) {
|
/**
|
||||||
|
* Build the updater responsible for validating new values, logging the new
|
||||||
|
* value, and eventually setting the value where it belongs.
|
||||||
|
*/
|
||||||
|
AbstractScopedSettings.SettingUpdater<T> newUpdater(Consumer<T> consumer, ESLogger logger, Consumer<T> validator) {
|
||||||
if (isDynamic()) {
|
if (isDynamic()) {
|
||||||
return new Updater(consumer, logger, validator);
|
return new Updater(consumer, logger, validator);
|
||||||
} else {
|
} else {
|
||||||
|
@ -343,6 +352,7 @@ public class Setting<T> extends ToXContentToBytes {
|
||||||
return array == null ? defaultValue.apply(settings) : arrayToParsableString(array);
|
return array == null ? defaultValue.apply(settings) : arrayToParsableString(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean match(String toTest) {
|
public boolean match(String toTest) {
|
||||||
return pattern.matcher(toTest).matches();
|
return pattern.matcher(toTest).matches();
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class SettingTests extends ESTestCase {
|
||||||
ByteSizeValue byteSizeValue = byteSizeValueSetting.get(Settings.EMPTY);
|
ByteSizeValue byteSizeValue = byteSizeValueSetting.get(Settings.EMPTY);
|
||||||
assertEquals(byteSizeValue.bytes(), 1024);
|
assertEquals(byteSizeValue.bytes(), 1024);
|
||||||
AtomicReference<ByteSizeValue> value = new AtomicReference<>(null);
|
AtomicReference<ByteSizeValue> value = new AtomicReference<>(null);
|
||||||
ClusterSettings.SettingUpdater settingUpdater = byteSizeValueSetting.newUpdater(value::set, logger);
|
ClusterSettings.SettingUpdater<ByteSizeValue> settingUpdater = byteSizeValueSetting.newUpdater(value::set, logger);
|
||||||
try {
|
try {
|
||||||
settingUpdater.apply(Settings.builder().put("a.byte.size", 12).build(), Settings.EMPTY);
|
settingUpdater.apply(Settings.builder().put("a.byte.size", 12).build(), Settings.EMPTY);
|
||||||
fail("no unit");
|
fail("no unit");
|
||||||
|
@ -60,7 +60,7 @@ public class SettingTests extends ESTestCase {
|
||||||
public void testSimpleUpdate() {
|
public void testSimpleUpdate() {
|
||||||
Setting<Boolean> booleanSetting = Setting.boolSetting("foo.bar", false, true, Setting.Scope.CLUSTER);
|
Setting<Boolean> booleanSetting = Setting.boolSetting("foo.bar", false, true, Setting.Scope.CLUSTER);
|
||||||
AtomicReference<Boolean> atomicBoolean = new AtomicReference<>(null);
|
AtomicReference<Boolean> atomicBoolean = new AtomicReference<>(null);
|
||||||
ClusterSettings.SettingUpdater settingUpdater = booleanSetting.newUpdater(atomicBoolean::set, logger);
|
ClusterSettings.SettingUpdater<Boolean> settingUpdater = booleanSetting.newUpdater(atomicBoolean::set, logger);
|
||||||
Settings build = Settings.builder().put("foo.bar", false).build();
|
Settings build = Settings.builder().put("foo.bar", false).build();
|
||||||
settingUpdater.apply(build, Settings.EMPTY);
|
settingUpdater.apply(build, Settings.EMPTY);
|
||||||
assertNull(atomicBoolean.get());
|
assertNull(atomicBoolean.get());
|
||||||
|
@ -94,8 +94,7 @@ public class SettingTests extends ESTestCase {
|
||||||
Setting<Boolean> booleanSetting = Setting.boolSetting("foo.bar", false, true, Setting.Scope.CLUSTER);
|
Setting<Boolean> booleanSetting = Setting.boolSetting("foo.bar", false, true, Setting.Scope.CLUSTER);
|
||||||
AtomicReference<Boolean> ab1 = new AtomicReference<>(null);
|
AtomicReference<Boolean> ab1 = new AtomicReference<>(null);
|
||||||
AtomicReference<Boolean> ab2 = new AtomicReference<>(null);
|
AtomicReference<Boolean> ab2 = new AtomicReference<>(null);
|
||||||
ClusterSettings.SettingUpdater settingUpdater = booleanSetting.newUpdater(ab1::set, logger);
|
ClusterSettings.SettingUpdater<Boolean> settingUpdater = booleanSetting.newUpdater(ab1::set, logger);
|
||||||
ClusterSettings.SettingUpdater settingUpdater2 = booleanSetting.newUpdater(ab2::set, logger);
|
|
||||||
settingUpdater.apply(Settings.builder().put("foo.bar", true).build(), Settings.EMPTY);
|
settingUpdater.apply(Settings.builder().put("foo.bar", true).build(), Settings.EMPTY);
|
||||||
assertTrue(ab1.get());
|
assertTrue(ab1.get());
|
||||||
assertNull(ab2.get());
|
assertNull(ab2.get());
|
||||||
|
@ -120,7 +119,7 @@ public class SettingTests extends ESTestCase {
|
||||||
assertFalse(setting.isGroupSetting());
|
assertFalse(setting.isGroupSetting());
|
||||||
ref.set(setting.get(Settings.EMPTY));
|
ref.set(setting.get(Settings.EMPTY));
|
||||||
ComplexType type = ref.get();
|
ComplexType type = ref.get();
|
||||||
ClusterSettings.SettingUpdater settingUpdater = setting.newUpdater(ref::set, logger);
|
ClusterSettings.SettingUpdater<ComplexType> settingUpdater = setting.newUpdater(ref::set, logger);
|
||||||
assertFalse(settingUpdater.apply(Settings.EMPTY, Settings.EMPTY));
|
assertFalse(settingUpdater.apply(Settings.EMPTY, Settings.EMPTY));
|
||||||
assertSame("no update - type has not changed", type, ref.get());
|
assertSame("no update - type has not changed", type, ref.get());
|
||||||
|
|
||||||
|
@ -147,7 +146,7 @@ public class SettingTests extends ESTestCase {
|
||||||
AtomicReference<Settings> ref = new AtomicReference<>(null);
|
AtomicReference<Settings> ref = new AtomicReference<>(null);
|
||||||
Setting<Settings> setting = Setting.groupSetting("foo.bar.", true, Setting.Scope.CLUSTER);
|
Setting<Settings> setting = Setting.groupSetting("foo.bar.", true, Setting.Scope.CLUSTER);
|
||||||
assertTrue(setting.isGroupSetting());
|
assertTrue(setting.isGroupSetting());
|
||||||
ClusterSettings.SettingUpdater settingUpdater = setting.newUpdater(ref::set, logger);
|
ClusterSettings.SettingUpdater<Settings> settingUpdater = setting.newUpdater(ref::set, logger);
|
||||||
|
|
||||||
Settings currentInput = Settings.builder().put("foo.bar.1.value", "1").put("foo.bar.2.value", "2").put("foo.bar.3.value", "3").build();
|
Settings currentInput = Settings.builder().put("foo.bar.1.value", "1").put("foo.bar.2.value", "2").put("foo.bar.3.value", "3").build();
|
||||||
Settings previousInput = Settings.EMPTY;
|
Settings previousInput = Settings.EMPTY;
|
||||||
|
@ -191,7 +190,7 @@ public class SettingTests extends ESTestCase {
|
||||||
assertTrue(setting.match("foo.bar.baz"));
|
assertTrue(setting.match("foo.bar.baz"));
|
||||||
assertFalse(setting.match("foo.baz.bar"));
|
assertFalse(setting.match("foo.baz.bar"));
|
||||||
|
|
||||||
ClusterSettings.SettingUpdater predicateSettingUpdater = setting.newUpdater(ref::set, logger,(s) -> assertFalse(true));
|
ClusterSettings.SettingUpdater<Settings> predicateSettingUpdater = setting.newUpdater(ref::set, logger,(s) -> assertFalse(true));
|
||||||
try {
|
try {
|
||||||
predicateSettingUpdater.apply(Settings.builder().put("foo.bar.1.value", "1").put("foo.bar.2.value", "2").build(), Settings.EMPTY);
|
predicateSettingUpdater.apply(Settings.builder().put("foo.bar.1.value", "1").put("foo.bar.2.value", "2").build(), Settings.EMPTY);
|
||||||
fail("not accepted");
|
fail("not accepted");
|
||||||
|
@ -273,7 +272,7 @@ public class SettingTests extends ESTestCase {
|
||||||
assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0]));
|
assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0]));
|
||||||
|
|
||||||
AtomicReference<List<String>> ref = new AtomicReference<>();
|
AtomicReference<List<String>> ref = new AtomicReference<>();
|
||||||
AbstractScopedSettings.SettingUpdater settingUpdater = listSetting.newUpdater(ref::set, logger);
|
AbstractScopedSettings.SettingUpdater<List<String>> settingUpdater = listSetting.newUpdater(ref::set, logger);
|
||||||
assertTrue(settingUpdater.hasChanged(builder.build(), Settings.EMPTY));
|
assertTrue(settingUpdater.hasChanged(builder.build(), Settings.EMPTY));
|
||||||
settingUpdater.apply(builder.build(), Settings.EMPTY);
|
settingUpdater.apply(builder.build(), Settings.EMPTY);
|
||||||
assertEquals(input.size(), ref.get().size());
|
assertEquals(input.size(), ref.get().size());
|
||||||
|
|
Loading…
Reference in New Issue