Skip to content

Commit

Permalink
make all ThreadPool static final (alibaba#3243)
Browse files Browse the repository at this point in the history
* make all ThreadPool static final

* update github workflow
  • Loading branch information
robberphex authored Oct 30, 2023
1 parent d00798f commit 5108046
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ jobs:
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java }}
distribution: 'adopt'
distribution: 'temurin'
architecture: x64

- name: Test with Maven
run: mvn test
run: mvn --batch-mode test

- name: Build with Maven
run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V -DminimumPriority=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;

import java.io.UnsupportedEncodingException;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;

/**
* This Handler publishes log records to console by using {@link java.util.logging.StreamHandler}.
Expand All @@ -41,6 +39,19 @@
* @author cdfive
*/
class ConsoleHandler extends Handler {
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
1,
5,
1,
TimeUnit.HOURS,
new ArrayBlockingQueue<>(1024),
new NamedThreadFactory("sentinel-console-log-executor", true),
new LogRejectedExecutionHandler()
);

static {
executor.allowCoreThreadTimeOut(true);
}

/**
* A Handler which publishes log records to System.out.
Expand All @@ -52,21 +63,11 @@ class ConsoleHandler extends Handler {
*/
private StreamHandler stderrHandler;

private ExecutorService executor;
private AtomicReference<Future<?>> lastFuture = new AtomicReference<>();

public ConsoleHandler() {
this.stdoutHandler = new StreamHandler(System.out, new CspFormatter());
this.stderrHandler = new StreamHandler(System.err, new CspFormatter());

int corePoolSize = 1;
int maximumPoolSize = 1;
long keepAliveTime = 0;
/**insure the log can be recorded*/
int queueSize = 1024;
RejectedExecutionHandler handler = new LogRejectedExecutionHandler();
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize),
new NamedThreadFactory("sentinel-console-log-executor", true), handler);
}

@Override
Expand All @@ -83,7 +84,7 @@ public synchronized void setEncoding(String encoding) throws SecurityException,

@Override
public void publish(LogRecord record) {
executor.execute(new LogTask(record,stdoutHandler,stderrHandler));
lastFuture.set(executor.submit(new LogTask(record, stdoutHandler, stderrHandler)));
}

@Override
Expand All @@ -94,16 +95,18 @@ public void flush() {

@Override
public void close() throws SecurityException {
/**not need to record log if process is killed.*/
executor.shutdown();
Future<?> future = lastFuture.get();
if (future != null) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
stdoutHandler.close();
stderrHandler.close();
}

public ExecutorService getExecutor() {
return executor;
}

static class LogRejectedExecutionHandler implements RejectedExecutionHandler {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
import java.util.Calendar;
import java.util.Date;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
Expand All @@ -43,7 +40,20 @@ public SimpleDateFormat initialValue() {
}
};

private ExecutorService executor;
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
1,
5,
1,
TimeUnit.HOURS,
new ArrayBlockingQueue<Runnable>(1024),
new NamedThreadFactory("sentinel-datafile-log-executor", true),
new ThreadPoolExecutor.DiscardOldestPolicy()
);

static {
// allow all thread could be stopped
executor.allowCoreThreadTimeOut(true);
}

private volatile FileHandler handler;

Expand All @@ -66,22 +76,10 @@ public SimpleDateFormat initialValue() {
this.append = append;
rotateDate();
this.initialized = true;

int corePoolSize = 1;
int maximumPoolSize = 1;
long keepAliveTime = 0;
/**insure the log can be recorded*/
int queueSize = 1024;
RejectedExecutionHandler handler = new LogRejectedExecutionHandler();
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize),
new NamedThreadFactory("sentinel-datafile-log-executor", true), handler);
}

@Override
public void close() throws SecurityException {
/**not need to record log if process is killed.*/
executor.shutdown();
handler.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public void testInfoPublish() {
// Test INFO level, should log to stdout
logRecord = new LogRecord(Level.INFO, "test info message");
consoleHandler.publish(logRecord);
try {
consoleHandler.getExecutor().shutdown();
consoleHandler.getExecutor().awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

consoleHandler.close();
assertEquals(cspFormatter.format(logRecord), baosOut.toString());
assertEquals("", baosErr.toString());
Expand All @@ -80,12 +75,7 @@ public void testWarnPublish() {
// Test INFO level, should log to stderr
logRecord = new LogRecord(Level.WARNING, "test warning message");
consoleHandler.publish(logRecord);
try {
consoleHandler.getExecutor().shutdown();
consoleHandler.getExecutor().awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

consoleHandler.close();
assertEquals(cspFormatter.format(logRecord), baosErr.toString());
assertEquals("", baosOut.toString());
Expand All @@ -111,12 +101,7 @@ public void testFinePublish() {
// java.util.logging.StreamHandler.level=FINE
logRecord = new LogRecord(Level.FINE, "test fine message");
consoleHandler.publish(logRecord);
try {
consoleHandler.getExecutor().shutdown();
consoleHandler.getExecutor().awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

consoleHandler.close();
assertEquals("", baosOut.toString());
assertEquals("", baosErr.toString());
Expand All @@ -139,12 +124,7 @@ public void testSeverePublish() {
// Test SEVERE level, should log to stderr
logRecord = new LogRecord(Level.SEVERE, "test severe message");
consoleHandler.publish(logRecord);
try {
consoleHandler.getExecutor().shutdown();
consoleHandler.getExecutor().awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

consoleHandler.close();
assertEquals(cspFormatter.format(logRecord), baosErr.toString());
assertEquals("", baosOut.toString());
Expand Down

0 comments on commit 5108046

Please sign in to comment.