Browse Source

HADOOP-7287. Configuration deprecation mechanism doesn't work properly for GenericOptionsParser and Tools. (Aaron T. Myers via todd)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/yahoo-merge@1134088 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 14 years ago
parent
commit
828aab9d53

+ 3 - 0
CHANGES.txt

@@ -132,6 +132,9 @@ Trunk (unreleased changes)
 
 
     HADOOP-7284 Trash and shell's rm does not work for viewfs (Sanjay Radia)
     HADOOP-7284 Trash and shell's rm does not work for viewfs (Sanjay Radia)
 
 
+    HADOOP-7287. Configuration deprecation mechanism doesn't work properly for
+    GenericOptionsParser and Tools. (Aaron T. Myers via todd)
+
 Release 0.22.0 - Unreleased
 Release 0.22.0 - Unreleased
 
 
   INCOMPATIBLE CHANGES
   INCOMPATIBLE CHANGES

+ 26 - 2
src/java/org/apache/hadoop/conf/Configuration.java

@@ -247,8 +247,14 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
    * and custom message(if any provided).
    * and custom message(if any provided).
    */
    */
   private static Map<String, DeprecatedKeyInfo> deprecatedKeyMap = 
   private static Map<String, DeprecatedKeyInfo> deprecatedKeyMap = 
-    new HashMap<String, DeprecatedKeyInfo>();
+      new HashMap<String, DeprecatedKeyInfo>();
   
   
+  /**
+   * Stores a mapping from superseding keys to the keys which they deprecate.
+   */
+  private static Map<String, String> reverseDeprecatedKeyMap =
+      new HashMap<String, String>();
+
   /**
   /**
    * Adds the deprecated key to the deprecation map.
    * Adds the deprecated key to the deprecation map.
    * It does not override any existing entries in the deprecation map.
    * It does not override any existing entries in the deprecation map.
@@ -269,6 +275,9 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
       DeprecatedKeyInfo newKeyInfo;
       DeprecatedKeyInfo newKeyInfo;
       newKeyInfo = new DeprecatedKeyInfo(newKeys, customMessage);
       newKeyInfo = new DeprecatedKeyInfo(newKeys, customMessage);
       deprecatedKeyMap.put(key, newKeyInfo);
       deprecatedKeyMap.put(key, newKeyInfo);
+      for (String newKey : newKeys) {
+        reverseDeprecatedKeyMap.put(newKey, key);
+      }
     }
     }
   }
   }
 
 
@@ -301,7 +310,11 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
   /**
   /**
    * Checks for the presence of the property <code>name</code> in the
    * Checks for the presence of the property <code>name</code> in the
    * deprecation map. Returns the first of the list of new keys if present
    * deprecation map. Returns the first of the list of new keys if present
-   * in the deprecation map or the <code>name</code> itself.
+   * in the deprecation map or the <code>name</code> 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
    * @param name the property name
    * @return the first property in the list of properties mapping
    * @return the first property in the list of properties mapping
    *         the <code>name</code> or the <code>name</code> itself.
    *         the <code>name</code> or the <code>name</code> itself.
@@ -319,6 +332,17 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
         }
         }
       }
       }
     }
     }
+    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;
     return name;
   }
   }
   
   

+ 21 - 0
src/test/core/org/apache/hadoop/conf/TestConfigurationDeprecation.java

@@ -42,6 +42,10 @@ public class TestConfigurationDeprecation {
   final static String CONFIG3 = 
   final static String CONFIG3 = 
     new File("./test-config3.xml").getAbsolutePath();
     new File("./test-config3.xml").getAbsolutePath();
   BufferedWriter out;
   BufferedWriter out;
+  
+  static {
+    Configuration.addDefaultResource("test-fake-default.xml");
+  }
 
 
   @Before
   @Before
   public void setUp() throws Exception {
   public void setUp() throws Exception {
@@ -249,4 +253,21 @@ public class TestConfigurationDeprecation {
     assertNull(conf.get("I"));
     assertNull(conf.get("I"));
     assertNull(conf.get("J"));
     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"));
+  }
 }
 }

+ 29 - 0
src/test/test-fake-default.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0"?>
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF 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.
+-->
+<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
+
+<!-- This file is a fake version of a "default" file like
+  core-default or mapred-default, used for some of the unit tests.
+  -->
+<configuration>
+  <property>
+    <name>tests.fake-default.new-key</name>
+    <value>tests.fake-default.value</value>
+    <description>a default value for the "new" key of a deprecated pair.</description>
+  </property>
+</configuration>