gnodet commented on code in PR #14220:
URL: https://github.com/apache/maven/pull/14220#discussion_r4786746537


##########
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()

Review Comment:
   Nit (optional): Comparator uses getOrDefault(s.project, Integer.MAX_VALUE) 
to handle steps whose project is missing from plan.keySet(). This should never 
occur in correct execution and would indicate a logic error. The code silently 
handles it by sorting such steps last, potentially hiding bugs.
   
   **Fix:** Add logging or assertion when Integer.MAX_VALUE is returned to 
detect this error condition during development/testing.
   
   **Failure scenario:** Bug causes BuildStep to be created with project P that 
was never added to plan via addProject(). Step is silently sorted last instead 
of failing fast. Build proceeds with undefined behavior.



##########
impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanWeightsTest.java:
##########
@@ -171,6 +171,64 @@ void 
sortedNodesAreOrderedByDescendingWeightAfterComputeWeights() {
         assertEquals(a, sorted.get(0), "Heaviest step (root) must come first");
     }
 
+    // 
-------------------------------------------------------------------------

Review Comment:
   Nit (optional): New tests verify grouping by project order but don't test 
cross-project dependencies in single-thread mode. If step b1 depends on step a2 
(projects in order A, B), topological order could be [a1, a2, b1, b2]. The 
scheduler respects dependencies via predecessor counts, but test coverage would 
be stronger with explicit cross-project dependency validation.
   
   **Fix:** Add test with cross-project dependencies to verify scheduler 
correctness.
   
   **Failure scenario:** Future change breaks predecessor-based scheduling, 
relying only on list order. Without cross-project dependency test, bug goes 
undetected—single-thread builds execute steps before their cross-project 
predecessors complete.



-- 
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]

Reply via email to