GridToStringBuilder use ConcurrentHashMap to avoid global locks on classCache
GridToStringBuilder use ConcurrentHashMap to avoid global locks on classCache - Fixes
@@ -28,15 +28,14 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EventListener;
-import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteSystemProperties;
import org.apache.ignite.internal.util.typedef.F;
@@ -85,10 +84,7 @@ import static org.apache.ignite.IgniteSystemProperties.IGNITE_TO_STRING_INCLUDE_
*/
public class GridToStringBuilder {
/** */
- private static final Map<String, GridToStringClassDescriptor> classCache = new HashMap<>();
-
- /** */
- private static final ReadWriteLock rwLock = new ReentrantReadWriteLock();
+ private static final Map<String, GridToStringClassDescriptor> classCache = new ConcurrentHashMap<>();
/** {@link IgniteSystemProperties#IGNITE_TO_STRING_INCLUDE_SENSITIVE} */
public static final boolean INCLUDE_SENSITIVE =
@@ -1017,16 +1013,10 @@ public class GridToStringBuilder {
}
// Specifically catching all exceptions.
catch (Exception e) {
- rwLock.writeLock().lock();
// Remove entry from cache to avoid potential memory leak
// in case new class loader got loaded under the same identity hash.
- try {
classCache.remove(cls.getName() + System.identityHashCode(cls.getClassLoader()));
- }
- finally {
- rwLock.writeLock().unlock();
- }
// No other option here.
throw new IgniteException(e);
@@ -1737,14 +1727,7 @@ public class GridToStringBuilder {
GridToStringClassDescriptor cd;
- rwLock.readLock().lock();
-
- try {
cd = classCache.get(key);
- }
- finally {
- rwLock.readLock().unlock();
- }
if (cd == null) {
cd = new GridToStringClassDescriptor(cls);
@@ -1806,18 +1789,7 @@ public class GridToStringBuilder {
cd.sortFields();
- /*
- * Allow multiple puts for the same class - they will simply override.
- */
-
- rwLock.writeLock().lock();
-
- try {
- classCache.put(key, cd);
- }
- finally {
- rwLock.writeLock().unlock();
- }
+ classCache.putIfAbsent(key, cd);
}
return cd;
