Skip to content

Commit 828aab9

Browse files
author
Mahadev Konar
committed
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
1 parent 32af4f1 commit 828aab9

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ Trunk (unreleased changes)
132132

133133
HADOOP-7284 Trash and shell's rm does not work for viewfs (Sanjay Radia)
134134

135+
HADOOP-7287. Configuration deprecation mechanism doesn't work properly for
136+
GenericOptionsParser and Tools. (Aaron T. Myers via todd)
137+
135138
Release 0.22.0 - Unreleased
136139

137140
INCOMPATIBLE CHANGES

src/java/org/apache/hadoop/conf/Configuration.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,14 @@ private final String getWarningMessage(String key) {
247247
* and custom message(if any provided).
248248
*/
249249
private static Map<String, DeprecatedKeyInfo> deprecatedKeyMap =
250-
new HashMap<String, DeprecatedKeyInfo>();
250+
new HashMap<String, DeprecatedKeyInfo>();
251251

252+
/**
253+
* Stores a mapping from superseding keys to the keys which they deprecate.
254+
*/
255+
private static Map<String, String> reverseDeprecatedKeyMap =
256+
new HashMap<String, String>();
257+
252258
/**
253259
* Adds the deprecated key to the deprecation map.
254260
* It does not override any existing entries in the deprecation map.
@@ -269,6 +275,9 @@ public synchronized static void addDeprecation(String key, String[] newKeys,
269275
DeprecatedKeyInfo newKeyInfo;
270276
newKeyInfo = new DeprecatedKeyInfo(newKeys, customMessage);
271277
deprecatedKeyMap.put(key, newKeyInfo);
278+
for (String newKey : newKeys) {
279+
reverseDeprecatedKeyMap.put(newKey, key);
280+
}
272281
}
273282
}
274283

@@ -301,7 +310,11 @@ private static boolean isDeprecated(String key) {
301310
/**
302311
* Checks for the presence of the property <code>name</code> in the
303312
* deprecation map. Returns the first of the list of new keys if present
304-
* in the deprecation map or the <code>name</code> itself.
313+
* in the deprecation map or the <code>name</code> itself. If the property
314+
* is not presently set but the property map contains an entry for the
315+
* deprecated key, the value of the deprecated key is set as the value for
316+
* the provided property name.
317+
*
305318
* @param name the property name
306319
* @return the first property in the list of properties mapping
307320
* the <code>name</code> or the <code>name</code> itself.
@@ -319,6 +332,17 @@ private String handleDeprecation(String name) {
319332
}
320333
}
321334
}
335+
String deprecatedKey = reverseDeprecatedKeyMap.get(name);
336+
if (deprecatedKey != null && !getOverlay().containsKey(name) &&
337+
getOverlay().containsKey(deprecatedKey)) {
338+
getProps().setProperty(name, getOverlay().getProperty(deprecatedKey));
339+
getOverlay().setProperty(name, getOverlay().getProperty(deprecatedKey));
340+
341+
DeprecatedKeyInfo keyInfo = deprecatedKeyMap.get(deprecatedKey);
342+
if (!keyInfo.accessed) {
343+
LOG.warn(keyInfo.getWarningMessage(deprecatedKey));
344+
}
345+
}
322346
return name;
323347
}
324348

src/test/core/org/apache/hadoop/conf/TestConfigurationDeprecation.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public class TestConfigurationDeprecation {
4242
final static String CONFIG3 =
4343
new File("./test-config3.xml").getAbsolutePath();
4444
BufferedWriter out;
45+
46+
static {
47+
Configuration.addDefaultResource("test-fake-default.xml");
48+
}
4549

4650
@Before
4751
public void setUp() throws Exception {
@@ -249,4 +253,21 @@ public void testDeprecationForFinalParameters() throws IOException {
249253
assertNull(conf.get("I"));
250254
assertNull(conf.get("J"));
251255
}
256+
257+
@Test
258+
public void testSetBeforeAndGetAfterDeprecation() {
259+
Configuration conf = new Configuration();
260+
conf.set("oldkey", "hello");
261+
Configuration.addDeprecation("oldkey", new String[]{"newkey"});
262+
assertEquals("hello", conf.get("newkey"));
263+
}
264+
265+
@Test
266+
public void testSetBeforeAndGetAfterDeprecationAndDefaults() {
267+
Configuration conf = new Configuration();
268+
conf.set("tests.fake-default.old-key", "hello");
269+
Configuration.addDeprecation("tests.fake-default.old-key",
270+
new String[]{ "tests.fake-default.new-key" });
271+
assertEquals("hello", conf.get("tests.fake-default.new-key"));
272+
}
252273
}

src/test/test-fake-default.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
19+
20+
<!-- This file is a fake version of a "default" file like
21+
core-default or mapred-default, used for some of the unit tests.
22+
-->
23+
<configuration>
24+
<property>
25+
<name>tests.fake-default.new-key</name>
26+
<value>tests.fake-default.value</value>
27+
<description>a default value for the "new" key of a deprecated pair.</description>
28+
</property>
29+
</configuration>

0 commit comments

Comments
 (0)