Skip to content

perf: Standardize ExecutorService Shutdown with Safety Wrapper. #2430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions s3stream/src/main/java/com/automq/stream/s3/S3Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,7 @@ public void shutdown() {
request.cf.completeExceptionally(new IOException("S3Storage is shutdown"));
}
deltaWAL.shutdownGracefully();
backgroundExecutor.shutdown();
try {
if (!backgroundExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
LOGGER.warn("await backgroundExecutor timeout 10s");
}
} catch (InterruptedException e) {
backgroundExecutor.shutdownNow();
LOGGER.warn("await backgroundExecutor close fail", e);
}
ThreadUtils.shutdownExecutor(backgroundExecutor, 10, TimeUnit.SECONDS, LOGGER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,7 @@ public void shutdown() {
}

private void shutdownAndAwaitTermination(ExecutorService executor, int timeout, TimeUnit timeUnit) {
executor.shutdown();
try {
if (!executor.awaitTermination(timeout, timeUnit)) {
executor.shutdownNow();
}
} catch (InterruptedException ex) {
executor.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
ThreadUtils.shutdownExecutor(executor, timeout, timeUnit, logger);
}

public CompletableFuture<Void> compact() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,10 @@ public short bucketId() {

@Override
public void close() {
writeLimiterCallbackExecutor.shutdown();
readCallbackExecutor.shutdown();
writeCallbackExecutor.shutdown();
scheduler.shutdown();
ThreadUtils.shutdownExecutor(writeLimiterCallbackExecutor, 1, TimeUnit.SECONDS);
ThreadUtils.shutdownExecutor(readCallbackExecutor, 1, TimeUnit.SECONDS);
ThreadUtils.shutdownExecutor(writeCallbackExecutor, 1, TimeUnit.SECONDS);
ThreadUtils.shutdownExecutor(scheduler, 1, TimeUnit.SECONDS);
fastRetryTimer.stop();
doClose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,7 @@ private void run(Config config) {
}
Runnable stopLog = logIt(config, stat);

executor.shutdown();
try {
if (!executor.awaitTermination(config.durationSeconds + 10, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
}
ThreadUtils.shutdownExecutor(executor, config.durationSeconds + 10, TimeUnit.SECONDS);
stopLog.run();
stopTrim.run();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ public boolean shutdown(long timeout, TimeUnit unit) {
}

boolean gracefulShutdown;
this.ioExecutor.shutdown();
this.pollBlockScheduler.shutdownNow();
ThreadUtils.shutdownExecutor(this.ioExecutor, timeout, unit);
ThreadUtils.shutdownExecutor(this.pollBlockScheduler, 1, TimeUnit.SECONDS);
List<Runnable> tasks = new LinkedList<>();
try {
gracefulShutdown = this.ioExecutor.awaitTermination(timeout, unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/

package com.automq.stream.s3.operator;
import com.automq.stream.utils.ThreadUtils;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
Expand All @@ -21,6 +23,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -76,7 +79,7 @@ public void testConsumeBeforeUpdate() {
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
} finally {
executor.shutdown();
ThreadUtils.shutdownExecutor(executor, 1, TimeUnit.SECONDS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public void testMultiThreadWrite() throws IOException, InterruptedException {
}
executor.shutdown();
assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));

channel.close();
}

Expand Down
Loading