Search in sources :

Example 1 with SettingUpdater

use of org.elasticsearch.common.settings.AbstractScopedSettings.SettingUpdater in project crate by crate.

the class SettingTests method testAffixMapUpdateWithNullSettingValue.

@Test
public void testAffixMapUpdateWithNullSettingValue() {
    // GIVEN an affix setting changed from "prefix._foo"="bar" to "prefix._foo"=null
    final Settings current = Settings.builder().put("prefix._foo", (String) null).build();
    final Settings previous = Settings.builder().put("prefix._foo", "bar").build();
    final Setting.AffixSetting<String> affixSetting = Setting.prefixKeySetting("prefix" + ".", key -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope));
    final Consumer<Map<String, String>> consumer = (map) -> {
    };
    final BiConsumer<String, String> validator = (s1, s2) -> {
    };
    // WHEN creating an affix updater
    final SettingUpdater<Map<String, String>> updater = affixSetting.newAffixMapUpdater(consumer, logger, validator);
    // THEN affix updater is always expected to have changed (even when defaults are omitted)
    assertTrue(updater.hasChanged(current, previous));
    // THEN changes are expected when defaults aren't omitted
    final Map<String, String> updatedSettings = updater.getValue(current, previous);
    assertNotNull(updatedSettings);
    assertEquals(1, updatedSettings.size());
    // THEN changes are reported when defaults aren't omitted
    final String key = updatedSettings.keySet().iterator().next();
    final String value = updatedSettings.get(key);
    assertEquals("_foo", key);
    assertEquals("", value);
}
Also used : Matchers.hasToString(org.hamcrest.Matchers.hasToString) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) Arrays(java.util.Arrays) Tuple(io.crate.common.collections.Tuple) Property(org.elasticsearch.common.settings.Setting.Property) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) JvmInfo(org.elasticsearch.monitor.jvm.JvmInfo) Iterator(java.util.Iterator) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) DataTypes(io.crate.types.DataTypes) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TimeValue(io.crate.common.unit.TimeValue) Matchers.is(org.hamcrest.Matchers.is) SettingUpdater(org.elasticsearch.common.settings.AbstractScopedSettings.SettingUpdater) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) Test(org.junit.Test)

Example 2 with SettingUpdater

use of org.elasticsearch.common.settings.AbstractScopedSettings.SettingUpdater in project crate by crate.

the class SettingTests method testMemorySize.

public void testMemorySize() {
    Setting<ByteSizeValue> memorySizeValueSetting = Setting.memorySizeSetting("a.byte.size", new ByteSizeValue(1024), Property.Dynamic, Property.NodeScope);
    assertFalse(memorySizeValueSetting.isGroupSetting());
    ByteSizeValue memorySizeValue = memorySizeValueSetting.get(Settings.EMPTY);
    assertEquals(memorySizeValue.getBytes(), 1024);
    memorySizeValueSetting = Setting.memorySizeSetting("a.byte.size", s -> "2048b", Property.Dynamic, Property.NodeScope);
    memorySizeValue = memorySizeValueSetting.get(Settings.EMPTY);
    assertEquals(memorySizeValue.getBytes(), 2048);
    memorySizeValueSetting = Setting.memorySizeSetting("a.byte.size", "50%", Property.Dynamic, Property.NodeScope);
    assertFalse(memorySizeValueSetting.isGroupSetting());
    memorySizeValue = memorySizeValueSetting.get(Settings.EMPTY);
    assertEquals(memorySizeValue.getBytes(), JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() * 0.5, 1.0);
    memorySizeValueSetting = Setting.memorySizeSetting("a.byte.size", s -> "25%", Property.Dynamic, Property.NodeScope);
    memorySizeValue = memorySizeValueSetting.get(Settings.EMPTY);
    assertEquals(memorySizeValue.getBytes(), JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() * 0.25, 1.0);
    AtomicReference<ByteSizeValue> value = new AtomicReference<>(null);
    ClusterSettings.SettingUpdater<ByteSizeValue> settingUpdater = memorySizeValueSetting.newUpdater(value::set, logger);
    assertTrue(settingUpdater.apply(Settings.builder().put("a.byte.size", "12").build(), Settings.EMPTY));
    assertEquals(new ByteSizeValue(12), value.get());
    assertTrue(settingUpdater.apply(Settings.builder().put("a.byte.size", "12b").build(), Settings.EMPTY));
    assertEquals(new ByteSizeValue(12), value.get());
    assertTrue(settingUpdater.apply(Settings.builder().put("a.byte.size", "20%").build(), Settings.EMPTY));
    assertEquals(new ByteSizeValue((int) (JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() * 0.2)), value.get());
}
Also used : Matchers.hasToString(org.hamcrest.Matchers.hasToString) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) Arrays(java.util.Arrays) Tuple(io.crate.common.collections.Tuple) Property(org.elasticsearch.common.settings.Setting.Property) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) JvmInfo(org.elasticsearch.monitor.jvm.JvmInfo) Iterator(java.util.Iterator) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) DataTypes(io.crate.types.DataTypes) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TimeValue(io.crate.common.unit.TimeValue) Matchers.is(org.hamcrest.Matchers.is) SettingUpdater(org.elasticsearch.common.settings.AbstractScopedSettings.SettingUpdater) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

Tuple (io.crate.common.collections.Tuple)2 TimeValue (io.crate.common.unit.TimeValue)2 DataTypes (io.crate.types.DataTypes)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 BiConsumer (java.util.function.BiConsumer)2 Consumer (java.util.function.Consumer)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Version (org.elasticsearch.Version)2 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)2 SettingUpdater (org.elasticsearch.common.settings.AbstractScopedSettings.SettingUpdater)2 Property (org.elasticsearch.common.settings.Setting.Property)2 ByteSizeUnit (org.elasticsearch.common.unit.ByteSizeUnit)2