From f8841bea4df809a3771dbe5f38bbe3b9255accb7 Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Mon, 23 May 2011 20:30:48 +0000 Subject: [PATCH] HADOOP-7287. Configuration deprecation mechanism doesn't work properly for GenericOptionsParser and Tools. Contributed by Aaron T. Myers. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1126719 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 ++ .../org/apache/hadoop/conf/Configuration.java | 28 ++++++++++++++++-- .../conf/TestConfigurationDeprecation.java | 21 ++++++++++++++ src/test/test-fake-default.xml | 29 +++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/test/test-fake-default.xml diff --git a/CHANGES.txt b/CHANGES.txt index d93bb84567b..d75a6aa6381 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -720,6 +720,9 @@ Release 0.22.0 - Unreleased HADOOP-7318. MD5Hash factory should reset the digester it returns. (todd via eli) + HADOOP-7287. Configuration deprecation mechanism doesn't work properly for + GenericOptionsParser and Tools. (Aaron T. Myers via todd) + Release 0.21.1 - Unreleased IMPROVEMENTS diff --git a/src/java/org/apache/hadoop/conf/Configuration.java b/src/java/org/apache/hadoop/conf/Configuration.java index 0563600bc4a..e5e1adf2e23 100644 --- a/src/java/org/apache/hadoop/conf/Configuration.java +++ b/src/java/org/apache/hadoop/conf/Configuration.java @@ -247,8 +247,14 @@ public class Configuration implements Iterable>, * and custom message(if any provided). */ private static Map deprecatedKeyMap = - new HashMap(); + new HashMap(); + /** + * Stores a mapping from superseding keys to the keys which they deprecate. + */ + private static Map reverseDeprecatedKeyMap = + new HashMap(); + /** * Adds the deprecated key to the deprecation map. * It does not override any existing entries in the deprecation map. @@ -269,6 +275,9 @@ public class Configuration implements Iterable>, DeprecatedKeyInfo newKeyInfo; newKeyInfo = new DeprecatedKeyInfo(newKeys, customMessage); deprecatedKeyMap.put(key, newKeyInfo); + for (String newKey : newKeys) { + reverseDeprecatedKeyMap.put(newKey, key); + } } } @@ -301,7 +310,11 @@ public class Configuration implements Iterable>, /** * Checks for the presence of the property name in the * deprecation map. Returns the first of the list of new keys if present - * in the deprecation map or the name itself. + * in the deprecation map or the name itself. If the property + * is not presently set but the property map contains an entry for the + * deprecated key, the value of the deprecated key is set as the value for + * the provided property name. + * * @param name the property name * @return the first property in the list of properties mapping * the name or the name itself. @@ -319,6 +332,17 @@ public class Configuration implements Iterable>, } } } + String deprecatedKey = reverseDeprecatedKeyMap.get(name); + if (deprecatedKey != null && !getOverlay().containsKey(name) && + getOverlay().containsKey(deprecatedKey)) { + getProps().setProperty(name, getOverlay().getProperty(deprecatedKey)); + getOverlay().setProperty(name, getOverlay().getProperty(deprecatedKey)); + + DeprecatedKeyInfo keyInfo = deprecatedKeyMap.get(deprecatedKey); + if (!keyInfo.accessed) { + LOG.warn(keyInfo.getWarningMessage(deprecatedKey)); + } + } return name; } diff --git a/src/test/core/org/apache/hadoop/conf/TestConfigurationDeprecation.java b/src/test/core/org/apache/hadoop/conf/TestConfigurationDeprecation.java index a55781e49d6..a6f95ef097e 100644 --- a/src/test/core/org/apache/hadoop/conf/TestConfigurationDeprecation.java +++ b/src/test/core/org/apache/hadoop/conf/TestConfigurationDeprecation.java @@ -42,6 +42,10 @@ public class TestConfigurationDeprecation { final static String CONFIG3 = new File("./test-config3.xml").getAbsolutePath(); BufferedWriter out; + + static { + Configuration.addDefaultResource("test-fake-default.xml"); + } @Before public void setUp() throws Exception { @@ -249,4 +253,21 @@ public class TestConfigurationDeprecation { assertNull(conf.get("I")); assertNull(conf.get("J")); } + + @Test + public void testSetBeforeAndGetAfterDeprecation() { + Configuration conf = new Configuration(); + conf.set("oldkey", "hello"); + Configuration.addDeprecation("oldkey", new String[]{"newkey"}); + assertEquals("hello", conf.get("newkey")); + } + + @Test + public void testSetBeforeAndGetAfterDeprecationAndDefaults() { + Configuration conf = new Configuration(); + conf.set("tests.fake-default.old-key", "hello"); + Configuration.addDeprecation("tests.fake-default.old-key", + new String[]{ "tests.fake-default.new-key" }); + assertEquals("hello", conf.get("tests.fake-default.new-key")); + } } diff --git a/src/test/test-fake-default.xml b/src/test/test-fake-default.xml new file mode 100644 index 00000000000..efbb414e34b --- /dev/null +++ b/src/test/test-fake-default.xml @@ -0,0 +1,29 @@ + + + + + + + + tests.fake-default.new-key + tests.fake-default.value + a default value for the "new" key of a deprecated pair. + +