Skip to content
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

Fix threadpool monitor when using virtual thread #1344

Merged
merged 1 commit into from
Sep 25, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
Expand Down Expand Up @@ -275,9 +276,14 @@ private void startCustomThreadPoolMonitor() {
} else {
customThreadPoolMonitor.setPoolName(pool.getThreadPoolName());
}
customThreadPoolMonitor.setThreadPoolExecutor(pool.getExecutor());
customThreadPoolMonitor.start();
poolNames.add(pool.getThreadPoolName());

Executor tmp = pool.getUserExecutor();
if (tmp instanceof ThreadPoolExecutor) {
customThreadPoolMonitor.setThreadPoolExecutor((ThreadPoolExecutor) pool
.getUserExecutor());
customThreadPoolMonitor.start();
poolNames.add(pool.getThreadPoolName());
}
Comment on lines +279 to +286
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve type checking and error handling for custom thread pool executors

The addition of the type check for ThreadPoolExecutor is a good improvement for compatibility with different thread implementations, including virtual threads. However, there are a few suggestions to enhance this change:

  1. The indentation of the new code is inconsistent with the rest of the method. Please align it properly.
  2. Consider using a more descriptive variable name instead of tmp, such as executor.
  3. Add error handling or logging for cases where the executor is not a ThreadPoolExecutor.

Here's a suggested refactoring:

-
-                Executor tmp = pool.getUserExecutor();
-                if (tmp instanceof ThreadPoolExecutor) {
-                    customThreadPoolMonitor.setThreadPoolExecutor((ThreadPoolExecutor) pool
-                        .getUserExecutor());
-                    customThreadPoolMonitor.start();
-                    poolNames.add(pool.getThreadPoolName());
-                }
+                Executor executor = pool.getUserExecutor();
+                if (executor instanceof ThreadPoolExecutor) {
+                    customThreadPoolMonitor.setThreadPoolExecutor((ThreadPoolExecutor) executor);
+                    customThreadPoolMonitor.start();
+                    poolNames.add(pool.getThreadPoolName());
+                } else {
+                    LOGGER.warn("Custom thread pool '{}' does not use ThreadPoolExecutor. Monitoring will be skipped.", pool.getThreadPoolName());
+                }

This refactoring improves code readability, maintains consistent indentation, and adds logging for cases where the executor is not a ThreadPoolExecutor.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Executor tmp = pool.getUserExecutor();
if (tmp instanceof ThreadPoolExecutor) {
customThreadPoolMonitor.setThreadPoolExecutor((ThreadPoolExecutor) pool
.getUserExecutor());
customThreadPoolMonitor.start();
poolNames.add(pool.getThreadPoolName());
}
Executor executor = pool.getUserExecutor();
if (executor instanceof ThreadPoolExecutor) {
customThreadPoolMonitor.setThreadPoolExecutor((ThreadPoolExecutor) executor);
customThreadPoolMonitor.start();
poolNames.add(pool.getThreadPoolName());
} else {
LOGGER.warn("Custom thread pool '{}' does not use ThreadPoolExecutor. Monitoring will be skipped.", pool.getThreadPoolName());
}

}
}
}
Expand Down
Loading