You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ck...@apache.org on 2020/07/16 21:07:26 UTC

[logging-log4j2] 01/02: LOG4J2-2858: More flexible configuration of WaitStrategy of Disruptor (#361)

This is an automated email from the ASF dual-hosted git repository.

ckozak pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 669df9c8edc404c23167703d867d92579312cca6
Author: stepan2271 <go...@mail.ru>
AuthorDate: Thu Jul 16 23:45:03 2020 +0300

    LOG4J2-2858: More flexible configuration of WaitStrategy of Disruptor (#361)
    
    Added additional properties for setting sleepTimeNs and retries parameters of SleepingWaitStrategy
---
 .../logging/log4j/core/async/DisruptorUtil.java    | 64 +++++++++++++---------
 src/site/xdoc/manual/async.xml                     | 60 ++++++++++++++++++++
 src/site/xdoc/manual/configuration.xml.vm          | 39 +++++++++++++
 3 files changed, 136 insertions(+), 27 deletions(-)

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 0299541..5925187 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
@@ -17,7 +17,6 @@
 
 package org.apache.logging.log4j.core.async;
 
-import java.util.Locale;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
@@ -36,6 +35,7 @@ import org.apache.logging.log4j.core.util.Integers;
 import org.apache.logging.log4j.core.util.Loader;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.PropertiesUtil;
+import org.apache.logging.log4j.util.Strings;
 
 /**
  * Utility methods for getting Disruptor related configuration.
@@ -60,36 +60,46 @@ final class DisruptorUtil {
     private DisruptorUtil() {
     }
 
-    static long getTimeout(final String propertyName, final long defaultTimeout) {
-        return PropertiesUtil.getProperties().getLongProperty(propertyName, defaultTimeout);
+    static WaitStrategy createWaitStrategy(final String propertyName) {
+        final String strategy = PropertiesUtil.getProperties().getStringProperty(propertyName, "Timeout");
+        LOGGER.trace("property {}={}", propertyName, strategy);
+        final String strategyUp = Strings.toRootUpperCase(strategy);
+        final long timeoutMillis = parseAdditionalLongProperty(propertyName, "Timeout", 10L);
+        // String (not enum) is deliberately used here to avoid IllegalArgumentException being thrown. In case of
+        // incorrect property value, default WaitStrategy is created.
+        switch (strategyUp) {
+            case "SLEEP":
+                final long sleepTimeNs =
+                        parseAdditionalLongProperty(propertyName, "SleepTimeNs", 100L);
+                final String key = getFullPropertyKey(propertyName, "Retries");
+                final int retries =
+                        PropertiesUtil.getProperties().getIntegerProperty(key, 200);
+                return new SleepingWaitStrategy(retries, sleepTimeNs);
+            case "YIELD":
+                return new YieldingWaitStrategy();
+            case "BLOCK":
+                return new BlockingWaitStrategy();
+            case "BUSYSPIN":
+                return new BusySpinWaitStrategy();
+            case "TIMEOUT":
+                return new TimeoutBlockingWaitStrategy(timeoutMillis, TimeUnit.MILLISECONDS);
+            default:
+                return new TimeoutBlockingWaitStrategy(timeoutMillis, TimeUnit.MILLISECONDS);
+        }
     }
 
-    static WaitStrategy createWaitStrategy(final String propertyName) {
-        final String key = propertyName.startsWith("AsyncLogger.")
-                ? "AsyncLogger.Timeout"
-                : "AsyncLoggerConfig.Timeout";
-        final long timeoutMillis = DisruptorUtil.getTimeout(key, 10L);
-        return createWaitStrategy(propertyName, timeoutMillis);
+    private static String getFullPropertyKey(final String strategyKey, final String additionalKey) {
+        return strategyKey.startsWith("AsyncLogger.")
+                ? "AsyncLogger." + additionalKey
+                : "AsyncLoggerConfig." + additionalKey;
     }
 
-    static WaitStrategy createWaitStrategy(final String propertyName, final long timeoutMillis) {
-        final String strategy = PropertiesUtil.getProperties().getStringProperty(propertyName, "TIMEOUT");
-        LOGGER.trace("property {}={}", propertyName, strategy);
-        final String strategyUp = strategy.toUpperCase(Locale.ROOT); // TODO Refactor into Strings.toRootUpperCase(String)
-        switch (strategyUp) { // TODO Define a DisruptorWaitStrategy enum?
-        case "SLEEP":
-            return new SleepingWaitStrategy();
-        case "YIELD":
-            return new YieldingWaitStrategy();
-        case "BLOCK":
-            return new BlockingWaitStrategy();
-        case "BUSYSPIN":
-            return new BusySpinWaitStrategy();
-        case "TIMEOUT":
-            return new TimeoutBlockingWaitStrategy(timeoutMillis, TimeUnit.MILLISECONDS);
-        default:
-            return new TimeoutBlockingWaitStrategy(timeoutMillis, TimeUnit.MILLISECONDS);
-        }
+    private static long parseAdditionalLongProperty(
+            final String propertyName,
+            final String additionalKey,
+            long defaultValue) {
+        final String key = getFullPropertyKey(propertyName, additionalKey);
+        return PropertiesUtil.getProperties().getLongProperty(key, defaultValue);
     }
 
     static int calculateRingBufferSize(final String propertyName) {
diff --git a/src/site/xdoc/manual/async.xml b/src/site/xdoc/manual/async.xml
index 73e7967..699ec97 100644
--- a/src/site/xdoc/manual/async.xml
+++ b/src/site/xdoc/manual/async.xml
@@ -255,6 +255,36 @@
             </td>
           </tr>
           <tr>
+            <td>log4j2.asyncLoggerTimeout</td>
+            <td>
+              <tt>10</tt>
+            </td>
+            <td>
+              Timeout in milliseconds of <tt>TimeoutBlockingWaitStrategy</tt>. See
+              the WaitStrategy System Property for details.
+            </td>
+          </tr>
+          <tr>
+            <td>log4j2.asyncLoggerSleepTimeNs</td>
+            <td>
+              <tt>100</tt>
+            </td>
+            <td>
+              Sleep time (in nanoseconds) of <tt>SleepingWaitStrategy</tt>. See
+              the WaitStrategy System Property for details.
+            </td>
+          </tr>
+          <tr>
+            <td>log4j2.asyncLoggerRetries</td>
+            <td>
+              <tt>200</tt>
+            </td>
+            <td>
+              Total number of spin cycles and <tt>Thread.yield()</tt> cycles of <tt>SleepingWaitStrategy</tt>. See
+              the WaitStrategy System Property for details.
+            </td>
+          </tr>
+          <tr>
             <td>AsyncLogger.SynchronizeEnqueueWhenQueueFull</td>
             <td>
               <tt>true</tt>
@@ -449,6 +479,36 @@
             </td>
           </tr>
           <tr>
+            <td>log4j2.asyncLoggerConfigTimeout</td>
+            <td>
+              <tt>10</tt>
+            </td>
+            <td>
+              Timeout in milliseconds of <tt>TimeoutBlockingWaitStrategy</tt>. See
+              the WaitStrategy System Property for details.
+            </td>
+          </tr>
+          <tr>
+            <td>log4j2.asyncLoggerConfigSleepTimeNs</td>
+            <td>
+              <tt>100</tt>
+            </td>
+            <td>
+              Sleep time (in nanoseconds) of <tt>SleepingWaitStrategy</tt>. See
+              the WaitStrategy System Property for details.
+            </td>
+          </tr>
+          <tr>
+            <td>log4j2.asyncLoggerConfigRetries</td>
+            <td>
+              <tt>200</tt>
+            </td>
+            <td>
+              Total number of spin cycles and <tt>Thread.yield()</tt> cycles of <tt>SleepingWaitStrategy</tt>. See
+              the WaitStrategy System Property for details.
+            </td>
+          </tr>
+          <tr>
             <td>AsyncLoggerConfig.SynchronizeEnqueueWhenQueueFull</td>
             <td>
               <tt>true</tt>
diff --git a/src/site/xdoc/manual/configuration.xml.vm b/src/site/xdoc/manual/configuration.xml.vm
index a3da3b8..aac06d3 100644
--- a/src/site/xdoc/manual/configuration.xml.vm
+++ b/src/site/xdoc/manual/configuration.xml.vm
@@ -2185,6 +2185,45 @@ public class AwesomeTest {
     </td>
   </tr>
   <tr>
+    <td><a name="asyncLoggerTimeout"/>log4j2.asyncLoggerTimeout
+      <br />
+      (<a name="AsyncLogger.Timeout" />AsyncLogger.Timeout)
+    </td>
+    <td>LOG4J_ASYNC_LOGGER_TIMEOUT</td>
+    <td>
+      10
+    </td>
+    <td>
+      See <a href="async.html#SysPropsAllAsync">Async Logger System Properties</a> for details.
+    </td>
+  </tr>
+  <tr>
+    <td><a name="asyncLoggerSleepTimeNs"/>log4j2.asyncLoggerSleepTimeNs
+      <br />
+      (<a name="AsyncLogger.SleepTimeNs" />AsyncLogger.SleepTimeNs)
+    </td>
+    <td>LOG4J_ASYNC_LOGGER_SLEEP_TIME_NS</td>
+    <td>
+      100
+    </td>
+    <td>
+      See <a href="async.html#SysPropsAllAsync">Async Logger System Properties</a> for details.
+    </td>
+  </tr>
+  <tr>
+    <td><a name="asyncLoggerRetries"/>log4j2.asyncLoggerRetries
+      <br />
+      (<a name="AsyncLogger.Retries" />AsyncLogger.Retries)
+    </td>
+    <td>LOG4J_ASYNC_LOGGER_SLEEP_TIME_NS</td>
+    <td>
+      200
+    </td>
+    <td>
+      See <a href="async.html#SysPropsAllAsync">Async Logger System Properties</a> for details.
+    </td>
+  </tr>
+  <tr>
     <td><a name="AsyncLogger.SynchronizeEnqueueWhenQueueFull"/>AsyncLogger.SynchronizeEnqueueWhenQueueFull
     </td>
     <td>ASYNC_LOGGER_SYNCHRONIZE_ENQUEUE_WHEN_QUEUE_FULL</td>