Sitelet https://github.com/Mindgamesnl/ehcache3/commit/949910c4a8fee2ad23ebdde191b3891bfa93a424
Skip to content

Commit 949910c

Browse files
committed
Service resource ranking rework
1 parent cd640dd commit 949910c

32 files changed

Lines changed: 244 additions & 264 deletions

File tree

‎clustered/ehcache-client/src/main/java/org/ehcache/clustered/client/internal/loaderwriter/ClusteredLoaderWriterStore.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfigurat
345345
}
346346

347347
@Override
348-
public int rankAuthority(ResourceType<?> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
348+
public int rankAuthority(Set<ResourceType<?>> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
349349
int parentRank = super.rankAuthority(authorityResource, serviceConfigs);
350350
if (parentRank == 0 || serviceConfigs.stream().noneMatch(CacheLoaderWriterConfiguration.class::isInstance)) {
351351
return 0;

‎clustered/ehcache-client/src/main/java/org/ehcache/clustered/client/internal/loaderwriter/DelegatingLoaderWriterStoreProvider.java‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717

1818
import org.ehcache.clustered.client.service.ClusteringService;
1919
import org.ehcache.clustered.client.service.ClusteringService.ClusteredCacheIdentifier;
20-
import org.ehcache.config.ResourceType;
2120
import org.ehcache.core.spi.store.AbstractWrapperStoreProvider;
2221
import org.ehcache.core.spi.store.Store;
2322
import org.ehcache.spi.loaderwriter.CacheLoaderWriterConfiguration;
2423
import org.ehcache.spi.loaderwriter.CacheLoaderWriterProvider;
2524
import org.ehcache.spi.service.ServiceConfiguration;
2625
import org.ehcache.spi.service.ServiceDependencies;
2726
import java.util.Collection;
28-
import java.util.Set;
2927

3028
import static org.ehcache.core.spi.service.ServiceUtils.findSingletonAmongst;
3129

@@ -38,11 +36,6 @@ protected <K, V> Store<K, V> wrap(Store<K, V> store, Store.Configuration<K, V> s
3836
return loaderWriterStore;
3937
}
4038

41-
@Override
42-
public int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
43-
throw new UnsupportedOperationException("Its a Wrapper store provider, does not support regular ranking");
44-
}
45-
4639
@Override
4740
public int wrapperStoreRank(Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
4841
CacheLoaderWriterConfiguration<?> loaderWriterConfiguration = findSingletonAmongst(CacheLoaderWriterConfiguration.class, serviceConfigs);

‎clustered/ehcache-client/src/main/java/org/ehcache/clustered/client/internal/loaderwriter/writebehind/ClusteredWriteBehindStore.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfigurat
320320
}
321321

322322
@Override
323-
public int rankAuthority(ResourceType<?> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
323+
public int rankAuthority(Set<ResourceType<?>> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
324324
int parentRank = super.rankAuthority(authorityResource, serviceConfigs);
325325
if (parentRank == 0 || serviceConfigs.stream().noneMatch(WriteBehindConfiguration.class::isInstance)) {
326326
return 0;

‎clustered/ehcache-client/src/main/java/org/ehcache/clustered/client/internal/store/ClusteredStore.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -883,12 +883,8 @@ public int rank(final Set<ResourceType<?>> resourceTypes, final Collection<Servi
883883
}
884884

885885
@Override
886-
public int rankAuthority(ResourceType<?> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
887-
if (clusteringService == null) {
888-
return 0;
889-
} else {
890-
return CLUSTER_RESOURCES.contains(authorityResource) ? 1 : 0;
891-
}
886+
public int rankAuthority(Set<ResourceType<?>> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
887+
return rank(authorityResource, serviceConfigs);
892888
}
893889

894890
@Override
@@ -915,7 +911,7 @@ public void stop() {
915911
}
916912

917913
@Override
918-
public <K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
914+
public <K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Set<ResourceType<?>> resourceTypes, Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
919915
ClusteredStore<K, V> authoritativeTier = createStoreInternal(storeConfig, serviceConfigs);
920916

921917
tierOperationStatistics.put(authoritativeTier, new OperationStatistic<?>[] {

‎clustered/ehcache-client/src/test/java/org/ehcache/clustered/client/internal/loaderwriter/ClusteredLoaderWriterStoreProviderTest.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ public void testAuthoritativeRank() {
6565
ServiceLocator serviceLocator = dependencySet().with(mock(ClusteringService.class)).build();
6666
provider.start(serviceLocator);
6767

68-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED,
68+
assertThat(provider.rankAuthority(Collections.singleton(ClusteredResourceType.Types.DEDICATED),
6969
Collections.singletonList(cacheLoaderWriterConfiguration)),
70-
is(2));
71-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED, Collections.emptyList()),
70+
is(3));
71+
assertThat(provider.rankAuthority(Collections.singleton(ClusteredResourceType.Types.DEDICATED),
72+
Collections.emptyList()),
7273
is(0));
73-
assertThat(provider.rankAuthority(new ClusteredStoreProviderTest.UnmatchedResourceType(), Collections.singletonList(cacheLoaderWriterConfiguration)),
74+
assertThat(provider.rankAuthority(Collections.singleton(new ClusteredStoreProviderTest.UnmatchedResourceType()),
75+
Collections.singletonList(cacheLoaderWriterConfiguration)),
7476
is(0));
7577
}
7678
}

‎clustered/ehcache-client/src/test/java/org/ehcache/clustered/client/internal/loaderwriter/writebehind/ClusteredWriteBehindStoreProviderTest.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ public void testAuthoritativeRank() {
6868
ServiceLocator serviceLocator = dependencySet().with(mock(ClusteringService.class)).build();
6969
provider.start(serviceLocator);
7070

71-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED,
71+
assertThat(provider.rankAuthority(Collections.singleton(ClusteredResourceType.Types.DEDICATED),
7272
Arrays.asList(cacheLoaderWriterConfiguration, writeBehindConfiguration)),
73-
is(3));
74-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED,
73+
is(5));
74+
assertThat(provider.rankAuthority(Collections.singleton(ClusteredResourceType.Types.DEDICATED),
7575
Collections.singletonList(writeBehindConfiguration)),
7676
is(0));
77-
assertThat(provider.rankAuthority(new ClusteredStoreProviderTest.UnmatchedResourceType(), Arrays.asList(cacheLoaderWriterConfiguration,
78-
writeBehindConfiguration)),
77+
assertThat(provider.rankAuthority(Collections.singleton(new ClusteredStoreProviderTest.UnmatchedResourceType()),
78+
Arrays.asList(cacheLoaderWriterConfiguration, writeBehindConfiguration)),
7979
is(0));
8080
}
8181
}

‎clustered/ehcache-client/src/test/java/org/ehcache/clustered/client/internal/store/ClusteredStoreProviderTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ public void testAuthoritativeRank() throws Exception {
129129
ServiceLocator serviceLocator = dependencySet().with(mock(ClusteringService.class)).build();
130130
provider.start(serviceLocator);
131131

132-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED, Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
133-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.SHARED, Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
134-
assertThat(provider.rankAuthority(new UnmatchedResourceType(), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(0));
132+
assertThat(provider.rankAuthority(Collections.singleton(ClusteredResourceType.Types.DEDICATED), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
133+
assertThat(provider.rankAuthority(Collections.singleton(ClusteredResourceType.Types.SHARED), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
134+
assertThat(provider.rankAuthority(Collections.singleton(new UnmatchedResourceType()), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(0));
135135
}
136136

137137
private void assertRank(final Store.Provider provider, final int expectedRank, final ResourceType<?>... resources) {

‎ehcache-core/src/main/java/org/ehcache/core/EhcacheManager.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.ehcache.core.spi.service.ServiceUtils;
4242
import org.ehcache.core.spi.store.InternalCacheManager;
4343
import org.ehcache.core.spi.store.Store;
44+
import org.ehcache.core.spi.store.WrapperStore;
4445
import org.ehcache.core.store.StoreConfigurationImpl;
4546
import org.ehcache.core.store.StoreSupport;
4647
import org.ehcache.core.util.ClassLoading;
@@ -499,10 +500,9 @@ public void close() throws Exception {
499500
Store.Configuration<K, V> storeConfiguration = new StoreConfigurationImpl<>(config, dispatcherConcurrency,
500501
operationStatisticsEnabled, keySerializer, valueSerializer, loaderWriter, useLoaderInAtomics);
501502

502-
Store.Provider storeProvider = StoreSupport.selectWrapperStoreProvider(serviceLocator, serviceConfigs);
503-
if (storeProvider == null) {
504-
storeProvider = StoreSupport.selectStoreProvider(serviceLocator, resourceTypes, serviceConfigs);
505-
}
503+
504+
Store.Provider storeProvider = StoreSupport.trySelect(WrapperStore.Provider.class, serviceLocator, wrapper -> wrapper.wrapperStoreRank(serviceConfigs))
505+
.map(Store.Provider.class::cast).orElseGet(() -> StoreSupport.select(Store.Provider.class, serviceLocator, store -> store.rank(resourceTypes, serviceConfigs)));
506506

507507
Store<K, V> store = storeProvider.createStore(storeConfiguration, serviceConfigArray);
508508

‎ehcache-core/src/main/java/org/ehcache/core/spi/store/AbstractWrapperStoreProvider.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.ehcache.core.spi.store;
1717

18+
import org.ehcache.config.ResourceType;
1819
import org.ehcache.core.collections.ConcurrentWeakIdentityHashMap;
1920
import org.ehcache.core.spi.service.StatisticsService;
2021
import org.ehcache.spi.service.OptionalServiceDependencies;
@@ -23,9 +24,11 @@
2324
import org.ehcache.spi.service.ServiceProvider;
2425

2526
import java.util.Arrays;
27+
import java.util.List;
2628
import java.util.Map;
29+
import java.util.Set;
2730

28-
import static org.ehcache.core.store.StoreSupport.selectStoreProvider;
31+
import static org.ehcache.core.store.StoreSupport.select;
2932

3033
@OptionalServiceDependencies("org.ehcache.core.spi.service.StatisticsService")
3134
public abstract class AbstractWrapperStoreProvider implements WrapperStore.Provider {
@@ -38,8 +41,10 @@ public abstract class AbstractWrapperStoreProvider implements WrapperStore.Provi
3841
@Override
3942
public <K, V> Store<K, V> createStore(Store.Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
4043

41-
Store.Provider underlyingStoreProvider = selectStoreProvider(serviceProvider, storeConfig.getResourcePools().getResourceTypeSet(),
42-
Arrays.asList(serviceConfigs));
44+
Set<ResourceType<?>> resources = storeConfig.getResourcePools().getResourceTypeSet();
45+
List<ServiceConfiguration<?, ?>> configs = Arrays.asList(serviceConfigs);
46+
Store.Provider underlyingStoreProvider = select(Store.Provider.class, serviceProvider, store -> store.rank(resources, configs));
47+
4348
Store<K, V> store = underlyingStoreProvider.createStore(storeConfig, serviceConfigs);
4449

4550
Store<K, V> wrappedStore = wrap(store, storeConfig, serviceConfigs);

‎ehcache-core/src/main/java/org/ehcache/core/spi/store/WrapperStore.java‎

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

1717
package org.ehcache.core.spi.store;
1818

19+
import org.ehcache.config.ResourceType;
1920
import org.ehcache.spi.service.PluralService;
2021
import org.ehcache.spi.service.ServiceConfiguration;
2122

2223
import java.util.Collection;
24+
import java.util.Set;
2325

2426
/**
2527
* Marker interface for {@link Store}s which act like wrapper and does not have any storage, rather
@@ -35,6 +37,11 @@ public interface WrapperStore<K, V> extends Store<K, V> {
3537
@PluralService
3638
interface Provider extends Store.Provider {
3739

40+
@Override
41+
default int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
42+
return 0;
43+
}
44+
3845
/**
3946
* Gets the internal ranking for the {@code WrapperStore} instances provided by this {@code Provider} of the wrapper
4047
* store's

0 commit comments

Comments
 (0)