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 Feb 17, 2021
1 parent b015cb5 commit 7beb8be
Showing 1 changed file with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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 @@ -81,6 +81,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 @@ -103,10 +105,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 @@ -115,6 +117,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 @@ -352,10 +356,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 @@ -364,7 +368,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 @@ -403,6 +410,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 @@ -458,8 +467,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 7beb8be

Please sign in to comment.