Skip to content

Commit e189d87

Browse files
committed
Service resource ranking rework
1 parent 3c40c0d commit e189d87

34 files changed

Lines changed: 224 additions & 297 deletions

File tree

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,6 @@ public int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfigurat
343343
}
344344
return parentRank + 1;
345345
}
346-
347-
@Override
348-
public int rankAuthority(ResourceType<?> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
349-
int parentRank = super.rankAuthority(authorityResource, serviceConfigs);
350-
if (parentRank == 0 || serviceConfigs.stream().noneMatch(CacheLoaderWriterConfiguration.class::isInstance)) {
351-
return 0;
352-
}
353-
return parentRank + 1;
354-
}
355346
}
356347

357348
}

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: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,5 @@ public int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfigurat
318318
}
319319
return parentRank + 1;
320320
}
321-
322-
@Override
323-
public int rankAuthority(ResourceType<?> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
324-
int parentRank = super.rankAuthority(authorityResource, serviceConfigs);
325-
if (parentRank == 0 || serviceConfigs.stream().noneMatch(WriteBehindConfiguration.class::isInstance)) {
326-
return 0;
327-
}
328-
return parentRank + 1;
329-
}
330321
}
331322
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -882,15 +882,6 @@ public int rank(final Set<ResourceType<?>> resourceTypes, final Collection<Servi
882882
return 1;
883883
}
884884

885-
@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-
}
892-
}
893-
894885
@Override
895886
public void start(final ServiceProvider<Service> serviceProvider) {
896887
connectLock.lock();
@@ -915,7 +906,7 @@ public void stop() {
915906
}
916907

917908
@Override
918-
public <K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
909+
public <K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Set<ResourceType<?>> resourceTypes, Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
919910
ClusteredStore<K, V> authoritativeTier = createStoreInternal(storeConfig, serviceConfigs);
920911

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

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Collections;
3131
import java.util.HashSet;
3232

33+
import static java.util.Collections.singleton;
3334
import static org.ehcache.core.spi.ServiceLocator.dependencySet;
3435
import static org.hamcrest.MatcherAssert.assertThat;
3536
import static org.hamcrest.Matchers.is;
@@ -65,12 +66,12 @@ public void testAuthoritativeRank() {
6566
ServiceLocator serviceLocator = dependencySet().with(mock(ClusteringService.class)).build();
6667
provider.start(serviceLocator);
6768

68-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED,
69+
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED),
6970
Collections.singletonList(cacheLoaderWriterConfiguration)),
7071
is(2));
71-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED, Collections.emptyList()),
72+
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED), Collections.emptyList()),
7273
is(0));
73-
assertThat(provider.rankAuthority(new ClusteredStoreProviderTest.UnmatchedResourceType(), Collections.singletonList(cacheLoaderWriterConfiguration)),
74+
assertThat(provider.rank(singleton(new ClusteredStoreProviderTest.UnmatchedResourceType()), Collections.singletonList(cacheLoaderWriterConfiguration)),
7475
is(0));
7576
}
7677
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Collections;
3333
import java.util.HashSet;
3434

35+
import static java.util.Collections.singleton;
3536
import static org.ehcache.core.spi.ServiceLocator.dependencySet;
3637
import static org.hamcrest.MatcherAssert.assertThat;
3738
import static org.hamcrest.Matchers.is;
@@ -68,13 +69,13 @@ public void testAuthoritativeRank() {
6869
ServiceLocator serviceLocator = dependencySet().with(mock(ClusteringService.class)).build();
6970
provider.start(serviceLocator);
7071

71-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED,
72+
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED),
7273
Arrays.asList(cacheLoaderWriterConfiguration, writeBehindConfiguration)),
7374
is(3));
74-
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED,
75+
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED),
7576
Collections.singletonList(writeBehindConfiguration)),
7677
is(0));
77-
assertThat(provider.rankAuthority(new ClusteredStoreProviderTest.UnmatchedResourceType(), Arrays.asList(cacheLoaderWriterConfiguration,
78+
assertThat(provider.rank(singleton(new ClusteredStoreProviderTest.UnmatchedResourceType()), Arrays.asList(cacheLoaderWriterConfiguration,
7879
writeBehindConfiguration)),
7980
is(0));
8081
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import java.util.List;
5353
import java.util.Map;
5454

55+
import static java.util.Collections.singleton;
5556
import static org.ehcache.core.spi.ServiceLocator.dependencySet;
5657
import static org.hamcrest.MatcherAssert.assertThat;
5758
import static org.hamcrest.Matchers.is;
@@ -129,9 +130,9 @@ public void testAuthoritativeRank() throws Exception {
129130
ServiceLocator serviceLocator = dependencySet().with(mock(ClusteringService.class)).build();
130131
provider.start(serviceLocator);
131132

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));
133+
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
134+
assertThat(provider.rank(singleton(ClusteredResourceType.Types.SHARED), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
135+
assertThat(provider.rank(singleton(new UnmatchedResourceType()), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(0));
135136
}
136137

137138
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public void close() throws Exception {
501501

502502
Store.Provider storeProvider = StoreSupport.selectWrapperStoreProvider(serviceLocator, serviceConfigs);
503503
if (storeProvider == null) {
504-
storeProvider = StoreSupport.selectStoreProvider(serviceLocator, resourceTypes, serviceConfigs);
504+
storeProvider = StoreSupport.select(Store.Provider.class, serviceLocator, resourceTypes, serviceConfigs);
505505
}
506506

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.Arrays;
2626
import java.util.Map;
2727

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

3030
@OptionalServiceDependencies("org.ehcache.core.spi.service.StatisticsService")
3131
public abstract class AbstractWrapperStoreProvider implements WrapperStore.Provider {
@@ -38,7 +38,7 @@ public abstract class AbstractWrapperStoreProvider implements WrapperStore.Provi
3838
@Override
3939
public <K, V> Store<K, V> createStore(Store.Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
4040

41-
Store.Provider underlyingStoreProvider = selectStoreProvider(serviceProvider, storeConfig.getResourcePools().getResourceTypeSet(),
41+
Store.Provider underlyingStoreProvider = select(Store.Provider.class, serviceProvider, storeConfig.getResourcePools().getResourceTypeSet(),
4242
Arrays.asList(serviceConfigs));
4343
Store<K, V> store = underlyingStoreProvider.createStore(storeConfig, serviceConfigs);
4444

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright Terracotta, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ehcache.core.spi.store;
17+
18+
import org.ehcache.config.ResourceType;
19+
import org.ehcache.spi.service.PluralService;
20+
import org.ehcache.spi.service.Service;
21+
import org.ehcache.spi.service.ServiceConfiguration;
22+
23+
import java.util.Collection;
24+
import java.util.Set;
25+
26+
@PluralService
27+
public interface ResourceRankableService extends Service {
28+
29+
/**
30+
* Gets the internal ranking for the instances provided by this {@code Service}
31+
* ability to handle the specified resources. A higher rank value indicates a more preferable instance.
32+
*
33+
* @param resourceTypes the set of {@code ResourceType}s for the store to handle
34+
* @param serviceConfigs the collection of {@code ServiceConfiguration} instances that may contribute
35+
* to the ranking
36+
*
37+
* @return a non-negative rank indicating the ability of a instance created by this {@code Service}
38+
* to handle the resource types specified by {@code resourceTypes}; a rank of 0 indicates an
39+
* inability to handle all types specified in {@code resourceTypes}
40+
*/
41+
int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs);
42+
}

0 commit comments

Comments
 (0)