From d9b01438bbd7177c60c894e607af4881a266326f Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 1 Apr 2021 13:27:16 -0400 Subject: [PATCH] Add migration check for legacy role settings This commit adds a migration check for the legacy role settings. This check checks for any usage of the legacy role settings, and informs the user of the correct value to use for the node.roles settings instead. --- .../xpack/deprecation/DeprecationChecks.java | 80 +++++++++++-------- .../deprecation/NodeDeprecationChecks.java | 58 +++++++++++++- .../NodeDeprecationChecksTests.java | 31 ++++++- 3 files changed, 129 insertions(+), 40 deletions(-) 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 3573fa5c0e3c..f53d5ca0ec93 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 @@ -9,6 +9,7 @@ import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.deprecation.DeprecationInfoAction; @@ -21,6 +22,7 @@ import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Class containing all the cluster, node, and index deprecation checks that will be served @@ -40,40 +42,50 @@ private DeprecationChecks() { )); - static List> NODE_SETTINGS_CHECKS = - Collections.unmodifiableList(Arrays.asList( - NodeDeprecationChecks::javaVersionCheck, - NodeDeprecationChecks::checkPidfile, - NodeDeprecationChecks::checkProcessors, - NodeDeprecationChecks::checkMissingRealmOrders, - NodeDeprecationChecks::checkUniqueRealmOrders, - NodeDeprecationChecks::checkImplicitlyDisabledBasicRealms, - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkThreadPoolListenerQueueSize(settings), - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkThreadPoolListenerSize(settings), - NodeDeprecationChecks::checkClusterRemoteConnectSetting, - NodeDeprecationChecks::checkNodeLocalStorageSetting, - NodeDeprecationChecks::checkGeneralScriptSizeSetting, - NodeDeprecationChecks::checkGeneralScriptExpireSetting, - NodeDeprecationChecks::checkGeneralScriptCompileSettings, - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, - XPackSettings.ENRICH_ENABLED_SETTING), - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, - XPackSettings.FLATTENED_ENABLED), - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, - XPackSettings.INDEX_LIFECYCLE_ENABLED), - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, - XPackSettings.MONITORING_ENABLED), - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, - XPackSettings.ROLLUP_ENABLED), - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, - XPackSettings.SNAPSHOT_LIFECYCLE_ENABLED), - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, - XPackSettings.SQL_ENABLED), - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, - XPackSettings.TRANSFORM_ENABLED), - (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, - XPackSettings.VECTORS_ENABLED) - )); + static final List> NODE_SETTINGS_CHECKS; + + static { + final Stream> legacyRoleSettings = DiscoveryNode.getPossibleRoles() + .stream() + .filter(r -> r.legacySetting() != null) + .map(r -> (s, p) -> NodeDeprecationChecks.checkLegacyRoleSettings(r.legacySetting(), s, p)); + NODE_SETTINGS_CHECKS = Stream.concat( + legacyRoleSettings, + Stream.of( + NodeDeprecationChecks::javaVersionCheck, + NodeDeprecationChecks::checkPidfile, + NodeDeprecationChecks::checkProcessors, + NodeDeprecationChecks::checkMissingRealmOrders, + NodeDeprecationChecks::checkUniqueRealmOrders, + NodeDeprecationChecks::checkImplicitlyDisabledBasicRealms, + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkThreadPoolListenerQueueSize(settings), + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkThreadPoolListenerSize(settings), + NodeDeprecationChecks::checkClusterRemoteConnectSetting, + NodeDeprecationChecks::checkNodeLocalStorageSetting, + NodeDeprecationChecks::checkGeneralScriptSizeSetting, + NodeDeprecationChecks::checkGeneralScriptExpireSetting, + NodeDeprecationChecks::checkGeneralScriptCompileSettings, + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, + XPackSettings.ENRICH_ENABLED_SETTING), + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, + XPackSettings.FLATTENED_ENABLED), + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, + XPackSettings.INDEX_LIFECYCLE_ENABLED), + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, + XPackSettings.MONITORING_ENABLED), + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, + XPackSettings.ROLLUP_ENABLED), + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, + XPackSettings.SNAPSHOT_LIFECYCLE_ENABLED), + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, + XPackSettings.SQL_ENABLED), + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, + XPackSettings.TRANSFORM_ENABLED), + (settings, pluginsAndModules) -> NodeDeprecationChecks.checkNodeBasicLicenseFeatureEnabledSetting(settings, + XPackSettings.VECTORS_ENABLED) + ) + ).collect(Collectors.toList()); + } static List> INDEX_SETTINGS_CHECKS = Collections.unmodifiableList(Arrays.asList( 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 d9b67209946f..d5e11e63b94b 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 @@ -9,6 +9,8 @@ import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules; import org.elasticsearch.bootstrap.JavaVersion; +import org.elasticsearch.cluster.node.DiscoveryNode; +import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; @@ -17,6 +19,7 @@ import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.env.Environment; import org.elasticsearch.node.Node; +import org.elasticsearch.node.NodeRoleSettings; import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.FixedExecutorBuilder; import org.elasticsearch.transport.RemoteClusterService; @@ -32,6 +35,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.function.BiFunction; import java.util.stream.Collectors; class NodeDeprecationChecks { @@ -244,12 +248,45 @@ public static DeprecationIssue checkGeneralScriptCompileSettings(final Settings ); } + public static DeprecationIssue checkLegacyRoleSettings( + final Setting legacyRoleSetting, + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { + + return checkDeprecatedSetting( + settings, + pluginsAndModules, + legacyRoleSetting, + NodeRoleSettings.NODE_ROLES_SETTING, + (v, s) -> { + return DiscoveryNode.getRolesFromSettings(s) + .stream() + .map(DiscoveryNodeRole::roleName) + .collect(Collectors.joining(",")); + }, + "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html#breaking_80_settings_changes" + ); + } + + private static DeprecationIssue checkDeprecatedSetting( + final Settings settings, + final PluginsAndModules pluginsAndModules, + final Setting deprecatedSetting, + final Setting replacementSetting, + final String url + ) { + return checkDeprecatedSetting(settings, pluginsAndModules, deprecatedSetting, replacementSetting, (v, s) -> v, url); + } + private static DeprecationIssue checkDeprecatedSetting( final Settings settings, final PluginsAndModules pluginsAndModules, final Setting deprecatedSetting, final Setting replacementSetting, - final String url) { + final BiFunction replacementValue, + final String url + ) { assert deprecatedSetting.isDeprecated() : deprecatedSetting; if (deprecatedSetting.exists(settings) == false) { return null; @@ -268,7 +305,7 @@ private static DeprecationIssue checkDeprecatedSetting( deprecatedSettingKey, value, replacementSettingKey, - value); + replacementValue.apply(value, settings)); return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, details); } @@ -278,7 +315,20 @@ private static DeprecationIssue checkDeprecatedSetting( final Setting deprecatedSetting, final Setting.AffixSetting replacementSetting, final String star, - final String url) { + final String url + ) { + return checkDeprecatedSetting(settings, pluginsAndModules, deprecatedSetting, replacementSetting, (v, s) -> v, star, url); + } + + private static DeprecationIssue checkDeprecatedSetting( + final Settings settings, + final PluginsAndModules pluginsAndModules, + final Setting deprecatedSetting, + final Setting.AffixSetting replacementSetting, + final BiFunction replacementValue, + final String star, + final String url + ) { assert deprecatedSetting.isDeprecated() : deprecatedSetting; if (deprecatedSetting.exists(settings) == false) { return null; @@ -297,7 +347,7 @@ private static DeprecationIssue checkDeprecatedSetting( deprecatedSettingKey, value, replacementSettingKey, - value, + replacementValue.apply(value, settings), star); return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, details); } 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 f447111eaa6a..c95b0adb5cab 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 @@ -9,7 +9,8 @@ import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules; import org.elasticsearch.bootstrap.JavaVersion; -import org.elasticsearch.common.collect.Set; +import org.elasticsearch.cluster.node.DiscoveryNode; +import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsExecutors; @@ -401,7 +402,7 @@ public void testNodeLocalStorageSetting() { } public void testDeprecatedBasicLicenseSettings() { - Collection> deprecatedXpackSettings = Set.of( + Collection> deprecatedXpackSettings = org.elasticsearch.common.collect.Set.of( XPackSettings.ENRICH_ENABLED_SETTING, XPackSettings.FLATTENED_ENABLED, XPackSettings.INDEX_LIFECYCLE_ENABLED, @@ -430,6 +431,32 @@ public void testDeprecatedBasicLicenseSettings() { } } + public void testLegacyRoleSettings() { + final Collection> legacyRoleSettings = DiscoveryNode.getPossibleRoles() + .stream() + .filter(s -> s.legacySetting() != null) + .map(DiscoveryNodeRole::legacySetting).collect(Collectors.toList()); + for (final Setting legacyRoleSetting : legacyRoleSettings) { + final boolean value = randomBoolean(); + final Settings settings = Settings.builder().put(legacyRoleSetting.getKey(), value).build(); + final PluginsAndModules pluginsAndModules = new PluginsAndModules(Collections.emptyList(), Collections.emptyList()); + final List issues = getDeprecationIssues(settings, pluginsAndModules); + final String roles = DiscoveryNode.getRolesFromSettings(settings) + .stream() + .map(DiscoveryNodeRole::roleName) + .collect(Collectors.joining(",")); + final DeprecationIssue expected = new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, + "setting [" + legacyRoleSetting.getKey() + "] is deprecated in favor of setting [node.roles]", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html#breaking_80_settings_changes", + "the setting [" + legacyRoleSetting.getKey() + "] is currently set to [" + + value + "], instead set [node.roles] to [" + roles + "]" + ); + assertThat(issues, hasItem(expected)); + assertSettingDeprecationsAndWarnings(new Setting[]{legacyRoleSetting}); + } + } + public void testRemovedSettingNotSet() { final Settings settings = Settings.EMPTY; final Setting removedSetting = Setting.simpleString("node.removed_setting");