Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
1070326 - (JON3-42) RFE: Allow AS7 deployments to provide version in …
Browse files Browse the repository at this point in the history
…the artifact name

Adding one more thing to this feature, prevent discovery of siblings
resolving to the same resource key.  In the somewhat unlikely case that
two distinct sibling deployments resolve down to the same logical
deployment, don't let it get past discovery.  For example, if the user
has app-1.0.war and app-2.0.war and these are *really* different apps (and
they would probably have to be since EAP would stop deployment if they
had the same context).  In this case both would be seen as app.war, and
that is a problem on the RHQ side.  In this sutuation generate an
agent log warning that hopefully helps a user resolve the issue.

Note that resource upgrade already prevents an upgrade of siblings with
the same key, so this is an analogous change.
  • Loading branch information
jshaughn committed Jun 27, 2014
1 parent cef4088 commit dec8bae
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
Expand Up @@ -19,6 +19,10 @@

package org.rhq.modules.plugins.jbossas7;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -63,7 +67,7 @@ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext
// key and path.
Set<DiscoveredResourceDetails> details = super.discoverResources(context);

if (DISABLED) {
if (DISABLED || null == details || details.isEmpty()) {
return details;
}

Expand All @@ -73,6 +77,11 @@ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext
// Note that the version string is always set to the parent's version for these
// resources which are logically part of the umbrella deployment.

// Work with a list because we may update the key, which is used in the DiscoveredResourceDetails.equals()
ArrayList<DiscoveredResourceDetails> updatedDetails = new ArrayList<DiscoveredResourceDetails>(details);
HashMap<String, Integer> keyCount = new HashMap<String, Integer>(updatedDetails.size());
details.clear();

Configuration config = context.getDefaultPluginConfiguration();
String configPath = config.getSimpleValue("path", "");
boolean isType = (configPath == null || configPath.isEmpty() || configPath.contains("="));
Expand Down Expand Up @@ -124,8 +133,30 @@ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext
}
}
detail.setResourceKey(sb.toString());
Integer count = keyCount.get(detail.getResourceKey());
keyCount.put(detail.getResourceKey(), (null == count ? 1 : ++count));
}

// Now, make sure that after we've stripped the versions that we don't end up with multiple discoveries
// for the same key, this is an indication that there are multiple versions of the same Deployment deployed.
// In this case we remove the duplicates and issue a warning so the user can hopefully rectify the situation.
for (Map.Entry<String, Integer> entry : keyCount.entrySet()) {
if (entry.getValue() > 1) {
log.warn("Discovered multiple resources with resource key [" + entry.getKey()
+ "]. This is not allowed and they will be removed from discovery. This is typically caused by "
+ "having multiple versions of the same Deployment deployed. To solve the problem either remove "
+ "all but one version of the problem deployment or disable versioned deployment handling by "
+ "setting -Drhq.as7.VersionedSubsystemDiscovery.pattern=disable for the agent.");
for (Iterator<DiscoveredResourceDetails> i = updatedDetails.iterator(); i.hasNext();) {
DiscoveredResourceDetails detail = i.next();
if (detail.getResourceKey().equals(entry.getKey())) {
i.remove();
}
}
}
}

details.addAll(updatedDetails);
return details;
}

Expand Down
Expand Up @@ -19,6 +19,10 @@

package org.rhq.modules.plugins.jbossas7;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -74,7 +78,7 @@ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext
// key and path.
Set<DiscoveredResourceDetails> details = super.discoverResources(context);

if (DISABLED) {
if (DISABLED || null == details || details.isEmpty()) {
return details;
}

Expand All @@ -83,7 +87,12 @@ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext
// property, that value reflects the actual DMR used to query EAP.
// The stripped versions are then used to set the resource version string.

for (DiscoveredResourceDetails detail : details) {
// Work with a list because we may update the key, which is used in the DiscoveredResourceDetails.equals()
ArrayList<DiscoveredResourceDetails> updatedDetails = new ArrayList<DiscoveredResourceDetails>(details);
HashMap<String, Integer> keyCount = new HashMap<String, Integer>(updatedDetails.size());
details.clear();

for (DiscoveredResourceDetails detail : updatedDetails) {
MATCHER.reset(detail.getResourceName());
if (MATCHER.matches()) {
// reset the resource name with the stripped value
Expand Down Expand Up @@ -115,13 +124,36 @@ public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext
}
}
detail.setResourceKey(sb.toString());
Integer count = keyCount.get(detail.getResourceKey());
keyCount.put(detail.getResourceKey(), (null == count ? 1 : ++count));
}

// Now, make sure that after we've stripped the versions that we don't end up with multiple discoveries
// for the same key, this is an indication that there are multiple versions of the same Deployment deployed.
// In this case we remove the duplicates and issue a warning so the user can hopefully rectify the situation.
for (Map.Entry<String, Integer> entry : keyCount.entrySet()) {
if (entry.getValue() > 1) {
log.warn("Discovered multiple resources with resource key [" + entry.getKey()
+ "]. This is not allowed and they will be removed from discovery. This is typically caused by "
+ "having multiple versions of the same Deployment deployed. To solve the problem either remove "
+ "all but one version of the problem deployment or disable versioned deployment handling by "
+ "setting -Drhq.as7.VersionedSubsystemDiscovery.pattern=disable for the agent.");
for (Iterator<DiscoveredResourceDetails> i = updatedDetails.iterator(); i.hasNext();) {
DiscoveredResourceDetails detail = i.next();
if (detail.getResourceKey().equals(entry.getKey())) {
i.remove();
}
}
}
}

details.addAll(updatedDetails);
return details;
}

// The Matching logic here is the same as above, but instead of setting the discovery details we
// set new values in the upgrade report for name, version and key.
// set new values in the upgrade report for name, version and key. Note that if multiple resources
// upgrade to the same resource key it will be caught and fail downstream.
@Override
public ResourceUpgradeReport upgrade(ResourceUpgradeContext inventoriedResource) {
ResourceUpgradeReport result = null;
Expand Down

0 comments on commit dec8bae

Please sign in to comment.