gnodet-bot commented on code in PR #14193:
URL: https://github.com/apache/maven/pull/14193#discussion_r4746703582
##########
impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java:
##########
@@ -651,11 +672,10 @@ private void executePlan() {
// 2. Check if all their predecessors have completed (in a
terminal state)
// 3. Process each step (schedule or skip based on reactor
failure behavior)
plan.sortedNodes().stream()
- // 1. Filter steps that are in CREATED state
.filter(BuildStep::isCreated)
- // 2. Check if all predecessors are in a terminal state
.filter(step ->
step.predecessors.stream().allMatch(BuildStep::isDone))
- // 3. Process each step
+ .sorted(Comparator.comparingLong((BuildStep s) ->
s.weight)
+ .reversed())
Review Comment:
**⚡ Sort runs on every scheduler tick**
Weights are computed once in `plan()` via `computeWeights()`, but
`.sorted(Comparator.comparingLong(...).reversed())` is called inside
`executePlan()` on every scheduling cycle. For small graphs this is negligible,
but for large multi-module builds with many pending steps this is redundant
work per tick — the stream is re-sorted even though weights never change after
`plan()`.
Consider pre-sorting the steps (or storing them in a sorted structure like a
`PriorityQueue`) when weights are assigned in `computeWeights()`, so each
`executePlan()` tick picks from an already-ordered structure.
##########
impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/StepTimings.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.lifecycle.internal.concurrent;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.time.Duration;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Reads and writes step timing data for critical-path scheduling.
+ * Each line contains a step ID and its duration in milliseconds, separated by
a tab.
+ */
+class StepTimings {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(StepTimings.class);
+
+ static final String TIMINGS_FILE = ".mvn/target/step-timings.txt";
+
+ static Map<String, Long> read(Path rootDirectory) {
+ Path file = rootDirectory.resolve(TIMINGS_FILE);
+ if (!Files.isRegularFile(file)) {
+ return Map.of();
+ }
+ Map<String, Long> timings = new LinkedHashMap<>();
+ try (BufferedReader reader = Files.newBufferedReader(file,
StandardCharsets.UTF_8)) {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ line = line.strip();
+ if (line.isEmpty() || line.startsWith("#")) {
+ continue;
+ }
+ int sep = line.indexOf('\t');
+ if (sep > 0) {
+ String stepId = line.substring(0, sep);
+ try {
+ long millis = Long.parseLong(line.substring(sep +
1).strip());
+ timings.put(stepId, millis);
+ } catch (NumberFormatException e) {
+ // skip malformed lines
+ }
+ }
+ }
+ LOGGER.debug("Loaded {} step timings from {}", timings.size(),
file);
+ } catch (IOException e) {
+ LOGGER.debug("Could not read step timings from {}: {}", file,
e.getMessage());
+ }
+ return timings;
+ }
+
+ static void write(Path rootDirectory, BuildPlan plan) {
+ if (rootDirectory == null) {
+ return;
+ }
+ Path file = rootDirectory.resolve(TIMINGS_FILE);
+ try {
+ Files.createDirectories(file.getParent());
+ Path tmp = Files.createTempFile(file.getParent(),
".step-timings-", ".tmp");
+ try {
+ try (BufferedWriter writer = Files.newBufferedWriter(tmp,
StandardCharsets.UTF_8)) {
+ plan.allSteps()
+ .filter(step -> step.startTime != null &&
step.endTime != null)
+ .forEach(step -> {
Review Comment:
**⚠️ Failed steps included in timing data**
The filter `.filter(step -> step.startTime != null && step.endTime != null)`
does not exclude failed steps. Looking at `executeStep()`, `startTime` is set
before the try block and `endTime` is set in the `finally`, so both fields are
populated regardless of whether the step succeeded. Only a successfully
executed step sets `status` to `EXECUTED`; a failed step keeps `SCHEDULED`.
This means the timings file accumulates partial/failed step durations, which
will skew future critical-path estimates. Add a status check:
```suggestion
.filter(step -> step.startTime != null &&
step.endTime != null
&& step.status.get() ==
BuildStep.EXECUTED)
```
--
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]