fix cluster settings to accept complex list settigns
This commit is contained in:
parent
e586c966be
commit
c1ff619781
|
@ -21,7 +21,6 @@ package org.elasticsearch.common.settings;
|
||||||
|
|
||||||
import org.elasticsearch.ExceptionsHelper;
|
import org.elasticsearch.ExceptionsHelper;
|
||||||
import org.elasticsearch.common.component.AbstractComponent;
|
import org.elasticsearch.common.component.AbstractComponent;
|
||||||
import org.elasticsearch.common.logging.ESLoggerFactory;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
@ -34,7 +33,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<?>> groupSettings = 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;
|
||||||
|
|
||||||
|
@ -45,7 +44,7 @@ public abstract class AbstractScopedSettings extends AbstractComponent {
|
||||||
throw new IllegalArgumentException("Setting must be a cluster setting but was: " + entry.getScope());
|
throw new IllegalArgumentException("Setting must be a cluster setting but was: " + entry.getScope());
|
||||||
}
|
}
|
||||||
if (entry.isGroupSetting()) {
|
if (entry.isGroupSetting()) {
|
||||||
groupSettings.put(entry.getKey(), entry);
|
complexMatchers.put(entry.getKey(), entry);
|
||||||
} else {
|
} else {
|
||||||
keySettings.put(entry.getKey(), entry);
|
keySettings.put(entry.getKey(), entry);
|
||||||
}
|
}
|
||||||
|
@ -216,7 +215,7 @@ public abstract class AbstractScopedSettings extends AbstractComponent {
|
||||||
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 : groupSettings.entrySet()) {
|
for (Map.Entry<String, Setting<?>> entry : complexMatchers.entrySet()) {
|
||||||
if (entry.getValue().match(key)) {
|
if (entry.getValue().match(key)) {
|
||||||
return entry.getValue();
|
return entry.getValue();
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,7 @@ import java.util.List;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
|
@ -95,6 +96,10 @@ public class Setting<T> extends ToXContentToBytes {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean hasComplexMatcher() {
|
||||||
|
return isGroupSetting();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the default values string representation for this setting.
|
* Returns the default values string representation for this setting.
|
||||||
* @param settings a settings object for settings that has a default value depending on another setting if available
|
* @param settings a settings object for settings that has a default value depending on another setting if available
|
||||||
|
@ -143,7 +148,7 @@ public class Setting<T> extends ToXContentToBytes {
|
||||||
* @see #isGroupSetting()
|
* @see #isGroupSetting()
|
||||||
*/
|
*/
|
||||||
public boolean match(String toTest) {
|
public boolean match(String toTest) {
|
||||||
return Regex.simpleMatch(key, toTest);
|
return key.equals(toTest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -326,15 +331,21 @@ public class Setting<T> extends ToXContentToBytes {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return new Setting<List<T>>(key, arrayToParsableString(defaultStringValue.toArray(Strings.EMPTY_ARRAY)), parser, dynamic, scope) {
|
return new Setting<List<T>>(key, arrayToParsableString(defaultStringValue.toArray(Strings.EMPTY_ARRAY)), parser, dynamic, scope) {
|
||||||
|
private final Pattern pattern = Pattern.compile(Pattern.quote(key)+"(\\.\\d+)?");
|
||||||
@Override
|
@Override
|
||||||
public String getRaw(Settings settings) {
|
public String getRaw(Settings settings) {
|
||||||
String[] array = settings.getAsArray(key, null);
|
String[] array = settings.getAsArray(key, null);
|
||||||
|
|
||||||
return array == null ? defaultValue.apply(settings) : arrayToParsableString(array);
|
return array == null ? defaultValue.apply(settings) : arrayToParsableString(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean match(String toTest) {
|
||||||
|
return pattern.matcher(toTest).matches();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean hasComplexMatcher() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,12 +21,15 @@ package org.elasticsearch.common.settings;
|
||||||
import org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider;
|
import org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider;
|
||||||
import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider;
|
import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider;
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
import org.elasticsearch.transport.TransportService;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
public class ScopedSettingsTests extends ESTestCase {
|
public class ScopedSettingsTests extends ESTestCase {
|
||||||
|
|
||||||
|
@ -138,4 +141,15 @@ public class ScopedSettingsTests extends ESTestCase {
|
||||||
assertEquals(diff.getAsMap().size(), 1);
|
assertEquals(diff.getAsMap().size(), 1);
|
||||||
assertEquals(diff.getAsInt("foo.bar.baz", null), Integer.valueOf(17));
|
assertEquals(diff.getAsInt("foo.bar.baz", null), Integer.valueOf(17));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testUpdateTracer() {
|
||||||
|
ClusterSettings settings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
|
||||||
|
AtomicReference<List<String>> ref = new AtomicReference<>();
|
||||||
|
settings.addSettingsUpdateConsumer(TransportService.TRACE_LOG_INCLUDE_SETTING, ref::set);
|
||||||
|
settings.applySettings(Settings.builder().putArray("transport.tracer.include", "internal:index/shard/recovery/*", "internal:gateway/local*").build());
|
||||||
|
assertNotNull(ref.get().size());
|
||||||
|
assertEquals(ref.get().size(), 2);
|
||||||
|
assertTrue(ref.get().contains("internal:index/shard/recovery/*"));
|
||||||
|
assertTrue(ref.get().contains("internal:gateway/local*"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -300,6 +300,24 @@ public class SettingTests extends ESTestCase {
|
||||||
for (int i = 0; i < intValues.size(); i++) {
|
for (int i = 0; i < intValues.size(); i++) {
|
||||||
assertEquals(i, intValues.get(i).intValue());
|
assertEquals(i, intValues.get(i).intValue());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testListSettingAcceptsNumberSyntax() {
|
||||||
|
Setting<List<String>> listSetting = Setting.listSetting("foo.bar", Arrays.asList("foo,bar"), (s) -> s.toString(), true, Setting.Scope.CLUSTER);
|
||||||
|
List<String> input = Arrays.asList("test", "test1, test2", "test", ",,,,");
|
||||||
|
Settings.Builder builder = Settings.builder().putArray("foo.bar", input.toArray(new String[0]));
|
||||||
|
// try to parse this really annoying format
|
||||||
|
for (String key : builder.internalMap().keySet()) {
|
||||||
|
assertTrue("key: " + key + " doesn't match", listSetting.match(key));
|
||||||
|
}
|
||||||
|
builder = Settings.builder().put("foo.bar", "1,2,3");
|
||||||
|
for (String key : builder.internalMap().keySet()) {
|
||||||
|
assertTrue("key: " + key + " doesn't match", listSetting.match(key));
|
||||||
|
}
|
||||||
|
assertFalse(listSetting.match("foo_bar"));
|
||||||
|
assertFalse(listSetting.match("foo_bar.1"));
|
||||||
|
assertTrue(listSetting.match("foo.bar"));
|
||||||
|
assertTrue(listSetting.match("foo.bar." + randomIntBetween(0,10000)));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue