[ 
https://issues.apache.org/jira/browse/KARAF-5604?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16359297#comment-16359297
 ] 

ASF GitHub Bot commented on KARAF-5604:
---------------------------------------

jbonofre closed pull request #446: [KARAF-5604] Speed up descriptor generation
URL: https://github.com/apache/karaf/pull/446
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
 
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
index f38c2be9be..b644540d93 100644
--- 
a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
+++ 
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java
@@ -471,7 +471,7 @@ private void writeFeatures(PrintStream out) throws 
ArtifactResolutionException,
         // TODO Initialise the repositories from the existing feature file if 
any
         Map<Dependency, Feature> otherFeatures = new HashMap<>();
         Map<Feature, String> featureRepositories = new HashMap<>();
-        FeaturesCache cache = new FeaturesCache(featuresCacheSize);
+        FeaturesCache cache = new FeaturesCache(featuresCacheSize, 
artifactCacheSize);
         for (final LocalDependency entry : localDependencies) {
             Object artifact = entry.getArtifact();
 
@@ -585,10 +585,10 @@ private void processFeatureArtifact(Features features, 
Feature feature, Map<Depe
                 throw new MojoExecutionException(
                         "Cannot locate file for feature: " + artifact + " at " 
+ featuresFile);
             }
-            Features includedFeatures = cache.get(featuresFile);
+            Features includedFeatures = cache.getFeature(featuresFile);
             for (String repository : includedFeatures.getRepository()) {
                 processFeatureArtifact(features, feature, otherFeatures, 
featureRepositories, cache,
-                        new 
DefaultArtifact(MavenUtil.mvnToAether(repository)), parent, false);
+                        cache.getArtifact(repository), parent, false);
             }
             for (Feature includedFeature : includedFeatures.getFeature()) {
                 Dependency dependency = new 
Dependency(includedFeature.getName(), includedFeature.getVersion());
@@ -944,20 +944,27 @@ protected String saveTreeListing() throws IOException {
     }
 
     private static final class FeaturesCache {
-        private final SimpleLRUCache<File, Features> cache;
+        // Maven-to-Aether Artifact cache, as parsing strings is expensive
+        private final SimpleLRUCache<String, DefaultArtifact> artifactCache;
+        private final SimpleLRUCache<File, Features> featuresCache;
 
-        FeaturesCache(int featuresCacheSize) {
-            cache = new SimpleLRUCache<>(featuresCacheSize);
+        FeaturesCache(int featuresCacheSize, int artifactCacheSize) {
+            featuresCache = new SimpleLRUCache<>(featuresCacheSize);
+            artifactCache = new SimpleLRUCache<>(artifactCacheSize);
         }
 
-        Features get(final File featuresFile) throws XMLStreamException, 
JAXBException, IOException {
-            final Features existing = cache.get(featuresFile);
+        DefaultArtifact getArtifact(String mavenName) {
+            return artifactCache.computeIfAbsent(mavenName, 
MavenUtil::mvnToArtifact);
+        }
+
+        Features getFeature(final File featuresFile) throws 
XMLStreamException, JAXBException, IOException {
+            final Features existing = featuresCache.get(featuresFile);
             if (existing != null) {
                 return existing;
             }
 
             final Features computed = readFeaturesFile(featuresFile);
-            cache.put(featuresFile, computed);
+            featuresCache.put(featuresFile, computed);
             return computed;
         }
     }
diff --git 
a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/Dependency31Helper.java
 
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/Dependency31Helper.java
index 3c139008ff..29c25d0732 100644
--- 
a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/Dependency31Helper.java
+++ 
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/Dependency31Helper.java
@@ -328,7 +328,9 @@ private ArtifactResult resolveArtifact(Artifact artifact) 
throws ArtifactResolut
 
     @Override
     public File resolve(Object artifact, Log log) {
-        log.debug("Resolving artifact " + artifact + " from " + 
projectRepositories);
+        if (log.isDebugEnabled()) {
+            log.debug("Resolving artifact " + artifact + " from " + 
projectRepositories);
+        }
 
         ArtifactResult result;
         try {
@@ -338,7 +340,10 @@ public File resolve(Object artifact, Log log) {
             return null;
         }
 
-        log.debug("Resolved artifact " + artifact + " to " + 
result.getArtifact().getFile() + " from " + result.getRepository());
+        if (log.isDebugEnabled()) {
+            log.debug("Resolved artifact " + artifact + " to " + 
result.getArtifact().getFile() + " from "
+                    + result.getRepository());
+        }
 
         return result.getArtifact().getFile();
     }
@@ -355,17 +360,22 @@ public File resolveById(String id, Log log) throws 
MojoFailureException {
         }
         id = MavenUtil.mvnToAether(id);
 
-        log.debug("Resolving artifact " + id + " from " + projectRepositories);
+        if (log.isDebugEnabled()) {
+            log.debug("Resolving artifact " + id + " from " + 
projectRepositories);
+        }
 
         ArtifactResult result;
         try {
-            result = resolveArtifact(new DefaultArtifact(id));
+            result = resolveArtifact(MavenUtil.aetherToArtifact(id));
         } catch (ArtifactResolutionException e) {
             log.warn("Could not resolve " + id, e);
             throw new MojoFailureException(format("Couldn't resolve artifact 
%s", id), e);
         }
 
-        log.debug("Resolved artifact " + id + " to " + 
result.getArtifact().getFile() + " from " + result.getRepository());
+        if (log.isDebugEnabled()) {
+            log.debug("Resolved artifact " + id + " to " + 
result.getArtifact().getFile() + " from "
+                    + result.getRepository());
+        }
 
         return result.getArtifact().getFile();
     }
@@ -411,8 +421,7 @@ private static Artifact 
toArtifact(org.apache.maven.artifact.Artifact artifact)
 
     @Override
     public org.apache.maven.artifact.Artifact mvnToArtifact(String name) 
throws MojoExecutionException {
-        name = MavenUtil.mvnToAether(name);
-        DefaultArtifact artifact = new DefaultArtifact(name);
+        DefaultArtifact artifact = MavenUtil.mvnToArtifact(name);
         org.apache.maven.artifact.Artifact mavenArtifact = 
toArtifact(artifact);
         return mavenArtifact;
     }
@@ -431,7 +440,7 @@ public String pathFromMaven(String name) throws 
MojoExecutionException {
 
     @Override
     public String pathFromAether(String name) throws MojoExecutionException {
-        DefaultArtifact artifact = new DefaultArtifact(name);
+        DefaultArtifact artifact = MavenUtil.aetherToArtifact(name);
         org.apache.maven.artifact.Artifact mavenArtifact = 
toArtifact(artifact);
         return MavenUtil.layout.pathOf(mavenArtifact);
     }
diff --git 
a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MavenUtil.java
 
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MavenUtil.java
index 13fcd80538..1803e866ed 100644
--- 
a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MavenUtil.java
+++ 
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MavenUtil.java
@@ -34,6 +34,7 @@
 import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
 import org.apache.maven.artifact.repository.metadata.Versioning;
 import 
org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
+import org.eclipse.aether.artifact.DefaultArtifact;
 import org.eclipse.aether.repository.RemoteRepository;
 
 /**
@@ -79,6 +80,58 @@ public static String mvnToAether(String name) {
         return b.toString();
     }
 
+    /**
+     * Convert PAX URL mvn format to an aether Artifact.
+     * N.B. we do not handle repository-url in mvn urls.
+     * N.B. version is required in mvn urls.
+     *
+     * @param name PAX URL mvn format: mvn-uri := [ 'wrap:' ] 'mvn:' [ 
repository-url '!' ] group-id '/' artifact-id [ '/' [version] [ '/' [type] [ 
'/' classifier ] ] ] ]
+     * @return aether Artifact
+     */
+    public static DefaultArtifact mvnToArtifact(String name) {
+        Matcher m = mvnPattern.matcher(name);
+        if (!m.matches()) {
+            return new DefaultArtifact(name);
+        }
+
+        String groupId = m.group(1);
+        String artifactId = m.group(2);
+        String version = m.group(3);
+        String extension = m.group(5);
+        if (!present(extension)) {
+            extension = "jar";
+        }
+        String classifier = m.group(7);
+
+        return new DefaultArtifact(groupId, artifactId, present(classifier) ? 
classifier : "", extension, version);
+    }
+
+    /**
+     * Convert Aether coordinate format to an aether Artifact.
+     * N.B. we do not handle repository-url in mvn urls.
+     * N.B. version is required in mvn urls.
+     *
+     * @param name aether coordinate format: 
&lt;groupId&gt;:&lt;artifactId&gt;[:&lt;extension&gt;[:&lt;classifier&gt;]]:&lt;version&gt;
+     * @return aether Artifact
+     */
+    public static DefaultArtifact aetherToArtifact(String name) {
+        Matcher m = aetherPattern.matcher(name);
+        if (!m.matches()) {
+            return new DefaultArtifact(name);
+        }
+
+        String groupId = m.group(1);
+        String artifactId = m.group(2);
+        String version = m.group(7);
+        String extension = m.group(4);
+        if (!present(extension)) {
+            extension = "jar";
+        }
+        String classifier = m.group(6);
+
+        return new DefaultArtifact(groupId, artifactId, present(classifier) ? 
classifier : "", extension, version);
+    }
+
     private static boolean present(String part) {
         return part != null && !part.isEmpty();
     }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> karaf:features-generate-descriptor takes long when faced with complex feature 
> dependencies
> ------------------------------------------------------------------------------------------
>
>                 Key: KARAF-5604
>                 URL: https://issues.apache.org/jira/browse/KARAF-5604
>             Project: Karaf
>          Issue Type: Improvement
>          Components: karaf-tooling
>    Affects Versions: 4.1.3
>            Reporter: Robert Varga
>            Assignee: Jean-Baptiste Onofré
>            Priority: Major
>             Fix For: 4.2.0, 4.1.5
>
>
> Opendaylight's distribution-check jobs generate features which have complex 
> feature dependencies, which exposes scaling issues in 
> karaf:features-generate-descriptor.
> [https://github.com/opendaylight/integration-distribution/tree/master/features/singles/odl-integration-all]
>  takes ~270 seconds to generate:
> real 4m28.834s
> user 3m40.287s
> sys 1m23.629s
> [https://github.com/opendaylight/integration-distribution/tree/master/features/repos/index]
>  takes ~638 seconds to generate:
> real 10m38.859s
> user 7m55.004s
> sys 3m17.269s
> Running profiling shows that this time is dominated by short-lived 
> FileInputStreams being generated at a rate of 7K-8Kps – which are coming from 
> both feature reading and from artifact resolution.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to