Skip to content

Commit

Permalink
[UNDERTOW-1794] add configurable delay/retry on close
Browse files Browse the repository at this point in the history
  • Loading branch information
baranowb committed Apr 26, 2023
1 parent 4c91cd6 commit 8ff8540
Showing 1 changed file with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class DefaultAccessLogReceiver implements AccessLogReceiver, Runnable, Cl
//0 = not running
//1 = queued
//2 = running
//3 = closing
//3 = closing/closed
@SuppressWarnings("unused")
private volatile int state = 0;

Expand All @@ -80,6 +80,8 @@ public class DefaultAccessLogReceiver implements AccessLogReceiver, Runnable, Cl
private boolean initialRun = true;
private final boolean rotate;
private final LogFileHeaderGenerator fileHeaderGenerator;
private final int closeRetryCount;
private final int closeRetryDelay;

public DefaultAccessLogReceiver(final Executor logWriteExecutor, final File outputDirectory, final String logBaseName) {
this(logWriteExecutor, outputDirectory.toPath(), logBaseName, null);
Expand All @@ -102,10 +104,10 @@ public DefaultAccessLogReceiver(final Executor logWriteExecutor, final Path outp
}

public DefaultAccessLogReceiver(final Executor logWriteExecutor, final Path outputDirectory, final String logBaseName, final String logNameSuffix, boolean rotate) {
this(logWriteExecutor, outputDirectory, logBaseName, logNameSuffix, rotate, null);
this(logWriteExecutor, outputDirectory, logBaseName, logNameSuffix, rotate, null, 60, 50);
}

private DefaultAccessLogReceiver(final Executor logWriteExecutor, final Path outputDirectory, final String logBaseName, final String logNameSuffix, boolean rotate, LogFileHeaderGenerator fileHeader) {
private DefaultAccessLogReceiver(final Executor logWriteExecutor, final Path outputDirectory, final String logBaseName, final String logNameSuffix, boolean rotate, LogFileHeaderGenerator fileHeader, final int closeRetryCount, final int closeRetryDelay) {
this.logWriteExecutor = logWriteExecutor;
this.outputDirectory = outputDirectory;
this.logBaseName = logBaseName;
Expand All @@ -114,6 +116,8 @@ private DefaultAccessLogReceiver(final Executor logWriteExecutor, final Path out
this.logNameSuffix = (logNameSuffix != null) ? logNameSuffix : DEFAULT_LOG_SUFFIX;
this.pendingMessages = new ConcurrentLinkedDeque<>();
this.defaultLogFile = outputDirectory.resolve(logBaseName + this.logNameSuffix);
this.closeRetryCount = closeRetryCount;
this.closeRetryDelay = closeRetryDelay;
calculateChangeOverPoint();
}

Expand Down Expand Up @@ -351,10 +355,10 @@ public void close() throws IOException {
return;
}
// either failed race to 1->3 or we were in 2. We have to wait here sometime.
// wait ~1s, if situation does not clear up, try dumping stuff
for(int i=0; i<20;i++) {
// wait ~3s(by default), if situation does not clear up, try dumping stuff
for(int i=0; i<this.closeRetryCount;i++) {
try {
Thread.currentThread().sleep(50);
Thread.currentThread().sleep(this.closeRetryDelay);
} catch (InterruptedException e) {
UndertowLogger.ROOT_LOGGER.closeInterrupted(e);
break;
Expand All @@ -363,7 +367,10 @@ public void close() throws IOException {
break;
}
}
this.stateUpdater.set(this, 3);
final int tempEndState = this.stateUpdater.getAndSet(this, 3);
if(tempEndState == 2) {
UndertowLogger.ROOT_LOGGER.accessLogWorkerNoTermination();
}
flushAndTerminate();
}
}
Expand Down Expand Up @@ -402,6 +409,8 @@ public static class Builder {
private String logNameSuffix;
private boolean rotate;
private LogFileHeaderGenerator logFileHeaderGenerator;
private int closeRetryCount = 60;
private int closeRetryDelay = 50;

public Executor getLogWriteExecutor() {
return logWriteExecutor;
Expand Down Expand Up @@ -457,8 +466,30 @@ public Builder setLogFileHeaderGenerator(LogFileHeaderGenerator logFileHeaderGen
return this;
}

public int getCloseRetryCount() {
return closeRetryCount;
}

public Builder setCloseRetryCount(int closeRetryCount) {
this.closeRetryCount = closeRetryCount;
return this;
}

public int getCloseRetryDelay() {
return closeRetryDelay;
}

/**
* Delay in ms between retrying poll on state to check for proper termination of worker
* @param closeRetryDelay
*/
public Builder setCloseRetryDelay(int closeRetryDelay) {
this.closeRetryDelay = closeRetryDelay;
return this;
}

public DefaultAccessLogReceiver build() {
return new DefaultAccessLogReceiver(logWriteExecutor, outputDirectory, logBaseName, logNameSuffix, rotate, logFileHeaderGenerator);
return new DefaultAccessLogReceiver(logWriteExecutor, outputDirectory, logBaseName, logNameSuffix, rotate, logFileHeaderGenerator, closeRetryCount, closeRetryDelay);
}
}
}

0 comments on commit 8ff8540

Please sign in to comment.