gnodet commented on code in PR #14186:
URL: https://github.com/apache/maven/pull/14186#discussion_r4739748345
##########
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:
_Claude Code on behalf of Guillaume Nodet_
Skipping this one — `BuildStep` doesn't have a logger and adding one just
for a debug assertion would be heavier than the benefit. The `isCreated()`
guard in `notifySuccessors` and the `<= 0` check in `initReadyQueue` already
prevent double-enqueue from causing incorrect behavior if the counter goes
negative. Added unit tests (`ReadyQueueTest`) covering the sentinel arithmetic
edge cases instead.
##########
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:
_Claude Code on behalf of Guillaume Nodet_
Agreed it's minor — keeping the stream form for readability since this runs
under the write lock and isn't on the hot path.
--
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]