org.elasticsearch.common.unit.RatioValue Java Examples

The following examples show how to use org.elasticsearch.common.unit.RatioValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClusterStatsData.java    From elasticsearch-prometheus-exporter with Apache License 2.0 6 votes vote down vote up
/**
 * Try to extract and parse value from settings for given key.
 * First it tries to parse it as a RatioValue (pct) then as byte size value.
 * It assigns parsed value to corresponding argument references (passed via array hack).
 * If parsing fails the method fires exception, however, this should not happen - we rely on Elasticsearch
 * to already have parsed and validated these values before. Unless we screwed something up...
 */
private void parseValue(Settings s, String key, Long[] bytesPointer, Double[] pctPointer) {
    String value = s.get(key);
    if (value != null && pctPointer[0] == null) {
        try {
            pctPointer[0] = RatioValue.parseRatioValue(s.get(key, null)).getAsPercent();
        } catch (SettingsException | ElasticsearchParseException | NullPointerException e1) {
            if (bytesPointer[0] == null) {
                try {
                    bytesPointer[0] = s.getAsBytesSize(key, null).getBytes();
                } catch (SettingsException | ElasticsearchParseException | NullPointerException e2) {
                    // TODO(lvlcek): log.debug("This went wrong, but 'Keep Calm and Carry On'")
                    // We should avoid using logs in this class (due to perf impact), instead we should
                    // consider moving this logic to some static helper class/method going forward.
                }
            }
        }
    }
}
 
Example #2
Source File: DiskThresholdDecider.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to parse the watermark into a percentage, returning 100.0% if
 * it cannot be parsed.
 */
public double thresholdPercentageFromWatermark(String watermark) {
    try {
        return RatioValue.parseRatioValue(watermark).getAsPercent();
    } catch (ElasticsearchParseException ex) {
        // NOTE: this is not end-user leniency, since up above we check that it's a valid byte or percentage, and then store the two cases separately
        return 100.0;
    }
}
 
Example #3
Source File: DiskThresholdDecider.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if a watermark string is a valid percentage or byte size value,
 * returning true if valid, false if invalid.
 */
public boolean validWatermarkSetting(String watermark, String settingName) {
    try {
        RatioValue.parseRatioValue(watermark);
        return true;
    } catch (ElasticsearchParseException e) {
        try {
            ByteSizeValue.parseBytesSizeValue(watermark, settingName);
            return true;
        } catch (ElasticsearchParseException ex) {
            return false;
        }
    }
}
 
Example #4
Source File: DiskThresholdSettings.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to parse the watermark into a percentage, returning 100.0% if it can not be parsed and the specified lenient parameter is
 * true, otherwise throwing an {@link ElasticsearchParseException}.
 *
 * @param watermark the watermark to parse as a percentage
 * @param lenient true if lenient parsing should be applied
 * @return the parsed percentage
 */
private static double thresholdPercentageFromWatermark(String watermark, boolean lenient) {
    try {
        return RatioValue.parseRatioValue(watermark).getAsPercent();
    } catch (ElasticsearchParseException ex) {
        // NOTE: this is not end-user leniency, since up above we check that it's a valid byte or percentage, and then store the two
        // cases separately
        if (lenient) {
            return 100.0;
        }
        throw ex;
    }
}
 
Example #5
Source File: DiskThresholdSettings.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if a watermark string is a valid percentage or byte size value,
 * @return the watermark value given
 */
private static String validWatermarkSetting(String watermark, String settingName) {
    try {
        RatioValue.parseRatioValue(watermark);
    } catch (ElasticsearchParseException e) {
        try {
            ByteSizeValue.parseBytesSizeValue(watermark, settingName);
        } catch (ElasticsearchParseException ex) {
            ex.addSuppressed(e);
            throw ex;
        }
    }
    return watermark;
}
 
Example #6
Source File: Settings.java    From crate with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the setting value (as a RatioValue) associated with the setting key. Provided values can
 * either be a percentage value (eg. 23%), or expressed as a floating point number (eg. 0.23). If
 * it does not exist, parses the default value provided.
 */
public RatioValue getAsRatio(String setting, String defaultValue) throws SettingsException {
    return RatioValue.parseRatioValue(get(setting, defaultValue));
}