From d6294e579848e5c3cd179d495c3c668d5c642f32 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Fri, 24 Nov 2023 14:00:25 +0100 Subject: [PATCH] parameterizable invocation gate delay Signed-off-by: Ceki Gulcu --- .../core/rolling/SizeAndTimeBasedFNATP.java | 7 +++--- .../rolling/SizeBasedTriggeringPolicy.java | 10 ++++---- .../util/AlternateExecutorServiceUtil.java | 24 +++++++++++++++++++ .../core/util/SimpleInvocationGate.java | 10 ++++---- .../rolling/SizeAndTimeBasedFNATP_Test.java | 3 ++- .../core/rolling/SizeBasedRollingTest.java | 3 ++- .../logback/core/util/InvocationGateTest.java | 4 ++-- 7 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 logback-core/src/main/java/ch/qos/logback/core/util/AlternateExecutorServiceUtil.java diff --git a/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java b/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java index ad968806ed..68694f5f6d 100755 --- a/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java +++ b/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java @@ -23,6 +23,7 @@ import ch.qos.logback.core.rolling.helper.CompressionMode; import ch.qos.logback.core.rolling.helper.FileFilterUtil; import ch.qos.logback.core.rolling.helper.SizeAndTimeBasedArchiveRemover; +import ch.qos.logback.core.util.Duration; import ch.qos.logback.core.util.FileSize; import ch.qos.logback.core.util.DefaultInvocationGate; import ch.qos.logback.core.util.InvocationGate; @@ -40,7 +41,7 @@ enum Usage { volatile int currentPeriodsCounter = 0; FileSize maxFileSize; - Integer checkIncrement = null; + Duration checkIncrement = null; static String MISSING_INT_TOKEN = "Missing integer token, that is %i, in FileNamePattern ["; static String MISSING_DATE_TOKEN = "Missing date token, that is %d, in FileNamePattern ["; @@ -185,11 +186,11 @@ private boolean checkSizeBasedTrigger(File activeFile, long currentTime) { return false; } - public Integer getCheckIncrement() { + public Duration getCheckIncrement() { return checkIncrement; } - public void setCheckIncrement(Integer checkIncrement) { + public void setCheckIncrement(Duration checkIncrement) { this.checkIncrement = checkIncrement; } diff --git a/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java b/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java index 631a0c3fea..6ef9bbcf7b 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java +++ b/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java @@ -15,6 +15,7 @@ import java.io.File; +import ch.qos.logback.core.util.Duration; import ch.qos.logback.core.util.FileSize; import ch.qos.logback.core.util.DefaultInvocationGate; import ch.qos.logback.core.util.InvocationGate; @@ -41,10 +42,7 @@ public class SizeBasedTriggeringPolicy extends TriggeringPolicyBase { FileSize maxFileSize = new FileSize(DEFAULT_MAX_FILE_SIZE); InvocationGate invocationGate = new SimpleInvocationGate(); - - - - Integer checkIncrement = null; + Duration checkIncrement = null; public SizeBasedTriggeringPolicy() { } @@ -72,11 +70,11 @@ public void setMaxFileSize(FileSize aMaxFileSize) { this.maxFileSize = aMaxFileSize; } - public Integer getCheckIncrement() { + public Duration getCheckIncrement() { return checkIncrement; } - public void setCheckIncrement(Integer checkIncrement) { + public void setCheckIncrement(Duration checkIncrement) { this.checkIncrement = checkIncrement; } } diff --git a/logback-core/src/main/java/ch/qos/logback/core/util/AlternateExecutorServiceUtil.java b/logback-core/src/main/java/ch/qos/logback/core/util/AlternateExecutorServiceUtil.java new file mode 100644 index 0000000000..d45f3a290c --- /dev/null +++ b/logback-core/src/main/java/ch/qos/logback/core/util/AlternateExecutorServiceUtil.java @@ -0,0 +1,24 @@ +/* + * Logback: the reliable, generic, fast and flexible logging framework. + * Copyright (C) 1999-2023, QOS.ch. All rights reserved. + * + * This program and the accompanying materials are dual-licensed under + * either the terms of the Eclipse Public License v1.0 as published by + * the Eclipse Foundation + * + * or (per the licensee's choosing) + * + * under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation. + */ + +package ch.qos.logback.core.util; + +import java.util.concurrent.ExecutorService; + +public class AlternateExecutorServiceUtil { + + static public ExecutorService newThreadPoolExecutor() { + return ExecutorServiceUtil.newThreadPoolExecutor(); + } +} diff --git a/logback-core/src/main/java/ch/qos/logback/core/util/SimpleInvocationGate.java b/logback-core/src/main/java/ch/qos/logback/core/util/SimpleInvocationGate.java index 5166853dbd..b8dd6b58bb 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/util/SimpleInvocationGate.java +++ b/logback-core/src/main/java/ch/qos/logback/core/util/SimpleInvocationGate.java @@ -29,14 +29,16 @@ public class SimpleInvocationGate implements InvocationGate { //volatile long next = 0; AtomicLong atomicNext = new AtomicLong(0); - final long increment; - final public static int DEFAULT_INCREMENT = 10_000; + final Duration increment; + + // 60 seconds by default + final public static Duration DEFAULT_INCREMENT = Duration.buildBySeconds(60); public SimpleInvocationGate() { this(DEFAULT_INCREMENT); } - public SimpleInvocationGate(int anIncrement) { + public SimpleInvocationGate(Duration anIncrement) { this.increment = anIncrement; } @@ -47,7 +49,7 @@ public boolean isTooSoon(long currentTime) { long localNext = atomicNext.get(); if (currentTime >= localNext) { - long next2 = currentTime+increment; + long next2 = currentTime+increment.getMilliseconds(); // if success, we were able to set the variable, otherwise some other thread beat us to it boolean success = atomicNext.compareAndSet(localNext, next2); // while we have crossed 'next', the other thread already returned true. There is diff --git a/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java b/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java index ca11cfc3d4..bdc333262e 100755 --- a/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java +++ b/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java @@ -20,6 +20,7 @@ import java.util.concurrent.ExecutionException; import java.util.function.UnaryOperator; +import ch.qos.logback.core.util.Duration; import ch.qos.logback.core.util.StatusPrinter; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -61,7 +62,7 @@ private void initRollingFileAppender(RollingFileAppender rfa, String fil private void initPolicies(RollingFileAppender rfa, TimeBasedRollingPolicy tbrp, String filenamePattern, int sizeThreshold, long givenTime, long lastCheck) { sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP(); - sizeAndTimeBasedFNATP.setCheckIncrement(10); + sizeAndTimeBasedFNATP.setCheckIncrement(Duration.buildByMilliseconds(10)); tbrp.setContext(context); sizeAndTimeBasedFNATP.setMaxFileSize(new FileSize(sizeThreshold)); tbrp.setTimeBasedFileNamingAndTriggeringPolicy(sizeAndTimeBasedFNATP); diff --git a/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java b/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java index 4778c23b9a..94e5e87f51 100755 --- a/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java +++ b/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java @@ -16,6 +16,7 @@ import java.io.IOException; import java.util.List; +import ch.qos.logback.core.util.Duration; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -71,7 +72,7 @@ void generic(String testName, String fileName, String filenamePattern, List