From ab28e18e4c7c6212eb65a23002bb829fa1dae907 Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Tue, 31 Aug 2021 10:01:57 -0500 Subject: [PATCH] Adding a deprecation info API check for fractional byte value settings --- .../xpack/deprecation/DeprecationChecks.java | 1 + .../deprecation/NodeDeprecationChecks.java | 29 +++++++++++++++++++ .../NodeDeprecationChecksTests.java | 28 ++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java index dddccceb358ac..bc0a514be4a41 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java @@ -95,6 +95,7 @@ private DeprecationChecks() { NodeDeprecationChecks::checkSingleDataNodeWatermarkSetting, NodeDeprecationChecks::checkImplicitlyDisabledSecurityOnBasicAndTrial, NodeDeprecationChecks::checkMonitoringExporterPassword, + NodeDeprecationChecks::checkFractionalByteValueSettings, NodeDeprecationChecks::checkClusterRoutingAllocationIncludeRelocationsSetting ) ).collect(Collectors.toList()); diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecks.java index 4968eea4ed60f..03f911a3af0e3 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecks.java @@ -19,6 +19,7 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.env.Environment; @@ -38,6 +39,7 @@ import java.util.ArrayList; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -595,4 +597,31 @@ static DeprecationIssue checkClusterRoutingAllocationIncludeRelocationsSetting(f DeprecationIssue.Level.CRITICAL ); } + + static DeprecationIssue checkFractionalByteValueSettings(final Settings settings, + final PluginsAndModules pluginsAndModules, + final ClusterState clusterState, + final XPackLicenseState licenseState) { + Map fractionalByteSettings = new HashMap<>(); + for (String key : settings.keySet()) { + try { + settings.getAsBytesSize(key, ByteSizeValue.ZERO); + String stringValue = settings.get(key); + if (stringValue.contains(".")) { + fractionalByteSettings.put(key, stringValue); + } + } catch (Exception ignoreThis) { + // We expect anything that is not a byte setting to throw an exception, but we don't care about those + } + } + if (fractionalByteSettings.isEmpty()) { + return null; + } + String url = "https://www.elastic.co/guide/en/elasticsearch/reference/master/logging.html#deprecation-logging"; + String message = "support for fractional byte size values is deprecated and will be removed in a future release"; + String details = "change the following settings to non-fractional values: [" + + fractionalByteSettings.entrySet().stream().map(fractionalByteSetting -> fractionalByteSetting.getKey() + "->" + + fractionalByteSetting.getValue()).collect(Collectors.joining(", ")) + "]"; + return new DeprecationIssue(DeprecationIssue.Level.WARNING, message, url, details, false, null); + } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecksTests.java index 0886f0dfe28fd..b83b95811345a 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecksTests.java @@ -863,4 +863,32 @@ public void testImplicitlyConfiguredSecurityOnGoldPlus() { final List issues = getDeprecationIssues(settings, pluginsAndModules, licenseState); assertThat(issues, empty()); } + + public void testCheckFractionalByteValueSettings() { + String settingKey = "network.tcp.send_buffer_size"; + String unit = randomFrom(new String[] {"k", "kb", "m", "mb", "g", "gb", "t", "tb", "p", "pb"}); + float value = Math.abs(randomFloat()); + String settingValue = value + unit; + String unaffectedSettingKey = "some.other.setting"; + String unaffectedSettingValue = "54.32.43mb"; //Not an actual number, so we don't expect to see a deprecation log about it + final Settings nodeSettings = + Settings.builder().put(settingKey, settingValue).put(unaffectedSettingKey, unaffectedSettingValue).build(); + final XPackLicenseState licenseState = new XPackLicenseState(Settings.EMPTY, () -> 0); + final ClusterState clusterState = ClusterState.EMPTY_STATE; + final DeprecationIssue expectedIssue = new DeprecationIssue(DeprecationIssue.Level.WARNING, + "support for fractional byte size values is deprecated and will be removed in a future release", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/logging.html#deprecation-logging", + String.format(Locale.ROOT, + "change the following settings to non-fractional values: [%s->%s]", + settingKey, + settingValue), + false, null + ); + assertThat( + NodeDeprecationChecks.checkFractionalByteValueSettings(nodeSettings, null, clusterState, licenseState), + equalTo(expectedIssue) + ); + assertWarnings(String.format(Locale.ROOT,"Fractional bytes values are deprecated. Use non-fractional bytes values instead: [%s] " + + "found for setting [%s]", settingValue, settingKey)); + } }