Skip to content

Commit

Permalink
parameterizable invocation gate delay
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Nov 27, 2023
1 parent 3b91f6d commit d6294e5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 [";
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,10 +42,7 @@ public class SizeBasedTriggeringPolicy<E> extends TriggeringPolicyBase<E> {

FileSize maxFileSize = new FileSize(DEFAULT_MAX_FILE_SIZE);
InvocationGate invocationGate = new SimpleInvocationGate();



Integer checkIncrement = null;
Duration checkIncrement = null;

public SizeBasedTriggeringPolicy() {
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +62,7 @@ private void initRollingFileAppender(RollingFileAppender<Object> rfa, String fil
private void initPolicies(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp,
String filenamePattern, int sizeThreshold, long givenTime, long lastCheck) {
sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<Object>();
sizeAndTimeBasedFNATP.setCheckIncrement(10);
sizeAndTimeBasedFNATP.setCheckIncrement(Duration.buildByMilliseconds(10));
tbrp.setContext(context);
sizeAndTimeBasedFNATP.setMaxFileSize(new FileSize(sizeThreshold));
tbrp.setTimeBasedFileNamingAndTriggeringPolicy(sizeAndTimeBasedFNATP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,7 +72,7 @@ void generic(String testName, String fileName, String filenamePattern, List<Stri
initRFA(randomOutputDir + fileName);

sizeBasedTriggeringPolicy.setMaxFileSize(new FileSize(100));
sizeBasedTriggeringPolicy.setCheckIncrement(50);
sizeBasedTriggeringPolicy.setCheckIncrement(Duration.buildByMilliseconds(50));
fwrp.setMinIndex(0);
fwrp.setFileNamePattern(randomOutputDir + filenamePattern);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class InvocationGateTest {
@Test
public void smoke() {
InvocationGate sig = new SimpleInvocationGate();
int currentTime = SimpleInvocationGate.DEFAULT_INCREMENT + 1;
long currentTime = SimpleInvocationGate.DEFAULT_INCREMENT.getMilliseconds() + 1;
assertFalse(sig.isTooSoon(currentTime));
currentTime++;
assertTrue(sig.isTooSoon(currentTime));
Expand All @@ -52,7 +52,7 @@ public void smoke() {
@Disabled
@Test
void checkThreadSafety() throws InterruptedException {
InvocationGate sig = new SimpleInvocationGate(1);
InvocationGate sig = new SimpleInvocationGate(Duration.buildByMilliseconds(1));

long initialTime = currentTime.get();
sig.isTooSoon(initialTime); // sync invocation gate with current time
Expand Down

0 comments on commit d6294e5

Please sign in to comment.