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

Service resource ranking rework #3210

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,6 @@ public int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfigurat
}
return parentRank + 1;
}

@Override
public int rankAuthority(ResourceType<?> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
int parentRank = super.rankAuthority(authorityResource, serviceConfigs);
if (parentRank == 0 || serviceConfigs.stream().noneMatch(CacheLoaderWriterConfiguration.class::isInstance)) {
return 0;
}
return parentRank + 1;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

import org.ehcache.clustered.client.service.ClusteringService;
import org.ehcache.clustered.client.service.ClusteringService.ClusteredCacheIdentifier;
import org.ehcache.config.ResourceType;
import org.ehcache.core.spi.store.AbstractWrapperStoreProvider;
import org.ehcache.core.spi.store.Store;
import org.ehcache.spi.loaderwriter.CacheLoaderWriterConfiguration;
import org.ehcache.spi.loaderwriter.CacheLoaderWriterProvider;
import org.ehcache.spi.service.ServiceConfiguration;
import org.ehcache.spi.service.ServiceDependencies;
import java.util.Collection;
import java.util.Set;

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

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

@Override
public int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
throw new UnsupportedOperationException("Its a Wrapper store provider, does not support regular ranking");
}

@Override
public int wrapperStoreRank(Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
CacheLoaderWriterConfiguration<?> loaderWriterConfiguration = findSingletonAmongst(CacheLoaderWriterConfiguration.class, serviceConfigs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,5 @@ public int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfigurat
}
return parentRank + 1;
}

@Override
public int rankAuthority(ResourceType<?> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
int parentRank = super.rankAuthority(authorityResource, serviceConfigs);
if (parentRank == 0 || serviceConfigs.stream().noneMatch(WriteBehindConfiguration.class::isInstance)) {
return 0;
}
return parentRank + 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,6 @@ public int rank(final Set<ResourceType<?>> resourceTypes, final Collection<Servi
return 1;
}

@Override
public int rankAuthority(ResourceType<?> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
if (clusteringService == null) {
return 0;
} else {
return CLUSTER_RESOURCES.contains(authorityResource) ? 1 : 0;
}
}

@Override
public void start(final ServiceProvider<Service> serviceProvider) {
connectLock.lock();
Expand All @@ -915,7 +906,7 @@ public void stop() {
}

@Override
public <K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
public <K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Set<ResourceType<?>> resourceTypes, Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
ClusteredStore<K, V> authoritativeTier = createStoreInternal(storeConfig, serviceConfigs);

tierOperationStatistics.put(authoritativeTier, new OperationStatistic<?>[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Collections;
import java.util.HashSet;

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

assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED,
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED),
Collections.singletonList(cacheLoaderWriterConfiguration)),
is(2));
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED, Collections.emptyList()),
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED), Collections.emptyList()),
is(0));
assertThat(provider.rankAuthority(new ClusteredStoreProviderTest.UnmatchedResourceType(), Collections.singletonList(cacheLoaderWriterConfiguration)),
assertThat(provider.rank(singleton(new ClusteredStoreProviderTest.UnmatchedResourceType()), Collections.singletonList(cacheLoaderWriterConfiguration)),
is(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Collections;
import java.util.HashSet;

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

assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED,
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED),
Arrays.asList(cacheLoaderWriterConfiguration, writeBehindConfiguration)),
is(3));
assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED,
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED),
Collections.singletonList(writeBehindConfiguration)),
is(0));
assertThat(provider.rankAuthority(new ClusteredStoreProviderTest.UnmatchedResourceType(), Arrays.asList(cacheLoaderWriterConfiguration,
assertThat(provider.rank(singleton(new ClusteredStoreProviderTest.UnmatchedResourceType()), Arrays.asList(cacheLoaderWriterConfiguration,
writeBehindConfiguration)),
is(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.List;
import java.util.Map;

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

assertThat(provider.rankAuthority(ClusteredResourceType.Types.DEDICATED, Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
assertThat(provider.rankAuthority(ClusteredResourceType.Types.SHARED, Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
assertThat(provider.rankAuthority(new UnmatchedResourceType(), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(0));
assertThat(provider.rank(singleton(ClusteredResourceType.Types.DEDICATED), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
assertThat(provider.rank(singleton(ClusteredResourceType.Types.SHARED), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(1));
assertThat(provider.rank(singleton(new UnmatchedResourceType()), Collections.<ServiceConfiguration<?, ?>>emptyList()), is(0));
}

private void assertRank(final Store.Provider provider, final int expectedRank, final ResourceType<?>... resources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ public void close() throws Exception {

Store.Provider storeProvider = StoreSupport.selectWrapperStoreProvider(serviceLocator, serviceConfigs);
if (storeProvider == null) {
storeProvider = StoreSupport.selectStoreProvider(serviceLocator, resourceTypes, serviceConfigs);
storeProvider = StoreSupport.select(Store.Provider.class, serviceLocator, resourceTypes, serviceConfigs);
}

Store<K, V> store = storeProvider.createStore(storeConfiguration, serviceConfigArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Arrays;
import java.util.Map;

import static org.ehcache.core.store.StoreSupport.selectStoreProvider;
import static org.ehcache.core.store.StoreSupport.select;

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ehcache.core.spi.store;

import org.ehcache.config.ResourceType;
import org.ehcache.spi.service.PluralService;
import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceConfiguration;

import java.util.Collection;
import java.util.Set;

@PluralService
public interface ResourceRankableService extends Service {

/**
* Gets the internal ranking for the instances provided by this {@code Service}
* ability to handle the specified resources. A higher rank value indicates a more preferable instance.
*
* @param resourceTypes the set of {@code ResourceType}s for the store to handle
* @param serviceConfigs the collection of {@code ServiceConfiguration} instances that may contribute
* to the ranking
*
* @return a non-negative rank indicating the ability of a instance created by this {@code Service}
* to handle the resource types specified by {@code resourceTypes}; a rank of 0 indicates an
* inability to handle all types specified in {@code resourceTypes}
*/
int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs);
}
19 changes: 1 addition & 18 deletions ehcache-core/src/main/java/org/ehcache/core/spi/store/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
import org.ehcache.Cache;
import org.ehcache.config.EvictionAdvisor;
import org.ehcache.config.ResourcePools;
import org.ehcache.config.ResourceType;
import org.ehcache.core.spi.store.events.StoreEventSource;
import org.ehcache.expiry.ExpiryPolicy;
import org.ehcache.spi.loaderwriter.CacheLoaderWriter;
import org.ehcache.spi.resilience.StoreAccessException;
import org.ehcache.spi.serialization.Serializer;
import org.ehcache.spi.service.PluralService;
import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceConfiguration;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -555,7 +552,7 @@ interface ValueHolder<V> extends Supplier<V> {
* Implementation of {@link Provider} have to be thread-safe.
*/
@PluralService
interface Provider extends Service {
interface Provider extends ResourceRankableService {

/**
* Creates a new Store instance
Expand All @@ -577,20 +574,6 @@ interface Provider extends Service {
* @param resource the store to initialize
*/
void initStore(Store<?, ?> resource);

/**
* Gets the internal ranking for the {@link Store} instances provided by this {@code Provider} of the store's
* ability to handle the specified resources. A higher rank value indicates a more capable {@code Store}.
*
* @param resourceTypes the set of {@code ResourceType}s for the store to handle
* @param serviceConfigs the collection of {@code ServiceConfiguration} instances that may contribute
* to the ranking
*
* @return a non-negative rank indicating the ability of a {@code Store} created by this {@code Provider}
* to handle the resource types specified by {@code resourceTypes}; a rank of 0 indicates the store
* can not handle all types specified in {@code resourceTypes}
*/
int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package org.ehcache.core.spi.store;

import org.ehcache.config.ResourceType;
import org.ehcache.spi.service.PluralService;
import org.ehcache.spi.service.ServiceConfiguration;

import java.util.Collection;
import java.util.Set;

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

@Override
default int rank(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
return 0;
}

/**
* Gets the internal ranking for the {@code WrapperStore} instances provided by this {@code Provider} of the wrapper
* store's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
package org.ehcache.core.spi.store.tiering;

import org.ehcache.config.ResourceType;
import org.ehcache.core.spi.store.ResourceRankableService;
import org.ehcache.spi.resilience.StoreAccessException;
import org.ehcache.core.spi.store.Store;
import org.ehcache.spi.service.PluralService;
import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceConfiguration;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -121,7 +122,7 @@ interface InvalidationValve {
* Multiple providers may exist in a given {@link org.ehcache.CacheManager}.
*/
@PluralService
interface Provider extends Service {
interface Provider extends ResourceRankableService {

/**
* Creates a new {@link AuthoritativeTier} instance using the provided configuration.
Expand All @@ -133,7 +134,7 @@ interface Provider extends Service {
* @param serviceConfigs a collection of service configurations
* @return the new authoritative tier
*/
<K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs);
<K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Set<ResourceType<?>> resourceTypes, Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update method javadoc


/**
* Releases an {@link AuthoritativeTier}.
Expand All @@ -150,22 +151,6 @@ interface Provider extends Service {
* @param resource the authoritative tier to initialise
*/
void initAuthoritativeTier(AuthoritativeTier<?, ?> resource);

/**
* Gets the internal ranking for the {@link AuthoritativeTier} instances provided by this {@code Provider} of the
* authority's ability to handle the specified resource.
* <p>
* A higher rank value indicates a more capable {@code AuthoritativeTier}.
*
* @param authorityResource the {@code ResourceType} for the authority to handle
* @param serviceConfigs the collection of {@code ServiceConfiguration} instances that may contribute
* to the ranking
*
* @return a non-negative rank indicating the ability of a {@code AuthoritativeTier} created by this {@code Provider}
* to handle the resource type specified by {@code authorityResource}; a rank of 0 indicates the authority
* can not handle the type specified in {@code authorityResource}
*/
int rankAuthority(ResourceType<?> authorityResource, Collection<ServiceConfiguration<?, ?>> serviceConfigs);
}

}