gnodet commented on code in PR #14220:
URL: https://github.com/apache/maven/pull/14220#discussion_r4786744053
##########
impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlan.java:
##########
@@ -238,27 +239,52 @@ private static void visitNode(BuildStep node,
Set<BuildStep> visited, List<Build
}
/**
- * Computes critical-path weights for all steps in the plan. When {@code
previousTimings}
- * is non-empty, uses actual durations (in millis) from a previous build
as step weights;
- * steps not found in the map fall back to 1. When empty, uses graph depth
(each step = 1).
+ * Computes critical-path weights and sorts plan steps for scheduling.
+ *
+ * <p>When {@code parallel} is true, computes critical-path weights using
+ * {@code previousTimings} (actual durations in millis from a prior build)
or
+ * graph depth as fallback, then sorts by descending weight so the
scheduler
+ * picks the heaviest-path steps first. When weights are equal, steps from
the
+ * same project are grouped together (by reactor order) so that a project's
+ * phases run consecutively when no higher-weight step from another project
+ * is competing for a thread.</p>
+ *
+ * <p>When {@code parallel} is false (single-thread), weight computation is
+ * skipped entirely and steps are sorted by project reactor order alone,
+ * producing Maven-3-like sequential per-project execution.</p>
*/
- void computeWeights(Map<String, Long> previousTimings) {
+ void computeWeights(boolean parallel, Map<String, Long> previousTimings) {
List<BuildStep> sorted = sortedNodes();
- for (int i = sorted.size() - 1; i >= 0; i--) {
- BuildStep step = sorted.get(i);
- long maxSuccessorWeight = 0;
- for (BuildStep successor : step.successors) {
- maxSuccessorWeight = Math.max(maxSuccessorWeight,
successor.weight);
- }
- long selfWeight;
- if (previousTimings.isEmpty()) {
- selfWeight = 1;
- } else {
- Long millis = previousTimings.get(step.stepId());
- selfWeight = millis != null ? Math.max(1L, millis) : 1;
+ if (parallel) {
+ for (int i = sorted.size() - 1; i >= 0; i--) {
+ BuildStep step = sorted.get(i);
+ long maxSuccessorWeight = 0;
+ for (BuildStep successor : step.successors) {
+ maxSuccessorWeight = Math.max(maxSuccessorWeight,
successor.weight);
+ }
+ long selfWeight;
+ if (previousTimings.isEmpty()) {
+ selfWeight = 1;
+ } else {
+ Long millis = previousTimings.get(step.stepId());
+ selfWeight = millis != null ? Math.max(1L, millis) : 1;
+ }
+ step.weight = selfWeight + maxSuccessorWeight;
}
- step.weight = selfWeight + maxSuccessorWeight;
}
+ Map<MavenProject, Integer> projectIndex = projectIndex();
+ sorted.sort(Comparator.comparingLong((BuildStep s) -> s.weight)
+ .reversed()
+ .thenComparingInt(s -> projectIndex.getOrDefault(s.project,
Integer.MAX_VALUE)));
+ }
+
+ private Map<MavenProject, Integer> projectIndex() {
+ Map<MavenProject, Integer> index = new HashMap<>();
+ int i = 0;
+ for (MavenProject project : plan.keySet()) {
Review Comment:
Important: projectIndex() assumes plan.keySet() iterates in reactor order,
which depends on projects being added to the LinkedHashMap in reactor order.
This critical assumption is neither documented nor validated. If projects are
added out of order, single-thread builds would execute project phases in wrong
sequence.
**Fix:** Add javadoc comment explaining the insertion-order dependency, or
add assertion verifying the order matches expected reactor order.
**Failure scenario:** Projects A and B are added to plan in order [B, A]
instead of reactor order [A, B]. projectIndex maps B→0, A→1. Single-thread
build executes all B phases before A phases, violating dependency order if A
provides artifacts B needs.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]