Skip to content

Commit

Permalink
payara#3506 prevent processor wastage when not logging to file. Only …
Browse files Browse the repository at this point in the history
…start pump thread if logging to file. Store into queue only if logging to file.
  • Loading branch information
svendiedrichsen committed Dec 10, 2018
1 parent 53d3d98 commit 3c9c34a
Showing 1 changed file with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,6 @@ public void postConstruct() {
int otherFormatter = 0;
boolean mustRotate = false;

String propertyValue = manager.getProperty(className + ".logtoFile");
logToFile = true;

if (propertyValue != null) {
logToFile = Boolean.parseBoolean(propertyValue);
}

try (BufferedReader br = new BufferedReader(new FileReader(logFile))) {
while ((strLine = br.readLine()) != null) {
strLine = strLine.trim();
Expand Down Expand Up @@ -242,8 +235,12 @@ public void postConstruct() {
currentFileHandlerFormatter = "com.sun.enterprise.server.logging.UniformLogFormatter";
}

// start the Queue consumer thread.
initializePump();
String propertyValue = manager.getProperty(className + ".logtoFile");
boolean logToFile = true;
if (propertyValue != null) {
logToFile = Boolean.parseBoolean(propertyValue);
}
setLogToFile(logToFile);

logRecord.setParameters(new Object[]{Version.getFullVersion()});
logRecord.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
Expand Down Expand Up @@ -569,18 +566,24 @@ private void configureUniformLogFormatter(String excludeFields, boolean multiLin
}

void initializePump() {
pumpFuture = payaraExecutorService.submit(
() -> {
while (!done.isSignalled()) {
try {
log();
} catch (Exception e) {
// GLASSFISH-19125
// Continue the loop without exiting
if (pumpFuture != null) {
pumpFuture.cancel(true);
pumpFuture = null;
}
if (logToFile) {
pumpFuture = payaraExecutorService.submit(
() -> {
while (!done.isSignalled()) {
try {
log();
} catch (Exception e) {
// GLASSFISH-19125
// Continue the loop without exiting
}
}
}
}
);
);
}
}

@Override
Expand All @@ -590,7 +593,9 @@ public void preDestroy() {
LogFacade.LOGGING_LOGGER.fine("Logger handler killed");
}
done.tryReleaseShared(1);
pumpFuture.cancel(true);
if (pumpFuture != null) {
pumpFuture.cancel(true);
}

// drain and return
final int size = pendingRecords.size();
Expand Down Expand Up @@ -913,17 +918,17 @@ record = pendingRecords.take();
for (int j = 0; j < msgs; j++) {
super.publish(v.get(j));
}
}
flush();
if ((rotationRequested.get())
|| ((limitForFileRotation > 0)
&& (meter.written >= limitForFileRotation))) {
// If we have written more than the limit set for the
// file, or rotation requested from the Timer Task or LogMBean
// start fresh with a new file after renaming the old file.
synchronized (rotationLock) {
rotate();
rotationRequested.set(false);
flush();
if ((rotationRequested.get())
|| ((limitForFileRotation > 0)
&& (meter.written >= limitForFileRotation))) {
// If we have written more than the limit set for the
// file, or rotation requested from the Timer Task or LogMBean
// start fresh with a new file after renaming the old file.
synchronized (rotationLock) {
rotate();
rotationRequested.set(false);
}
}
}
}
Expand Down Expand Up @@ -961,14 +966,18 @@ public void publish(LogRecord record) {
recordWrapper.setThreadName(Thread.currentThread().getName());
}

try {
pendingRecords.add(recordWrapper);
} catch (IllegalStateException e) {
// queue is full, start waiting.
if (logToFile) {
try {
pendingRecords.put(recordWrapper);
} catch (InterruptedException e1) {
// too bad, record is lost...
pendingRecords.add(recordWrapper);
} catch (IllegalStateException e) {
// queue is full, start waiting.
new ErrorManager().error("GFFileHandler: Queue full. Waiting to submit.", e, ErrorManager.GENERIC_FAILURE);
try {
pendingRecords.put(recordWrapper);
} catch (InterruptedException e1) {
// too bad, record is lost...
new ErrorManager().error("GFFileHandler: Waiting was interrupted. Log record lost.", e1, ErrorManager.GENERIC_FAILURE);
}
}
}

Expand Down Expand Up @@ -1041,6 +1050,7 @@ public synchronized void setLogFile(String fileName) {

public synchronized void setLogToFile(boolean logToFile) {
this.logToFile = logToFile;
initializePump();
}

public synchronized void setRotationOnDateChange(boolean rotationOnDateChange) {
Expand Down

0 comments on commit 3c9c34a

Please sign in to comment.