From 464b15447fd9d26ed8d64660b24bb1fb74308e5d Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Tue, 26 May 2015 05:41:12 -0400 Subject: [PATCH] require setting name to Memory/ByteSizeValue parsers --- .../decider/DiskThresholdDecider.java | 7 +- .../cluster/settings/Validator.java | 4 +- .../common/settings/ImmutableSettings.java | 1155 ----------------- .../common/settings/Settings.java | 22 +- .../common/unit/ByteSizeValue.java | 16 +- .../common/unit/MemorySizeValue.java | 7 +- .../index/engine/EngineConfig.java | 4 +- .../mapper/internal/SourceFieldMapper.java | 2 +- .../index/translog/TranslogConfig.java | 4 +- .../cache/filter/IndicesFilterCache.java | 2 +- .../cache/query/IndicesQueryCache.java | 2 +- .../indices/recovery/RecoverySettings.java | 2 +- .../org/elasticsearch/rest/RestRequest.java | 2 +- .../benchmark/fs/FsAppendBenchmark.java | 4 +- .../TermsAggregationSearchBenchmark.java | 2 +- .../cluster/SimpleClusterStateTests.java | 3 +- .../DiskThresholdDeciderUnitTests.java | 8 +- .../common/unit/ByteSizeValueTests.java | 34 +- .../network/DirectBufferNetworkTests.java | 3 +- .../recovery/RecoverySettingsTest.java | 2 +- 20 files changed, 76 insertions(+), 1209 deletions(-) delete mode 100644 src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java diff --git a/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDecider.java b/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDecider.java index 517a729afbf..d032ac24de4 100644 --- a/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDecider.java +++ b/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDecider.java @@ -564,10 +564,11 @@ public class DiskThresholdDecider extends AllocationDecider { * a ByteSizeValue of 0 bytes if the value cannot be parsed. */ public ByteSizeValue thresholdBytesFromWatermark(String watermark) { + // nocommit: why be lenient here? try { - return ByteSizeValue.parseBytesSizeValue(watermark); + return ByteSizeValue.parseBytesSizeValue(watermark, "DiskThresholdDecider watermark"); } catch (ElasticsearchParseException ex) { - return ByteSizeValue.parseBytesSizeValue("0b"); + return ByteSizeValue.parseBytesSizeValue("0b", "DiskThresholdDecider watermark"); } } @@ -581,7 +582,7 @@ public class DiskThresholdDecider extends AllocationDecider { return true; } catch (ElasticsearchParseException e) { try { - ByteSizeValue.parseBytesSizeValue(watermark); + ByteSizeValue.parseBytesSizeValue(watermark, "DiskThresholdDecider watermark"); return true; } catch (ElasticsearchParseException ex) { return false; diff --git a/src/main/java/org/elasticsearch/cluster/settings/Validator.java b/src/main/java/org/elasticsearch/cluster/settings/Validator.java index ec3e8ea7129..2fa212ad3d9 100644 --- a/src/main/java/org/elasticsearch/cluster/settings/Validator.java +++ b/src/main/java/org/elasticsearch/cluster/settings/Validator.java @@ -197,7 +197,7 @@ public interface Validator { @Override public String validate(String setting, String value) { try { - parseBytesSizeValue(value); + parseBytesSizeValue(value, setting); } catch (ElasticsearchParseException ex) { return ex.getMessage(); } @@ -247,7 +247,7 @@ public interface Validator { @Override public String validate(String setting, String value) { try { - parseBytesSizeValueOrHeapRatio(value); + parseBytesSizeValueOrHeapRatio(value, setting); } catch (ElasticsearchParseException ex) { return ex.getMessage(); } diff --git a/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java b/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java deleted file mode 100644 index e8e0b0ed687..00000000000 --- a/src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java +++ /dev/null @@ -1,1155 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.settings; - -import com.google.common.base.Charsets; -import com.google.common.base.Predicate; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import org.elasticsearch.Version; -import org.elasticsearch.common.Booleans; -import org.elasticsearch.common.Classes; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.io.Streams; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.property.PropertyPlaceholder; -import org.elasticsearch.common.settings.loader.SettingsLoader; -import org.elasticsearch.common.settings.loader.SettingsLoaderFactory; -import org.elasticsearch.common.unit.*; -import org.elasticsearch.common.xcontent.XContentBuilder; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.*; -import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import static org.elasticsearch.common.Strings.toCamelCase; -import static org.elasticsearch.common.unit.ByteSizeValue.parseBytesSizeValue; -import static org.elasticsearch.common.unit.SizeValue.parseSizeValue; -import static org.elasticsearch.common.unit.TimeValue.parseTimeValue; - -/** - * An immutable implementation of {@link Settings}. - */ -public class ImmutableSettings implements Settings { - - public static final Settings EMPTY = new Builder().build(); - private final static Pattern ARRAY_PATTERN = Pattern.compile("(.*)\\.\\d+$"); - - private ImmutableMap settings; - private final ImmutableMap forcedUnderscoreSettings; - private transient ClassLoader classLoader; - - ImmutableSettings(Map settings, ClassLoader classLoader) { - this.settings = ImmutableMap.copyOf(settings); - Map forcedUnderscoreSettings = null; - for (Map.Entry entry : settings.entrySet()) { - String toUnderscoreCase = Strings.toUnderscoreCase(entry.getKey()); - if (!toUnderscoreCase.equals(entry.getKey())) { - if (forcedUnderscoreSettings == null) { - forcedUnderscoreSettings = new HashMap<>(); - } - forcedUnderscoreSettings.put(toUnderscoreCase, entry.getValue()); - } - } - this.forcedUnderscoreSettings = forcedUnderscoreSettings == null ? ImmutableMap.of() : ImmutableMap.copyOf(forcedUnderscoreSettings); - this.classLoader = classLoader; - } - - @Override - public ClassLoader getClassLoader() { - return this.classLoader == null ? Classes.getDefaultClassLoader() : classLoader; - } - - @Override - public ClassLoader getClassLoaderIfSet() { - return this.classLoader; - } - - @Override - public ImmutableMap getAsMap() { - return this.settings; - } - - @Override - public Map getAsStructuredMap() { - Map map = Maps.newHashMapWithExpectedSize(2); - for (Map.Entry entry : settings.entrySet()) { - processSetting(map, "", entry.getKey(), entry.getValue()); - } - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() instanceof Map) { - @SuppressWarnings("unchecked") Map valMap = (Map) entry.getValue(); - entry.setValue(convertMapsToArrays(valMap)); - } - } - - return map; - } - - private void processSetting(Map map, String prefix, String setting, String value) { - int prefixLength = setting.indexOf('.'); - if (prefixLength == -1) { - @SuppressWarnings("unchecked") Map innerMap = (Map) map.get(prefix + setting); - if (innerMap != null) { - // It supposed to be a value, but we already have a map stored, need to convert this map to "." notation - for (Map.Entry entry : innerMap.entrySet()) { - map.put(prefix + setting + "." + entry.getKey(), entry.getValue()); - } - } - map.put(prefix + setting, value); - } else { - String key = setting.substring(0, prefixLength); - String rest = setting.substring(prefixLength + 1); - Object existingValue = map.get(prefix + key); - if (existingValue == null) { - Map newMap = Maps.newHashMapWithExpectedSize(2); - processSetting(newMap, "", rest, value); - map.put(key, newMap); - } else { - if (existingValue instanceof Map) { - @SuppressWarnings("unchecked") - Map innerMap = (Map) existingValue; - processSetting(innerMap, "", rest, value); - map.put(key, innerMap); - } else { - // It supposed to be a map, but we already have a value stored, which is not a map - // fall back to "." notation - processSetting(map, prefix + key + ".", rest, value); - } - } - } - } - - private Object convertMapsToArrays(Map map) { - if (map.isEmpty()) { - return map; - } - boolean isArray = true; - int maxIndex = -1; - for (Map.Entry entry : map.entrySet()) { - if (isArray) { - try { - int index = Integer.parseInt(entry.getKey()); - if (index >= 0) { - maxIndex = Math.max(maxIndex, index); - } else { - isArray = false; - } - } catch (NumberFormatException ex) { - isArray = false; - } - } - if (entry.getValue() instanceof Map) { - @SuppressWarnings("unchecked") Map valMap = (Map) entry.getValue(); - entry.setValue(convertMapsToArrays(valMap)); - } - } - if (isArray && (maxIndex + 1) == map.size()) { - ArrayList newValue = Lists.newArrayListWithExpectedSize(maxIndex + 1); - for (int i = 0; i <= maxIndex; i++) { - Object obj = map.get(Integer.toString(i)); - if (obj == null) { - // Something went wrong. Different format? - // Bailout! - return map; - } - newValue.add(obj); - } - return newValue; - } - return map; - } - - @Override - public Settings getByPrefix(String prefix) { - Builder builder = new Builder(); - for (Map.Entry entry : getAsMap().entrySet()) { - if (entry.getKey().startsWith(prefix)) { - if (entry.getKey().length() < prefix.length()) { - // ignore this. one - continue; - } - builder.put(entry.getKey().substring(prefix.length()), entry.getValue()); - } - } - builder.classLoader(classLoader); - return builder.build(); - } - - @Override - public Settings getAsSettings(String setting) { - return getByPrefix(setting + "."); - } - - @Override - public String get(String setting) { - String retVal = settings.get(setting); - if (retVal != null) { - return retVal; - } - return forcedUnderscoreSettings.get(setting); - } - - @Override - public String get(String[] settings) { - for (String setting : settings) { - String retVal = get(setting); - if (retVal != null) { - return retVal; - } - } - return null; - } - - @Override - public String get(String setting, String defaultValue) { - String retVal = get(setting); - return retVal == null ? defaultValue : retVal; - } - - @Override - public String get(String[] settings, String defaultValue) { - String retVal = get(settings); - return retVal == null ? defaultValue : retVal; - } - - @Override - public Float getAsFloat(String setting, Float defaultValue) { - String sValue = get(setting); - if (sValue == null) { - return defaultValue; - } - try { - return Float.parseFloat(sValue); - } catch (NumberFormatException e) { - throw new SettingsException("Failed to parse float setting [" + setting + "] with value [" + sValue + "]", e); - } - } - - @Override - public Float getAsFloat(String[] settings, Float defaultValue) throws SettingsException { - String sValue = get(settings); - if (sValue == null) { - return defaultValue; - } - try { - return Float.parseFloat(sValue); - } catch (NumberFormatException e) { - throw new SettingsException("Failed to parse float setting [" + Arrays.toString(settings) + "] with value [" + sValue + "]", e); - } - } - - @Override - public Double getAsDouble(String setting, Double defaultValue) { - String sValue = get(setting); - if (sValue == null) { - return defaultValue; - } - try { - return Double.parseDouble(sValue); - } catch (NumberFormatException e) { - throw new SettingsException("Failed to parse double setting [" + setting + "] with value [" + sValue + "]", e); - } - } - - @Override - public Double getAsDouble(String[] settings, Double defaultValue) { - String sValue = get(settings); - if (sValue == null) { - return defaultValue; - } - try { - return Double.parseDouble(sValue); - } catch (NumberFormatException e) { - throw new SettingsException("Failed to parse double setting [" + Arrays.toString(settings) + "] with value [" + sValue + "]", e); - } - } - - - @Override - public Integer getAsInt(String setting, Integer defaultValue) { - String sValue = get(setting); - if (sValue == null) { - return defaultValue; - } - try { - return Integer.parseInt(sValue); - } catch (NumberFormatException e) { - throw new SettingsException("Failed to parse int setting [" + setting + "] with value [" + sValue + "]", e); - } - } - - @Override - public Integer getAsInt(String[] settings, Integer defaultValue) { - String sValue = get(settings); - if (sValue == null) { - return defaultValue; - } - try { - return Integer.parseInt(sValue); - } catch (NumberFormatException e) { - throw new SettingsException("Failed to parse int setting [" + Arrays.toString(settings) + "] with value [" + sValue + "]", e); - } - } - - @Override - public Long getAsLong(String setting, Long defaultValue) { - String sValue = get(setting); - if (sValue == null) { - return defaultValue; - } - try { - return Long.parseLong(sValue); - } catch (NumberFormatException e) { - throw new SettingsException("Failed to parse long setting [" + setting + "] with value [" + sValue + "]", e); - } - } - - @Override - public Long getAsLong(String[] settings, Long defaultValue) { - String sValue = get(settings); - if (sValue == null) { - return defaultValue; - } - try { - return Long.parseLong(sValue); - } catch (NumberFormatException e) { - throw new SettingsException("Failed to parse long setting [" + Arrays.toString(settings) + "] with value [" + sValue + "]", e); - } - } - - @Override - public Boolean getAsBoolean(String setting, Boolean defaultValue) { - return Booleans.parseBoolean(get(setting), defaultValue); - } - - @Override - public Boolean getAsBoolean(String[] settings, Boolean defaultValue) { - return Booleans.parseBoolean(get(settings), defaultValue); - } - - @Override - public TimeValue getAsTime(String setting, TimeValue defaultValue) { - return parseTimeValue(get(setting), defaultValue, setting); - } - - @Override - public TimeValue getAsTime(String[] settings, TimeValue defaultValue) { - // NOTE: duplicated from get(String[]) so we can pass which setting name was actually used to parseTimeValue: - for (String setting : settings) { - String retVal = get(setting); - if (retVal != null) { - parseTimeValue(get(settings), defaultValue, setting); - } - } - return defaultValue; - } - - @Override - public ByteSizeValue getAsBytesSize(String setting, ByteSizeValue defaultValue) throws SettingsException { - return parseBytesSizeValue(get(setting), defaultValue); - } - - @Override - public ByteSizeValue getAsBytesSize(String[] settings, ByteSizeValue defaultValue) throws SettingsException { - return parseBytesSizeValue(get(settings), defaultValue); - } - - @Override - public ByteSizeValue getAsMemory(String setting, String defaultValue) throws SettingsException { - return MemorySizeValue.parseBytesSizeValueOrHeapRatio(get(setting, defaultValue)); - } - - @Override - public ByteSizeValue getAsMemory(String[] settings, String defaultValue) throws SettingsException { - return MemorySizeValue.parseBytesSizeValueOrHeapRatio(get(settings, defaultValue)); - } - - @Override - public RatioValue getAsRatio(String setting, String defaultValue) throws SettingsException { - return RatioValue.parseRatioValue(get(setting, defaultValue)); - } - - @Override - public RatioValue getAsRatio(String[] settings, String defaultValue) throws SettingsException { - return RatioValue.parseRatioValue(get(settings, defaultValue)); - } - - @Override - public SizeValue getAsSize(String setting, SizeValue defaultValue) throws SettingsException { - return parseSizeValue(get(setting), defaultValue); - } - - @Override - public SizeValue getAsSize(String[] settings, SizeValue defaultValue) throws SettingsException { - return parseSizeValue(get(settings), defaultValue); - } - - @SuppressWarnings({"unchecked"}) - @Override - public Class getAsClass(String setting, Class defaultClazz) throws NoClassSettingsException { - String sValue = get(setting); - if (sValue == null) { - return defaultClazz; - } - try { - return (Class) getClassLoader().loadClass(sValue); - } catch (ClassNotFoundException e) { - throw new NoClassSettingsException("Failed to load class setting [" + setting + "] with value [" + sValue + "]", e); - } - } - - @SuppressWarnings({"unchecked"}) - @Override - public Class getAsClass(String setting, Class defaultClazz, String prefixPackage, String suffixClassName) throws NoClassSettingsException { - String sValue = get(setting); - if (sValue == null) { - return defaultClazz; - } - String fullClassName = sValue; - try { - return (Class) getClassLoader().loadClass(fullClassName); - } catch (ClassNotFoundException e) { - String prefixValue = prefixPackage; - int packageSeparator = sValue.lastIndexOf('.'); - if (packageSeparator > 0) { - prefixValue = sValue.substring(0, packageSeparator + 1); - sValue = sValue.substring(packageSeparator + 1); - } - fullClassName = prefixValue + Strings.capitalize(toCamelCase(sValue)) + suffixClassName; - try { - return (Class) getClassLoader().loadClass(fullClassName); - } catch (ClassNotFoundException e1) { - return loadClass(prefixValue, sValue, suffixClassName, setting); - } catch (NoClassDefFoundError e1) { - return loadClass(prefixValue, sValue, suffixClassName, setting); - } - } - } - - private Class loadClass(String prefixValue, String sValue, String suffixClassName, String setting) { - String fullClassName = prefixValue + toCamelCase(sValue).toLowerCase(Locale.ROOT) + "." + Strings.capitalize(toCamelCase(sValue)) + suffixClassName; - try { - return (Class) getClassLoader().loadClass(fullClassName); - } catch (ClassNotFoundException e2) { - throw new NoClassSettingsException("Failed to load class setting [" + setting + "] with value [" + get(setting) + "]", e2); - } - } - - @Override - public String[] getAsArray(String settingPrefix) throws SettingsException { - return getAsArray(settingPrefix, Strings.EMPTY_ARRAY, true); - } - - @Override - public String[] getAsArray(String settingPrefix, String[] defaultArray) throws SettingsException { - return getAsArray(settingPrefix, defaultArray, true); - } - - @Override - public String[] getAsArray(String settingPrefix, String[] defaultArray, Boolean commaDelimited) throws SettingsException { - List result = Lists.newArrayList(); - - if (get(settingPrefix) != null) { - if (commaDelimited) { - String[] strings = Strings.splitStringByCommaToArray(get(settingPrefix)); - if (strings.length > 0) { - for (String string : strings) { - result.add(string.trim()); - } - } - } else { - result.add(get(settingPrefix).trim()); - } - } - - int counter = 0; - while (true) { - String value = get(settingPrefix + '.' + (counter++)); - if (value == null) { - break; - } - result.add(value.trim()); - } - if (result.isEmpty()) { - return defaultArray; - } - return result.toArray(new String[result.size()]); - } - - @Override - public Map getGroups(String settingPrefix) throws SettingsException { - return getGroups(settingPrefix, false); - } - - @Override - public Map getGroups(String settingPrefix, boolean ignoreNonGrouped) throws SettingsException { - if (!Strings.hasLength(settingPrefix)) { - throw new IllegalArgumentException("illegal setting prefix " + settingPrefix); - } - if (settingPrefix.charAt(settingPrefix.length() - 1) != '.') { - settingPrefix = settingPrefix + "."; - } - // we don't really care that it might happen twice - Map> map = new LinkedHashMap<>(); - for (Object o : settings.keySet()) { - String setting = (String) o; - if (setting.startsWith(settingPrefix)) { - String nameValue = setting.substring(settingPrefix.length()); - int dotIndex = nameValue.indexOf('.'); - if (dotIndex == -1) { - if (ignoreNonGrouped) { - continue; - } - throw new SettingsException("Failed to get setting group for [" + settingPrefix + "] setting prefix and setting [" + setting + "] because of a missing '.'"); - } - String name = nameValue.substring(0, dotIndex); - String value = nameValue.substring(dotIndex + 1); - Map groupSettings = map.get(name); - if (groupSettings == null) { - groupSettings = new LinkedHashMap<>(); - map.put(name, groupSettings); - } - groupSettings.put(value, get(setting)); - } - } - Map retVal = new LinkedHashMap<>(); - for (Map.Entry> entry : map.entrySet()) { - retVal.put(entry.getKey(), new ImmutableSettings(Collections.unmodifiableMap(entry.getValue()), classLoader)); - } - return Collections.unmodifiableMap(retVal); - } - - @Override - public Version getAsVersion(String setting, Version defaultVersion) throws SettingsException { - String sValue = get(setting); - if (sValue == null) { - return defaultVersion; - } - try { - return Version.fromId(Integer.parseInt(sValue)); - } catch (Exception e) { - throw new SettingsException("Failed to parse version setting [" + setting + "] with value [" + sValue + "]", e); - } - } - - @Override - public Set names() { - Set names = new HashSet<>(); - for (String key : settings.keySet()) { - int i = key.indexOf("."); - if (i < 0) { - names.add(key); - } else { - names.add(key.substring(0, i)); - } - } - return names; - } - - @Override - public String toDelimitedString(char delimiter) { - StringBuilder sb = new StringBuilder(); - for (Map.Entry entry : settings.entrySet()) { - sb.append(entry.getKey()).append("=").append(entry.getValue()).append(delimiter); - } - return sb.toString(); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - ImmutableSettings that = (ImmutableSettings) o; - - if (classLoader != null ? !classLoader.equals(that.classLoader) : that.classLoader != null) return false; - if (settings != null ? !settings.equals(that.settings) : that.settings != null) return false; - - return true; - } - - @Override - public int hashCode() { - int result = settings != null ? settings.hashCode() : 0; - result = 31 * result + (classLoader != null ? classLoader.hashCode() : 0); - return result; - } - - public static Settings readSettingsFromStream(StreamInput in) throws IOException { - Builder builder = new Builder(); - int numberOfSettings = in.readVInt(); - for (int i = 0; i < numberOfSettings; i++) { - builder.put(in.readString(), in.readString()); - } - return builder.build(); - } - - public static void writeSettingsToStream(Settings settings, StreamOutput out) throws IOException { - out.writeVInt(settings.getAsMap().size()); - for (Map.Entry entry : settings.getAsMap().entrySet()) { - out.writeString(entry.getKey()); - out.writeString(entry.getValue()); - } - } - - public static Builder builder() { - return new Builder(); - } - - /** - * Returns a builder to be used in order to build settings. - */ - public static Builder settingsBuilder() { - return new Builder(); - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - Settings settings = SettingsFilter.filterSettings(params, this); - if (!params.paramAsBoolean("flat_settings", false)) { - for (Map.Entry entry : settings.getAsStructuredMap().entrySet()) { - builder.field(entry.getKey(), entry.getValue()); - } - } else { - for (Map.Entry entry : settings.getAsMap().entrySet()) { - builder.field(entry.getKey(), entry.getValue(), XContentBuilder.FieldCaseConversion.NONE); - } - } - return builder; - } - - /** - * A builder allowing to put different settings and then {@link #build()} an immutable - * settings implementation. Use {@link ImmutableSettings#settingsBuilder()} in order to - * construct it. - */ - public static class Builder implements Settings.Builder { - - public static final Settings EMPTY_SETTINGS = new Builder().build(); - - private final Map map = new LinkedHashMap<>(); - - private ClassLoader classLoader; - - private Builder() { - - } - - public Map internalMap() { - return this.map; - } - - /** - * Removes the provided setting from the internal map holding the current list of settings. - */ - public String remove(String key) { - return map.remove(key); - } - - /** - * Returns a setting value based on the setting key. - */ - public String get(String key) { - String retVal = map.get(key); - if (retVal != null) { - return retVal; - } - // try camel case version - return map.get(toCamelCase(key)); - } - - /** - * Puts tuples of key value pairs of settings. Simplified version instead of repeating calling - * put for each one. - */ - public Builder put(Object... settings) { - if (settings.length == 1) { - // support cases where the actual type gets lost down the road... - if (settings[0] instanceof Map) { - //noinspection unchecked - return put((Map) settings[0]); - } else if (settings[0] instanceof Settings) { - return put((Settings) settings[0]); - } - } - if ((settings.length % 2) != 0) { - throw new IllegalArgumentException("array settings of key + value order doesn't hold correct number of arguments (" + settings.length + ")"); - } - for (int i = 0; i < settings.length; i++) { - put(settings[i++].toString(), settings[i].toString()); - } - return this; - } - - /** - * Sets a setting with the provided setting key and value. - * - * @param key The setting key - * @param value The setting value - * @return The builder - */ - public Builder put(String key, String value) { - map.put(key, value); - return this; - } - - /** - * Sets a setting with the provided setting key and class as value. - * - * @param key The setting key - * @param clazz The setting class value - * @return The builder - */ - public Builder put(String key, Class clazz) { - map.put(key, clazz.getName()); - return this; - } - - /** - * Sets the setting with the provided setting key and the boolean value. - * - * @param setting The setting key - * @param value The boolean value - * @return The builder - */ - public Builder put(String setting, boolean value) { - put(setting, String.valueOf(value)); - return this; - } - - /** - * Sets the setting with the provided setting key and the int value. - * - * @param setting The setting key - * @param value The int value - * @return The builder - */ - public Builder put(String setting, int value) { - put(setting, String.valueOf(value)); - return this; - } - - public Builder put(String setting, Version version) { - put(setting, version.id); - return this; - } - - /** - * Sets the setting with the provided setting key and the long value. - * - * @param setting The setting key - * @param value The long value - * @return The builder - */ - public Builder put(String setting, long value) { - put(setting, String.valueOf(value)); - return this; - } - - /** - * Sets the setting with the provided setting key and the float value. - * - * @param setting The setting key - * @param value The float value - * @return The builder - */ - public Builder put(String setting, float value) { - put(setting, String.valueOf(value)); - return this; - } - - /** - * Sets the setting with the provided setting key and the double value. - * - * @param setting The setting key - * @param value The double value - * @return The builder - */ - public Builder put(String setting, double value) { - put(setting, String.valueOf(value)); - return this; - } - - /** - * Sets the setting with the provided setting key and the time value. - * - * @param setting The setting key - * @param value The time value - * @return The builder - */ - public Builder put(String setting, long value, TimeUnit timeUnit) { - put(setting, timeUnit.toMillis(value) + "ms"); - return this; - } - - /** - * Sets the setting with the provided setting key and the size value. - * - * @param setting The setting key - * @param value The size value - * @return The builder - */ - public Builder put(String setting, long value, ByteSizeUnit sizeUnit) { - put(setting, sizeUnit.toBytes(value)); - return this; - } - - /** - * Sets the setting with the provided setting key and an array of values. - * - * @param setting The setting key - * @param values The values - * @return The builder - */ - public Builder putArray(String setting, String... values) { - remove(setting); - int counter = 0; - while (true) { - String value = map.remove(setting + '.' + (counter++)); - if (value == null) { - break; - } - } - for (int i = 0; i < values.length; i++) { - put(setting + "." + i, values[i]); - } - return this; - } - - /** - * Sets the setting group. - */ - public Builder put(String settingPrefix, String groupName, String[] settings, String[] values) throws SettingsException { - if (settings.length != values.length) { - throw new SettingsException("The settings length must match the value length"); - } - for (int i = 0; i < settings.length; i++) { - if (values[i] == null) { - continue; - } - put(settingPrefix + "." + groupName + "." + settings[i], values[i]); - } - return this; - } - - /** - * Sets all the provided settings. - */ - public Builder put(Settings settings) { - removeNonArraysFieldsIfNewSettingsContainsFieldAsArray(settings.getAsMap()); - map.putAll(settings.getAsMap()); - classLoader = settings.getClassLoaderIfSet(); - return this; - } - - /** - * Sets all the provided settings. - */ - public Builder put(Map settings) { - removeNonArraysFieldsIfNewSettingsContainsFieldAsArray(settings); - map.putAll(settings); - return this; - } - - /** - * Removes non array values from the existing map, if settings contains an array value instead - * - * Example: - * Existing map contains: {key:value} - * New map contains: {key:[value1,value2]} (which has been flattened to {}key.0:value1,key.1:value2}) - * - * This ensure that that the 'key' field gets removed from the map in order to override all the - * data instead of merging - */ - private void removeNonArraysFieldsIfNewSettingsContainsFieldAsArray(Map settings) { - List prefixesToRemove = new ArrayList<>(); - for (final Map.Entry entry : settings.entrySet()) { - final Matcher matcher = ARRAY_PATTERN.matcher(entry.getKey()); - if (matcher.matches()) { - prefixesToRemove.add(matcher.group(1)); - } else if (Iterables.any(map.keySet(), startsWith(entry.getKey() + "."))) { - prefixesToRemove.add(entry.getKey()); - } - } - for (String prefix : prefixesToRemove) { - Iterator> iterator = map.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - if (entry.getKey().startsWith(prefix + ".") || entry.getKey().equals(prefix)) { - iterator.remove(); - } - } - } - } - - /** - * Sets all the provided settings. - */ - public Builder put(Properties properties) { - for (Map.Entry entry : properties.entrySet()) { - map.put((String) entry.getKey(), (String) entry.getValue()); - } - return this; - } - - public Builder loadFromDelimitedString(String value, char delimiter) { - String[] values = Strings.splitStringToArray(value, delimiter); - for (String s : values) { - int index = s.indexOf('='); - if (index == -1) { - throw new IllegalArgumentException("value [" + s + "] for settings loaded with delimiter [" + delimiter + "] is malformed, missing ="); - } - map.put(s.substring(0, index), s.substring(index + 1)); - } - return this; - } - - /** - * Loads settings from the actual string content that represents them using the - * {@link SettingsLoaderFactory#loaderFromSource(String)}. - */ - public Builder loadFromSource(String source) { - SettingsLoader settingsLoader = SettingsLoaderFactory.loaderFromSource(source); - try { - Map loadedSettings = settingsLoader.load(source); - put(loadedSettings); - } catch (Exception e) { - throw new SettingsException("Failed to load settings from [" + source + "]", e); - } - return this; - } - - /** - * Loads settings from a url that represents them using the - * {@link SettingsLoaderFactory#loaderFromSource(String)}. - */ - public Builder loadFromUrl(URL url) throws SettingsException { - try { - return loadFromStream(url.toExternalForm(), url.openStream()); - } catch (IOException e) { - throw new SettingsException("Failed to open stream for url [" + url.toExternalForm() + "]", e); - } - } - - /** - * Loads settings from a url that represents them using the - * {@link SettingsLoaderFactory#loaderFromSource(String)}. - */ - public Builder loadFromPath(Path path) throws SettingsException { - try { - return loadFromStream(path.getFileName().toString(), Files.newInputStream(path)); - } catch (IOException e) { - throw new SettingsException("Failed to open stream for url [" + path + "]", e); - } - } - - /** - * Loads settings from a stream that represents them using the - * {@link SettingsLoaderFactory#loaderFromSource(String)}. - */ - public Builder loadFromStream(String resourceName, InputStream is) throws SettingsException { - SettingsLoader settingsLoader = SettingsLoaderFactory.loaderFromResource(resourceName); - try { - Map loadedSettings = settingsLoader.load(Streams.copyToString(new InputStreamReader(is, Charsets.UTF_8))); - put(loadedSettings); - } catch (Exception e) { - throw new SettingsException("Failed to load settings from [" + resourceName + "]", e); - } - return this; - } - - /** - * Loads settings from classpath that represents them using the - * {@link SettingsLoaderFactory#loaderFromSource(String)}. - */ - public Builder loadFromClasspath(String resourceName) throws SettingsException { - ClassLoader classLoader = this.classLoader; - if (classLoader == null) { - classLoader = Classes.getDefaultClassLoader(); - } - InputStream is = classLoader.getResourceAsStream(resourceName); - if (is == null) { - return this; - } - - return loadFromStream(resourceName, is); - } - - /** - * Sets the class loader associated with the settings built. - */ - public Builder classLoader(ClassLoader classLoader) { - this.classLoader = classLoader; - return this; - } - - /** - * Puts all the properties with keys starting with the provided prefix. - * - * @param prefix The prefix to filter property key by - * @param properties The properties to put - * @return The builder - */ - public Builder putProperties(String prefix, Properties properties) { - for (Object key1 : properties.keySet()) { - String key = (String) key1; - String value = properties.getProperty(key); - if (key.startsWith(prefix)) { - map.put(key.substring(prefix.length()), value); - } - } - return this; - } - - /** - * Puts all the properties with keys starting with the provided prefix. - * - * @param prefix The prefix to filter property key by - * @param properties The properties to put - * @return The builder - */ - public Builder putProperties(String prefix, Properties properties, String[] ignorePrefixes) { - for (Object key1 : properties.keySet()) { - String key = (String) key1; - String value = properties.getProperty(key); - if (key.startsWith(prefix)) { - boolean ignore = false; - for (String ignorePrefix : ignorePrefixes) { - if (key.startsWith(ignorePrefix)) { - ignore = true; - break; - } - } - if (!ignore) { - map.put(key.substring(prefix.length()), value); - } - } - } - return this; - } - - /** - * Runs across all the settings set on this builder and replaces ${...} elements in the - * each setting value according to the following logic: - *

- *

First, tries to resolve it against a System property ({@link System#getProperty(String)}), next, - * tries and resolve it against an environment variable ({@link System#getenv(String)}), and last, tries - * and replace it with another setting already set on this builder. - */ - public Builder replacePropertyPlaceholders() { - PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false); - PropertyPlaceholder.PlaceholderResolver placeholderResolver = new PropertyPlaceholder.PlaceholderResolver() { - @Override - public String resolvePlaceholder(String placeholderName) { - if (placeholderName.startsWith("env.")) { - // explicit env var prefix - return System.getenv(placeholderName.substring("env.".length())); - } - String value = System.getProperty(placeholderName); - if (value != null) { - return value; - } - value = System.getenv(placeholderName); - if (value != null) { - return value; - } - return map.get(placeholderName); - } - - @Override - public boolean shouldIgnoreMissing(String placeholderName) { - // if its an explicit env var, we are ok with not having a value for it and treat it as optional - if (placeholderName.startsWith("env.")) { - return true; - } - return false; - } - }; - for (Map.Entry entry : Maps.newHashMap(map).entrySet()) { - String value = propertyPlaceholder.replacePlaceholders(entry.getValue(), placeholderResolver); - // if the values exists and has length, we should maintain it in the map - // otherwise, the replace process resolved into removing it - if (Strings.hasLength(value)) { - map.put(entry.getKey(), value); - } else { - map.remove(entry.getKey()); - } - } - return this; - } - - /** - * Checks that all settings in the builder start with the specified prefix. - * - * If a setting doesn't start with the prefix, the builder appends the prefix to such setting. - */ - public Builder normalizePrefix(String prefix) { - Map replacements = Maps.newHashMap(); - Iterator> iterator = map.entrySet().iterator(); - while(iterator.hasNext()) { - Map.Entry entry = iterator.next(); - if (entry.getKey().startsWith(prefix) == false) { - replacements.put(prefix + entry.getKey(), entry.getValue()); - iterator.remove(); - } - } - map.putAll(replacements); - return this; - } - - /** - * Builds a {@link Settings} (underlying uses {@link ImmutableSettings}) based on everything - * set on this builder. - */ - @Override - public Settings build() { - return new ImmutableSettings(Collections.unmodifiableMap(map), classLoader); - } - } - - private static StartsWithPredicate startsWith(String prefix) { - return new StartsWithPredicate(prefix); - } - - private static final class StartsWithPredicate implements Predicate { - - private String prefix; - - public StartsWithPredicate(String prefix) { - this.prefix = prefix; - } - - @Override - public boolean apply(String input) { - return input.startsWith(prefix); - } - } -} diff --git a/src/main/java/org/elasticsearch/common/settings/Settings.java b/src/main/java/org/elasticsearch/common/settings/Settings.java index 3d11cadac6c..b7bee46cca8 100644 --- a/src/main/java/org/elasticsearch/common/settings/Settings.java +++ b/src/main/java/org/elasticsearch/common/settings/Settings.java @@ -440,7 +440,7 @@ public final class Settings implements ToXContent { * returns the default value provided. */ public ByteSizeValue getAsBytesSize(String setting, ByteSizeValue defaultValue) throws SettingsException { - return parseBytesSizeValue(get(setting), defaultValue); + return parseBytesSizeValue(get(setting), defaultValue, setting); } /** @@ -448,7 +448,14 @@ public final class Settings implements ToXContent { * returns the default value provided. */ public ByteSizeValue getAsBytesSize(String[] settings, ByteSizeValue defaultValue) throws SettingsException { - return parseBytesSizeValue(get(settings), defaultValue); + // NOTE: duplicated from get(String[]) so we can pass which setting name was actually used to parseBytesSizeValue + for (String setting : settings) { + String retVal = get(setting); + if (retVal != null) { + parseBytesSizeValue(get(settings), defaultValue, setting); + } + } + return defaultValue; } /** @@ -457,7 +464,7 @@ public final class Settings implements ToXContent { * (eg. 12%). If it does not exists, parses the default value provided. */ public ByteSizeValue getAsMemory(String setting, String defaultValue) throws SettingsException { - return MemorySizeValue.parseBytesSizeValueOrHeapRatio(get(setting, defaultValue)); + return MemorySizeValue.parseBytesSizeValueOrHeapRatio(get(setting, defaultValue), setting); } /** @@ -466,7 +473,14 @@ public final class Settings implements ToXContent { * (eg. 12%). If it does not exists, parses the default value provided. */ public ByteSizeValue getAsMemory(String[] settings, String defaultValue) throws SettingsException { - return MemorySizeValue.parseBytesSizeValueOrHeapRatio(get(settings, defaultValue)); + // NOTE: duplicated from get(String[]) so we can pass which setting name was actually used to parseBytesSizeValueOrHeapRatio + for (String setting : settings) { + String retVal = get(setting); + if (retVal != null) { + return MemorySizeValue.parseBytesSizeValueOrHeapRatio(retVal, setting); + } + } + return MemorySizeValue.parseBytesSizeValueOrHeapRatio(defaultValue, settings[0]); } /** diff --git a/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java b/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java index 153417f869f..7918f9d5117 100644 --- a/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java +++ b/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java @@ -19,16 +19,17 @@ package org.elasticsearch.common.unit; +import java.io.IOException; +import java.io.Serializable; +import java.util.Locale; +import java.util.Objects; + import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; -import java.io.IOException; -import java.io.Serializable; -import java.util.Locale; - /** * */ @@ -171,11 +172,12 @@ public class ByteSizeValue implements Serializable, Streamable { return Strings.format1Decimals(value, suffix); } - public static ByteSizeValue parseBytesSizeValue(String sValue) throws ElasticsearchParseException { - return parseBytesSizeValue(sValue, null); + public static ByteSizeValue parseBytesSizeValue(String sValue, String settingName) throws ElasticsearchParseException { + return parseBytesSizeValue(sValue, null, settingName); } - public static ByteSizeValue parseBytesSizeValue(String sValue, ByteSizeValue defaultValue) throws ElasticsearchParseException { + public static ByteSizeValue parseBytesSizeValue(String sValue, ByteSizeValue defaultValue, String settingName) throws ElasticsearchParseException { + settingName = Objects.requireNonNull(settingName); if (sValue == null) { return defaultValue; } diff --git a/src/main/java/org/elasticsearch/common/unit/MemorySizeValue.java b/src/main/java/org/elasticsearch/common/unit/MemorySizeValue.java index 2204fe8cac5..daf0ffd209a 100644 --- a/src/main/java/org/elasticsearch/common/unit/MemorySizeValue.java +++ b/src/main/java/org/elasticsearch/common/unit/MemorySizeValue.java @@ -19,6 +19,8 @@ package org.elasticsearch.common.unit; +import java.util.Objects; + import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.monitor.jvm.JvmInfo; @@ -31,7 +33,8 @@ public enum MemorySizeValue { /** Parse the provided string as a memory size. This method either accepts absolute values such as * 42 (default assumed unit is byte) or 2mb, or percentages of the heap size: if * the heap is 1G, 10% will be parsed as 100mb. */ - public static ByteSizeValue parseBytesSizeValueOrHeapRatio(String sValue) { + public static ByteSizeValue parseBytesSizeValueOrHeapRatio(String sValue, String settingName) { + settingName = Objects.requireNonNull(settingName); if (sValue != null && sValue.endsWith("%")) { final String percentAsString = sValue.substring(0, sValue.length() - 1); try { @@ -44,7 +47,7 @@ public enum MemorySizeValue { throw new ElasticsearchParseException("Failed to parse [" + percentAsString + "] as a double", e); } } else { - return parseBytesSizeValue(sValue); + return parseBytesSizeValue(sValue, settingName); } } } diff --git a/src/main/java/org/elasticsearch/index/engine/EngineConfig.java b/src/main/java/org/elasticsearch/index/engine/EngineConfig.java index 5c3fe0b616a..cf377ab1d66 100644 --- a/src/main/java/org/elasticsearch/index/engine/EngineConfig.java +++ b/src/main/java/org/elasticsearch/index/engine/EngineConfig.java @@ -129,7 +129,7 @@ public final class EngineConfig { public static final TimeValue DEFAULT_REFRESH_INTERVAL = new TimeValue(1, TimeUnit.SECONDS); public static final TimeValue DEFAULT_GC_DELETES = TimeValue.timeValueSeconds(60); public static final ByteSizeValue DEFAUTL_INDEX_BUFFER_SIZE = new ByteSizeValue(64, ByteSizeUnit.MB); - public static final ByteSizeValue INACTIVE_SHARD_INDEXING_BUFFER = ByteSizeValue.parseBytesSizeValue("500kb"); + public static final ByteSizeValue INACTIVE_SHARD_INDEXING_BUFFER = ByteSizeValue.parseBytesSizeValue("500kb", "INACTIVE_SHARD_INDEXING_BUFFER"); public static final String DEFAULT_VERSION_MAP_SIZE = "25%"; @@ -180,7 +180,7 @@ public final class EngineConfig { double percent = Double.parseDouble(versionMapSizeSetting.substring(0, versionMapSizeSetting.length() - 1)); versionMapSize = new ByteSizeValue((long) (((double) indexingBufferSize.bytes() * (percent / 100)))); } else { - versionMapSize = ByteSizeValue.parseBytesSizeValue(versionMapSizeSetting); + versionMapSize = ByteSizeValue.parseBytesSizeValue(versionMapSizeSetting, "VersionMapSize"); } } diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java index 7c8ae58d5fd..f09e86bbdaa 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/SourceFieldMapper.java @@ -165,7 +165,7 @@ public class SourceFieldMapper extends AbstractFieldMapper implements RootMapper builder.compressThreshold(((Number) fieldNode).longValue()); builder.compress(true); } else { - builder.compressThreshold(ByteSizeValue.parseBytesSizeValue(fieldNode.toString()).bytes()); + builder.compressThreshold(ByteSizeValue.parseBytesSizeValue(fieldNode.toString(), "compress_threshold").bytes()); builder.compress(true); } } diff --git a/src/main/java/org/elasticsearch/index/translog/TranslogConfig.java b/src/main/java/org/elasticsearch/index/translog/TranslogConfig.java index e9f4089b0b7..4d74961619c 100644 --- a/src/main/java/org/elasticsearch/index/translog/TranslogConfig.java +++ b/src/main/java/org/elasticsearch/index/translog/TranslogConfig.java @@ -42,7 +42,7 @@ public final class TranslogConfig { public static final String INDEX_TRANSLOG_FS_TYPE = "index.translog.fs.type"; public static final String INDEX_TRANSLOG_BUFFER_SIZE = "index.translog.fs.buffer_size"; public static final String INDEX_TRANSLOG_SYNC_INTERVAL = "index.translog.sync_interval"; - public static final ByteSizeValue INACTIVE_SHARD_TRANSLOG_BUFFER = ByteSizeValue.parseBytesSizeValue("1kb"); + public static final ByteSizeValue INACTIVE_SHARD_TRANSLOG_BUFFER = ByteSizeValue.parseBytesSizeValue("1kb", "INACTIVE_SHARD_TRANSLOG_BUFFER"); private final TimeValue syncInterval; private final BigArrays bigArrays; @@ -73,7 +73,7 @@ public final class TranslogConfig { this.threadPool = threadPool; this.bigArrays = bigArrays; this.type = TranslogWriter.Type.fromString(indexSettings.get(INDEX_TRANSLOG_FS_TYPE, TranslogWriter.Type.BUFFERED.name())); - this.bufferSize = (int) indexSettings.getAsBytesSize(INDEX_TRANSLOG_BUFFER_SIZE, ByteSizeValue.parseBytesSizeValue("64k")).bytes(); // Not really interesting, updated by IndexingMemoryController... + this.bufferSize = (int) indexSettings.getAsBytesSize(INDEX_TRANSLOG_BUFFER_SIZE, ByteSizeValue.parseBytesSizeValue("64k", INDEX_TRANSLOG_BUFFER_SIZE)).bytes(); // Not really interesting, updated by IndexingMemoryController... syncInterval = indexSettings.getAsTime(INDEX_TRANSLOG_SYNC_INTERVAL, TimeValue.timeValueSeconds(5)); if (syncInterval.millis() > 0 && threadPool != null) { diff --git a/src/main/java/org/elasticsearch/indices/cache/filter/IndicesFilterCache.java b/src/main/java/org/elasticsearch/indices/cache/filter/IndicesFilterCache.java index b5d8e0a0762..1eb232cb286 100644 --- a/src/main/java/org/elasticsearch/indices/cache/filter/IndicesFilterCache.java +++ b/src/main/java/org/elasticsearch/indices/cache/filter/IndicesFilterCache.java @@ -65,7 +65,7 @@ public class IndicesFilterCache extends AbstractComponent implements QueryCache, public IndicesFilterCache(Settings settings) { super(settings); final String sizeString = settings.get(INDICES_CACHE_QUERY_SIZE, "10%"); - final ByteSizeValue size = MemorySizeValue.parseBytesSizeValueOrHeapRatio(sizeString); + final ByteSizeValue size = MemorySizeValue.parseBytesSizeValueOrHeapRatio(sizeString, INDICES_CACHE_QUERY_SIZE); final int count = settings.getAsInt(INDICES_CACHE_QUERY_COUNT, 100000); logger.debug("using [node] weighted filter cache with size [{}], actual_size [{}], max filter count [{}]", sizeString, size, count); diff --git a/src/main/java/org/elasticsearch/indices/cache/query/IndicesQueryCache.java b/src/main/java/org/elasticsearch/indices/cache/query/IndicesQueryCache.java index 06b2480f008..2bbaa7e4d23 100644 --- a/src/main/java/org/elasticsearch/indices/cache/query/IndicesQueryCache.java +++ b/src/main/java/org/elasticsearch/indices/cache/query/IndicesQueryCache.java @@ -128,7 +128,7 @@ public class IndicesQueryCache extends AbstractComponent implements RemovalListe } private void buildCache() { - long sizeInBytes = MemorySizeValue.parseBytesSizeValueOrHeapRatio(size).bytes(); + long sizeInBytes = MemorySizeValue.parseBytesSizeValueOrHeapRatio(size, INDICES_CACHE_QUERY_SIZE).bytes(); CacheBuilder cacheBuilder = CacheBuilder.newBuilder() .maximumWeight(sizeInBytes).weigher(new QueryCacheWeigher()).removalListener(this); diff --git a/src/main/java/org/elasticsearch/indices/recovery/RecoverySettings.java b/src/main/java/org/elasticsearch/indices/recovery/RecoverySettings.java index 7c7e40a4d42..72d7c77e881 100644 --- a/src/main/java/org/elasticsearch/indices/recovery/RecoverySettings.java +++ b/src/main/java/org/elasticsearch/indices/recovery/RecoverySettings.java @@ -73,7 +73,7 @@ public class RecoverySettings extends AbstractComponent implements Closeable { public static final String INDICES_RECOVERY_INTERNAL_LONG_ACTION_TIMEOUT = "indices.recovery.internal_action_long_timeout"; - public static final long SMALL_FILE_CUTOFF_BYTES = ByteSizeValue.parseBytesSizeValue("5mb").bytes(); + public static final long SMALL_FILE_CUTOFF_BYTES = ByteSizeValue.parseBytesSizeValue("5mb", "SMALL_FILE_CUTOFF_BYTES").bytes(); /** * Use {@link #INDICES_RECOVERY_MAX_BYTES_PER_SEC} instead diff --git a/src/main/java/org/elasticsearch/rest/RestRequest.java b/src/main/java/org/elasticsearch/rest/RestRequest.java index 4ccb1a6161f..272bfd038b4 100644 --- a/src/main/java/org/elasticsearch/rest/RestRequest.java +++ b/src/main/java/org/elasticsearch/rest/RestRequest.java @@ -144,7 +144,7 @@ public abstract class RestRequest extends ContextAndHeaderHolder implements ToXC } public ByteSizeValue paramAsSize(String key, ByteSizeValue defaultValue) { - return parseBytesSizeValue(param(key), defaultValue); + return parseBytesSizeValue(param(key), defaultValue, key); } public String[] paramAsStringArray(String key, String[] defaultValue) { diff --git a/src/test/java/org/elasticsearch/benchmark/fs/FsAppendBenchmark.java b/src/test/java/org/elasticsearch/benchmark/fs/FsAppendBenchmark.java index 7dd6481a0c9..06fc39deaba 100644 --- a/src/test/java/org/elasticsearch/benchmark/fs/FsAppendBenchmark.java +++ b/src/test/java/org/elasticsearch/benchmark/fs/FsAppendBenchmark.java @@ -39,8 +39,8 @@ public class FsAppendBenchmark { Path path = PathUtils.get("work/test.log"); IOUtils.deleteFilesIgnoringExceptions(path); - int CHUNK = (int) ByteSizeValue.parseBytesSizeValue("1k").bytes(); - long DATA = ByteSizeValue.parseBytesSizeValue("10gb").bytes(); + int CHUNK = (int) ByteSizeValue.parseBytesSizeValue("1k", "CHUNK").bytes(); + long DATA = ByteSizeValue.parseBytesSizeValue("10gb", "DATA").bytes(); byte[] data = new byte[CHUNK]; new Random().nextBytes(data); diff --git a/src/test/java/org/elasticsearch/benchmark/search/aggregations/TermsAggregationSearchBenchmark.java b/src/test/java/org/elasticsearch/benchmark/search/aggregations/TermsAggregationSearchBenchmark.java index 55d3db53dd0..c6545b9a3d2 100644 --- a/src/test/java/org/elasticsearch/benchmark/search/aggregations/TermsAggregationSearchBenchmark.java +++ b/src/test/java/org/elasticsearch/benchmark/search/aggregations/TermsAggregationSearchBenchmark.java @@ -397,6 +397,6 @@ public class TermsAggregationSearchBenchmark { totalQueryTime += searchResponse.getTookInMillis(); } System.out.println("--> Terms stats agg (" + name + "): " + (totalQueryTime / QUERY_COUNT) + "ms"); - return new StatsResult(name, totalQueryTime, ByteSizeValue.parseBytesSizeValue("0b")); + return new StatsResult(name, totalQueryTime, ByteSizeValue.parseBytesSizeValue("0b", "StatsResult")); } } diff --git a/src/test/java/org/elasticsearch/cluster/SimpleClusterStateTests.java b/src/test/java/org/elasticsearch/cluster/SimpleClusterStateTests.java index 750432f0a1f..c102251862f 100644 --- a/src/test/java/org/elasticsearch/cluster/SimpleClusterStateTests.java +++ b/src/test/java/org/elasticsearch/cluster/SimpleClusterStateTests.java @@ -131,7 +131,8 @@ public class SimpleClusterStateTests extends ElasticsearchIntegrationTest { @Test public void testLargeClusterStatePublishing() throws Exception { - int estimatedBytesSize = scaledRandomIntBetween(ByteSizeValue.parseBytesSizeValue("10k").bytesAsInt(), ByteSizeValue.parseBytesSizeValue("256k").bytesAsInt()); + int estimatedBytesSize = scaledRandomIntBetween(ByteSizeValue.parseBytesSizeValue("10k", "estimatedBytesSize").bytesAsInt(), + ByteSizeValue.parseBytesSizeValue("256k", "estimatedBytesSize").bytesAsInt()); XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties"); int counter = 0; int numberOfFields = 0; diff --git a/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderUnitTests.java b/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderUnitTests.java index 4424af66fa6..0c26828d3b1 100644 --- a/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderUnitTests.java +++ b/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderUnitTests.java @@ -58,9 +58,9 @@ public class DiskThresholdDeciderUnitTests extends ElasticsearchTestCase { }; DiskThresholdDecider decider = new DiskThresholdDecider(Settings.EMPTY, nss, cis, null); - assertThat(decider.getFreeBytesThresholdHigh(), equalTo(ByteSizeValue.parseBytesSizeValue("0b"))); + assertThat(decider.getFreeBytesThresholdHigh(), equalTo(ByteSizeValue.parseBytesSizeValue("0b", "decider"))); assertThat(decider.getFreeDiskThresholdHigh(), equalTo(10.0d)); - assertThat(decider.getFreeBytesThresholdLow(), equalTo(ByteSizeValue.parseBytesSizeValue("0b"))); + assertThat(decider.getFreeBytesThresholdLow(), equalTo(ByteSizeValue.parseBytesSizeValue("0b", "decider"))); assertThat(decider.getFreeDiskThresholdLow(), equalTo(15.0d)); assertThat(decider.getRerouteInterval().seconds(), equalTo(60L)); assertTrue(decider.isEnabled()); @@ -79,11 +79,11 @@ public class DiskThresholdDeciderUnitTests extends ElasticsearchTestCase { applySettings.onRefreshSettings(newSettings); assertThat("high threshold bytes should be unset", - decider.getFreeBytesThresholdHigh(), equalTo(ByteSizeValue.parseBytesSizeValue("0b"))); + decider.getFreeBytesThresholdHigh(), equalTo(ByteSizeValue.parseBytesSizeValue("0b", "high threshold"))); assertThat("high threshold percentage should be changed", decider.getFreeDiskThresholdHigh(), equalTo(30.0d)); assertThat("low threshold bytes should be set to 500mb", - decider.getFreeBytesThresholdLow(), equalTo(ByteSizeValue.parseBytesSizeValue("500mb"))); + decider.getFreeBytesThresholdLow(), equalTo(ByteSizeValue.parseBytesSizeValue("500mb", "low threshold"))); assertThat("low threshold bytes should be unset", decider.getFreeDiskThresholdLow(), equalTo(0.0d)); assertThat("reroute interval should be changed to 30 seconds", diff --git a/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java b/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java index 0522f879cd1..0a6e77b05f3 100644 --- a/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java +++ b/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java @@ -70,29 +70,29 @@ public class ByteSizeValueTests extends ElasticsearchTestCase { @Test public void testParsing() { - assertThat(ByteSizeValue.parseBytesSizeValue("42pb").toString(), is("42pb")); - assertThat(ByteSizeValue.parseBytesSizeValue("42P").toString(), is("42pb")); - assertThat(ByteSizeValue.parseBytesSizeValue("42PB").toString(), is("42pb")); - assertThat(ByteSizeValue.parseBytesSizeValue("54tb").toString(), is("54tb")); - assertThat(ByteSizeValue.parseBytesSizeValue("54T").toString(), is("54tb")); - assertThat(ByteSizeValue.parseBytesSizeValue("54TB").toString(), is("54tb")); - assertThat(ByteSizeValue.parseBytesSizeValue("12gb").toString(), is("12gb")); - assertThat(ByteSizeValue.parseBytesSizeValue("12G").toString(), is("12gb")); - assertThat(ByteSizeValue.parseBytesSizeValue("12GB").toString(), is("12gb")); - assertThat(ByteSizeValue.parseBytesSizeValue("12M").toString(), is("12mb")); - assertThat(ByteSizeValue.parseBytesSizeValue("1b").toString(), is("1b")); - assertThat(ByteSizeValue.parseBytesSizeValue("23kb").toString(), is("23kb")); - assertThat(ByteSizeValue.parseBytesSizeValue("23k").toString(), is("23kb")); - assertThat(ByteSizeValue.parseBytesSizeValue("23").toString(), is("23b")); + assertThat(ByteSizeValue.parseBytesSizeValue("42pb", "testParsing").toString(), is("42pb")); + assertThat(ByteSizeValue.parseBytesSizeValue("42P", "testParsing").toString(), is("42pb")); + assertThat(ByteSizeValue.parseBytesSizeValue("42PB", "testParsing").toString(), is("42pb")); + assertThat(ByteSizeValue.parseBytesSizeValue("54tb", "testParsing").toString(), is("54tb")); + assertThat(ByteSizeValue.parseBytesSizeValue("54T", "testParsing").toString(), is("54tb")); + assertThat(ByteSizeValue.parseBytesSizeValue("54TB", "testParsing").toString(), is("54tb")); + assertThat(ByteSizeValue.parseBytesSizeValue("12gb", "testParsing").toString(), is("12gb")); + assertThat(ByteSizeValue.parseBytesSizeValue("12G", "testParsing").toString(), is("12gb")); + assertThat(ByteSizeValue.parseBytesSizeValue("12GB", "testParsing").toString(), is("12gb")); + assertThat(ByteSizeValue.parseBytesSizeValue("12M", "testParsing").toString(), is("12mb")); + assertThat(ByteSizeValue.parseBytesSizeValue("1b", "testParsing").toString(), is("1b")); + assertThat(ByteSizeValue.parseBytesSizeValue("23kb", "testParsing").toString(), is("23kb")); + assertThat(ByteSizeValue.parseBytesSizeValue("23k", "testParsing").toString(), is("23kb")); + assertThat(ByteSizeValue.parseBytesSizeValue("23", "testParsing").toString(), is("23b")); } @Test(expected = ElasticsearchParseException.class) public void testFailOnEmptyParsing() { - assertThat(ByteSizeValue.parseBytesSizeValue("").toString(), is("23kb")); + assertThat(ByteSizeValue.parseBytesSizeValue("", "emptyParsing").toString(), is("23kb")); } @Test(expected = ElasticsearchParseException.class) public void testFailOnEmptyNumberParsing() { - assertThat(ByteSizeValue.parseBytesSizeValue("g").toString(), is("23b")); + assertThat(ByteSizeValue.parseBytesSizeValue("g", "emptyNumberParsing").toString(), is("23b")); } -} \ No newline at end of file +} diff --git a/src/test/java/org/elasticsearch/network/DirectBufferNetworkTests.java b/src/test/java/org/elasticsearch/network/DirectBufferNetworkTests.java index e41b4b4d7ef..979bc8b4543 100644 --- a/src/test/java/org/elasticsearch/network/DirectBufferNetworkTests.java +++ b/src/test/java/org/elasticsearch/network/DirectBufferNetworkTests.java @@ -56,7 +56,8 @@ public class DirectBufferNetworkTests extends ElasticsearchIntegrationTest { public void verifySaneDirectBufferAllocations() throws Exception { createIndex("test"); - int estimatedBytesSize = scaledRandomIntBetween(ByteSizeValue.parseBytesSizeValue("1.1mb").bytesAsInt(), ByteSizeValue.parseBytesSizeValue("1.5mb").bytesAsInt()); + int estimatedBytesSize = scaledRandomIntBetween(ByteSizeValue.parseBytesSizeValue("1.1mb", "estimatedBytesSize").bytesAsInt(), + ByteSizeValue.parseBytesSizeValue("1.5mb", "estimatedBytesSize").bytesAsInt()); byte[] data = new byte[estimatedBytesSize]; getRandom().nextBytes(data); diff --git a/src/test/java/org/elasticsearch/recovery/RecoverySettingsTest.java b/src/test/java/org/elasticsearch/recovery/RecoverySettingsTest.java index 7cce91272f2..8d918509061 100644 --- a/src/test/java/org/elasticsearch/recovery/RecoverySettingsTest.java +++ b/src/test/java/org/elasticsearch/recovery/RecoverySettingsTest.java @@ -123,7 +123,7 @@ public class RecoverySettingsTest extends ElasticsearchSingleNodeTest { } private void innerTestSettings(String key, int newValue, TimeUnit timeUnit, Validator validator) { - client().admin().cluster().prepareUpdateSettings().setTransientSettings(ImmutableSettings.builder().put(key, newValue, timeUnit)).get(); + client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(key, newValue, timeUnit)).get(); validator.validate(getInstanceFromNode(RecoverySettings.class), newValue); }