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

danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hudi.git


The following commit(s) were added to refs/heads/master by this push:
     new e6be39661a0 [HUDI-6894] ReflectionUtils is not thread safe (#9786)
e6be39661a0 is described below

commit e6be39661a0aaed4227f8bcbec54e0abaf97fb22
Author: zhuanshenbsj1 <[email protected]>
AuthorDate: Sat Oct 14 10:22:00 2023 +0800

    [HUDI-6894] ReflectionUtils is not thread safe (#9786)
---
 .../apache/hudi/common/util/ReflectionUtils.java   | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git 
a/hudi-common/src/main/java/org/apache/hudi/common/util/ReflectionUtils.java 
b/hudi-common/src/main/java/org/apache/hudi/common/util/ReflectionUtils.java
index a0d604f6a94..21d91a8a334 100644
--- a/hudi-common/src/main/java/org/apache/hudi/common/util/ReflectionUtils.java
+++ b/hudi-common/src/main/java/org/apache/hudi/common/util/ReflectionUtils.java
@@ -32,10 +32,10 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Enumeration;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Stream;
 
 /**
@@ -45,22 +45,16 @@ public class ReflectionUtils {
 
   private static final Logger LOG = 
LoggerFactory.getLogger(ReflectionUtils.class);
 
-  private static final Map<String, Class<?>> CLAZZ_CACHE = new HashMap<>();
+  private static final Map<String, Class<?>> CLAZZ_CACHE = new 
ConcurrentHashMap<>();
 
   public static Class<?> getClass(String clazzName) {
-    if (!CLAZZ_CACHE.containsKey(clazzName)) {
-      synchronized (CLAZZ_CACHE) {
-        if (!CLAZZ_CACHE.containsKey(clazzName)) {
-          try {
-            Class<?> clazz = Class.forName(clazzName);
-            CLAZZ_CACHE.put(clazzName, clazz);
-          } catch (ClassNotFoundException e) {
-            throw new HoodieException("Unable to load class", e);
-          }
-        }
+    return CLAZZ_CACHE.computeIfAbsent(clazzName, c -> {
+      try {
+        return Class.forName(c);
+      } catch (ClassNotFoundException e) {
+        throw new HoodieException("Unable to load class", e);
       }
-    }
-    return CLAZZ_CACHE.get(clazzName);
+    });
   }
 
   public static <T> T loadClass(String className) {

Reply via email to