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


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

Review Comment:
   Nit (optional): Javadoc claims steps are sorted 'by project reactor order 
alone' when parallel=false, but implementation doesn't reset existing weights 
to zero. If computeWeights(true, ...) is called followed by 
computeWeights(false, ...) on the same BuildPlan, the second call would still 
use weights from the first call, contradicting the documented behavior. 
   
   **Fix:** Either reset all step weights to 0 when parallel=false, or update 
javadoc to clarify that existing weights are preserved.
   
   **Failure scenario:** Call computeWeights(true, timings) on a plan setting 
weights 100,200,300. Then call computeWeights(false, Map.of()). Expected per 
javadoc: sorting by project order alone. Actual: sorting by old weights 
(100,200,300) then project order.



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

Review Comment:
   Nit (optional): computeWeights() modifies the cached List returned by 
sortedNodes() in place via sorted.sort(). The sortedNodes() method appears to 
be a read-only getter, but its returned list is mutated. If code calls 
sortedNodes() after computeWeights() expecting topological order, it gets 
weight-sorted order instead. This side effect is non-obvious from method names.
   
   **Fix:** Add explicit javadoc to sortedNodes() stating the list may be 
reordered by computeWeights(), or return a defensive copy.
   
   **Failure scenario:** Code calls sortedNodes() to get topological order, 
caches the result. Later computeWeights() is called, modifying that cached 
reference. Original caller's list is now unexpectedly reordered, breaking 
assumptions about topological order.



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