SGF-499 - Prevent SDG-defined Pools from being destroyed before the Regions that use them

This commit is contained in:
John Blum
2016-06-02 17:12:02 -07:00
parent 27cf4a8729
commit 1bca496309
12 changed files with 412 additions and 59 deletions

View File

@@ -522,19 +522,22 @@ public abstract class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K,
@Override
public void destroy() throws Exception {
if (getRegion() != null) {
Region<K, V> region = getObject();
if (region != null) {
if (close) {
if (!getRegion().getRegionService().isClosed()) {
if (!region.getRegionService().isClosed()) {
try {
getRegion().close();
region.close();
}
catch (Exception ignore) {
}
}
}
if (destroy) {
getRegion().destroyRegion();
region.destroyRegion();
}
}
}
@@ -567,7 +570,8 @@ public abstract class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K,
* @see com.gemstone.gemfire.cache.RegionAttributes
*/
public RegionAttributes getAttributes() {
return (getRegion() != null ? getRegion().getAttributes() : attributes);
Region<K, V> region = getRegion();
return (region != null ? region.getAttributes() : attributes);
}
/**

View File

@@ -31,7 +31,6 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.gemstone.gemfire.cache.CacheClosedException;
import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.CacheLoader;
import com.gemstone.gemfire.cache.CacheWriter;
@@ -339,13 +338,12 @@ public class ClientRegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V>
try {
region.close();
}
catch (CacheClosedException ignore) {
catch (Exception ignore) {
}
}
}
// TODO perhaps 'destroy' should take precedence over 'close' since 'destroy' is a functional superset
// of 'close'
else if (destroy) {
if (destroy) {
region.destroyRegion();
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2012 the original author or authors.
*
* 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.springframework.data.gemfire.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.client.PoolFactoryBean;
/**
* The ClientRegionAndPoolBeanFactoryPostProcessor class is a Spring {@link BeanFactoryPostProcessor} that ensures
* a proper dependency is declared between a GemFire client Region and the GemFire Pool it references and uses, if
* the GemFire Pool has been defined and configured in Spring (Data GemFire) configuration meta-data (XML).
*
* @author John Blum
* @see org.springframework.beans.factory.config.BeanFactoryPostProcessor
* @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory
* @since 1.8.2
*/
public class ClientRegionAndPoolBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
protected static final String POOL_NAME_PROPERTY = "poolName";
/* (non-Javadoc)*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
Set<String> clientRegionBeanNames = new HashSet<String>();
Set<String> poolBeanNames = new HashSet<String>();
for (String beanName : beanFactory.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
if (isClientRegionBean(beanDefinition)) {
clientRegionBeanNames.add(beanName);
}
else if (isPoolBean(beanDefinition)) {
poolBeanNames.add(beanName);
}
}
for (String clientRegionBeanName : clientRegionBeanNames) {
BeanDefinition clientRegionBean = beanFactory.getBeanDefinition(clientRegionBeanName);
String poolName = getPoolName(clientRegionBean);
if (poolBeanNames.contains(poolName)) {
addDependsOn(clientRegionBean, poolName);
}
}
}
/* (non-Javadoc)*/
boolean isClientRegionBean(BeanDefinition beanDefinition) {
return ClientRegionFactoryBean.class.getName().equals(beanDefinition.getBeanClassName());
}
/* (non-Javadoc)*/
boolean isPoolBean(BeanDefinition beanDefinition) {
return PoolFactoryBean.class.getName().equals(beanDefinition.getBeanClassName());
}
/* (non-Javadoc) */
String getPoolName(BeanDefinition clientRegionBean) {
PropertyValue poolNameProperty = clientRegionBean.getPropertyValues().getPropertyValue(POOL_NAME_PROPERTY);
return (poolNameProperty != null ? String.valueOf(poolNameProperty.getValue()) : null);
}
/* (non-Javadoc) */
BeanDefinition addDependsOn(BeanDefinition bean, String beanName) {
String[] dependsOn = bean.getDependsOn();
List<String> dependsOnList = new ArrayList<String>();
if (dependsOn != null) {
Collections.addAll(dependsOnList, dependsOn);
}
dependsOnList.add(beanName);
bean.setDependsOn(dependsOnList.toArray(new String[dependsOnList.size()]));
return bean;
}
}

View File

@@ -36,10 +36,10 @@ class GemfireDataNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
// Repository namespace
RepositoryConfigurationExtension extension = new GemfireRepositoryConfigurationExtension();
registerBeanDefinitionParser("repositories", new RepositoryBeanDefinitionParser(extension));
registerBeanDefinitionParser("function-executions", new FunctionExecutionBeanDefinitionParser());
registerBeanDefinitionParser("datasource", new GemfireDataSourceParser());
registerBeanDefinitionParser("function-executions", new FunctionExecutionBeanDefinitionParser());
registerBeanDefinitionParser("json-region-autoproxy", new GemfireRegionAutoProxyParser());
registerBeanDefinitionParser("repositories", new RepositoryBeanDefinitionParser(extension));
registerBeanDefinitionParser("snapshot-service", new SnapshotServiceParser());
}
}

View File

@@ -19,6 +19,7 @@ package org.springframework.data.gemfire.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import org.springframework.beans.factory.BeanDefinitionStoreException;
@@ -49,6 +50,8 @@ import org.w3c.dom.Element;
*/
class PoolParser extends AbstractSingleBeanDefinitionParser {
static final AtomicBoolean INFRASTRUCTURE_REGISTRATION = new AtomicBoolean(false);
protected static final int DEFAULT_LOCATOR_PORT = GemfireUtils.DEFAULT_LOCATOR_PORT;
protected static final int DEFAULT_SERVER_PORT = GemfireUtils.DEFAULT_CACHE_SERVER_PORT;
@@ -62,6 +65,18 @@ class PoolParser extends AbstractSingleBeanDefinitionParser {
protected static final String SERVER_ELEMENT_NAME = "server";
protected static final String SERVERS_ATTRIBUTE_NAME = "servers";
/* (non-Javadoc) */
static void registerSupportingInfrastructureComponents(ParserContext parserContext) {
if (INFRASTRUCTURE_REGISTRATION.compareAndSet(false, true)) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder
.genericBeanDefinition(ClientRegionAndPoolBeanFactoryPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
.getBeanDefinition();
BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, parserContext.getRegistry());
}
}
@Override
protected Class<?> getBeanClass(Element element) {
return PoolFactoryBean.class;
@@ -69,6 +84,9 @@ class PoolParser extends AbstractSingleBeanDefinitionParser {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
registerSupportingInfrastructureComponents(parserContext);
ParsingUtils.setPropertyValue(element, builder, "free-connection-timeout");
ParsingUtils.setPropertyValue(element, builder, "idle-timeout");
ParsingUtils.setPropertyValue(element, builder, "keep-alive");

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository.config;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -28,18 +29,20 @@ import org.springframework.data.repository.config.XmlRepositoryConfigurationSour
import org.springframework.util.StringUtils;
/**
* {@link RepositoryConfigurationExtension} implementation to add Gemfire specific extensions to the repository XML
* namespace and annotation based configuration.
* {@link RepositoryConfigurationExtension} implementation handling GemFire specific extensions to the Repository XML
* namespace and annotation-based configuration meta-data.
*
* @author Oliver Gierke
* @author John Blum
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension
*/
public class GemfireRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
private static final String ANNOTATION_MAPPING_CONTEXT_REF = "mappingContextRef";
private static final String GEMFIRE_MODULE_PREFIX = "gemfire";
private static final String MAPPING_CONTEXT_PROPERTY_NAME = "gemfireMappingContext";
private static final String XML_MAPPING_CONTEXT_REF = "mapping-context-ref";
private static final String MAPPING_CONTEXT_REF_ANNOTATION_ATTRIBUTE = "mappingContextRef";
private static final String MAPPING_CONTEXT_REF_XML_ATTRIBUTE = "mapping-context-ref";
static final String DEFAULT_MAPPING_CONTEXT_BEAN_NAME = String.format("%1$s.%2$s",
GemfireMappingContext.class.getName(), "DEFAULT");
@@ -58,7 +61,7 @@ public class GemfireRepositoryConfigurationExtension extends RepositoryConfigura
*/
@Override
protected String getModulePrefix() {
return "gemfire";
return GEMFIRE_MODULE_PREFIX;
}
/*
@@ -69,12 +72,10 @@ public class GemfireRepositoryConfigurationExtension extends RepositoryConfigura
public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource configurationSource) {
super.registerBeansForRoot(registry, configurationSource);
String attribute = configurationSource.getAttribute(ANNOTATION_MAPPING_CONTEXT_REF);
if (!StringUtils.hasText(attribute)) {
if (!StringUtils.hasText(configurationSource.getAttribute(MAPPING_CONTEXT_REF_ANNOTATION_ATTRIBUTE))) {
registry.registerBeanDefinition(DEFAULT_MAPPING_CONTEXT_BEAN_NAME,
new RootBeanDefinition(GemfireMappingContext.class));
new RootBeanDefinition(GemfireMappingContext.class));
}
}
@@ -83,10 +84,9 @@ public class GemfireRepositoryConfigurationExtension extends RepositoryConfigura
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource)
*/
@Override
public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {
String mappingContextRef = config.getAttribute(ANNOTATION_MAPPING_CONTEXT_REF);
builder.addPropertyReference(MAPPING_CONTEXT_PROPERTY_NAME, getDefaultedMappingContextBeanName(mappingContextRef));
public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource configurationSource) {
builder.addPropertyReference(MAPPING_CONTEXT_PROPERTY_NAME, resolveMappingContextBeanName(
configurationSource.getAttribute(MAPPING_CONTEXT_REF_ANNOTATION_ATTRIBUTE)));
}
/*
@@ -94,13 +94,13 @@ public class GemfireRepositoryConfigurationExtension extends RepositoryConfigura
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.XmlRepositoryConfigurationSource)
*/
@Override
public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource config) {
String mappingContextRef = config.getElement().getAttribute(XML_MAPPING_CONTEXT_REF);
builder.addPropertyReference(MAPPING_CONTEXT_PROPERTY_NAME, getDefaultedMappingContextBeanName(mappingContextRef));
public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource configurationSource) {
builder.addPropertyReference(MAPPING_CONTEXT_PROPERTY_NAME, resolveMappingContextBeanName(
configurationSource.getElement().getAttribute(MAPPING_CONTEXT_REF_XML_ATTRIBUTE)));
}
private static String getDefaultedMappingContextBeanName(String source) {
return StringUtils.hasText(source) ? source : DEFAULT_MAPPING_CONTEXT_BEAN_NAME;
/* (non-Javadoc) */
private static String resolveMappingContextBeanName(String source) {
return (StringUtils.hasText(source) ? source : DEFAULT_MAPPING_CONTEXT_BEAN_NAME);
}
}

View File

@@ -56,7 +56,7 @@ public class GemfireRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ext
private Iterable<Region<?, ?>> regions;
private MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> context;
private MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> mappingContext;
/**
* Sets a reference to the Spring {@link ApplicationContext} in which this object runs.
@@ -81,11 +81,12 @@ public class GemfireRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ext
*/
public void setGemfireMappingContext(MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> mappingContext) {
setMappingContext(mappingContext);
this.context = mappingContext;
this.mappingContext = mappingContext;
}
/**
* Gets a reference to the {@link MappingContext} used to perform domain object type to store mappings.
* Returns a reference to the Spring Data {@link MappingContext} used to perform domain object type
* to data store mappings.
*
* @return a reference to the {@link MappingContext}.
* @see org.springframework.data.gemfire.mapping.GemfireMappingContext
@@ -93,11 +94,17 @@ public class GemfireRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ext
* @see #setGemfireMappingContext(MappingContext)
*/
protected MappingContext<? extends GemfirePersistentEntity<?>, GemfirePersistentProperty> getGemfireMappingContext() {
return this.context;
return this.mappingContext;
}
Iterable<Region<?, ?>> getRegions() {
return regions;
/**
* Returns an {@link Iterable} reference to the GemFire {@link Region}s defined
* in the Spring {@link ApplicationContext}.
*
* @return a reference to all GemFire {@link Region}s defined in the Spring {@link ApplicationContext}.
*/
protected Iterable<Region<?, ?>> getRegions() {
return this.regions;
}
/**
@@ -108,7 +115,7 @@ public class GemfireRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ext
*/
@Override
protected RepositoryFactorySupport createRepositoryFactory() {
return new GemfireRepositoryFactory(regions, context);
return new GemfireRepositoryFactory(getRegions(), getGemfireMappingContext());
}
/*
@@ -117,8 +124,7 @@ public class GemfireRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ext
*/
@Override
public void afterPropertiesSet() {
Assert.state(context != null, "GemfireMappingContext must not be null!");
Assert.state(getGemfireMappingContext() != null, "GemfireMappingContext must not be null");
super.afterPropertiesSet();
}
}