Skip to content

Commit 1b16981

Browse files
committed
move prewarm to remotechecksumcalculator
1 parent c9b88ae commit 1b16981

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/checksum/AbstractChecksumCalculator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.nio.file.Files;
66
import java.nio.file.Path;
77
import java.security.MessageDigest;
8+
import java.util.Collection;
89
import java.util.Locale;
910
import org.apache.maven.artifact.Artifact;
1011

@@ -37,6 +38,14 @@ public String getChecksumAlgorithm() {
3738

3839
public abstract RepositoryInformation getPluginResolvedField(Artifact artifact);
3940

41+
/**
42+
* Pre-warm internal caches for the given artifacts. Implementations may use this
43+
* to fetch checksums and repository information in parallel. The default is a no-op.
44+
*/
45+
public void prewarmArtifactCache(Collection<Artifact> artifacts) {
46+
// No-op by default; overridden by RemoteChecksumCalculator
47+
}
48+
4049
public String calculatePomChecksum(Path path) {
4150
try {
4251
MessageDigest messageDigest = MessageDigest.getInstance(checksumAlgorithm);

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/checksum/RemoteChecksumCalculator.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
import java.net.http.HttpRequest;
1010
import java.net.http.HttpResponse;
1111
import java.security.MessageDigest;
12+
import java.util.ArrayList;
13+
import java.util.Collection;
14+
import java.util.List;
1215
import java.util.Locale;
1316
import java.util.Map;
1417
import java.util.Optional;
1518
import java.util.concurrent.ConcurrentHashMap;
19+
import java.util.concurrent.ExecutorService;
20+
import java.util.concurrent.Executors;
21+
import java.util.concurrent.Future;
1622
import org.apache.maven.artifact.Artifact;
1723
import org.apache.maven.artifact.repository.ArtifactRepository;
1824
import org.apache.maven.project.ProjectBuildingRequest;
@@ -223,6 +229,37 @@ private Optional<RepositoryInformation> getResolvedFieldInternal(
223229
}
224230
}
225231

232+
@Override
233+
public void prewarmArtifactCache(Collection<Artifact> artifacts) {
234+
if (artifacts.isEmpty()) {
235+
return;
236+
}
237+
int poolSize = Math.min(16, Math.max(4, Runtime.getRuntime().availableProcessors() * 2));
238+
PluginLogManager.getLog()
239+
.info(String.format(
240+
"Pre-warming checksum cache for %d unique artifacts with %d threads",
241+
artifacts.size(), poolSize));
242+
ExecutorService executor = Executors.newFixedThreadPool(poolSize);
243+
try {
244+
List<Future<?>> futures = new ArrayList<>();
245+
for (var artifact : artifacts) {
246+
futures.add(executor.submit(() -> {
247+
calculateArtifactChecksum(artifact);
248+
getArtifactResolvedField(artifact);
249+
}));
250+
}
251+
for (var future : futures) {
252+
try {
253+
future.get();
254+
} catch (Exception e) {
255+
PluginLogManager.getLog().debug("Pre-warm task failed: " + e.getMessage());
256+
}
257+
}
258+
} finally {
259+
executor.shutdown();
260+
}
261+
}
262+
226263
@Override
227264
public String calculateArtifactChecksum(Artifact artifact) {
228265
return calculateChecksumInternal(artifact, artifactBuildingRequest).orElse("");

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/graph/DependencyGraph.java

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import io.github.chains_project.maven_lockfile.data.VersionNumber;
1313
import io.github.chains_project.maven_lockfile.reporting.PluginLogManager;
1414
import java.util.*;
15-
import java.util.concurrent.ExecutorService;
16-
import java.util.concurrent.Executors;
17-
import java.util.concurrent.Future;
1815
import java.util.stream.Collectors;
16+
import org.apache.maven.artifact.Artifact;
1917
import org.apache.maven.shared.dependency.graph.internal.SpyingDependencyNodeUtils;
2018

2119
public class DependencyGraph {
@@ -67,43 +65,20 @@ public static DependencyGraph of(
6765
.filter(it -> graph.predecessors(it).isEmpty())
6866
.collect(Collectors.toList());
6967

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
7469
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);
10378
}
10479
}
105-
executor.shutdown();
10680
}
81+
calc.prewarmArtifactCache(uniqueArtifacts);
10782

10883
Set<DependencyNode> nodes = new TreeSet<>(Comparator.comparing(DependencyNode::getComparatorString));
10984
for (var artifact : roots) {

0 commit comments

Comments
 (0)