-
Notifications
You must be signed in to change notification settings - Fork 40
Refactor ParallelExecutor #2820
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @brfrn169, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request primarily focuses on enhancing the robustness and maintainability of the ParallelExecutor class by refactoring its core task execution and exception handling mechanisms. It introduces a more structured approach to managing exceptions, particularly when multiple failures can occur, and includes a minor performance optimization for single-task operations. The changes are backed by a substantial increase in unit test coverage.
Highlights
- Exception Handling Refinement: I've significantly refactored the exception handling logic within
executeTasksInParallelandexecuteTasksSerially. This includes centralizing the rethrowing of specific exception types into a newrethrowhelper method, and improving how multiple exceptions are aggregated usingThrowable.addSuppressed()whenstopOnErroris false, ensuring all relevant failures are captured. - Single Task Optimization: For scenarios where only a single task needs to be executed and
noWaitis false, I've optimized theexecuteTasksmethod to directly run the task, bypassing the overhead of parallel execution setup. - Testability and Coverage: The
executeTasksmethod's visibility has been adjusted to package-private and annotated with@VisibleForTestingto facilitate more comprehensive unit testing. I've also added extensive new tests forexecuteTasksto cover various parallel, serial, single-task, and exception handling scenarios, including the new suppressed exception behavior.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors ParallelExecutor by simplifying task execution paths, unifying exception handling via a new rethrow helper, and exposing executeTasks for testing. It also adds comprehensive unit tests for various execution and error scenarios.
- Changed
executeTasksvisibility and inlined single-task handling. - Rewrote parallel execution loop and exception aggregation.
- Introduced
rethrowmethod and cleaned up serial execution logic. - Added new tests covering parallel/serial,
noWait, andstopOnErrorcombinations.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| core/src/main/java/com/scalar/db/transaction/consensuscommit/ParallelExecutor.java | Refactored executeTasks, rewrote loops, added rethrow and removed duplicate branching. |
| core/src/test/java/com/scalar/db/transaction/consensuscommit/ParallelExecutorTest.java | Added many new test cases and a mock import; missing some static imports. |
Comments suppressed due to low confidence (1)
core/src/test/java/com/scalar/db/transaction/consensuscommit/ParallelExecutorTest.java:8
- The test uses
when(...)andtimes(...)but does not import these statics. Please addimport static org.mockito.Mockito.when;andimport static org.mockito.Mockito.times;to avoid compilation errors.
import static org.mockito.Mockito.mock;
| for (ParallelExecutorTask task : tasks) { | ||
| try { | ||
| task.run(); | ||
| } catch (ExecutionException | ValidationConflictException | CrudException e) { |
Copilot
AI
Jun 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Serial execution no longer logs failures while parallel execution does. Consider re-adding a logger.warn inside this catch so serial runs also record taskName and transactionId on error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This PR refactors the ParallelExecutor to simplify exception handling and improve code structure. The introduction of a rethrow helper method and the use of suppressed exceptions are great improvements. However, the refactoring has removed critical exception logging for serial execution paths, which could hinder debugging.
|
|
||
| private void executeTasksSerially( | ||
| List<ParallelExecutorTask> tasks, boolean stopOnError, String taskName, String transactionId) | ||
| private void executeTasksSerially(List<ParallelExecutorTask> tasks, boolean stopOnError) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The refactoring removed the logging of exceptions that occur during serial task execution. The logger.warn call was present in the previous version and provided valuable context, which is crucial for debugging. Consider adding the logging back to provide better context when exceptions occur.
private void executeTasksSerially(List<ParallelExecutorTask> tasks, boolean stopOnError, String taskName, String transactionId)
throws ExecutionException, ValidationConflictException, CrudException {
Exception exception = null;
for (ParallelExecutorTask task : tasks) {
try {
task.run();
} catch (ExecutionException | ValidationConflictException | CrudException e) {
logger.warn("Failed to run a {} task. Transaction ID: {}", taskName, transactionId, e);
if (!stopOnError) {
if (exception == null) {
exception = e;
} else {
exception.addSuppressed(e);
}
} else {
throw e;
}
}
}
if (!stopOnError && exception != null) {
rethrow(exception);
}
}| // If there is only one task and noWait is false, we can run it directly without parallel | ||
| // execution. | ||
| executeTasksSerially(tasks, stopOnError, taskName, transactionId); | ||
| tasks.get(0).run(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to directly call tasks.get(0).run().
| throwable = cause; | ||
| } else { | ||
| throw new AssertionError("Can't reach here. Maybe a bug", e); | ||
| throwable.addSuppressed(cause); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use addSuppressed().
| if (exception == null) { | ||
| exception = e; | ||
| } else { | ||
| exception.addSuppressed(e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto. Updated to use addSuppressed().
| try { | ||
| task.run(); | ||
| } catch (ExecutionException | ValidationConflictException | CrudException e) { | ||
| logger.warn("Failed to run a {} task. Transaction ID: {}", taskName, transactionId, e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After using addSuppressed(), we can remove this log because all exceptions should now be included as suppressed exceptions.
| logger.warn( | ||
| "Failed to run a {} task. Transaction ID: {}", taskName, transactionId, e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to keep this log because, even after using addSuppressed(), some exceptions may not be included as suppressed when stopOnError is set to true.
komamitsu
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 👍
feeblefakie
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thank you!
BTW, I'm just wondering what the motivation is for the refactoring?
It would be great if you could clarify it.
Torch3333
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
@feeblefakie The initial motivation was to use the |
Description
This PR refactors
ParallelExecutor.Related issues and/or PRs
N/A
Changes made
Added some inline comments. Please take a look for the details.
Checklist
Additional notes (optional)
N/A
Release notes
N/A