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


##########
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 -> {
+                                long millis = Duration.between(step.startTime, 
step.endTime)
+                                        .toMillis();
+                                try {
+                                    writer.write(step.stepId());
+                                    writer.write('\t');
+                                    writer.write(Long.toString(millis));
+                                    writer.newLine();
+                                } catch (IOException e) {
+                                    throw new RuntimeException(e);
+                                }
+                            });
+                }
+                Files.move(tmp, file, StandardCopyOption.REPLACE_EXISTING);
+            } catch (IOException | RuntimeException e) {
+                Files.deleteIfExists(tmp);
+                throw e;
+            }
+            LOGGER.debug("Wrote step timings to {}", file);
+        } catch (IOException | RuntimeException e) {
+            LOGGER.debug("Could not write step timings to {}: {}", file, 
e.getMessage());
+        }

Review Comment:
   **🔕 Silent write failure for a user-enabled feature**
   
   When `StepTimings.write()` fails (disk full, permission error, etc.), the 
exception is caught and logged at `LOGGER.debug()`. Since this feature is 
opt-in via `-Dmaven.builder.stepTimings.enabled=true`, a user who explicitly 
enabled it and observes no scheduling improvement would have no indication that 
timings are silently not being written.
   
   This should be `LOGGER.warn()` to surface the problem to the user:
   
   ```suggestion
               LOGGER.warn("Could not write step timings to {}: {}", file, 
e.getMessage());
   ```



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