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

[logging-log4j2] branch release-2.x updated: LOG4J2-2717 - Slow initialization on Windows due to accessing network interfaces.

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

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


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 6669321  LOG4J2-2717 - Slow initialization on Windows due to accessing network interfaces.
6669321 is described below

commit 66693211f3ed47297ca0bf9139ae4ff1889424eb
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Feb 23 13:58:46 2020 -0700

    LOG4J2-2717 - Slow initialization on Windows due to accessing network interfaces.
---
 .../logging/log4j/core/util/WatchManager.java      | 33 +++++++++++++++--
 .../apache/logging/log4j/core/util/InitTest.java   | 41 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 ++
 3 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/WatchManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/WatchManager.java
index 3604bdb..4baefc7 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/WatchManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/WatchManager.java
@@ -28,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.AbstractLifeCycle;
@@ -45,12 +46,14 @@ import org.apache.logging.log4j.util.LoaderUtil;
 public class WatchManager extends AbstractLifeCycle {
 
     private static Logger logger = StatusLogger.getLogger();
+
     private final ConcurrentMap<Source, ConfigurationMonitor> watchers = new ConcurrentHashMap<>();
     private int intervalSeconds = 0;
     private ScheduledFuture<?> future;
     private final ConfigurationScheduler scheduler;
     private final List<WatchEventService> eventServiceList;
-    private final UUID id = UuidUtil.getTimeBasedUuid();
+    // This just needs to be a unique key within the WatchEventManager.
+    private final UUID id = LocalUUID.get();
 
     public WatchManager(final ConfigurationScheduler scheduler) {
         this.scheduler = scheduler;
@@ -336,7 +339,6 @@ public class WatchManager extends AbstractLifeCycle {
         public String toString() {
             return "ConfigurationMonitor [watcher=" + watcher + ", lastModifiedMillis=" + lastModifiedMillis + "]";
         }
-
     }
 
     @Override
@@ -344,4 +346,31 @@ public class WatchManager extends AbstractLifeCycle {
         return "WatchManager [intervalSeconds=" + intervalSeconds + ", watchers=" + watchers + ", scheduler="
             + scheduler + ", future=" + future + "]";
     }
+
+    private static class LocalUUID {
+        private static final long LOW_MASK = 0xffffffffL;
+        private static final long MID_MASK = 0xffff00000000L;
+        private static final long HIGH_MASK = 0xfff000000000000L;
+        private static final int NODE_SIZE = 8;
+        private static final int SHIFT_2 = 16;
+        private static final int SHIFT_4 = 32;
+        private static final int SHIFT_6 = 48;
+        private static final int HUNDRED_NANOS_PER_MILLI = 10000;
+        private static final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
+        private static final AtomicInteger COUNT = new AtomicInteger(0);
+        private static final long TYPE1 = 0x1000L;
+        private static final byte VARIANT = (byte) 0x80;
+        private static final int SEQUENCE_MASK = 0x3FFF;
+
+
+        public static UUID get() {
+            final long time = ((System.currentTimeMillis() * HUNDRED_NANOS_PER_MILLI) +
+                    NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) + (COUNT.incrementAndGet() % HUNDRED_NANOS_PER_MILLI);
+            final long timeLow = (time & LOW_MASK) << SHIFT_4;
+            final long timeMid = (time & MID_MASK) >> SHIFT_2;
+            final long timeHi = (time & HIGH_MASK) >> SHIFT_6;
+            final long most = timeLow | timeMid | TYPE1 | timeHi;
+            return new UUID(most, COUNT.incrementAndGet());
+        }
+    }
 }
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/InitTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/InitTest.java
new file mode 100644
index 0000000..453fa19
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/InitTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.util;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.util.Timer;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test initialization.
+ */
+public class InitTest {
+
+    @Test
+    public void initTest() {
+        Timer timer = new Timer("Log4j Initialization");
+        timer.start();
+        Logger logger = LogManager.getLogger();
+        timer.stop();
+        long elapsed = timer.getElapsedNanoTime();
+        System.out.println(timer.toString());
+        assertTrue("Initialization time exceeded threshold; elapsed " + elapsed, elapsed < 1000000000);
+    }
+}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 38d97a7..2c21f44 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
          - "remove" - Removed
     -->
     <release version="2.13.1" date="2019-MM-DD" description="GA Release 2.13.1">
+      <action issue="LOG4J2-2717" dev="rgoers" type="fix">
+        Slow initialization on Windows due to accessing network interfaces.
+      </action>
       <action issue="LOG4J2-2789" dev="rgeors" type="update" due-to="Marius Volkhart">
         Conditionally perform status logging calculations in PluginRegistry.
       </action>