Skip to content

Commit

Permalink
Create explicit RuntimeResourceRegistration interface to support expl…
Browse files Browse the repository at this point in the history
…icit unregistration of runtime resources/definitions.
  • Loading branch information
pferraro committed Jun 12, 2017
1 parent a7f54a5 commit 9801723
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 48 deletions.
Expand Up @@ -110,7 +110,7 @@ public void execute(OperationContext context, ModelNode operation) throws Operat
super.execute(context, operation); super.execute(context, operation);


if (this.requiresRuntime(context)) { if (this.requiresRuntime(context)) {
this.descriptor.getRuntimeResourceRegistrations().forEach(registration -> context.addStep(registration, OperationContext.Stage.MODEL)); this.descriptor.getRuntimeResourceRegistrations().forEach(r -> context.addStep((c, op) -> r.register(c), OperationContext.Stage.MODEL));
} }
} }


Expand Down
Expand Up @@ -62,13 +62,6 @@ public interface AddStepHandlerDescriptor extends WriteAttributeStepHandlerDescr
*/ */
Map<AttributeDefinition, AttributeTranslation> getAttributeTranslations(); Map<AttributeDefinition, AttributeTranslation> getAttributeTranslations();


/**
* Returns a collection of handlers that register runtime resources
* Runtime resource registrations are executed in a separate MODEL stage step.
* @return a collection of operation step handlers
*/
Collection<OperationStepHandler> getRuntimeResourceRegistrations();

/** /**
* Returns a transformer for the add operation handler. * Returns a transformer for the add operation handler.
* This is typically used to adapt legacy operations to conform to the current version of the model. * This is typically used to adapt legacy operations to conform to the current version of the model.
Expand Down
Expand Up @@ -30,7 +30,6 @@
import org.jboss.as.controller.OperationContext; import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException; import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.PathAddress; import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.SimpleOperationDefinitionBuilder; import org.jboss.as.controller.SimpleOperationDefinitionBuilder;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants; import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.controller.registry.AttributeAccess; import org.jboss.as.controller.registry.AttributeAccess;
Expand Down Expand Up @@ -96,32 +95,12 @@ protected void performRemove(OperationContext context, ModelNode operation, Mode


this.descriptor.getResourceCapabilityReferences().forEach((reference, resolver) -> reference.removeCapabilityRequirements(context, resource, null, resolver.apply(address))); this.descriptor.getResourceCapabilityReferences().forEach((reference, resolver) -> reference.removeCapabilityRequirements(context, resource, null, resolver.apply(address)));


// Remove any runtime child resources if (this.requiresRuntime(context)) {
removeRuntimeChildren(context, PathAddress.EMPTY_ADDRESS); this.descriptor.getRuntimeResourceRegistrations().forEach(r -> r.unregister(context));
}

super.performRemove(context, operation, model);

if (remove) {
PathAddress address = context.getResourceRegistration().getPathAddress();
PathElement path = address.getLastElement();
// If override model was registered, unregister it
if (!path.isWildcard() && (context.getResourceRegistration().getParent().getSubModel(PathAddress.pathAddress(path.getKey(), PathElement.WILDCARD_VALUE)) != null)) {
context.getResourceRegistrationForUpdate().unregisterOverrideModel(context.getCurrentAddressValue());
} }
} }
}


private static void removeRuntimeChildren(OperationContext context, PathAddress address) { super.performRemove(context, operation, model);
Resource resource = context.readResource(address);
for (String type : resource.getChildTypes()) {
for (Resource.ResourceEntry entry : resource.getChildren(type)) {
if (entry.isRuntime()) {
removeRuntimeChildren(context, address.append(entry.getPathElement()));
context.removeResource(address);
}
}
}
} }


@Override @Override
Expand Down
Expand Up @@ -21,6 +21,7 @@
*/ */
package org.jboss.as.clustering.controller; package org.jboss.as.clustering.controller;


import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
Expand All @@ -41,6 +42,15 @@ public interface RemoveStepHandlerDescriptor extends OperationStepHandlerDescrip
*/ */
ResourceDescriptionResolver getDescriptionResolver(); ResourceDescriptionResolver getDescriptionResolver();


/**
* Returns a collection of handlers that register runtime resources
* Runtime resource registrations are executed in a separate MODEL stage step.
* @return a collection of operation step handlers
*/
default Collection<RuntimeResourceRegistration> getRuntimeResourceRegistrations() {
return Collections.emptyList();
}

/** /**
* Returns a mapping of capability references to an ancestor resource. * Returns a mapping of capability references to an ancestor resource.
* @return a tuple of capability references and requirement resolvers. * @return a tuple of capability references and requirement resolvers.
Expand Down
Expand Up @@ -71,7 +71,7 @@ public class ResourceDescriptor implements AddStepHandlerDescriptor {
private final Set<PathElement> requiredChildren = new TreeSet<>(PATH_COMPARATOR); private final Set<PathElement> requiredChildren = new TreeSet<>(PATH_COMPARATOR);
private final Set<PathElement> requiredSingletonChildren = new TreeSet<>(PATH_COMPARATOR); private final Set<PathElement> requiredSingletonChildren = new TreeSet<>(PATH_COMPARATOR);
private final Map<AttributeDefinition, AttributeTranslation> attributeTranslations = new TreeMap<>(ATTRIBUTE_COMPARATOR); private final Map<AttributeDefinition, AttributeTranslation> attributeTranslations = new TreeMap<>(ATTRIBUTE_COMPARATOR);
private final List<OperationStepHandler> runtimeResourceRegistrations = new LinkedList<>(); private final List<RuntimeResourceRegistration> runtimeResourceRegistrations = new LinkedList<>();
private final Map<CapabilityReferenceRecorder, Function<PathAddress, String>> resourceCapabilityReferences = new HashMap<>(); private final Map<CapabilityReferenceRecorder, Function<PathAddress, String>> resourceCapabilityReferences = new HashMap<>();
private volatile UnaryOperator<OperationStepHandler> addOperationTransformer = UnaryOperator.identity(); private volatile UnaryOperator<OperationStepHandler> addOperationTransformer = UnaryOperator.identity();


Expand Down Expand Up @@ -216,11 +216,11 @@ public AttributeValueTranslator getWriteTranslator() {
} }


@Override @Override
public Collection<OperationStepHandler> getRuntimeResourceRegistrations() { public Collection<RuntimeResourceRegistration> getRuntimeResourceRegistrations() {
return this.runtimeResourceRegistrations; return this.runtimeResourceRegistrations;
} }


public ResourceDescriptor addRuntimeResourceRegistration(OperationStepHandler registration) { public ResourceDescriptor addRuntimeResourceRegistration(RuntimeResourceRegistration registration) {
this.runtimeResourceRegistrations.add(registration); this.runtimeResourceRegistrations.add(registration);
return this; return this;
} }
Expand Down
@@ -0,0 +1,44 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.clustering.controller;

import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;

/**
* Encapsulates logic for runtime resource registration.
* @author Paul Ferraro
*/
public interface RuntimeResourceRegistration {
/**
* Registers runtime resources as part of an add operation.
* @param context an operation context
*/
void register(OperationContext context) throws OperationFailedException;

/**
* Removes runtime resources created during {@link #register(OperationContext)}.
* @param context an operation context
*/
void unregister(OperationContext context);
}
Expand Up @@ -237,7 +237,7 @@ public void register(ManagementResourceRegistration parentRegistration) {
} }
handler.execute(context, operation); handler.execute(context, operation);
}) })
.addRuntimeResourceRegistration(new ProtocolResourceRegistrationHandler()) .addRuntimeResourceRegistration(new ChannelRuntimeResourceRegistration())
; ;
ResourceServiceHandler handler = new ChannelServiceHandler(); ResourceServiceHandler handler = new ChannelServiceHandler();
new SimpleResourceRegistration(descriptor, handler).register(registration); new SimpleResourceRegistration(descriptor, handler).register(registration);
Expand Down
Expand Up @@ -29,13 +29,13 @@
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;


import org.jboss.as.clustering.controller.RuntimeResourceRegistration;
import org.jboss.as.clustering.controller.descriptions.SimpleResourceDescriptionResolver; import org.jboss.as.clustering.controller.descriptions.SimpleResourceDescriptionResolver;
import org.jboss.as.clustering.jgroups.logging.JGroupsLogger; import org.jboss.as.clustering.jgroups.logging.JGroupsLogger;
import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.Attribute; import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.Attribute;
import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.FieldType; import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.FieldType;
import org.jboss.as.controller.OperationContext; import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException; import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.PathAddress; import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.ResourceBuilder; import org.jboss.as.controller.ResourceBuilder;
import org.jboss.as.controller.ResourceDefinition; import org.jboss.as.controller.ResourceDefinition;
Expand Down Expand Up @@ -65,7 +65,7 @@
/** /**
* @author Paul Ferraro * @author Paul Ferraro
*/ */
public class ProtocolResourceRegistrationHandler implements OperationStepHandler, ProtocolMetricsHandler.ProtocolLocator { public class ChannelRuntimeResourceRegistration implements RuntimeResourceRegistration, ProtocolMetricsHandler.ProtocolLocator {


@Override @Override
public Protocol findProtocol(OperationContext context) throws ClassNotFoundException, ModuleLoadException { public Protocol findProtocol(OperationContext context) throws ClassNotFoundException, ModuleLoadException {
Expand Down Expand Up @@ -100,8 +100,7 @@ public Protocol findProtocol(OperationContext context) throws ClassNotFoundExcep
} }


@Override @Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException { public void register(OperationContext context) throws OperationFailedException {

OverrideDescriptionProvider provider = new OverrideDescriptionProvider() { OverrideDescriptionProvider provider = new OverrideDescriptionProvider() {
@Override @Override
public Map<String, ModelNode> getAttributeOverrideDescriptions(Locale locale) { public Map<String, ModelNode> getAttributeOverrideDescriptions(Locale locale) {
Expand Down Expand Up @@ -148,6 +147,12 @@ public Map<String, ModelNode> getChildTypeOverrideDescriptions(Locale locale) {
} }
} }


@Override
public void unregister(OperationContext context) {
context.readResource(PathAddress.EMPTY_ADDRESS).getChildrenNames(ProtocolResourceDefinition.WILDCARD_PATH.getKey()).forEach(name -> context.removeResource(PathAddress.pathAddress(ProtocolResourceDefinition.pathElement(name))));
context.getResourceRegistrationForUpdate().unregisterOverrideModel(context.getCurrentAddressValue());
}

private ResourceDefinition createProtocolResourceDefinition(String protocolName, Class<? extends Protocol> protocolClass) { private ResourceDefinition createProtocolResourceDefinition(String protocolName, Class<? extends Protocol> protocolClass) {


SimpleResourceDescriptionResolver resolver = new SimpleResourceDescriptionResolver(protocolName, protocolClass.getSimpleName()); SimpleResourceDescriptionResolver resolver = new SimpleResourceDescriptionResolver(protocolName, protocolClass.getSimpleName());
Expand Down
Expand Up @@ -27,16 +27,17 @@
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;


import org.jboss.as.clustering.controller.RuntimeResourceRegistration;
import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.Attribute; import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.Attribute;
import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.FieldType; import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.FieldType;
import org.jboss.as.controller.OperationContext; import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException; import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.PathAddress; import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder; import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants; import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.controller.descriptions.OverrideDescriptionProvider; import org.jboss.as.controller.descriptions.OverrideDescriptionProvider;
import org.jboss.as.controller.registry.ManagementResourceRegistration; import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.registry.Resource;
import org.jboss.dmr.ModelNode; import org.jboss.dmr.ModelNode;
import org.jboss.modules.ModuleLoadException; import org.jboss.modules.ModuleLoadException;
import org.jboss.msc.service.ServiceController; import org.jboss.msc.service.ServiceController;
Expand All @@ -54,7 +55,7 @@
* Operation handler for registration of fork protocol runtime resources. * Operation handler for registration of fork protocol runtime resources.
* @author Paul Ferraro * @author Paul Ferraro
*/ */
public class ForkProtocolResourceRegistrationHandler implements OperationStepHandler, ProtocolMetricsHandler.ProtocolLocator { public class ForkProtocolRuntimeResourceRegistration implements RuntimeResourceRegistration, ProtocolMetricsHandler.ProtocolLocator {


@Override @Override
public Protocol findProtocol(OperationContext context) throws ClassNotFoundException, ModuleLoadException { public Protocol findProtocol(OperationContext context) throws ClassNotFoundException, ModuleLoadException {
Expand Down Expand Up @@ -95,12 +96,13 @@ public Protocol findProtocol(OperationContext context) throws ClassNotFoundExcep
} }


@Override @Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException { public void register(OperationContext context) throws OperationFailedException {


Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS);
ManagementResourceRegistration registration = context.getResourceRegistrationForUpdate(); ManagementResourceRegistration registration = context.getResourceRegistrationForUpdate();
String protocolName = context.getCurrentAddressValue(); String protocolName = context.getCurrentAddressValue();
String moduleName = ProtocolResourceDefinition.Attribute.MODULE.resolveModelAttribute(context, operation).asString(); String moduleName = ProtocolResourceDefinition.Attribute.MODULE.resolveModelAttribute(context, resource.getModel()).asString();
Class<? extends Protocol> protocolClass = ProtocolResourceRegistrationHandler.findProtocolClass(context, protocolName, moduleName); Class<? extends Protocol> protocolClass = ChannelRuntimeResourceRegistration.findProtocolClass(context, protocolName, moduleName);


final Map<String, Attribute> attributes = ProtocolMetricsHandler.findProtocolAttributes(protocolClass); final Map<String, Attribute> attributes = ProtocolMetricsHandler.findProtocolAttributes(protocolClass);


Expand Down Expand Up @@ -131,4 +133,9 @@ public Map<String, ModelNode> getChildTypeOverrideDescriptions(Locale locale) {
protocolRegistration.registerMetric(new SimpleAttributeDefinitionBuilder(name, type.getModelType()).setStorageRuntime().build(), handler); protocolRegistration.registerMetric(new SimpleAttributeDefinitionBuilder(name, type.getModelType()).setStorageRuntime().build(), handler);
} }
} }

@Override
public void unregister(OperationContext context) {
context.getResourceRegistrationForUpdate().unregisterOverrideModel(context.getCurrentAddressValue());
}
} }
Expand Up @@ -92,6 +92,6 @@ public void register(ManagementResourceRegistration parentRegistration) {
ResourceServiceHandler handler = new ForkServiceHandler(builderFactory); ResourceServiceHandler handler = new ForkServiceHandler(builderFactory);
new SimpleResourceRegistration(descriptor, handler).register(registration); new SimpleResourceRegistration(descriptor, handler).register(registration);


new ProtocolRegistration(builderFactory, new ForkProtocolResourceRegistrationHandler()).register(registration); new ProtocolRegistration(builderFactory, new ForkProtocolRuntimeResourceRegistration()).register(registration);
} }
} }
Expand Up @@ -32,9 +32,9 @@
import org.jboss.as.clustering.controller.Registration; import org.jboss.as.clustering.controller.Registration;
import org.jboss.as.clustering.controller.ResourceDescriptor; import org.jboss.as.clustering.controller.ResourceDescriptor;
import org.jboss.as.clustering.controller.ResourceServiceBuilderFactory; import org.jboss.as.clustering.controller.ResourceServiceBuilderFactory;
import org.jboss.as.clustering.controller.RuntimeResourceRegistration;
import org.jboss.as.clustering.function.Consumers; import org.jboss.as.clustering.function.Consumers;
import org.jboss.as.controller.ModelVersion; import org.jboss.as.controller.ModelVersion;
import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.registry.ManagementResourceRegistration; import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder; import org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder;
import org.wildfly.clustering.jgroups.spi.ChannelFactory; import org.wildfly.clustering.jgroups.spi.ChannelFactory;
Expand Down Expand Up @@ -127,7 +127,7 @@ static void buildTransformation(ModelVersion version, ResourceTransformationDesc
this.descriptorConfigurator = Consumers.empty(); this.descriptorConfigurator = Consumers.empty();
} }


ProtocolRegistration(ResourceServiceBuilderFactory<ChannelFactory> parentBuilderFactory, OperationStepHandler runtimeResourceRegistration) { ProtocolRegistration(ResourceServiceBuilderFactory<ChannelFactory> parentBuilderFactory, RuntimeResourceRegistration runtimeResourceRegistration) {
this.parentBuilderFactory = parentBuilderFactory; this.parentBuilderFactory = parentBuilderFactory;
this.descriptorConfigurator = descriptor -> descriptor.addRuntimeResourceRegistration(runtimeResourceRegistration); this.descriptorConfigurator = descriptor -> descriptor.addRuntimeResourceRegistration(runtimeResourceRegistration);
} }
Expand Down

0 comments on commit 9801723

Please sign in to comment.