From daa86a079d9deeb8b429f10a7904ef1e7481c30b Mon Sep 17 00:00:00 2001 From: lprimak Date: Mon, 22 Jun 2020 20:29:38 -0500 Subject: [PATCH 1/3] invoke tenant control when creating cache, so when factories get invoked, appropriate tenant environment is created --- .../cache/impl/AbstractCacheRecordStore.java | 105 ++++++++++-------- 1 file changed, 58 insertions(+), 47 deletions(-) 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..3b1cede2829f 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,53 +164,62 @@ 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!"); - } - this.wanReplicationEnabled = cacheService.isWanReplicationEnabled(cacheNameWithPrefix); - this.disablePerEntryInvalidationEvents = cacheConfig.isDisablePerEntryInvalidationEvents(); - if (cacheConfig.isStatisticsEnabled()) { - statistics = cacheService.createCacheStatIfAbsent(cacheNameWithPrefix); - } - if (cacheConfig.getCacheLoaderFactory() != null) { - Factory cacheLoaderFactory = cacheConfig.getCacheLoaderFactory(); - injectDependencies(cacheLoaderFactory); - cacheLoader = cacheLoaderFactory.create(); - injectDependencies(cacheLoader); - } - if (cacheConfig.getCacheWriterFactory() != null) { - Factory cacheWriterFactory = cacheConfig.getCacheWriterFactory(); - injectDependencies(cacheWriterFactory); - cacheWriter = cacheWriterFactory.create(); - injectDependencies(cacheWriter); - } - if (cacheConfig.getExpiryPolicyFactory() != null) { - Factory expiryPolicyFactory = cacheConfig.getExpiryPolicyFactory(); - injectDependencies(expiryPolicyFactory); - defaultExpiryPolicy = expiryPolicyFactory.create(); - injectDependencies(defaultExpiryPolicy); - } 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(); + 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(); + if (cacheConfig.isStatisticsEnabled()) { + statistics = cacheService.createCacheStatIfAbsent(cacheNameWithPrefix); + } + if (cacheConfig.getCacheLoaderFactory() != null) { + Factory cacheLoaderFactory = cacheConfig.getCacheLoaderFactory(); + injectDependencies(cacheLoaderFactory); + cacheLoader = cacheLoaderFactory.create(); + injectDependencies(cacheLoader); + } + if (cacheConfig.getCacheWriterFactory() != null) { + Factory cacheWriterFactory = cacheConfig.getCacheWriterFactory(); + injectDependencies(cacheWriterFactory); + cacheWriter = cacheWriterFactory.create(); + injectDependencies(cacheWriter); + } + if (cacheConfig.getExpiryPolicyFactory() != null) { + Factory expiryPolicyFactory = cacheConfig.getExpiryPolicyFactory(); + injectDependencies(expiryPolicyFactory); + defaultExpiryPolicy = expiryPolicyFactory.create(); + injectDependencies(defaultExpiryPolicy); + } 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(); + } finally { + try { + tenantContext.close(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } } // Overridden in EE From 17c413484339ec2d470ff292ed5a6f472bfb531f Mon Sep 17 00:00:00 2001 From: lprimak Date: Tue, 23 Jun 2020 11:49:40 -0500 Subject: [PATCH 2/3] - using ExceptionUtils.rethrow() instead of throw RuntimeException() - moved some of constructor's functionality into a private method to comply with checkstyle rules --- .../cache/impl/AbstractCacheRecordStore.java | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) 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 3b1cede2829f..145be6078f0a 100644 --- a/hazelcast/src/main/java/com/hazelcast/cache/impl/AbstractCacheRecordStore.java +++ b/hazelcast/src/main/java/com/hazelcast/cache/impl/AbstractCacheRecordStore.java @@ -173,30 +173,7 @@ public AbstractCacheRecordStore(String cacheNameWithPrefix, int partitionId, Nod } this.wanReplicationEnabled = cacheService.isWanReplicationEnabled(cacheNameWithPrefix); this.disablePerEntryInvalidationEvents = cacheConfig.isDisablePerEntryInvalidationEvents(); - if (cacheConfig.isStatisticsEnabled()) { - statistics = cacheService.createCacheStatIfAbsent(cacheNameWithPrefix); - } - if (cacheConfig.getCacheLoaderFactory() != null) { - Factory cacheLoaderFactory = cacheConfig.getCacheLoaderFactory(); - injectDependencies(cacheLoaderFactory); - cacheLoader = cacheLoaderFactory.create(); - injectDependencies(cacheLoader); - } - if (cacheConfig.getCacheWriterFactory() != null) { - Factory cacheWriterFactory = cacheConfig.getCacheWriterFactory(); - injectDependencies(cacheWriterFactory); - cacheWriter = cacheWriterFactory.create(); - injectDependencies(cacheWriter); - } - if (cacheConfig.getExpiryPolicyFactory() != null) { - Factory expiryPolicyFactory = cacheConfig.getExpiryPolicyFactory(); - injectDependencies(expiryPolicyFactory); - defaultExpiryPolicy = expiryPolicyFactory.create(); - injectDependencies(defaultExpiryPolicy); - } else { - throw new IllegalStateException("Expiry policy factory cannot be null!"); - } - + initializeStatisticsAndFactories(cacheNameWithPrefix); this.cacheContext = cacheService.getOrCreateCacheContext(cacheNameWithPrefix); this.records = createRecordCacheMap(); this.evictionChecker = createCacheEvictionChecker(evictionConfig.getSize(), evictionConfig.getMaximumSizePolicy()); @@ -217,11 +194,37 @@ public AbstractCacheRecordStore(String cacheNameWithPrefix, int partitionId, Nod try { tenantContext.close(); } catch (IOException ex) { - throw new RuntimeException(ex); + ExceptionUtil.rethrow(ex); } } } + private void initializeStatisticsAndFactories(String cacheNameWithPrefix) { + if (cacheConfig.isStatisticsEnabled()) { + statistics = cacheService.createCacheStatIfAbsent(cacheNameWithPrefix); + } + if (cacheConfig.getCacheLoaderFactory() != null) { + Factory cacheLoaderFactory = cacheConfig.getCacheLoaderFactory(); + injectDependencies(cacheLoaderFactory); + cacheLoader = cacheLoaderFactory.create(); + injectDependencies(cacheLoader); + } + if (cacheConfig.getCacheWriterFactory() != null) { + Factory cacheWriterFactory = cacheConfig.getCacheWriterFactory(); + injectDependencies(cacheWriterFactory); + cacheWriter = cacheWriterFactory.create(); + injectDependencies(cacheWriter); + } + if (cacheConfig.getExpiryPolicyFactory() != null) { + Factory expiryPolicyFactory = cacheConfig.getExpiryPolicyFactory(); + injectDependencies(expiryPolicyFactory); + defaultExpiryPolicy = expiryPolicyFactory.create(); + injectDependencies(defaultExpiryPolicy); + } else { + throw new IllegalStateException("Expiry policy factory cannot be null!"); + } + } + // Overridden in EE protected ValueComparator getValueComparatorOf(InMemoryFormat inMemoryFormat) { return ValueComparatorUtil.getValueComparatorOf(inMemoryFormat); From 308765aa5d0b96441f7cd804e15c56ac97766433 Mon Sep 17 00:00:00 2001 From: lprimak Date: Tue, 23 Jun 2020 15:17:30 -0500 Subject: [PATCH 3/3] fixed failing test --- .../java/com/hazelcast/cache/CacheTenantControlTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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()); }