Skip to content

Commit

Permalink
Base logging on some generic condition
Browse files Browse the repository at this point in the history
  • Loading branch information
dlmarion committed Apr 23, 2024
1 parent 8fb8e6d commit f679cd3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ public enum Property {
GENERAL_SIMPLETIMER_THREADPOOL_SIZE("general.server.simpletimer.threadpool.size", "1",
PropertyType.COUNT, "The number of threads to use for server-internal scheduled tasks.",
"1.7.0"),
GENERAL_TIME_FREQUENCY_LOGGER_INTERVAL("general.logger.time.frequency.interval", "1m",
PropertyType.TIMEDURATION,
"Some classes can log items very frequently spamming the"
+ " logs with information. These classes may use a logger that logs only one"
+ " message per unit time. This property determines that unit time. A value of"
+ " zero disables this logic, but could produce a lot of log messages on all logs.",
"2.1.3"),
// If you update the default type, be sure to update the default used for initialization failures
// in VolumeManagerImpl
@Experimental
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.accumulo.core.logging;

import java.time.Duration;
import java.util.function.Supplier;

import org.slf4j.Logger;
import org.slf4j.Marker;
Expand All @@ -29,18 +29,37 @@
* Logger that wraps another Logger and only emits a log message once per the supplied duration.
*
*/
public class IntervalLogger extends AbstractLogger {
public class ConditionalLogger extends AbstractLogger {

public static Logger createTimeFrequencyLogger(Logger log, Supplier<Long> intervalNanos) {

final Supplier<Boolean> condition = new Supplier<>() {
private volatile long last = 0L;

@Override
public Boolean get() {
if (intervalNanos.get() == 0) {
return false;
} else if (System.nanoTime() - last > intervalNanos.get()) {
last = System.nanoTime();
return true;
} else {
return false;
}
}
};

return new ConditionalLogger(log, condition);
}

private static final long serialVersionUID = 1L;

private final Logger delegate;
private final long intervalNanos;

private volatile long last = 0L;
private final Supplier<Boolean> condition;

public IntervalLogger(Logger log, Duration interval) {
private ConditionalLogger(Logger log, Supplier<Boolean> condition) {
this.delegate = log;
this.intervalNanos = interval.toNanos();
this.condition = condition;
}

@Override
Expand Down Expand Up @@ -101,9 +120,8 @@ protected String getFullyQualifiedCallerName() {
@Override
protected void handleNormalizedLoggingCall(Level level, Marker marker, String messagePattern,
Object[] arguments, Throwable throwable) {
if (System.nanoTime() - last > intervalNanos) {
if (condition.get()) {
delegate.atLevel(level).addMarker(marker).setCause(throwable).log(messagePattern, arguments);
last = System.nanoTime();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -52,6 +53,7 @@
import org.apache.accumulo.core.data.TableId;
import org.apache.accumulo.core.fate.zookeeper.ZooReader;
import org.apache.accumulo.core.fate.zookeeper.ZooReaderWriter;
import org.apache.accumulo.core.logging.ConditionalLogger;
import org.apache.accumulo.core.metadata.schema.Ample;
import org.apache.accumulo.core.rpc.SslConnectionParams;
import org.apache.accumulo.core.singletons.SingletonReservation;
Expand Down Expand Up @@ -445,4 +447,12 @@ public AuditedSecurityOperation getSecurityOperation() {
return securityOperation.get();
}

public Logger createTimeFrequencyLogger(Logger log) {
return ConditionalLogger
.createTimeFrequencyLogger(log,
() -> Duration.ofMillis(
getConfiguration().getTimeInMillis(Property.GENERAL_TIME_FREQUENCY_LOGGER_INTERVAL))
.toNanos());
}

}

0 comments on commit f679cd3

Please sign in to comment.