Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load workspace modules in parallel #40712

Merged
merged 1 commit into from
May 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collection;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.Phaser;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -41,6 +46,8 @@ public class WorkspaceLoader implements WorkspaceModelResolver, WorkspaceReader

private static final String POM_XML = "pom.xml";

private static final Model MISSING_MODEL = new Model();

private static Path locateCurrentProjectPom(Path path) throws BootstrapMavenException {
Path p = path;
while (p != null) {
Expand All @@ -53,11 +60,11 @@ private static Path locateCurrentProjectPom(Path path) throws BootstrapMavenExce
throw new BootstrapMavenException("Failed to locate project pom.xml for " + path);
}

private final List<RawModule> moduleQueue = new ArrayList<>();
private final Map<Path, Model> loadedPoms = new HashMap<>();
private final Deque<RawModule> moduleQueue = new ConcurrentLinkedDeque<>();
private final Map<Path, Model> loadedPoms = new ConcurrentHashMap<>();

private final Function<Path, Model> modelProvider;
private final Map<GAV, Model> loadedModules = new HashMap<>();
private final Map<GAV, Model> loadedModules = new ConcurrentHashMap<>();

private final LocalWorkspace workspace = new LocalWorkspace();
private final Path currentProjectPom;
Expand Down Expand Up @@ -102,7 +109,7 @@ private static Path locateCurrentProjectPom(Path path) throws BootstrapMavenExce

private void addModulePom(Path pom) {
if (pom != null) {
moduleQueue.add(new RawModule(pom));
moduleQueue.push(new RawModule(pom));
}
}

Expand Down Expand Up @@ -152,11 +159,22 @@ LocalProject load() throws BootstrapMavenException {
};
}

int i = 0;
while (i < moduleQueue.size()) {
var newModules = new ArrayList<RawModule>();
while (i < moduleQueue.size()) {
loadModule(moduleQueue.get(i++), newModules);
while (!moduleQueue.isEmpty()) {
ConcurrentLinkedDeque<RawModule> newModules = new ConcurrentLinkedDeque<>();
while (!moduleQueue.isEmpty()) {
final Phaser phaser = new Phaser(1);
while (!moduleQueue.isEmpty()) {
phaser.register();
final RawModule module = moduleQueue.removeLast();
CompletableFuture.runAsync(() -> {
try {
loadModule(module, newModules);
} finally {
phaser.arriveAndDeregister();
}
});
}
phaser.arriveAndAwaitAdvance();
}
for (var newModule : newModules) {
newModule.process(processor);
Expand All @@ -169,7 +187,7 @@ LocalProject load() throws BootstrapMavenException {
return currentProject.get();
}

private void loadModule(RawModule rawModule, List<RawModule> newModules) {
private void loadModule(RawModule rawModule, Collection<RawModule> newModules) {
var moduleDir = rawModule.pom.getParent();
if (moduleDir == null) {
moduleDir = getFsRootDir();
Expand All @@ -183,7 +201,7 @@ private void loadModule(RawModule rawModule, List<RawModule> newModules) {
rawModule.model = readModel(rawModule.pom);
}
loadedPoms.put(moduleDir, rawModule.model);
if (rawModule.model == null) {
if (rawModule.model == MISSING_MODEL) {
return;
}

Expand Down Expand Up @@ -212,9 +230,8 @@ private void loadModule(RawModule rawModule, List<RawModule> newModules) {
parentDir = getFsRootDir();
}
if (!loadedPoms.containsKey(parentDir)) {
var parent = new RawModule(parentPom);
rawModule.parent = parent;
moduleQueue.add(parent);
rawModule.parent = new RawModule(parentPom);
moduleQueue.push(rawModule.parent);
}
}
}
Expand All @@ -226,7 +243,7 @@ private static Path getFsRootDir() {

private void queueModule(Path dir) {
if (!loadedPoms.containsKey(dir)) {
moduleQueue.add(new RawModule(dir.resolve(POM_XML)));
moduleQueue.push(new RawModule(dir.resolve(POM_XML)));
}
}

Expand Down Expand Up @@ -273,7 +290,7 @@ private static Model readModel(Path pom) {
// which we don't support in this workspace loader
log.warn("Module(s) under " + pom.getParent() + " will be handled as thirdparty dependencies because " + pom
+ " does not exist");
return null;
return MISSING_MODEL;
} catch (IOException e) {
throw new UncheckedIOException("Failed to load POM from " + pom, e);
}
Expand Down
Loading