Avoid using a thread local map as it keeps large objects hanging around as ghosts.
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.network.rpc;
+import com.yahoo.concurrent.CopyOnWriteHashMap;
+
import java.util.LinkedHashMap;
import java.util.Map;
@@ -12,7 +14,7 @@ import java.util.Map;
public class RPCServicePool {
private final RPCNetwork net;
- private final ThreadLocalCache services = new ThreadLocalCache();
+ private final Map<Long, ServiceLRUCache> mapOfServiceCache;
private final int maxSize;
/**
@@ -23,6 +25,7 @@ public class RPCServicePool {
*/
public RPCServicePool(RPCNetwork net, int maxSize) {
this.net = net;
+ mapOfServiceCache = new CopyOnWriteHashMap<>();
this.maxSize = maxSize;
}
@@ -34,14 +37,20 @@ public class RPCServicePool {
* @return A service address for the given pattern.
*/
public RPCServiceAddress resolve(String pattern) {
- RPCService service = services.get().get(pattern);
+
+ ServiceLRUCache cache = getPerThreadCache();
+ RPCService service = cache.get(pattern);
if (service == null) {
service = RPCService.create(net.getMirror(), pattern);
- services.get().put(pattern, service);
+ cache.put(pattern, service);
}
return service.resolve();
}
+ private ServiceLRUCache getPerThreadCache() {
+ return mapOfServiceCache.computeIfAbsent(Thread.currentThread().getId(), (key) -> new ServiceLRUCache(maxSize));
+ }
+
/**
* Returns the number of services available in the pool. This number will never exceed the limit given at
* construction time.
@@ -49,7 +58,7 @@ public class RPCServicePool {
* @return The current size of this pool.
*/
public int getSize() {
- return services.get().size();
+ return getPerThreadCache().size();
}
/**
@@ -59,21 +68,15 @@ public class RPCServicePool {
* @return True if a corresponding service is in the pool.
*/
public boolean hasService(String pattern) {
- return services.get().containsKey(pattern);
+ return getPerThreadCache().containsKey(pattern);
}
- private class ThreadLocalCache extends ThreadLocal<ServiceLRUCache> {
-
- @Override
- protected ServiceLRUCache initialValue() {
- return new ServiceLRUCache();
- }
- }
-
- private class ServiceLRUCache extends LinkedHashMap<String, RPCService> {
+ private static class ServiceLRUCache extends LinkedHashMap<String, RPCService> {
+ private final int maxSize;
- ServiceLRUCache() {
+ ServiceLRUCache(int maxSize) {
super(16, 0.75f, true);
+ this.maxSize = maxSize;
}
@Override
