From ed79e3289dbe3b47ef58522028806b22455abd3e Mon Sep 17 00:00:00 2001 From: Moiz Raja Date: Mon, 11 Nov 2013 13:54:40 -0800 Subject: [PATCH] Do not create the operational/configuration cache if it already exists Change-Id: Ib658499f8eaf81dcad91df8a26539287644a292b Signed-off-by: Moiz Raja --- .../internal/ClusteredDataStoreImpl.java | 22 +++++++++++++--- .../internal/ClusteredDataStoreImplTest.java | 25 +++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/opendaylight/md-sal/clustered-data-store/implementation/src/main/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImpl.java b/opendaylight/md-sal/clustered-data-store/implementation/src/main/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImpl.java index f2e7773a45..18b9892563 100644 --- a/opendaylight/md-sal/clustered-data-store/implementation/src/main/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImpl.java +++ b/opendaylight/md-sal/clustered-data-store/implementation/src/main/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImpl.java @@ -35,19 +35,19 @@ public class ClusteredDataStoreImpl implements ClusteredDataStore { public static final String OPERATIONAL_DATA_CACHE = "clustered_data_store.operational_data_cache"; public static final String CONFIGURATION_DATA_CACHE = "clustered_data_store.configuration_data_cache"; - private ConcurrentMap operationalDataCache; - private ConcurrentMap configurationDataCache; + private final ConcurrentMap operationalDataCache; + private final ConcurrentMap configurationDataCache; public ClusteredDataStoreImpl(IClusterGlobalServices clusterGlobalServices) throws CacheExistException, CacheConfigException { Preconditions.checkNotNull(clusterGlobalServices, "clusterGlobalServices cannot be null"); - operationalDataCache = clusterGlobalServices.createCache(OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL)); + operationalDataCache = getOrCreateCache(clusterGlobalServices, OPERATIONAL_DATA_CACHE); if(operationalDataCache == null){ Preconditions.checkNotNull(operationalDataCache, "operationalDataCache cannot be null"); } - configurationDataCache = clusterGlobalServices.createCache(CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL)); + configurationDataCache = getOrCreateCache(clusterGlobalServices, CONFIGURATION_DATA_CACHE); if(configurationDataCache == null){ Preconditions.checkNotNull(configurationDataCache, "configurationDataCache cannot be null"); @@ -95,6 +95,20 @@ public class ClusteredDataStoreImpl implements ClusteredDataStore { return Rpcs.getRpcResult(true, null, _emptySet); } + + private ConcurrentMap getOrCreateCache(IClusterGlobalServices clusterGlobalServices, String name) throws CacheConfigException { + ConcurrentMap cache = clusterGlobalServices.getCache(name); + + if(cache == null) { + try { + cache = clusterGlobalServices.createCache(name, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL)); + } catch (CacheExistException e) { + cache = clusterGlobalServices.getCache(name); + } + } + return cache; + } + private class ClusteredDataStoreTransaction implements DataCommitTransaction, Object> { private final DataModification,Object> modification; diff --git a/opendaylight/md-sal/clustered-data-store/implementation/src/test/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImplTest.java b/opendaylight/md-sal/clustered-data-store/implementation/src/test/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImplTest.java index 8049bae5af..d7ae4bf2e5 100644 --- a/opendaylight/md-sal/clustered-data-store/implementation/src/test/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImplTest.java +++ b/opendaylight/md-sal/clustered-data-store/implementation/src/test/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImplTest.java @@ -64,6 +64,31 @@ public class ClusteredDataStoreImplTest { } } + @Test + public void constructor_WhenOperationalDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException { + IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class); + + Mockito.>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap()); + Mockito.>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap()); + + new ClusteredDataStoreImpl(mockClusterGlobalServices); + + verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL)); + } + + @Test + public void constructor_WhenConfigurationDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException { + IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class); + + Mockito.>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap()); + Mockito.>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap()); + + new ClusteredDataStoreImpl(mockClusterGlobalServices); + + verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL)); + } + + @Test public void constructor_WhenPassedAValidClusteringServices_ShouldNotThrowAnyExceptions() throws CacheExistException, CacheConfigException { IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices(); -- 2.36.6