Skip to content

Commit

Permalink
PAYARA-4118 Sppedup Payara shutdown by having GFFileHandler use its o…
Browse files Browse the repository at this point in the history
…wn thread

GFFileHandler no longer uses PayaraExecutorService
This is a partial revert of f60de6a / #3437
  • Loading branch information
Cousjava committed Oct 4, 2019
1 parent b689f53 commit 4ddcf7f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 125 deletions.
13 changes: 7 additions & 6 deletions nucleus/core/logging/pom.xml
Expand Up @@ -93,12 +93,6 @@
<artifactId>jakarta.json-api</artifactId>
<version>${jsonp.version}</version>
</dependency>
<dependency>
<groupId>fish.payara.server.internal.payara-modules</groupId>
<artifactId>payara-executor-service</artifactId>
<version>${project.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand All @@ -108,6 +102,13 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
Expand Down
Expand Up @@ -51,8 +51,7 @@
import com.sun.enterprise.util.io.FileUtils;
import com.sun.enterprise.v3.logging.AgentFormatterDelegate;
import fish.payara.enterprise.server.logging.JSONLogFormatter;
import fish.payara.enterprise.server.logging.PayaraNotificationLogRotationTimer;
import fish.payara.nucleus.executorservice.PayaraExecutorService;
import fish.payara.enterprise.server.logging.PayaraNotificationLogRotationTimer;;
import java.io.*;
import java.security.PrivilegedAction;
import java.text.FieldPosition;
Expand Down Expand Up @@ -120,9 +119,6 @@ public class GFFileHandler extends StreamHandler implements
@Inject
private ServiceLocator habitat;

@Inject
private PayaraExecutorService payaraExecutorService;

// This is a OutputStream to keep track of number of bytes
// written out to the stream
private MeteredStream meter;
Expand Down Expand Up @@ -175,23 +171,19 @@ public class GFFileHandler extends StreamHandler implements
public static final int MINIMUM_ROTATION_LIMIT_VALUE = 500*1000;

private BooleanLatch done = new BooleanLatch();

private boolean dayBasedFileRotation = false;

private List<LogEventListener> logEventListeners = new ArrayList<>();

private Future<?> pumpFuture;
private Thread pump;

protected String logFileProperty = "";
private final LogManager manager = LogManager.getLogManager();
private final String className = getClass().getName();
private static final String GF_FILE_HANDLER = GFFileHandler.class.getCanonicalName() ;
private LogRecord logRecord = new LogRecord(Level.INFO, LogFacade.GF_VERSION_INFO);

void setPayaraExecutorService(PayaraExecutorService payaraExecutorService) {
this.payaraExecutorService = payaraExecutorService;
}

@Override
public void postConstruct() {

Expand Down Expand Up @@ -433,12 +425,10 @@ private void rotationOnDateChange() {

if (className.equals(GF_FILE_HANDLER)) {
LogRotationTimer.getInstance().startTimer(
payaraExecutorService.getUnderlyingScheduledExecutorService(),
new LogRotationTimerTask(rotationTask,
rotationTimeLimitValue / 60000));
} else {
PayaraNotificationLogRotationTimer.getInstance().startTimer(
payaraExecutorService.getUnderlyingScheduledExecutorService(),
new LogRotationTimerTask(rotationTask,
rotationTimeLimitValue / 60000));
}
Expand All @@ -453,17 +443,15 @@ private void rotationOnTimeLimit() {

if (className.equals(GF_FILE_HANDLER)) {
LogRotationTimer.getInstance().startTimer(
payaraExecutorService.getUnderlyingScheduledExecutorService(),
new LogRotationTimerTask(rotationTask,
rotationTimeLimitValue));
} else {
PayaraNotificationLogRotationTimer.getInstance().startTimer(
payaraExecutorService.getUnderlyingScheduledExecutorService(),
new LogRotationTimerTask(rotationTask,
rotationTimeLimitValue));
}
}
}
}

private void rotationOnFileSizeLimit(String propertyValue) {
try {
Expand Down Expand Up @@ -585,14 +573,10 @@ private void configureUniformLogFormatter(String excludeFields, boolean multiLin
}

void initializePump() {
if (pumpFuture != null) {
pumpFuture.cancel(true);
pumpFuture = null;
}
if (logToFile) {
pumpFuture = payaraExecutorService.submit(
() -> {
while (!done.isSignalled() && logToFile) {
pump = new Thread() {
@Override
public void run() {
while (!done.isSignalled()) {
try {
log();
} catch (Exception e) {
Expand All @@ -601,11 +585,10 @@ void initializePump() {
}
}
}
);
} else {
drainAllPendingRecords();
flush();
}
};
pump.setName("GFFileHandler log pump");
pump.setDaemon(true);
pump.start();
}

@Override
Expand All @@ -631,8 +614,8 @@ public void preDestroy() {
}

done.tryReleaseShared(1);
if (pumpFuture != null) {
pumpFuture.cancel(true);
if (pump != null) {
pump.interrupt();
}

// drain and return all
Expand Down Expand Up @@ -934,18 +917,18 @@ private void restartTimeBasedLogRotation() {
if (dayBasedFileRotation) {
if (className.equals(GF_FILE_HANDLER)) {
LogRotationTimer.getInstance()
.restartTimerForDayBasedRotation(payaraExecutorService.getUnderlyingScheduledExecutorService());
.restartTimerForDayBasedRotation();
} else {
PayaraNotificationLogRotationTimer.getInstance()
.restartTimerForDayBasedRotation(payaraExecutorService.getUnderlyingScheduledExecutorService());
.restartTimerForDayBasedRotation();
}
} else {
if (className.equals(GF_FILE_HANDLER)) {
LogRotationTimer.getInstance()
.restartTimer(payaraExecutorService.getUnderlyingScheduledExecutorService());
.restartTimer();
} else {
PayaraNotificationLogRotationTimer.getInstance()
.restartTimer(payaraExecutorService.getUnderlyingScheduledExecutorService());
.restartTimer();
}
}

Expand Down
Expand Up @@ -37,75 +37,64 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018] [Payara Foundation and/or its affiliates]
// Portions Copyright [2018-2019] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.server.logging;

import java.util.concurrent.ScheduledExecutorService;
import java.util.Timer;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class LogRotationTimer {

private LogRotationTimerTask rotationTimerTask;
private ScheduledFuture<?> logRotationFuture;

private static LogRotationTimer instance = new LogRotationTimer();
private Timer rotationTimer;

private LogRotationTimer() {}
private LogRotationTimer() {
rotationTimer = new Timer("log-rotation-timer");
}

public static LogRotationTimer getInstance() {
return instance;
}

public void startTimer(ScheduledExecutorService scheduledExecutorService, LogRotationTimerTask timerTask) {
public void startTimer(LogRotationTimerTask timerTask) {
rotationTimerTask = timerTask;
logRotationFuture = scheduledExecutorService.schedule(
rotationTimerTask,
timerTask.getRotationTimerValue(),
TimeUnit.MILLISECONDS
);
rotationTimer.schedule(rotationTimerTask, timerTask.getRotationTimerValue());
}

public void stopTimer() {
if (logRotationFuture != null) {
logRotationFuture.cancel(false);
}
rotationTimer.cancel();
}

public void restartTimer(ScheduledExecutorService scheduledExecutorService) {
public void restartTimer() {
// We will restart the timer only if the timerTask is set which
// means user has set a value for LogRotation based on Time
if (logRotationFuture != null) {
logRotationFuture.cancel(false);
if (rotationTimerTask != null) {
rotationTimerTask.cancel();
rotationTimerTask = new LogRotationTimerTask(
// This is wierd, We need to have a fresh TimerTask object
// to reschedule the work.
rotationTimerTask.task,
rotationTimerTask.getRotationTimerValueInMinutes());
logRotationFuture = scheduledExecutorService.schedule(
rotationTimerTask,
rotationTimerTask.getRotationTimerValue(),
TimeUnit.MILLISECONDS);
rotationTimer.schedule(rotationTimerTask, rotationTimerTask.getRotationTimerValue());
}
}

public void restartTimerForDayBasedRotation(ScheduledExecutorService scheduledExecutorService) {
public void restartTimerForDayBasedRotation() {
// We will restart the timer only if the timerTask is set which
// means user has set a value for LogRotation based on Time
if (logRotationFuture != null) {
logRotationFuture.cancel(false);
if (rotationTimerTask != null) {
rotationTimerTask.cancel();
rotationTimerTask = new LogRotationTimerTask(
// This is wierd, We need to have a fresh TimerTask object
// to reschedule the work.
rotationTimerTask.task,
60 * 24
);
logRotationFuture = scheduledExecutorService.schedule(
rotationTimerTask,
1000 * 60 * 60 * 24,
TimeUnit.MILLISECONDS
);
rotationTimer.schedule(rotationTimerTask, 1000 * 60 * 60 * 24);
}
}

Expand Down
Expand Up @@ -37,13 +37,14 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018] [Payara Foundation and/or its affiliates]
// Portions Copyright [2018-2019] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.server.logging;

import java.util.TimerTask;
import org.glassfish.api.logging.Task;

public class LogRotationTimerTask implements Runnable {
public class LogRotationTimerTask extends TimerTask {

private long timerValue;
public Task task;
Expand Down
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2019] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -41,10 +41,9 @@
package fish.payara.enterprise.server.logging;

import com.sun.enterprise.server.logging.LogRotationTimerTask;
import java.util.Timer;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
*
Expand All @@ -54,59 +53,48 @@ public class PayaraNotificationLogRotationTimer {

private LogRotationTimerTask rotationTimerTask;
private ScheduledFuture<?> logRotationFuture;
private Timer rotationTimer;

private static PayaraNotificationLogRotationTimer instance = new PayaraNotificationLogRotationTimer();

private PayaraNotificationLogRotationTimer() {
rotationTimer = new Timer("payara-log-rotation-timer");
}

public static PayaraNotificationLogRotationTimer getInstance() {
return instance;
}

public void startTimer(ScheduledExecutorService scheduledExecutorService, LogRotationTimerTask timerTask) {
public void startTimer(LogRotationTimerTask timerTask) {
rotationTimerTask = timerTask;
logRotationFuture = scheduledExecutorService.schedule(
rotationTimerTask,
timerTask.getRotationTimerValue(),
TimeUnit.MILLISECONDS
);
rotationTimer.schedule(rotationTimerTask, timerTask.getRotationTimerValue());
}

public void stopTimer() {
if (logRotationFuture != null) {
logRotationFuture.cancel(false);
}
rotationTimer.cancel();
}

public void restartTimer(ScheduledExecutorService scheduledExecutorService) {
public void restartTimer() {
// We will restart the timer only if the timerTask is set which
// means user has set a value for LogRotation based on Time
if (logRotationFuture != null) {
logRotationFuture.cancel(false);
if (rotationTimerTask != null) {
rotationTimerTask.cancel();
rotationTimerTask = new LogRotationTimerTask(
rotationTimerTask.task,
rotationTimerTask.getRotationTimerValueInMinutes());
logRotationFuture = scheduledExecutorService.schedule(
rotationTimerTask,
rotationTimerTask.getRotationTimerValue(),
TimeUnit.MILLISECONDS
);
rotationTimer.schedule(rotationTimerTask, rotationTimerTask.getRotationTimerValue());
}
}

public void restartTimerForDayBasedRotation(ScheduledExecutorService scheduledExecutorService) {
public void restartTimerForDayBasedRotation() {
// We will restart the timer only if the timerTask is set which
// means user has set a value for LogRotation based on Time
if (logRotationFuture != null) {
logRotationFuture.cancel(false);
if (rotationTimerTask != null) {
rotationTimerTask.cancel();
rotationTimerTask = new LogRotationTimerTask(
rotationTimerTask.task,
60 * 24);
logRotationFuture = scheduledExecutorService.schedule(
rotationTimerTask,
1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS
);
rotationTimer.schedule(rotationTimerTask, 1000 * 60 * 60 * 24);
}
}
}

0 comments on commit 4ddcf7f

Please sign in to comment.