Skip to content

Commit

Permalink
Tenant cache create fix master (#17122)
Browse files Browse the repository at this point in the history
Invoke tenant control when creating cache, so

when factories get invoked, appropriate tenant
environment is created.
  • Loading branch information
lprimak committed Jun 24, 2020
1 parent 50c8332 commit aa01066
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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.checkCacheEvictionConfig;
import static com.hazelcast.internal.nio.IOUtil.closeResource;
Expand All @@ -101,6 +102,7 @@
import static com.hazelcast.internal.util.SetUtil.createHashSet;
import static com.hazelcast.internal.util.ThreadUtil.assertRunningOnPartitionThread;
import static com.hazelcast.spi.impl.merge.MergingValueFactory.createMergingEntry;
import java.io.IOException;
import static java.util.Collections.emptySet;

@SuppressWarnings({"checkstyle:methodcount", "checkstyle:classfanoutcomplexity"})
Expand Down Expand Up @@ -161,13 +163,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 = cacheConfig.getEventJournalConfig();
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 = cacheConfig.getEventJournalConfig();
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.getMaxSizePolicy());
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);
}
Expand All @@ -191,23 +222,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.getMaxSizePolicy());
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down

0 comments on commit aa01066

Please sign in to comment.