Sitelet https://github.com/Mindgamesnl/ehcache3/commit/4cb66440246f6c39f3cd52c865bfb12e97284d77
Skip to content

Commit 4cb6644

Browse files
committed
Cleanup cache state lifecycling
1 parent 6304407 commit 4cb6644

19 files changed

Lines changed: 308 additions & 62 deletions

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/AbstractPartition.java‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.ehcache.impl.internal.store.shared;
1818

19+
import org.ehcache.config.ResourceType;
1920
import org.ehcache.core.spi.store.Store;
2021
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
2122

@@ -26,15 +27,21 @@
2627

2728
public class AbstractPartition<S> {
2829

29-
protected final int id;
30+
private final ResourceType<?> type;
31+
private final int id;
3032
private final S shared;
3133

32-
public AbstractPartition(int id, S shared) {
34+
public AbstractPartition(ResourceType<?> type, int id, S shared) {
35+
this.type = type;
3336
this.id = id;
3437
this.shared = shared;
3538
}
3639

37-
protected int id() {
40+
public ResourceType<?> type() {
41+
return type;
42+
}
43+
44+
public int id() {
3845
return id;
3946
}
4047

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/SharedStorage.java‎

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.ehcache.impl.internal.store.shared;
1818

19-
import org.ehcache.Cache;
2019
import org.ehcache.CachePersistenceException;
2120
import org.ehcache.config.Eviction;
2221
import org.ehcache.config.EvictionAdvisor;
@@ -38,12 +37,10 @@
3837
import org.ehcache.impl.internal.store.shared.composites.CompositeInvalidationValve;
3938
import org.ehcache.impl.internal.store.shared.composites.CompositeSerializer;
4039
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
41-
import org.ehcache.impl.internal.store.shared.store.StorePartition;
4240
import org.ehcache.impl.internal.store.shared.composites.CompositeInvalidationListener;
4341
import org.ehcache.spi.loaderwriter.CacheLoaderWriter;
4442
import org.ehcache.spi.persistence.PersistableIdentityService;
4543
import org.ehcache.spi.persistence.PersistableResourceService;
46-
import org.ehcache.spi.resilience.StoreAccessException;
4744
import org.ehcache.spi.serialization.Serializer;
4845
import org.ehcache.spi.service.Service;
4946
import org.ehcache.spi.service.ServiceConfiguration;
@@ -183,18 +180,19 @@ protected <T, U, K, V> U createPartition(String alias, Store.Configuration<K, V>
183180
return partitionFactory.createPartition(storeId, (T) store, this);
184181
}
185182

186-
public void releaseStore(Store<?, ?> store) {
187-
if (!(store instanceof StorePartition)) {
188-
throw new IllegalArgumentException("Given store is not managed by this provider : " + store);
189-
}
190-
try {
191-
store.clear();
192-
} catch (Exception ex) {
193-
LOGGER.error("Error clearing the store", ex);
194-
}
183+
public void releasePartition(AbstractPartition<?> partition) {
184+
int id = partition.id();
185+
keySerializerMap.remove(id);
186+
valueSerializerMap.remove(id);
187+
expiryPolicyMap.remove(id);
188+
evictionAdvisorMap.remove(id);
189+
expiryPolicyMap.remove(id);
190+
invalidationValveMap.remove(id);
191+
invalidationListenerMap.remove(id);
192+
195193
StatisticsService statisticsService = serviceProvider.getService(StatisticsService.class);
196194
if (statisticsService != null) {
197-
statisticsService.cleanForNode(store);
195+
statisticsService.cleanForNode(partition);
198196
}
199197
}
200198

@@ -208,18 +206,7 @@ public boolean supports(Class<?> providerClass) {
208206

209207
public void destroyPartition(String alias) {
210208
if (persistent) {
211-
int id = persistentPartitionIds.map(alias);
212-
Store.Iterator<Cache.Entry<CompositeValue<?>, Store.ValueHolder<CompositeValue<?>>>> iterator = store.iterator();
213-
while (iterator.hasNext()) {
214-
try {
215-
Cache.Entry<CompositeValue<?>, Store.ValueHolder<CompositeValue<?>>> next = iterator.next();
216-
if (next.getKey().getStoreId() == id) {
217-
store.remove(next.getKey());
218-
}
219-
} catch (StoreAccessException e) {
220-
//ignore
221-
}
222-
}
209+
persistentPartitionIds.purge(alias);
223210
} else {
224211
//nothing?!
225212
}

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/SharedStorageProvider.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,8 @@ public void destroyPartition(ResourceType<?> resourceType, String alias) {
8585
public void destroyPartition(String name) {
8686
storage.values().forEach(type -> type.destroyPartition(name));
8787
}
88+
89+
public void releasePartition(AbstractPartition<?> partition) {
90+
storage.get(partition.type()).releasePartition(partition);
91+
}
8892
}

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/StateHolderIdGenerator.java‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
import java.io.Serializable;
2323

2424
public class StateHolderIdGenerator<K extends Serializable> {
25+
private static final Object DEAD_SENTINEL = new Sentinel();
2526
volatile int lastUsedId;
2627
final private StateHolder<K, Integer> forwardMap;
27-
final private StateHolder<Integer, K> reverseMap;
28+
final private StateHolder<Integer, Serializable> reverseMap;
2829

2930
public StateHolderIdGenerator(StateRepository repository, Class<K> keyClazz) {
3031
forwardMap = repository.getPersistentStateHolder("forward-map", keyClazz, Integer.class, c -> true, null);
31-
reverseMap = repository.getPersistentStateHolder("reverse-map", Integer.class, keyClazz, c -> true, null);
32+
reverseMap = repository.getPersistentStateHolder("reverse-map", Integer.class, Serializable.class, c -> true, null);
3233
}
3334

3435
/***
@@ -41,7 +42,7 @@ public int map(K key) {
4142
if (existing == null) {
4243
// key is not mapped, first try to reserve an id for it
4344
for (int candidate = lastUsedId + 1; ; candidate++) {
44-
K mappedKey = reverseMap.putIfAbsent(candidate, key);
45+
Serializable mappedKey = reverseMap.putIfAbsent(candidate, key);
4546
if (mappedKey == null || mappedKey.equals(key)) {
4647
// candidate Id is now mapped to key, but check if the key has been mapped since the call to get() above?
4748
existing = forwardMap.putIfAbsent(key, candidate);
@@ -56,10 +57,30 @@ public int map(K key) {
5657
}
5758
}
5859
} else {
60+
setLastUsedId(existing);
5961
return existing;
6062
}
6163
}
6264
private synchronized void setLastUsedId(Integer existing) {
6365
lastUsedId = Integer.max(existing, lastUsedId);
6466
}
67+
68+
public void clear(K key, int id) {
69+
if (!forwardMap.remove(key, id) || !reverseMap.remove(id, key)) {
70+
throw new IllegalStateException();
71+
}
72+
}
73+
74+
@SuppressWarnings("unchecked")
75+
public void purge(K alias) {
76+
int id = forwardMap.get(alias);
77+
78+
reverseMap.remove(id, alias);
79+
reverseMap.putIfAbsent(id, (K) DEAD_SENTINEL);
80+
forwardMap.remove(alias, id);
81+
}
82+
83+
static final class Sentinel implements Serializable {
84+
private static final long serialVersionUID = 1L;
85+
}
6586
}

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/authoritative/AuthoritativeTierPartition.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.ehcache.impl.internal.store.shared.authoritative;
1818

19+
import org.ehcache.config.ResourceType;
1920
import org.ehcache.core.CacheConfigurationChangeListener;
2021
import org.ehcache.core.spi.store.tiering.AuthoritativeTier;
2122
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
@@ -36,8 +37,8 @@ protected AuthoritativeTier<CompositeValue<K>, CompositeValue<V>> shared() {
3637
return (AuthoritativeTier<CompositeValue<K>, CompositeValue<V>>) super.shared();
3738
}
3839

39-
public AuthoritativeTierPartition(int id, Class<K> keyType, Class<V> valueType, AuthoritativeTier<CompositeValue<K>, CompositeValue<V>> store) {
40-
super(id, keyType, valueType, store);
40+
public AuthoritativeTierPartition(ResourceType<?> type, int id, Class<K> keyType, Class<V> valueType, AuthoritativeTier<CompositeValue<K>, CompositeValue<V>> store) {
41+
super(type, id, keyType, valueType, store);
4142
}
4243

4344
@Override

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/authoritative/SharedAuthoritativeTierProvider.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.ehcache.config.ResourceType;
2020
import org.ehcache.core.spi.store.Store;
2121
import org.ehcache.core.spi.store.tiering.AuthoritativeTier;
22+
import org.ehcache.impl.internal.store.shared.AbstractPartition;
2223
import org.ehcache.impl.internal.store.shared.AbstractSharedTierProvider;
2324
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
2425
import org.ehcache.spi.service.ServiceConfiguration;
@@ -38,15 +39,16 @@ public <K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Set<ResourceType<?
3839
ResourceType.SharedResource<?> resourceType = assertResourceIsShareable(resourceTypes);
3940
String alias = extractAlias(serviceConfigs);
4041
return sharedStorageProvider.<AuthoritativeTier<CompositeValue<K>, CompositeValue<V>>, AuthoritativeTier<K, V>, K, V>partition(alias, resourceType.getResourceType(), storeConfig, (id, store, storage) -> {
41-
AuthoritativeTierPartition<K, V> partition = new AuthoritativeTierPartition<>(id, storeConfig.getKeyType(), storeConfig.getValueType(), store);
42+
AuthoritativeTierPartition<K, V> partition = new AuthoritativeTierPartition<>(resourceType.getResourceType(), id, storeConfig.getKeyType(), storeConfig.getValueType(), store);
4243
associateStoreStatsWithPartition(store, partition);
4344
return partition;
4445
});
4546
}
4647

4748
@Override
4849
public void releaseAuthoritativeTier(AuthoritativeTier<?, ?> resource) {
49-
50+
AbstractPartition<?> partition = (AbstractPartition<?>) resource;
51+
sharedStorageProvider.releasePartition(partition);
5052
}
5153

5254
@Override

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/caching/CachingTierPartition.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.ehcache.impl.internal.store.shared.caching;
1818

1919
import org.ehcache.Cache;
20+
import org.ehcache.config.ResourceType;
2021
import org.ehcache.core.CacheConfigurationChangeListener;
2122
import org.ehcache.core.spi.store.Store;
2223
import org.ehcache.core.spi.store.Store.ValueHolder;
@@ -40,9 +41,8 @@ public class CachingTierPartition<K, V> extends AbstractPartition<CachingTier<Co
4041
private static final Logger LOGGER = LoggerFactory.getLogger(CachingTierPartition.class);
4142
private final Map<Integer, CachingTier.InvalidationListener<?, ?>> invalidationListenerMap;
4243

43-
@SuppressWarnings({"rawtypes", "unchecked"})
44-
public CachingTierPartition(int id, CachingTier<CompositeValue<K>, CompositeValue<V>> store, Map<Integer, CachingTier.InvalidationListener<?, ?>> invalidationListenerMap) {
45-
super(id, store);
44+
public CachingTierPartition(ResourceType<?> type, int id, CachingTier<CompositeValue<K>, CompositeValue<V>> store, Map<Integer, CachingTier.InvalidationListener<?, ?>> invalidationListenerMap) {
45+
super(type, id, store);
4646
this.invalidationListenerMap = invalidationListenerMap;
4747
}
4848

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/caching/SharedCachingTierProvider.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.ehcache.config.ResourceType;
2020
import org.ehcache.core.spi.store.Store;
2121
import org.ehcache.core.spi.store.tiering.CachingTier;
22+
import org.ehcache.impl.internal.store.shared.AbstractPartition;
2223
import org.ehcache.impl.internal.store.shared.AbstractSharedTierProvider;
2324
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
2425
import org.ehcache.spi.service.ServiceConfiguration;
@@ -37,15 +38,16 @@ public int rankCachingTier(Set<ResourceType<?>> resourceTypes, Collection<Servic
3738
public <K, V> CachingTier<K, V> createCachingTier(Set<ResourceType<?>> resourceTypes, Store.Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
3839
ResourceType.SharedResource<?> resourceType = assertResourceIsShareable(resourceTypes);
3940
return sharedStorageProvider.<CachingTier<CompositeValue<K>, CompositeValue<V>>, CachingTier<K, V>, K, V>partition(extractAlias(serviceConfigs), resourceType.getResourceType(), storeConfig, (id, store, shared) -> {
40-
CachingTierPartition<K, V> partition = new CachingTierPartition<>(id, store, shared.getInvalidationListeners());
41+
CachingTierPartition<K, V> partition = new CachingTierPartition<>(resourceType.getResourceType(), id, store, shared.getInvalidationListeners());
4142
associateStoreStatsWithPartition(store, partition);
4243
return partition;
4344
});
4445
}
4546

4647
@Override
4748
public void releaseCachingTier(CachingTier<?, ?> resource) {
48-
49+
AbstractPartition<?> partition = (AbstractPartition<?>) resource;
50+
sharedStorageProvider.releasePartition(partition);
4951
}
5052

5153
@Override

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/caching/higher/HigherCachingTierPartition.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.ehcache.impl.internal.store.shared.caching.higher;
1818

19+
import org.ehcache.config.ResourceType;
1920
import org.ehcache.core.spi.store.Store.ValueHolder;
2021
import org.ehcache.core.spi.store.tiering.HigherCachingTier;
2122
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
@@ -28,8 +29,8 @@
2829

2930
public class HigherCachingTierPartition<K, V> extends CachingTierPartition<K, V> implements HigherCachingTier<K, V> {
3031

31-
public HigherCachingTierPartition(int id, HigherCachingTier<CompositeValue<K>, CompositeValue<V>> store, Map<Integer, InvalidationListener<?, ?>> invalidationListenerMap) {
32-
super(id, store, invalidationListenerMap);
32+
public HigherCachingTierPartition(ResourceType<?> type, int id, HigherCachingTier<CompositeValue<K>, CompositeValue<V>> store, Map<Integer, InvalidationListener<?, ?>> invalidationListenerMap) {
33+
super(type, id, store, invalidationListenerMap);
3334
}
3435

3536
@Override

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/caching/higher/SharedHigherCachingTierProvider.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.ehcache.config.ResourceType;
2020
import org.ehcache.core.spi.store.Store;
2121
import org.ehcache.core.spi.store.tiering.HigherCachingTier;
22+
import org.ehcache.impl.internal.store.shared.AbstractPartition;
2223
import org.ehcache.impl.internal.store.shared.AbstractSharedTierProvider;
2324
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
2425
import org.ehcache.spi.service.ServiceConfiguration;
@@ -37,15 +38,16 @@ public int rankHigherCachingTier(Set<ResourceType<?>> resourceTypes, Collection<
3738
public <K, V> HigherCachingTier<K, V> createHigherCachingTier(Set<ResourceType<?>> resourceTypes, Store.Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
3839
ResourceType.SharedResource<?> resourceType = assertResourceIsShareable(resourceTypes);
3940
return sharedStorageProvider.<HigherCachingTier<CompositeValue<K>, CompositeValue<V>>, HigherCachingTier<K, V>, K, V>partition(extractAlias(serviceConfigs), resourceType.getResourceType(), storeConfig, (id, store, shared) -> {
40-
HigherCachingTierPartition<K, V> partition = new HigherCachingTierPartition<>(id, store, shared.getInvalidationListeners());
41+
HigherCachingTierPartition<K, V> partition = new HigherCachingTierPartition<>(resourceType.getResourceType(), id, store, shared.getInvalidationListeners());
4142
associateStoreStatsWithPartition(store, partition);
4243
return partition;
4344
});
4445
}
4546

4647
@Override
4748
public void releaseHigherCachingTier(HigherCachingTier<?, ?> resource) {
48-
49+
AbstractPartition<?> partition = (AbstractPartition<?>) resource;
50+
sharedStorageProvider.releasePartition(partition);
4951
}
5052

5153
@Override

0 commit comments

Comments
 (0)