This is an automated email from the ASF dual-hosted git repository. ckozak pushed a commit to branch LOG4J2-2606 in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 8d88db612ac7124739a40f5e18ea2f33864e4200 Author: Carter Kozak <[email protected]> AuthorDate: Wed May 22 15:18:59 2019 -0400 Move Semaphore creation into DisruptorUtil --- .../core/async/AsyncLoggerConfigDisruptor.java | 4 +-- .../log4j/core/async/AsyncLoggerDisruptor.java | 4 +-- .../logging/log4j/core/async/DisruptorUtil.java | 38 +++++++++++++++++----- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java index a453170..9d3ef58 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java @@ -188,9 +188,7 @@ public class AsyncLoggerConfigDisruptor extends AbstractLifeCycle implements Asy private volatile boolean alreadyLoggedWarning = false; // queueFullEnqueueSemaphore is null when the semaphore is disabled (non-positive value) - private final Semaphore queueFullEnqueueSemaphore = DisruptorUtil.ASYNC_CONFIG_ENQUEUE_SEMAPHORE_PERMITS > 0 - ? new Semaphore(DisruptorUtil.ASYNC_CONFIG_ENQUEUE_SEMAPHORE_PERMITS) - : null; + private final Semaphore queueFullEnqueueSemaphore = DisruptorUtil.createAsyncConfigQueueFullEnqueuSemaphore(); public AsyncLoggerConfigDisruptor() { } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java index 1e294e4..9d79e88 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java @@ -48,9 +48,7 @@ class AsyncLoggerDisruptor extends AbstractLifeCycle { private static final int MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN = 200; // queueFullEnqueueSemaphore is null when the semaphore is disabled (non-positive value) - private final Semaphore queueFullEnqueueSemaphore = DisruptorUtil.ASYNC_LOGGER_ENQUEUE_SEMAPHORE_PERMITS > 0 - ? new Semaphore(DisruptorUtil.ASYNC_LOGGER_ENQUEUE_SEMAPHORE_PERMITS) - : null; + private final Semaphore queueFullEnqueueSemaphore = DisruptorUtil.createAsyncLoggerQueueFullEnqueuSemaphore(); private volatile Disruptor<RingBufferLogEvent> disruptor; private String contextName; diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java index 5a415a0..298faef 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java @@ -21,6 +21,7 @@ import java.util.Locale; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; +import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import com.lmax.disruptor.BlockingWaitStrategy; @@ -46,14 +47,6 @@ final class DisruptorUtil { private static final int RINGBUFFER_DEFAULT_SIZE = 256 * 1024; private static final int RINGBUFFER_NO_GC_DEFAULT_SIZE = 4 * 1024; - // LOG4J2-2606: Disruptor spins enqueuing events across multiple threads when the queue is full. - // CPU utilization is significantly reduced by restricting access to the enqueue operation. - // Non-positive values disable the semaphore. - static final int ASYNC_LOGGER_ENQUEUE_SEMAPHORE_PERMITS = PropertiesUtil.getProperties() - .getIntegerProperty("AsyncLogger.AsyncQueueFullEnqueueSemaphorePermits", 1); - static final int ASYNC_CONFIG_ENQUEUE_SEMAPHORE_PERMITS = PropertiesUtil.getProperties() - .getIntegerProperty("AsyncLoggerConfig.AsyncQueueFullEnqueueSemaphorePermits", 1); - private DisruptorUtil() { } @@ -161,4 +154,33 @@ final class DisruptorUtil { throw new IllegalStateException(msg, ex); } } + + /** + * Creates a new {@link Semaphore} or null based on + * <pre>AsyncLogger.AsyncQueueFullEnqueueSemaphorePermits</pre>. + */ + static Semaphore createAsyncLoggerQueueFullEnqueuSemaphore() { + return createAsyncQueueFullEnqueuSemaphore("AsyncLogger.AsyncQueueFullEnqueueSemaphorePermits"); + } + + /** + * Creates a new {@link Semaphore} or null based on + * <pre>AsyncLoggerConfig.AsyncQueueFullEnqueueSemaphorePermits</pre>. + */ + static Semaphore createAsyncConfigQueueFullEnqueuSemaphore() { + return createAsyncQueueFullEnqueuSemaphore("AsyncLoggerConfig.AsyncQueueFullEnqueueSemaphorePermits"); + } + + /** + * LOG4J2-2606: Disruptor spins enqueuing events across multiple threads when the queue is full. + * CPU utilization is significantly reduced by restricting access to the enqueue operation. + * Non-positive values disable the semaphore. + */ + private static Semaphore createAsyncQueueFullEnqueuSemaphore(String property) { + int permits = PropertiesUtil.getProperties().getIntegerProperty(property, 1); + if (permits > 0) { + return new Semaphore(permits); + } + return null; + } }
