gnodet-bot commented on code in PR #14186:
URL: https://github.com/apache/maven/pull/14186#discussion_r4739589266


##########
impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlan.java:
##########
@@ -234,4 +235,36 @@ private static void visitNode(BuildStep node, 
Set<BuildStep> visited, List<Build
             sortedNodes.add(node);
         }
     }
+
+    /**
+     * Initializes the pending-predecessor counters on every CREATED step in 
the plan and
+     * seeds {@code readyQueue} with the steps that have no pending 
predecessors (they can
+     * run immediately).
+     * <p>
+     * Called after the plan is fully constructed and again whenever new steps 
are
+     * added by a {@code plan()} invocation (dynamic planning). Idempotent: 
steps
+     * whose counters were already initialized by a prior call are skipped.
+     * In-flight (SCHEDULED) steps are left untouched.
+     * <p>
+     * Uses an atomic {@code addAndGet} rather than {@code set} so that any
+     * {@code notifySuccessors} calls that race against this method (possible 
because
+     * async executor threads do not hold the write lock) are correctly 
accounted for.
+     * <p>
+     * Thread-safety: call under the write lock that protects plan mutations.
+     *
+     * @param readyQueue queue to seed with immediately-runnable steps
+     */
+    void initReadyQueue(Queue<BuildStep> readyQueue) {
+        allSteps().filter(BuildStep::isCreated).forEach(step -> {
+            if (step.pendingPredecessors.get() > BuildStep.NOT_INITIALIZED) {
+                return;
+            }
+            int pending =
+                    (int) step.predecessors.stream().filter(p -> 
!p.isDone()).count();

Review Comment:
   🔍 **Nit:** The `(int)` cast on `stream().count()` is safe for any realistic 
reactor, but `predecessors` is a `Collection<BuildStep>` — 
`predecessors.size()` minus the done count would avoid allocating a stream here 
(this runs under the write lock, so not on the hot path, but still cleaner):
   
   ```suggestion
               int pending =
                       (int) step.predecessors.stream().filter(p -> 
!p.isDone()).count();
   ```
   *(Minor; leave as-is if you prefer the stream form for readability.)*



##########
impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildStep.java:
##########
@@ -117,15 +138,53 @@ public void addMojo(MojoExecution mojo, int priority) {
 
     public void executeAfter(BuildStep stepToExecuteBefore) {
         if (!isSuccessorOf(stepToExecuteBefore) && 
!stepToExecuteBefore.isSuccessorOf(this)) {
-            predecessors.add(stepToExecuteBefore);
-            stepToExecuteBefore.successors.add(this);
+            executeAfterUnchecked(stepToExecuteBefore);
         }
     }
 
+    /**
+     * Links this step to execute after {@code stepToExecuteBefore} without the
+     * O(graph-size) cycle/redundancy check.  Safe to use during plan 
construction
+     * where the graph structure guarantees acyclicity (lifecycle phase 
ordering,
+     * validated reactor dependency order).
+     */
+    void executeAfterUnchecked(BuildStep stepToExecuteBefore) {
+        predecessors.add(stepToExecuteBefore);
+        stepToExecuteBefore.successors.add(this);
+    }
+
     public void executeBefore(BuildStep stepToExecuteAfter) {
         stepToExecuteAfter.executeAfter(this);
     }
 
+    void executeBeforeUnchecked(BuildStep stepToExecuteAfter) {
+        stepToExecuteAfter.executeAfterUnchecked(this);
+    }
+
+    /**
+     * Notifies successors that this step is done. For each successor whose
+     * {@code pendingPredecessors} counter drops to zero, it is enqueued in
+     * {@code readyQueue} for immediate processing.
+     * <p>
+     * Call this method exactly once, immediately after this step transitions 
to a
+     * terminal state (EXECUTED, FAILED or SKIPPED).
+     * <p>
+     * Thread-safety: safe to call from any thread; {@code 
pendingPredecessors} is
+     * decremented atomically and {@code successors} is a {@link 
CopyOnWriteArraySet}
+     * that tolerates concurrent iteration.
+     */
+    void notifySuccessors(Queue<BuildStep> readyQueue) {
+        boolean failed = status.get() != EXECUTED;
+        for (BuildStep successor : successors) {
+            if (failed) {
+                successor.predecessorFailed.set(true);
+            }
+            if (successor.pendingPredecessors.decrementAndGet() == 0 && 
successor.isCreated()) {

Review Comment:
   🔍 **Nit:** If `notifySuccessors` were ever called twice on the same 
predecessor by a future change, the counter would go negative without any 
detection (the `isCreated()` guard prevents double-enqueue, but the counter 
corruption would be silent). Consider adding a debug-level log when 
`decrementAndGet() < 0` to make future misuse easier to diagnose:
   
   ```suggestion
               if (successor.pendingPredecessors.decrementAndGet() == 0 && 
successor.isCreated()) {
                   readyQueue.add(successor);
               } else if (successor.pendingPredecessors.get() < 0) {
                   logger.debug("pendingPredecessors went negative for step {} 
— notifySuccessors called more than once?", successor);
               }
   ```



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