|
12 | 12 | import io.github.chains_project.maven_lockfile.data.VersionNumber; |
13 | 13 | import io.github.chains_project.maven_lockfile.reporting.PluginLogManager; |
14 | 14 | import java.util.*; |
15 | | -import java.util.concurrent.ExecutorService; |
16 | | -import java.util.concurrent.Executors; |
17 | | -import java.util.concurrent.Future; |
18 | 15 | import java.util.stream.Collectors; |
| 16 | +import org.apache.maven.artifact.Artifact; |
19 | 17 | import org.apache.maven.shared.dependency.graph.internal.SpyingDependencyNodeUtils; |
20 | 18 |
|
21 | 19 | public class DependencyGraph { |
@@ -67,43 +65,20 @@ public static DependencyGraph of( |
67 | 65 | .filter(it -> graph.predecessors(it).isEmpty()) |
68 | 66 | .collect(Collectors.toList()); |
69 | 67 |
|
70 | | - // Pre-warm checksum cache in parallel for I/O-bound remote fetching |
71 | | - var nonRootNodes = graph.nodes().stream() |
72 | | - .filter(it -> !graph.predecessors(it).isEmpty()) |
73 | | - .collect(Collectors.toList()); |
| 68 | + // Collect unique non-root artifacts and let the calculator pre-warm its cache |
74 | 69 | Set<String> seen = new HashSet<>(); |
75 | | - List<org.apache.maven.shared.dependency.graph.DependencyNode> uniqueNodes = new ArrayList<>(); |
76 | | - for (var node : nonRootNodes) { |
77 | | - var a = node.getArtifact(); |
78 | | - String key = a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion() + ":" |
79 | | - + (a.getClassifier() != null ? a.getClassifier() : "") + ":" + a.getType(); |
80 | | - if (seen.add(key)) { |
81 | | - uniqueNodes.add(node); |
82 | | - } |
83 | | - } |
84 | | - if (!uniqueNodes.isEmpty()) { |
85 | | - int poolSize = Math.min(16, Math.max(4, Runtime.getRuntime().availableProcessors() * 2)); |
86 | | - PluginLogManager.getLog() |
87 | | - .info(String.format( |
88 | | - "Pre-warming checksum cache for %d unique artifacts with %d threads", |
89 | | - uniqueNodes.size(), poolSize)); |
90 | | - ExecutorService executor = Executors.newFixedThreadPool(poolSize); |
91 | | - List<Future<?>> futures = new ArrayList<>(); |
92 | | - for (var node : uniqueNodes) { |
93 | | - futures.add(executor.submit(() -> { |
94 | | - calc.calculateArtifactChecksum(node.getArtifact()); |
95 | | - calc.getArtifactResolvedField(node.getArtifact()); |
96 | | - })); |
97 | | - } |
98 | | - for (var future : futures) { |
99 | | - try { |
100 | | - future.get(); |
101 | | - } catch (Exception e) { |
102 | | - PluginLogManager.getLog().debug("Pre-warm task failed: " + e.getMessage()); |
| 70 | + List<Artifact> uniqueArtifacts = new ArrayList<>(); |
| 71 | + for (var node : graph.nodes()) { |
| 72 | + if (!graph.predecessors(node).isEmpty()) { |
| 73 | + var a = node.getArtifact(); |
| 74 | + String key = a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion() + ":" |
| 75 | + + (a.getClassifier() != null ? a.getClassifier() : "") + ":" + a.getType(); |
| 76 | + if (seen.add(key)) { |
| 77 | + uniqueArtifacts.add(a); |
103 | 78 | } |
104 | 79 | } |
105 | | - executor.shutdown(); |
106 | 80 | } |
| 81 | + calc.prewarmArtifactCache(uniqueArtifacts); |
107 | 82 |
|
108 | 83 | Set<DependencyNode> nodes = new TreeSet<>(Comparator.comparing(DependencyNode::getComparatorString)); |
109 | 84 | for (var artifact : roots) { |
|
0 commit comments