Просмотр исходного кода

HADOOP-6252. Provide a method to determine if a deprecated key is set in config file. Contributed by Jakob Homan.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@813639 13f79535-47bb-0310-9956-ffa450edef68
Suresh Srinivas 15 лет назад
Родитель
Сommit
ae6721a85a

+ 3 - 0
CHANGES.txt

@@ -547,6 +547,9 @@ Trunk (unreleased changes)
     alleviate a performance regression introduced in a compatibility layer.
     (Todd Lipcon via cdouglas)
 
+    HADOOP-6252. Provide a method to determine if a deprecated key is set in
+    config file. (Jakob Homan via suresh)
+
   OPTIMIZATIONS
 
     HADOOP-5595. NameNode does not need to run a replicator to choose a

+ 14 - 1
src/java/org/apache/hadoop/conf/Configuration.java

@@ -44,7 +44,6 @@ import java.util.Properties;
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.WeakHashMap;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -297,6 +296,20 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
     return deprecatedKeyMap.containsKey(key);
   }
  
+  /**
+   * Check whether or not the deprecated key has been specified in the
+   * configuration file rather than the new key
+   * 
+   * Returns false if the specified key is not included in the deprecated
+   * key mapping.
+   * 
+   * @param oldKey Old configuration key 
+   * @return If the old configuration key was specified rather than the new one
+   */
+  public boolean deprecatedKeyWasSet(String oldKey) {
+    return isDeprecated(oldKey) && deprecatedKeyMap.get(oldKey).accessed;
+  }
+  
   /**
    * 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

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

@@ -19,6 +19,8 @@
 package org.apache.hadoop.conf;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import java.io.BufferedWriter;
 import java.io.File;
@@ -304,5 +306,28 @@ public class TestConfigurationDeprecation {
     assertEquals("valueG", conf.get("H"));
     assertEquals("valueG", conf.get("I"));
   }
+  
+  // Ensure that wasDeprecatedKeySet returns the correct result under
+  // the three code paths possible 
+  @Test
+  public void testWasDeprecatedKeySet() {
+    Configuration.addDeprecation("oldKeyA", new String [] { "newKeyA"});
+    Configuration.addDeprecation("oldKeyB", new String [] { "newKeyB"});
+    
+    // Used the deprecated key rather than the new, therefore should trigger
+    conf.set("oldKeyA", "AAA");
+    assertEquals("AAA", conf.get("newKeyA"));
+    assertTrue(conf.deprecatedKeyWasSet("oldKeyA"));
+  
+    // There is a deprecated key, but it wasn't specified. Therefore, don't trigger
+    conf.set("newKeyB", "AndrewBird");
+    assertEquals("AndrewBird", conf.get("newKeyB"));
+    assertFalse(conf.deprecatedKeyWasSet("oldKeyB"));
+    
+    // Not a deprecated key, therefore shouldn't trigger deprecatedKeyWasSet
+    conf.set("BrandNewKey", "BrandNewValue");
+    assertEquals("BrandNewValue", conf.get("BrandNewKey"));
+    assertFalse(conf.deprecatedKeyWasSet("BrandNewKey"));
+  }
 
 }