SGF-691 - Fix Off-Heap Annotation config to properly handle Entity-defined and Java-based Region configuration.
Additionally, this change set also renames the spring.data.gemfire.cache.off-heap-memory-size property to spring.data.gemfire.cache.off-heap.memory-size and the spring.data.gemfire.cache.region-names property to spring.data.gemfire.cache.off-heap.region-names.
This commit is contained in:
@@ -29,9 +29,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* The {@link EnableOffHeap} annotation marks a Spring {@link Configuration @Configuration} annotated {@link Class}
|
||||
* to configure and enable Pivotal GemFire/Apache Geode Off-Heap Memory support and data storage
|
||||
* in cache {@link org.apache.geode.cache.Region Regions}.
|
||||
* The {@link EnableOffHeap} annotation marks a Spring {@link Configuration @Configuration} annotated application
|
||||
* {@link Class} to configure and enable Off-Heap Memory data storage in cache {@link Region Regions}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see java.lang.annotation.Annotation
|
||||
@@ -62,16 +61,16 @@ public @interface EnableOffHeap {
|
||||
*
|
||||
* Defaults to unset.
|
||||
*
|
||||
* Use the {@literal spring.data.gemfire.cache.off-heap-memory-size} property in {@literal application.properties}.
|
||||
* Use the {@literal spring.data.gemfire.cache.off-heap.memory-size} property in {@literal application.properties}.
|
||||
*/
|
||||
String memorySize();
|
||||
|
||||
/**
|
||||
* Identifies all the {@link Region Regions} by name in which the off-heap memory setting will be applied.
|
||||
* Identifies all the {@link Region Regions} by name in which the Off-Heap Memory settings will be applied.
|
||||
*
|
||||
* Defaults to all Regions.
|
||||
* Defaults to all {@link Region Regions}.
|
||||
*
|
||||
* Use the {@literal spring.data.gemfire.cache.region-names} property in {@literal application.properties}.
|
||||
* Use the {@literal spring.data.gemfire.cache.off-heap.region-names} property in {@literal application.properties}.
|
||||
*/
|
||||
String[] regionNames() default {};
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.springframework.data.gemfire.config.annotation;
|
||||
import static java.util.Arrays.stream;
|
||||
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -39,11 +40,8 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.data.gemfire.GenericRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.LocalRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.RegionLookupFactoryBean;
|
||||
import org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport;
|
||||
import org.springframework.data.gemfire.config.annotation.support.EmbeddedServiceConfigurationSupport;
|
||||
import org.springframework.data.gemfire.util.CollectionUtils;
|
||||
import org.springframework.data.gemfire.util.PropertiesBuilder;
|
||||
@@ -67,7 +65,7 @@ public class OffHeapConfiguration extends EmbeddedServiceConfigurationSupport {
|
||||
* @see org.springframework.data.gemfire.config.annotation.EnableOffHeap
|
||||
*/
|
||||
@Override
|
||||
protected Class getAnnotationType() {
|
||||
protected Class<? extends Annotation> getAnnotationType() {
|
||||
return EnableOffHeap.class;
|
||||
}
|
||||
|
||||
@@ -79,7 +77,7 @@ public class OffHeapConfiguration extends EmbeddedServiceConfigurationSupport {
|
||||
BeanDefinitionBuilder builder =
|
||||
BeanDefinitionBuilder.genericBeanDefinition(OffHeapBeanFactoryPostProcessor.class);
|
||||
|
||||
builder.addConstructorArgValue(resolveProperty(cacheProperty("region-names"),
|
||||
builder.addConstructorArgValue(resolveProperty(cacheOffHeapProperty("region-names"),
|
||||
String[].class, (String[]) annotationAttributes.get("regionNames")));
|
||||
|
||||
registry.registerBeanDefinition(generateBeanName(OffHeapBeanFactoryPostProcessor.class),
|
||||
@@ -92,63 +90,58 @@ public class OffHeapConfiguration extends EmbeddedServiceConfigurationSupport {
|
||||
|
||||
return PropertiesBuilder.create()
|
||||
.setProperty("off-heap-memory-size",
|
||||
resolveProperty(cacheProperty("off-heap-memory-size"),
|
||||
resolveProperty(cacheOffHeapProperty("memory-size"),
|
||||
(String) annotationAttributes.get("memorySize")))
|
||||
.build();
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
@SuppressWarnings("unused")
|
||||
protected static class OffHeapBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||
|
||||
protected static final Set<String> REGION_FACTORY_BEAN_TYPES = new HashSet<>(5);
|
||||
|
||||
static {
|
||||
REGION_FACTORY_BEAN_TYPES.add(ClientRegionFactoryBean.class.getName());
|
||||
REGION_FACTORY_BEAN_TYPES.add(GenericRegionFactoryBean.class.getName());
|
||||
REGION_FACTORY_BEAN_TYPES.add(LocalRegionFactoryBean.class.getName());
|
||||
REGION_FACTORY_BEAN_TYPES.add(PartitionedRegionFactoryBean.class.getName());
|
||||
REGION_FACTORY_BEAN_TYPES.add(ReplicatedRegionFactoryBean.class.getName());
|
||||
}
|
||||
protected static class OffHeapBeanFactoryPostProcessor extends AbstractAnnotationConfigSupport
|
||||
implements BeanFactoryPostProcessor {
|
||||
|
||||
private final Set<String> regionNames;
|
||||
|
||||
protected OffHeapBeanFactoryPostProcessor(String[] regionNames) {
|
||||
this(CollectionUtils.asSet(nullSafeArray(regionNames, String.class)));
|
||||
}
|
||||
|
||||
protected OffHeapBeanFactoryPostProcessor(Set<String> regionNames) {
|
||||
this.regionNames = CollectionUtils.nullSafeSet(regionNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends Annotation> getAnnotationType() {
|
||||
throw new UnsupportedOperationException("Not Implemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
|
||||
stream(nullSafeArray(beanFactory.getBeanDefinitionNames(), String.class)).forEach(beanName -> {
|
||||
|
||||
Optional.ofNullable(beanFactory.getBeanDefinition(beanName))
|
||||
.filter(bean -> isTargetedRegionBean(beanName, bean, beanFactory))
|
||||
.ifPresent(bean ->
|
||||
bean.getPropertyValues().addPropertyValue("offHeap", true));
|
||||
});
|
||||
stream(nullSafeArray(beanFactory.getBeanDefinitionNames(), String.class)).forEach(beanName ->
|
||||
Optional.of(beanFactory.getBeanDefinition(beanName))
|
||||
.filter(beanDefinition -> isTargetedRegionBean(beanName, beanDefinition, beanFactory))
|
||||
.ifPresent(beanDefinition -> beanDefinition.getPropertyValues()
|
||||
.addPropertyValue("offHeap", true)));
|
||||
}
|
||||
|
||||
boolean isTargetedRegionBean(String beanName, BeanDefinition bean,
|
||||
boolean isTargetedRegionBean(String beanName, BeanDefinition beanDefinition,
|
||||
ConfigurableListableBeanFactory beanFactory) {
|
||||
|
||||
return (isRegionBean(bean) && isNamedRegion(beanName, bean, beanFactory));
|
||||
return isNamedRegion(beanName, beanDefinition, beanFactory) && isRegionBean(beanDefinition, beanFactory);
|
||||
}
|
||||
|
||||
boolean isRegionBean(BeanDefinition bean) {
|
||||
return (bean != null && REGION_FACTORY_BEAN_TYPES.contains(bean.getBeanClassName()));
|
||||
boolean isRegionBean(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
|
||||
|
||||
return Optional.ofNullable(beanDefinition)
|
||||
.flatMap(it -> resolveBeanClass(it, beanFactory.getBeanClassLoader()))
|
||||
.filter(beanClass -> RegionLookupFactoryBean.class.isAssignableFrom(beanClass))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
boolean isNamedRegion(String beanName, BeanDefinition bean, ConfigurableListableBeanFactory beanFactory) {
|
||||
return (CollectionUtils.isEmpty(regionNames) || CollectionUtils.containsAny(regionNames,
|
||||
getBeanNames(beanName, bean, beanFactory)));
|
||||
boolean isNamedRegion(String beanName, BeanDefinition beanDefinition, BeanFactory beanFactory) {
|
||||
|
||||
return CollectionUtils.isEmpty(regionNames)
|
||||
|| CollectionUtils.containsAny(regionNames, getBeanNames(beanName, beanDefinition, beanFactory));
|
||||
}
|
||||
|
||||
Collection<String> getBeanNames(String beanName, BeanDefinition bean, BeanFactory beanFactory) {
|
||||
Collection<String> getBeanNames(String beanName, BeanDefinition beanDefinition, BeanFactory beanFactory) {
|
||||
|
||||
Collection<String> beanNames = new HashSet<>();
|
||||
|
||||
@@ -156,7 +149,7 @@ public class OffHeapConfiguration extends EmbeddedServiceConfigurationSupport {
|
||||
|
||||
Collections.addAll(beanNames, beanFactory.getAliases(beanName));
|
||||
|
||||
PropertyValue regionName = bean.getPropertyValues().getPropertyValue("regionName");
|
||||
PropertyValue regionName = beanDefinition.getPropertyValues().getPropertyValue("regionName");
|
||||
|
||||
if (regionName != null) {
|
||||
|
||||
|
||||
@@ -682,6 +682,16 @@ public abstract class AbstractAnnotationConfigSupport
|
||||
return String.format("%1$s%2$s", propertyName("cache.client."), propertyNameSuffix);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
protected String cacheCompressionProperty(String propertyNameSuffix) {
|
||||
return String.format("%1$s%2$s", propertyName("cache.compression."), propertyNameSuffix);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
protected String cacheOffHeapProperty(String propertyNameSuffix) {
|
||||
return String.format("%1$s%2$s", propertyName("cache.off-heap."), propertyNameSuffix);
|
||||
}
|
||||
|
||||
/* (non-Javadoc) */
|
||||
protected String cachePeerProperty(String propertyNameSuffix) {
|
||||
return String.format("%1$s%2$s", propertyName("cache.peer."), propertyNameSuffix);
|
||||
@@ -871,7 +881,7 @@ public abstract class AbstractAnnotationConfigSupport
|
||||
* @see org.springframework.util.ClassUtils#forName(String, ClassLoader)
|
||||
* @see #resolveBeanClassName(BeanDefinition)
|
||||
*/
|
||||
private Optional<Class<?>> resolveBeanClass(BeanDefinition beanDefinition, ClassLoader classLoader) {
|
||||
protected Optional<Class<?>> resolveBeanClass(BeanDefinition beanDefinition, ClassLoader classLoader) {
|
||||
|
||||
Class<?> beanClass = (beanDefinition instanceof AbstractBeanDefinition
|
||||
? safeResolveType(() -> ((AbstractBeanDefinition) beanDefinition).resolveBeanClass(classLoader)) : null);
|
||||
|
||||
@@ -77,6 +77,7 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
|
||||
private GemFireCache gemfireCache;
|
||||
|
||||
private Boolean close = false;
|
||||
private Boolean offHeap = false;
|
||||
|
||||
private Class<K> keyConstraint;
|
||||
private Class<V> valueConstraint;
|
||||
@@ -181,6 +182,7 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
|
||||
serverRegionFactory.setDiskStoreName(getDiskStoreName());
|
||||
serverRegionFactory.setKeyConstraint(getKeyConstraint());
|
||||
serverRegionFactory.setLookupEnabled(getLookupEnabled());
|
||||
serverRegionFactory.setOffHeap(getOffHeap());
|
||||
serverRegionFactory.setRegionConfigurers(this.regionConfigurers);
|
||||
serverRegionFactory.setRegionName(regionName);
|
||||
serverRegionFactory.setShortcut(getServerRegionShortcut());
|
||||
@@ -276,6 +278,14 @@ public class CacheTypeAwareRegionFactoryBean<K, V> extends RegionLookupFactoryBe
|
||||
return this.keyConstraint;
|
||||
}
|
||||
|
||||
public void setOffHeap(Boolean offHeap) {
|
||||
this.offHeap = offHeap;
|
||||
}
|
||||
|
||||
protected Boolean getOffHeap() {
|
||||
return this.offHeap;
|
||||
}
|
||||
|
||||
public void setPoolName(String poolName) {
|
||||
this.poolName = poolName;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user