gnodet-bot commented on code in PR #13832:
URL: https://github.com/apache/maven/pull/13832#discussion_r4592366390
##########
impl/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java:
##########
@@ -589,7 +600,7 @@ protected BindingStyler getBindingStyler() {
injector.bindInstance(Session.class, sessionV4);
injector.bindInstance(Project.class, project);
injector.bindInstance(org.apache.maven.api.MojoExecution.class,
execution);
- injector.bindInstance(Log.class, log);
+ injector.bindInstance(org.apache.maven.api.plugin.Log.class, log);
Review Comment:
⚠️ **`Logger.class` still not bound in `loadV4Mojo()` (carry-over from last
review)**
`DefaultBuildPluginManager.executeMojo()` now seeds both `Log.class` and
`Logger.class` on lines 683–684. `loadV4Mojo()` creates its own independent
`InjectorImpl` and only binds `Log.class`. The `Logger.class` seed from
`executeMojo`'s Plexus scope does not carry through — `InjectorImpl` is
entirely independent.
Result: a Maven 4 API plugin that uses `@Inject
org.apache.maven.api.logging.Logger` will get a DI failure or null at injection
time when loaded through this code path. The new IT
(`MavenITgh13522LoggerContextTest`) does not catch this because
`CheckContextMojo` uses SLF4J directly rather than `@Inject Logger`.
```suggestion
injector.bindInstance(org.apache.maven.api.plugin.Log.class,
log);
injector.bindInstance(org.apache.maven.api.logging.Logger.class,
log);
```
##########
api/maven-api-log/src/main/java/org/apache/maven/api/logging/BufferingLoggerManager.java:
##########
@@ -0,0 +1,374 @@
+/*
+ * 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.api.logging;
+
+import java.time.Instant;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Supplier;
+
+/**
+ * Bootstrap {@link LoggerManager} that buffers log events until the real
+ * implementation is installed via {@link LoggerManager#set(LoggerManager)}.
+ * <p>
+ * Only {@link LogLevel#WARN WARN} and above are enabled by default to
+ * avoid buffering noisy debug/info output during bootstrap. The buffer
+ * is capped at {@value #MAX_BUFFER_SIZE} events; when exceeded, the oldest
+ * events are dropped and the count of dropped events is tracked.
+ * <p>
+ * After the real manager is installed, any cached {@link BufferingLogger}
+ * instances dynamically resolve the current manager for level checks
+ * and sink dispatch, so they continue to work correctly.
+ */
+final class BufferingLoggerManager implements LoggerManager {
+
+ /** Maximum number of events kept in the bootstrap buffer. */
+ static final int MAX_BUFFER_SIZE = 1000;
+
+ /** Default minimum level during the bootstrap phase. */
+ private static final LogLevel DEFAULT_LEVEL = LogLevel.WARN;
+
+ private final ConcurrentLinkedQueue<LogEvent> buffer = new
ConcurrentLinkedQueue<>();
+ private final ConcurrentHashMap<String, Logger> loggers = new
ConcurrentHashMap<>();
+ private final AtomicLong sequence = new AtomicLong();
+ private final AtomicInteger bufferSize = new AtomicInteger();
+ private final AtomicLong droppedCount = new AtomicLong();
+ private volatile LogSink customSink;
+
+ @Override
+ public Logger getLogger(String name) {
+ return loggers.computeIfAbsent(name, BufferingLogger::new);
+ }
+
+ @Override
+ public LogSink getLogSink() {
+ LogSink s = customSink;
+ return s != null ? s : this::bufferEvent;
+ }
+
+ private void bufferEvent(LogEvent event) {
+ buffer.add(event);
+ if (bufferSize.incrementAndGet() > MAX_BUFFER_SIZE) {
+ if (buffer.poll() != null) {
+ bufferSize.decrementAndGet();
+ droppedCount.incrementAndGet();
+ }
+ }
+ }
Review Comment:
⚠️ **`bufferEvent()`: counter–queue inconsistency under concurrent access**
The `bufferSize` counter and the `ConcurrentLinkedQueue` are not updated
atomically. Under concurrent producers:
1. Thread A calls `buffer.add(e1)` → `bufferSize.incrementAndGet()` → 1001
2. Thread B calls `buffer.add(e2)` → `bufferSize.incrementAndGet()` → 1002
3. Both pass the `> MAX_BUFFER_SIZE` check and each call `buffer.poll()`,
removing 2 events
4. Each decrements `bufferSize` → counter ends at 1000 but queue has 1000
elements ← still consistent here
The more subtle issue: `drainTo()` decrements `bufferSize` while polling. If
`drainTo()` runs concurrently with active `bufferEvent()` calls, the counter
can go negative (or overflow), breaking the cap invariant for any future
events. A counter that drifts negative makes every subsequent event skip the
eviction branch even when the buffer is at capacity.
For a bootstrap buffer that is drained exactly once before concurrent
production begins, the practical risk is low — but the test `assertEquals(100,
mgr.droppedCount())` relies on single-threaded execution that the class doesn't
enforce. Document this clearly, or synchronize `drainTo()` to block until
concurrent `bufferEvent()` calls complete.
The simplest safe fix: use a `synchronized` block in `drainTo()` around the
drain loop, and in `bufferEvent()` around the add + evict check. Both are
called rarely (bootstrap only), so coarse synchronization is acceptable:
```suggestion
private synchronized void bufferEvent(LogEvent event) {
buffer.add(event);
if (buffer.size() > MAX_BUFFER_SIZE) {
if (buffer.poll() != null) {
droppedCount.incrementAndGet();
}
}
}
```
(Remove the separate `bufferSize` counter — `ConcurrentLinkedQueue.size()`
is O(n) but bootstrap traffic is bounded.)
--
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]