From a9f9e0a750f902cbeacebbd0c7a75e6e82708e87 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Tue, 10 Oct 2023 22:04:11 -0400 Subject: [PATCH] Add disruptor-4.0.0 support --- .../AsyncWaitStrategyFactoryConfigTest.java | 1 + ...ctoryIncorrectConfigGlobalLoggersTest.java | 1 + .../async/AsyncLoggerConfigDisruptor.java | 4 +- .../DefaultAsyncWaitStrategyFactory.java | 1 + .../core/async/RingBufferLogEventHandler.java | 5 +- .../async/TimeoutBlockingWaitStrategy.java | 117 ------------------ pom.xml | 2 +- 7 files changed, 8 insertions(+), 123 deletions(-) delete mode 100644 log4j-core/src/main/java/org/apache/logging/log4j/core/async/TimeoutBlockingWaitStrategy.java diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryConfigTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryConfigTest.java index f634380eea0..b26c907183c 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryConfigTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryConfigTest.java @@ -16,6 +16,7 @@ */ package org.apache.logging.log4j.core.async; +import com.lmax.disruptor.TimeoutBlockingWaitStrategy; import com.lmax.disruptor.WaitStrategy; import com.lmax.disruptor.YieldingWaitStrategy; import org.apache.logging.log4j.core.LoggerContext; diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryIncorrectConfigGlobalLoggersTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryIncorrectConfigGlobalLoggersTest.java index dcf3c9aead3..dcb552a4de6 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryIncorrectConfigGlobalLoggersTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncWaitStrategyFactoryIncorrectConfigGlobalLoggersTest.java @@ -16,6 +16,7 @@ */ package org.apache.logging.log4j.core.async; +import com.lmax.disruptor.TimeoutBlockingWaitStrategy; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.ConfigurationFactory; 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 7c8d0cfa19b..c8ec089aea9 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 @@ -20,11 +20,11 @@ import java.util.concurrent.TimeUnit; import com.lmax.disruptor.EventFactory; +import com.lmax.disruptor.EventHandler; import com.lmax.disruptor.EventTranslatorTwoArg; import com.lmax.disruptor.ExceptionHandler; import com.lmax.disruptor.RingBuffer; import com.lmax.disruptor.Sequence; -import com.lmax.disruptor.SequenceReportingEventHandler; import com.lmax.disruptor.TimeoutException; import com.lmax.disruptor.WaitStrategy; import com.lmax.disruptor.dsl.Disruptor; @@ -94,7 +94,7 @@ public String toString() { /** * EventHandler performs the work in a separate thread. */ - private static class Log4jEventWrapperHandler implements SequenceReportingEventHandler { + private static class Log4jEventWrapperHandler implements EventHandler { private static final int NOTIFY_PROGRESS_THRESHOLD = 50; private Sequence sequenceCallback; private int counter; diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DefaultAsyncWaitStrategyFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DefaultAsyncWaitStrategyFactory.java index 7e10c33d1cf..057a1fd6597 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DefaultAsyncWaitStrategyFactory.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DefaultAsyncWaitStrategyFactory.java @@ -21,6 +21,7 @@ import com.lmax.disruptor.BlockingWaitStrategy; import com.lmax.disruptor.BusySpinWaitStrategy; import com.lmax.disruptor.SleepingWaitStrategy; +import com.lmax.disruptor.TimeoutBlockingWaitStrategy; import com.lmax.disruptor.WaitStrategy; import com.lmax.disruptor.YieldingWaitStrategy; import org.apache.logging.log4j.Logger; diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEventHandler.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEventHandler.java index 460bebd49a1..bf0dcb0a9bf 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEventHandler.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEventHandler.java @@ -16,9 +16,8 @@ */ package org.apache.logging.log4j.core.async; -import com.lmax.disruptor.LifecycleAware; +import com.lmax.disruptor.EventHandler; import com.lmax.disruptor.Sequence; -import com.lmax.disruptor.SequenceReportingEventHandler; /** * This event handler gets passed messages from the RingBuffer as they become @@ -27,7 +26,7 @@ * constructor. */ public class RingBufferLogEventHandler implements - SequenceReportingEventHandler, LifecycleAware { + EventHandler { private static final int NOTIFY_PROGRESS_THRESHOLD = 50; private Sequence sequenceCallback; diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/TimeoutBlockingWaitStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/TimeoutBlockingWaitStrategy.java deleted file mode 100644 index 9346032b0b1..00000000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/TimeoutBlockingWaitStrategy.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * 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.logging.log4j.core.async; - -import java.util.concurrent.TimeUnit; - -import com.lmax.disruptor.AlertException; -import com.lmax.disruptor.Sequence; -import com.lmax.disruptor.SequenceBarrier; -import com.lmax.disruptor.TimeoutException; -import com.lmax.disruptor.WaitStrategy; - -/** - * Blocking strategy that uses a lock and condition variable for {@link EventProcessor}s waiting on a barrier. - * However, it will periodically wake up if it has been idle for specified period by throwing a - * {@link TimeoutException}. To make use of this, the event handler class should override - * {@link EventHandler#onTimeout(long)}, which the {@link BatchEventProcessor} will call if the timeout occurs. - * - *

This strategy can be used when throughput and low-latency are not as important as CPU resource. - */ -// IMPLEMENTATION NOTE: -// This is a copy of the com.lmax.disruptor.TimeoutBlockingWaitStrategy class in disruptor-4.0.0-RC1. -// The difference between this code and the implementation of this class in disruptor-3.4.4 -// is that this class is garbage-free, because it uses a synchronized block instead of a ReentrantLock. -// This class is package-protected, so that it can be used internally as the default WaitStrategy -// by Log4j Async Loggers, but can be removed in a future Log4j release without impacting binary compatibility. -// (disruptor-4.0.0-RC1 requires Java 11 and has other incompatible changes so cannot be used in Log4j 2.x.) -class TimeoutBlockingWaitStrategy implements WaitStrategy { - private final Object mutex = new Object(); - private final long timeoutInNanos; - - /** - * @param timeout how long to wait before waking up - * @param units the unit in which timeout is specified - */ - public TimeoutBlockingWaitStrategy(final long timeout, final TimeUnit units) { - timeoutInNanos = units.toNanos(timeout); - } - - @Override - public long waitFor( - final long sequence, - final Sequence cursorSequence, - final Sequence dependentSequence, - final SequenceBarrier barrier) - throws AlertException, InterruptedException, TimeoutException { - long timeoutNanos = timeoutInNanos; - - long availableSequence; - if (cursorSequence.get() < sequence) { - synchronized (mutex) { - while (cursorSequence.get() < sequence) { - barrier.checkAlert(); - timeoutNanos = awaitNanos(mutex, timeoutNanos); - if (timeoutNanos <= 0) { - throw TimeoutException.INSTANCE; - } - } - } - } - - while ((availableSequence = dependentSequence.get()) < sequence) { - barrier.checkAlert(); - } - - return availableSequence; - } - - @Override - public void signalAllWhenBlocking() { - synchronized (mutex) { - mutex.notifyAll(); - } - } - - @Override - public String toString() { - return "TimeoutBlockingWaitStrategy{" + - "mutex=" + mutex + - ", timeoutInNanos=" + timeoutInNanos + - '}'; - } - - // below code is from com.lmax.disruptor.util.Util class in disruptor 4.0.0-RC1 - private static final int ONE_MILLISECOND_IN_NANOSECONDS = 1_000_000; - - /** - * @param mutex The object to wait on - * @param timeoutNanos The number of nanoseconds to wait for - * @return the number of nanoseconds waited (approximately) - * @throws InterruptedException if the underlying call to wait is interrupted - */ - private static long awaitNanos(final Object mutex, final long timeoutNanos) throws InterruptedException { - final long millis = timeoutNanos / ONE_MILLISECOND_IN_NANOSECONDS; - final long nanos = timeoutNanos % ONE_MILLISECOND_IN_NANOSECONDS; - - final long t0 = System.nanoTime(); - mutex.wait(millis, (int) nanos); - final long t1 = System.nanoTime(); - - return timeoutNanos - (t1 - t0); - } -} diff --git a/pom.xml b/pom.xml index faa9f5b7146..b88847a8fc3 100644 --- a/pom.xml +++ b/pom.xml @@ -375,7 +375,7 @@ 1.2 1.2.15 - 3.4.4 + 4.0.0 7.17.13 0.9.0 7.0.5