diff --git a/hazelcast/src/main/java/com/hazelcast/cache/impl/AbstractCacheRecordStore.java b/hazelcast/src/main/java/com/hazelcast/cache/impl/AbstractCacheRecordStore.java index 100cc324ce1d..145be6078f0a 100644 --- a/hazelcast/src/main/java/com/hazelcast/cache/impl/AbstractCacheRecordStore.java +++ b/hazelcast/src/main/java/com/hazelcast/cache/impl/AbstractCacheRecordStore.java @@ -94,6 +94,7 @@ import static com.hazelcast.cache.impl.operation.MutableOperation.IGNORE_COMPLETION; import static com.hazelcast.cache.impl.record.CacheRecord.TIME_NOT_AVAILABLE; import static com.hazelcast.cache.impl.record.CacheRecordFactory.isExpiredAt; +import com.hazelcast.config.CacheConfigAccessor; import static com.hazelcast.config.CacheConfigAccessor.getTenantControl; import static com.hazelcast.internal.config.ConfigValidator.checkEvictionConfig; import static com.hazelcast.nio.IOUtil.closeResource; @@ -102,6 +103,7 @@ import static com.hazelcast.util.MapUtil.createHashMap; import static com.hazelcast.util.SetUtil.createHashSet; import static com.hazelcast.util.ThreadUtil.assertRunningOnPartitionThread; +import java.io.IOException; import static java.util.Collections.emptySet; @SuppressWarnings({"checkstyle:methodcount", "checkstyle:classfanoutcomplexity"}) @@ -162,13 +164,42 @@ public AbstractCacheRecordStore(String cacheNameWithPrefix, int partitionId, Nod throw new CacheNotExistsException("Cache " + cacheNameWithPrefix + " is already destroyed or not created yet, on " + nodeEngine.getLocalMember()); } - this.eventJournalConfig = nodeEngine.getConfig().findCacheEventJournalConfig(cacheConfig.getName()); - this.evictionConfig = cacheConfig.getEvictionConfig(); - if (evictionConfig == null) { - throw new IllegalStateException("Eviction config cannot be null!"); + Closeable tenantContext = CacheConfigAccessor.getTenantControl(cacheConfig).setTenant(true); + try { + this.eventJournalConfig = nodeEngine.getConfig().findCacheEventJournalConfig(cacheConfig.getName()); + this.evictionConfig = cacheConfig.getEvictionConfig(); + if (evictionConfig == null) { + throw new IllegalStateException("Eviction config cannot be null!"); + } + this.wanReplicationEnabled = cacheService.isWanReplicationEnabled(cacheNameWithPrefix); + this.disablePerEntryInvalidationEvents = cacheConfig.isDisablePerEntryInvalidationEvents(); + initializeStatisticsAndFactories(cacheNameWithPrefix); + this.cacheContext = cacheService.getOrCreateCacheContext(cacheNameWithPrefix); + this.records = createRecordCacheMap(); + this.evictionChecker = createCacheEvictionChecker(evictionConfig.getSize(), evictionConfig.getMaximumSizePolicy()); + this.evictionPolicyEvaluator = createEvictionPolicyEvaluator(evictionConfig); + this.evictionStrategy = createEvictionStrategy(evictionConfig); + this.objectNamespace = CacheService.getObjectNamespace(cacheNameWithPrefix); + this.persistWanReplicatedData = canPersistWanReplicatedData(cacheConfig, nodeEngine); + this.cacheRecordFactory = new CacheRecordFactory(cacheConfig.getInMemoryFormat(), ss); + this.valueComparator = getValueComparatorOf(cacheConfig.getInMemoryFormat()); + this.clearExpiredRecordsTask = cacheService.getExpirationManager().getTask(); + + injectDependencies(evictionPolicyEvaluator.getEvictionPolicyComparator()); + registerResourceIfItIsClosable(cacheWriter); + registerResourceIfItIsClosable(cacheLoader); + registerResourceIfItIsClosable(defaultExpiryPolicy); + init(); + } finally { + try { + tenantContext.close(); + } catch (IOException ex) { + ExceptionUtil.rethrow(ex); + } } - this.wanReplicationEnabled = cacheService.isWanReplicationEnabled(cacheNameWithPrefix); - this.disablePerEntryInvalidationEvents = cacheConfig.isDisablePerEntryInvalidationEvents(); + } + + private void initializeStatisticsAndFactories(String cacheNameWithPrefix) { if (cacheConfig.isStatisticsEnabled()) { statistics = cacheService.createCacheStatIfAbsent(cacheNameWithPrefix); } @@ -192,23 +223,6 @@ public AbstractCacheRecordStore(String cacheNameWithPrefix, int partitionId, Nod } else { throw new IllegalStateException("Expiry policy factory cannot be null!"); } - - this.cacheContext = cacheService.getOrCreateCacheContext(cacheNameWithPrefix); - this.records = createRecordCacheMap(); - this.evictionChecker = createCacheEvictionChecker(evictionConfig.getSize(), evictionConfig.getMaximumSizePolicy()); - this.evictionPolicyEvaluator = createEvictionPolicyEvaluator(evictionConfig); - this.evictionStrategy = createEvictionStrategy(evictionConfig); - this.objectNamespace = CacheService.getObjectNamespace(cacheNameWithPrefix); - this.persistWanReplicatedData = canPersistWanReplicatedData(cacheConfig, nodeEngine); - this.cacheRecordFactory = new CacheRecordFactory(cacheConfig.getInMemoryFormat(), ss); - this.valueComparator = getValueComparatorOf(cacheConfig.getInMemoryFormat()); - this.clearExpiredRecordsTask = cacheService.getExpirationManager().getTask(); - - injectDependencies(evictionPolicyEvaluator.getEvictionPolicyComparator()); - registerResourceIfItIsClosable(cacheWriter); - registerResourceIfItIsClosable(cacheLoader); - registerResourceIfItIsClosable(defaultExpiryPolicy); - init(); } // Overridden in EE diff --git a/hazelcast/src/test/java/com/hazelcast/cache/CacheTenantControlTest.java b/hazelcast/src/test/java/com/hazelcast/cache/CacheTenantControlTest.java index 79e15580e446..5a7610ca9274 100644 --- a/hazelcast/src/test/java/com/hazelcast/cache/CacheTenantControlTest.java +++ b/hazelcast/src/test/java/com/hazelcast/cache/CacheTenantControlTest.java @@ -138,11 +138,11 @@ public void testTenantControl_executionBeforeAfterOps() { cache.destroy(); assertEquals(1, saveCurrentCount.get()); - // expecting tenant context is created & closed 4 times: - // 1 time on creation of record store (wrapping initialization of eviction policy) + // expecting tenant context is created & closed 5 times: + // 2 times on creation of record store (wrapping initialization of eviction policy) // + 3 times on before/afterRun of put, get & getAndPut operations - assertEquals(4, setTenantCount.get()); - assertEquals(4, closeTenantCount.get()); + assertEquals(5, setTenantCount.get()); + assertEquals(5, closeTenantCount.get()); assertEquals(1, unregisterTenantCount.get()); }