Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TenantControl - creating cache records must use tenant #17121

Merged
merged 4 commits into from Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand All @@ -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"})
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand Down
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