From: Tony Tkacik Date: Fri, 18 Jul 2014 08:45:04 +0000 (+0000) Subject: Merge "Improve RpcProviderRegistry loading" X-Git-Tag: release/helium~471 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=c12b1a72f126b4679a2428de2e99d8253e19c213;hp=224e583f77a6181119b0cc447f5a040d1f5b261d Merge "Improve RpcProviderRegistry loading" --- diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java index 952d84d885..c61ec4926a 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/RpcProviderRegistryImpl.java @@ -9,6 +9,10 @@ package org.opendaylight.controller.sal.binding.impl; import static com.google.common.base.Preconditions.checkState; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + import java.util.EventListener; import java.util.HashMap; import java.util.Map; @@ -37,14 +41,21 @@ import org.opendaylight.yangtools.yang.binding.RpcService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class RpcProviderRegistryImpl implements // - RpcProviderRegistry, // - RouteChangePublisher> { +public class RpcProviderRegistryImpl implements RpcProviderRegistry, RouteChangePublisher> { private RuntimeCodeGenerator rpcFactory = SingletonHolder.RPC_GENERATOR_IMPL; - // publicProxies is a cache of proxy objects where each value in the map corresponds to a specific RpcService - private final Map, RpcService> publicProxies = new WeakHashMap<>(); + // cache of proxy objects where each value in the map corresponds to a specific RpcService + private final LoadingCache, RpcService> publicProxies = CacheBuilder.newBuilder().weakKeys(). + build(new CacheLoader, RpcService>() { + @Override + public RpcService load(final Class type) { + final RpcService proxy = rpcFactory.getDirectProxyFor(type); + LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this); + return proxy; + } + }); + private final Map, RpcRouter> rpcRouters = new WeakHashMap<>(); private final ListenerRegistry>> routeChangeListeners = ListenerRegistry .create(); @@ -93,26 +104,7 @@ public class RpcProviderRegistryImpl implements // @SuppressWarnings("unchecked") @Override public final T getRpcService(final Class type) { - - T potentialProxy = (T) publicProxies.get(type); - if (potentialProxy != null) { - return potentialProxy; - } - synchronized (this) { - /** - * Potential proxy could be instantiated by other thread while we - * were waiting for the lock. - */ - - potentialProxy = (T) publicProxies.get(type); - if (potentialProxy != null) { - return potentialProxy; - } - T proxy = rpcFactory.getDirectProxyFor(type); - LOG.debug("Created {} as public proxy for {} in {}", proxy, type.getSimpleName(), this); - publicProxies.put(type, proxy); - return proxy; - } + return (T) publicProxies.getUnchecked(type); } @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -205,8 +197,7 @@ public class RpcProviderRegistryImpl implements // } - private class RouteChangeForwarder implements - RouteChangeListener, InstanceIdentifier> { + private class RouteChangeForwarder implements RouteChangeListener, InstanceIdentifier> { private final Class type; @@ -240,8 +231,7 @@ public class RpcProviderRegistryImpl implements // } } - public static class RpcProxyRegistration extends AbstractObjectRegistration implements - RpcRegistration { + public static class RpcProxyRegistration extends AbstractObjectRegistration implements RpcRegistration { private final Class serviceType; private RpcProviderRegistryImpl registry;