vy commented on code in PR #2230:
URL: https://github.com/apache/logging-log4j2/pull/2230#discussion_r1464763541


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java:
##########
@@ -1178,4 +1152,28 @@ public NanoClock getNanoClock() {
     public void setNanoClock(final NanoClock nanoClock) {
         instanceFactory.registerBinding(NanoClock.KEY, () -> nanoClock);
     }
+
+    @Override
+    public void addExtension(ConfigurationExtension extension) {
+        Objects.requireNonNull(extension);
+        extensions = Arrays.copyOf(extensions, extensions.length + 1);
+        extensions[extensions.length - 1] = extension;

Review Comment:
   Why is `extensions` not of type `List`?



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java:
##########
@@ -256,4 +236,21 @@ default LogEventFactory getLogEventFactory() {
     default RecyclerFactory getRecyclerFactory() {
         return getComponent(Key.forClass(RecyclerFactory.class));
     }
+
+    /**
+     * Registers a new configuration extension.
+     *
+     * @param extension a configuration extension.
+     * @since 3.0
+     */
+    void addExtension(ConfigurationExtension extension);
+
+    /**
+     * Returns an extension of the given type.
+     *
+     * @param extensionType a type of extension,
+     * @return an extension the matches the given type.
+     * @since 3.0
+     */
+    <T extends ConfigurationExtension> T getExtension(Class<T> extensionType);

Review Comment:
   [In accordance with my comment above regarding suppressing unexpected 
multiple matches,] I would rather replace this with a simple 
`Collection<ConfigurationExtension> getExtensions()`.



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationExtension.java:
##########
@@ -0,0 +1,22 @@
+/*
+ * 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.config;
+
+/**
+ * A {@link ConfigurationExtension} is used to add new child elements to a 
{@link Configuration}.

Review Comment:
   Nit: Shall we avoid recursive definitions, please?
   ```suggestion
    * The contract to add new child elements to a {@link Configuration}.
   ```



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java:
##########
@@ -732,12 +705,12 @@ protected void doConfigure() {
                 final List<CustomLevelConfig> copy = new 
ArrayList<>(customLevels);
                 copy.add(child.getObject(CustomLevelConfig.class));
                 customLevels = copy;
-            } else if 
(child.isInstanceOf(AsyncWaitStrategyFactoryConfig.class)) {
-                AsyncWaitStrategyFactoryConfig awsfc = 
child.getObject(AsyncWaitStrategyFactoryConfig.class);
-                if (awsfc == null) {
-                    LOGGER.error("Unable to load 
AsyncWaitStrategyFactoryConfig");
+            } else if (child.isInstanceOf(ConfigurationExtension.class)) {
+                final ConfigurationExtension extension = 
child.getObject(ConfigurationExtension.class);
+                if (extension == null) {

Review Comment:
   Checking `Node.java`... Can this ever be null? That is, can 
`child.isInstanceOf(T.class) && child.getObject(T.class) == null` ever return 
`true`?



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorConfiguration.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.Objects;
+import java.util.concurrent.TimeUnit;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.AbstractLifeCycle;
+import org.apache.logging.log4j.core.config.ConfigurationExtension;
+import org.apache.logging.log4j.plugins.Configurable;
+import org.apache.logging.log4j.plugins.Plugin;
+import org.apache.logging.log4j.plugins.PluginAliases;
+import org.apache.logging.log4j.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.plugins.PluginFactory;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.LoaderUtil;
+
+@Configurable(printObject = true)
+@Plugin("Disruptor")
+@PluginAliases("AsyncWaitStrategyFactory")
+public final class DisruptorConfiguration extends AbstractLifeCycle implements 
ConfigurationExtension {
+
+    private static final Logger LOGGER = StatusLogger.getLogger();
+
+    private final AsyncWaitStrategyFactory waitStrategyFactory;
+    private AsyncLoggerConfigDisruptor loggerConfigDisruptor;
+
+    private DisruptorConfiguration(final AsyncWaitStrategyFactory 
waitStrategyFactory) {
+        this.waitStrategyFactory = waitStrategyFactory;
+    }
+
+    public AsyncWaitStrategyFactory getWaitStrategyFactory() {
+        return waitStrategyFactory;
+    }
+
+    public AsyncLoggerConfigDelegate getAsyncLoggerConfigDelegate() {
+        if (loggerConfigDisruptor == null) {
+            loggerConfigDisruptor = new 
AsyncLoggerConfigDisruptor(waitStrategyFactory);
+        }
+        return loggerConfigDisruptor;
+    }
+
+    @Override
+    public void start() {
+        if (loggerConfigDisruptor != null) {
+            LOGGER.info("Starting AsyncLoggerConfigDisruptor.");
+            loggerConfigDisruptor.start();
+        }
+        super.start();
+    }
+
+    @Override
+    public boolean stop(long timeout, TimeUnit timeUnit) {
+        if (loggerConfigDisruptor != null) {
+            LOGGER.info("Stopping AsyncLoggerConfigDisruptor.");
+            loggerConfigDisruptor.stop(timeout, timeUnit);
+        }
+        return super.stop(timeout, timeUnit);
+    }
+
+    @PluginFactory
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
+    public static final class Builder implements 
org.apache.logging.log4j.plugins.util.Builder<DisruptorConfiguration> {
+
+        @PluginBuilderAttribute("class")
+        private String factoryClassName;
+
+        @PluginBuilderAttribute
+        private String waitFactory;
+
+        public Builder setFactoryClassName(final String factoryClassName) {
+            this.factoryClassName = factoryClassName;
+            return this;
+        }
+
+        public Builder setWaitFactory(final String waitFactory) {
+            this.waitFactory = waitFactory;
+            return this;
+        }
+
+        @Override
+        public DisruptorConfiguration build() {
+            final String effectiveClassName = Objects.toString(waitFactory, 
factoryClassName);
+            final AsyncWaitStrategyFactory effectiveFactory;
+            if (effectiveClassName != null) {
+                effectiveFactory = 
createWaitStrategyFactory(effectiveClassName);
+            } else {
+                effectiveFactory = null;
+            }
+            if (effectiveFactory != null) {
+                LOGGER.info("Using configured AsyncWaitStrategy factory {}.", 
effectiveClassName);
+            } else {
+                LOGGER.info("Using default AsyncWaitStrategy factory.");
+            }

Review Comment:
   Shall we move this business logic to `createWaitStrategyFactory()`, that is, 
where it belongs?



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java:
##########
@@ -1178,4 +1152,28 @@ public NanoClock getNanoClock() {
     public void setNanoClock(final NanoClock nanoClock) {
         instanceFactory.registerBinding(NanoClock.KEY, () -> nanoClock);
     }
+
+    @Override
+    public void addExtension(ConfigurationExtension extension) {
+        Objects.requireNonNull(extension);
+        extensions = Arrays.copyOf(extensions, extensions.length + 1);
+        extensions[extensions.length - 1] = extension;
+    }
+
+    @Override
+    public <T extends ConfigurationExtension> T getExtension(Class<T> 
extensionType) {
+        T result = null;
+        for (final ConfigurationExtension extension : extensions) {
+            if (extensionType.isInstance(extension)) {
+                if (result == null) {
+                    result = (T) extension;
+                } else {
+                    LOGGER.warn(
+                            "Multiple configuration elements found for type 
{}. Only the first will be used.",
+                            extensionType.getName());
+                }
+            }
+        }
+        return result;
+    }

Review Comment:
   Suppressing unexpected conditions is not a good idea, IMO. I would rather 
return the `extensions` collection as is and let the call-site deal with the 
issue of multiple matches or filtering in general. We don't expect too many 
places where this filtering is needed – there are 7 call-sites, to be precise. 
Hence, in the very first iteration, let them do the filtering themselves.



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorConfiguration.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.Objects;
+import java.util.concurrent.TimeUnit;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.AbstractLifeCycle;
+import org.apache.logging.log4j.core.config.ConfigurationExtension;
+import org.apache.logging.log4j.plugins.Configurable;
+import org.apache.logging.log4j.plugins.Plugin;
+import org.apache.logging.log4j.plugins.PluginAliases;
+import org.apache.logging.log4j.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.plugins.PluginFactory;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.LoaderUtil;
+
+@Configurable(printObject = true)
+@Plugin("Disruptor")
+@PluginAliases("AsyncWaitStrategyFactory")
+public final class DisruptorConfiguration extends AbstractLifeCycle implements 
ConfigurationExtension {
+
+    private static final Logger LOGGER = StatusLogger.getLogger();
+
+    private final AsyncWaitStrategyFactory waitStrategyFactory;
+    private AsyncLoggerConfigDisruptor loggerConfigDisruptor;
+
+    private DisruptorConfiguration(final AsyncWaitStrategyFactory 
waitStrategyFactory) {
+        this.waitStrategyFactory = waitStrategyFactory;
+    }
+
+    public AsyncWaitStrategyFactory getWaitStrategyFactory() {
+        return waitStrategyFactory;
+    }
+
+    public AsyncLoggerConfigDelegate getAsyncLoggerConfigDelegate() {
+        if (loggerConfigDisruptor == null) {
+            loggerConfigDisruptor = new 
AsyncLoggerConfigDisruptor(waitStrategyFactory);
+        }

Review Comment:
   1. This code is not thread-safe.
   2. Why do we do late/on-demand initialization?
   3. If late initialization is *not* a necessity, why don't we mark the field 
`final` and initialize it in ctor?
   4. If late initialization is necessary, why don't we use a `Lazy` instead?



-- 
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: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to