Skip to content

Remove JMX support #2228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import java.util.function.LongSupplier;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.ThreadContextTestAccess;
import org.apache.logging.log4j.core.impl.Log4jContextFactory;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
import org.apache.logging.log4j.core.selector.ContextSelector;
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
Expand Down Expand Up @@ -157,19 +157,22 @@ private static void runTest(

final Logger log = context.getLogger("com.foo.Bar");
final String loggerContextName = context.getClass().getSimpleName();
RingBufferAdmin ring;
final LongSupplier remainingCapacity;
if (context instanceof AsyncLoggerContext) {
ring = ((AsyncLoggerContext) context).createRingBufferAdmin();
remainingCapacity =
((AsyncLoggerContext) context).getAsyncLoggerDisruptor().getRingBuffer()::remainingCapacity;
} else {
ring = ((AsyncLoggerConfig) log.get()).createRingBufferAdmin("");
remainingCapacity =
((AsyncLoggerConfigDisruptor) ((AsyncLoggerConfig) log.get()).getAsyncLoggerConfigDelegate())
.getRingBuffer()::remainingCapacity;
}

for (int i = 0; i < LINE_COUNT; i++) {
// buffer may be full
if (i >= 128) {
waitAtMost(500, TimeUnit.MILLISECONDS)
.pollDelay(10, TimeUnit.MILLISECONDS)
.until(() -> ring.getRemainingCapacity() > 0);
.until(() -> remainingCapacity.getAsLong() > 0);
}
if ((i & 1) == 1) {
ThreadContext.put("count", String.valueOf(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;

import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.RingBuffer;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Stack;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
Expand All @@ -36,7 +37,6 @@
import org.apache.logging.log4j.core.appender.AsyncAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
import org.apache.logging.log4j.core.util.Constants;
import org.apache.logging.log4j.status.StatusData;
import org.apache.logging.log4j.status.StatusLogger;
Expand Down Expand Up @@ -234,29 +234,19 @@ static List<String> getMessages(final List<LogEvent> logEvents) {
}

static long asyncRemainingCapacity(final Logger logger) {
if (logger instanceof AsyncLogger) {
try {
final Field f = field(AsyncLogger.class, "loggerDisruptor");
return ((AsyncLoggerDisruptor) f.get(logger))
.getDisruptor()
.getRingBuffer()
.remainingCapacity();
} catch (final Exception ex) {
throw new RuntimeException(ex);
}
if (logger instanceof AsyncLogger asyncLogger) {
return Optional.ofNullable(asyncLogger.getAsyncLoggerDisruptor())
.map(AsyncLoggerDisruptor::getRingBuffer)
.map(RingBuffer::remainingCapacity)
.orElse(0L);
} else {
final LoggerConfig loggerConfig = ((org.apache.logging.log4j.core.Logger) logger).get();
if (loggerConfig instanceof AsyncLoggerConfig) {
try {
final Object delegate =
field(AsyncLoggerConfig.class, "delegate").get(loggerConfig);
return ((Disruptor) field(AsyncLoggerConfigDisruptor.class, "disruptor")
.get(delegate))
.getRingBuffer()
.remainingCapacity();
} catch (final Exception ex) {
throw new RuntimeException(ex);
}
if (loggerConfig instanceof AsyncLoggerConfig asyncLoggerConfig) {
return Optional.ofNullable(
(AsyncLoggerConfigDisruptor) asyncLoggerConfig.getAsyncLoggerConfigDelegate())
.map(AsyncLoggerConfigDisruptor::getRingBuffer)
.map(RingBuffer::remainingCapacity)
.orElse(0L);
} else {
final Appender async = loggerConfig.getAppenders().get("async");
if (async instanceof AsyncAppender) {
Expand Down Expand Up @@ -287,26 +277,25 @@ protected static void assertAsyncAppender(final LoggerContext ctx) {

protected static void assertAsyncLogger(final LoggerContext ctx, final int expectedBufferSize) {
assertThat(ctx).isInstanceOf(AsyncLoggerContext.class);
final RingBufferAdmin ringBufferAdmin = ((AsyncLoggerContext) ctx).createRingBufferAdmin();
assertThat(ringBufferAdmin.getRemainingCapacity()).isEqualTo(expectedBufferSize);
assertThat(((AsyncLoggerContext) ctx)
.getAsyncLoggerDisruptor()
.getRingBuffer()
.getBufferSize())
.isEqualTo(expectedBufferSize);

final Configuration config = ctx.getConfiguration();
assertThat(config).isNotNull();
assertThat(config.getRootLogger()).isNotInstanceOf(AsyncLoggerConfig.class);
}

protected static void assertAsyncLoggerConfig(final LoggerContext ctx, final int expectedBufferSize)
throws ReflectiveOperationException {
protected static void assertAsyncLoggerConfig(final LoggerContext ctx, final int expectedBufferSize) {
assertThat(ctx).isNotInstanceOf(AsyncLoggerContext.class);

final Configuration config = ctx.getConfiguration();
assertThat(config).isNotNull();
assertThat(config.getRootLogger()).isInstanceOf(AsyncLoggerConfig.class);
final AsyncLoggerConfigDisruptor disruptorWrapper =
(AsyncLoggerConfigDisruptor) config.getAsyncLoggerConfigDelegate();
final Disruptor<?> disruptor = (Disruptor<?>)
field(AsyncLoggerConfigDisruptor.class, "disruptor").get(disruptorWrapper);
assertThat(disruptor.getBufferSize()).isEqualTo(expectedBufferSize);
final AsyncLoggerConfigDisruptor disruptor = (AsyncLoggerConfigDisruptor) config.getAsyncLoggerConfigDelegate();
assertThat(disruptor.getRingBuffer().getBufferSize()).isEqualTo(expectedBufferSize);
}

protected static void assertFormatMessagesInBackground() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.apache.logging.log4j.core.config.NullConfiguration;
import org.apache.logging.log4j.core.config.Reconfigurable;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.jmx.Server;
import org.apache.logging.log4j.core.util.Cancellable;
import org.apache.logging.log4j.core.util.ExecutorServices;
import org.apache.logging.log4j.core.util.NetUtils;
Expand Down Expand Up @@ -436,13 +435,7 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
}

this.setStopping();
String name = getName();
try {
Server.unregisterLoggerContext(name); // LOG4J2-406, LOG4J2-500
} catch (final LinkageError | Exception e) {
// LOG4J2-1506 Hello Android, GAE
LOGGER.error("Unable to unregister MBeans", e);
}

if (shutdownCallback != null) {
shutdownCallback.cancel();
shutdownCallback = null;
Expand Down Expand Up @@ -736,13 +729,6 @@ public Configuration setConfiguration(final Configuration config) {
notifyConfigurationStopped(prev);
}

try {
Server.reregisterMBeansAfterReconfigure();
} catch (final LinkageError | Exception e) {
// LOG4J2-716: Android has no java.lang.management
LOGGER.error("Could not reconfigure JMX", e);
}

return prev;
} finally {
configLock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.impl.LogEventFactory;
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
import org.apache.logging.log4j.plugins.Configurable;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.plugins.PluginFactory;
Expand Down Expand Up @@ -210,17 +209,6 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
return true;
}

/**
* Creates and returns a new {@code RingBufferAdmin} that instruments the
* ringbuffer of this {@code AsyncLoggerConfig}.
*
* @param contextName name of the {@code LoggerContext}
* @return a new {@code RingBufferAdmin} that instruments the ringbuffer
*/
public RingBufferAdmin createRingBufferAdmin(final String contextName) {
return delegate.createRingBufferAdmin(contextName, getName());
}

// Note: for asynchronous loggers, includeLocation default is FALSE
protected static boolean includeLocation(final String includeLocationConfigValue) {
return Boolean.parseBoolean(includeLocationConfigValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,13 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.LogEventFactory;
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;

/**
* Encapsulates the mechanism used to log asynchronously. There is one delegate per configuration, which is shared by
* all AsyncLoggerConfig objects in the configuration.
*/
public interface AsyncLoggerConfigDelegate {

/**
* Creates and returns a new {@code RingBufferAdmin} that instruments the ringbuffer of this
* {@code AsyncLoggerConfig}.
*
* @param contextName name of the {@code LoggerContext}
* @param loggerConfigName name of the logger config
* @return the RingBufferAdmin that instruments the ringbuffer
*/
RingBufferAdmin createRingBufferAdmin(final String contextName, final String loggerConfigName);

/**
* Returns the {@code EventRoute} for the event with the specified level.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.apache.logging.log4j.core.impl.LogEventFactory;
import org.apache.logging.log4j.core.impl.MutableLogEvent;
import org.apache.logging.log4j.core.impl.ReusableLogEventFactory;
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
import org.apache.logging.log4j.core.util.Log4jThread;
import org.apache.logging.log4j.core.util.Log4jThreadFactory;
import org.apache.logging.log4j.core.util.Throwables;
Expand Down Expand Up @@ -424,14 +423,8 @@ private LogEvent ensureImmutable(final LogEvent event) {
return result;
}

/*
* (non-Javadoc)
*
* @see org.apache.logging.log4j.core.async.AsyncLoggerConfigDelegate#createRingBufferAdmin(java.lang.String,
* java.lang.String)
*/
@Override
public RingBufferAdmin createRingBufferAdmin(final String contextName, final String loggerConfigName) {
return RingBufferAdmin.forAsyncLoggerConfig(disruptor.getRingBuffer(), contextName, loggerConfigName);
// package-protected for tests
RingBuffer<Log4jEventWrapper> getRingBuffer() {
return disruptor.getRingBuffer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.plugins.di.ConfigurableInstanceFactory;
import org.apache.logging.log4j.status.StatusLogger;
Expand Down Expand Up @@ -131,13 +130,8 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
return true;
}

/**
* Creates and returns a new {@code RingBufferAdmin} that instruments the ringbuffer of the {@code AsyncLogger}
* objects in this {@code LoggerContext}.
*
* @return a new {@code RingBufferAdmin} that instruments the ringbuffer
*/
public RingBufferAdmin createRingBufferAdmin() {
return loggerDisruptor.createRingBufferAdmin(getName());
// package-protected for tests
AsyncLoggerDisruptor getAsyncLoggerDisruptor() {
return loggerDisruptor;
}
}
Loading