You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by aa...@apache.org on 2021/12/17 16:07:57 UTC

[hadoop] branch branch-2.10 updated: HADOOP-13500. Synchronizing iteration of Configuration properties object (#3776)

This is an automated email from the ASF dual-hosted git repository.

aajisaka pushed a commit to branch branch-2.10
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/branch-2.10 by this push:
     new ebf5697  HADOOP-13500. Synchronizing iteration of Configuration properties object (#3776)
ebf5697 is described below

commit ebf569793b3736ad2c57236bef969529757677fb
Author: Dhananjay Badaya <11...@users.noreply.github.com>
AuthorDate: Fri Dec 17 21:37:38 2021 +0530

    HADOOP-13500. Synchronizing iteration of Configuration properties object (#3776)
    
    Signed-off-by: Akira Ajisaka <aa...@apache.org>
---
 .../java/org/apache/hadoop/conf/Configuration.java | 10 ++++---
 .../org/apache/hadoop/conf/TestConfiguration.java  | 35 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java
index 5d34a8e..efe34ef 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java
@@ -2696,11 +2696,13 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
     // methods that allow non-strings to be put into configurations are removed,
     // we could replace properties with a Map<String,String> and get rid of this
     // code.
-    Map<String,String> result = new HashMap<String,String>();
-    for(Map.Entry<Object,Object> item: getProps().entrySet()) {
-      if (item.getKey() instanceof String &&
-          item.getValue() instanceof String) {
+    Properties props = getProps();
+    Map<String, String> result = new HashMap<>();
+    synchronized (props) {
+      for (Map.Entry<Object, Object> item : props.entrySet()) {
+        if (item.getKey() instanceof String && item.getValue() instanceof String) {
           result.put((String) item.getKey(), (String) item.getValue());
+        }
       }
     }
     return result.entrySet().iterator();
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfiguration.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfiguration.java
index 50bf56c..e2ad643 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfiguration.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfiguration.java
@@ -36,12 +36,14 @@ import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.ConcurrentModificationException;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.regex.Pattern;
 import static java.util.concurrent.TimeUnit.*;
 
@@ -2133,6 +2135,39 @@ public class TestConfiguration extends TestCase {
     checkCDATA(os.toByteArray());
   }
 
+  @Test
+  public void testConcurrentModificationDuringIteration() throws InterruptedException {
+    final Configuration configuration = new Configuration();
+    new Thread(new Runnable() {
+      @Override
+      public void run() {
+        while (true) {
+          configuration.set(String.valueOf(Math.random()), String.valueOf(Math.random()));
+        }
+      }
+    }).start();
+
+    final AtomicBoolean exceptionOccurred = new AtomicBoolean(false);
+
+    new Thread(new Runnable() {
+      @Override
+      public void run() {
+        while (true) {
+          try {
+            configuration.iterator();
+          } catch (final ConcurrentModificationException e) {
+            exceptionOccurred.set(true);
+            break;
+          }
+        }
+      }
+    }).start();
+
+    Thread.sleep(1000); //give enough time for threads to run
+
+    assertFalse("ConcurrentModificationException occurred", exceptionOccurred.get());
+  }
+
   private static Configuration checkCDATA(byte[] bytes) {
     Configuration conf = new Configuration(false);
     conf.addResource(new ByteArrayInputStream(bytes));

---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org