Fix the build after #3697 (#3807)

This commit is contained in:
Roman Leventov 2016-12-27 07:06:48 +08:00 committed by Himanshu
parent 33800122ad
commit 49d71e9b38
1 changed files with 8 additions and 6 deletions

View File

@ -28,8 +28,10 @@ import io.druid.java.util.common.lifecycle.Lifecycle;
import io.druid.java.util.common.logger.Logger; import io.druid.java.util.common.logger.Logger;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
@ -48,8 +50,9 @@ public class OnHeapNamespaceExtractionCacheManager extends NamespaceExtractionCa
* <p>{@link WeakReference} doesn't override Object's identity equals() and hashCode(), so effectively this map plays * <p>{@link WeakReference} doesn't override Object's identity equals() and hashCode(), so effectively this map plays
* like concurrent {@link java.util.IdentityHashMap}. * like concurrent {@link java.util.IdentityHashMap}.
*/ */
private final ConcurrentHashMap<WeakReference<ConcurrentMap<String, String>>, Boolean> caches = private final Set<WeakReference<ConcurrentMap<String, String>>> caches = Collections.newSetFromMap(
new ConcurrentHashMap<>(); new ConcurrentHashMap<WeakReference<ConcurrentMap<String, String>>, Boolean>()
);
@Inject @Inject
public OnHeapNamespaceExtractionCacheManager(Lifecycle lifecycle, ServiceEmitter serviceEmitter) public OnHeapNamespaceExtractionCacheManager(Lifecycle lifecycle, ServiceEmitter serviceEmitter)
@ -59,8 +62,7 @@ public class OnHeapNamespaceExtractionCacheManager extends NamespaceExtractionCa
private void expungeCollectedCaches() private void expungeCollectedCaches()
{ {
for (Iterator<WeakReference<ConcurrentMap<String, String>>> iterator = caches.keySet().iterator(); for (Iterator<WeakReference<ConcurrentMap<String, String>>> iterator = caches.iterator(); iterator.hasNext(); ) {
iterator.hasNext(); ) {
WeakReference<?> cacheRef = iterator.next(); WeakReference<?> cacheRef = iterator.next();
if (cacheRef.get() == null) { if (cacheRef.get() == null) {
// This may not necessarily mean leak of CacheHandler, because disposeCache() may be called concurrently with // This may not necessarily mean leak of CacheHandler, because disposeCache() may be called concurrently with
@ -81,7 +83,7 @@ public class OnHeapNamespaceExtractionCacheManager extends NamespaceExtractionCa
ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); ConcurrentMap<String, String> cache = new ConcurrentHashMap<>();
WeakReference<ConcurrentMap<String, String>> cacheRef = new WeakReference<>(cache); WeakReference<ConcurrentMap<String, String>> cacheRef = new WeakReference<>(cache);
expungeCollectedCaches(); expungeCollectedCaches();
caches.put(cacheRef, true); caches.add(cacheRef);
return new CacheHandler(this, cache, cacheRef); return new CacheHandler(this, cache, cacheRef);
} }
@ -107,7 +109,7 @@ public class OnHeapNamespaceExtractionCacheManager extends NamespaceExtractionCa
long numEntries = 0; long numEntries = 0;
long size = 0; long size = 0;
expungeCollectedCaches(); expungeCollectedCaches();
for (WeakReference<ConcurrentMap<String, String>> cacheRef : caches.keySet()) { for (WeakReference<ConcurrentMap<String, String>> cacheRef : caches) {
final Map<String, String> cache = cacheRef.get(); final Map<String, String> cache = cacheRef.get();
if (cache == null) { if (cache == null) {
continue; continue;