Skip to content

Commit

Permalink
IGNITE-8587 GridToStringBuilder use ConcurrentHashMap to avoid global…
Browse files Browse the repository at this point in the history
… locks on classCache

IGNITE-8587 GridToStringBuilder use ConcurrentHashMap to avoid global locks on classCache
  • Loading branch information
Sergey Kosarev committed May 27, 2018
1 parent 5564a14 commit 34d2642
Showing 1 changed file with 5 additions and 33 deletions.
Expand Up @@ -28,15 +28,14 @@
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;
Expand Down Expand Up @@ -85,10 +84,7 @@
*/
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 =
Expand Down Expand Up @@ -1017,16 +1013,10 @@ private static <T> String toStringImpl(
}
// 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();
}
classCache.remove(cls.getName() + System.identityHashCode(cls.getClassLoader()));

// No other option here.
throw new IgniteException(e);
Expand Down Expand Up @@ -1737,14 +1727,7 @@ private static <T> GridToStringClassDescriptor getClassDescriptor(Class<T> cls)

GridToStringClassDescriptor cd;

rwLock.readLock().lock();

try {
cd = classCache.get(key);
}
finally {
rwLock.readLock().unlock();
}
cd = classCache.get(key);

if (cd == null) {
cd = new GridToStringClassDescriptor(cls);
Expand Down Expand Up @@ -1806,18 +1789,7 @@ else if (!f.isAnnotationPresent(GridToStringExclude.class) &&

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;
Expand Down

0 comments on commit 34d2642

Please sign in to comment.