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

RATIS-2146 Fixed possible issues caused by concurrent deletion and election when member changes #1140

Merged
merged 1 commit into from
Sep 3, 2024

Conversation

OneSizeFitsQuorum
Copy link
Contributor

see jira

Signed-off-by: OneSizeFitQuorum <[email protected]>
@OneSizeFitsQuorum
Copy link
Contributor Author

@szetszwo PTAL!

Copy link
Contributor

@szetszwo szetszwo left a comment

Choose a reason for hiding this comment

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

@OneSizeFitsQuorum , thanks a lot for working on this!

In the close() method, some items are async (e.g. role.shutdownFollowerState()). The current code may not help.

For role.shutdownFollowerState(), we may need the following fix.

@@ -504,7 +513,10 @@ class RaftServerImpl implements RaftServer.Division,
         LOG.warn("{}: Failed to un-register RaftServer JMX bean", getMemberId(), e);
       }
       try {
-        role.shutdownFollowerState();
+        final FollowerState follower = role.shutdownFollowerState();
+        if (follower != null) {
+          follower.join();
+        }
       } catch (Exception e) {
         LOG.warn("{}: Failed to shutdown FollowerState", getMemberId(), e);

There may be other items need similar changes.

@@ -463,6 +465,13 @@ void groupRemove(boolean deleteDirectory, boolean renameDirectory) {
/* Shutdown is triggered here inorder to avoid any locked files. */
state.getStateMachineUpdater().setRemoving();
close();
try {
closeFinishedLatch.await();
Copy link
Contributor

Choose a reason for hiding this comment

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

The CountDownLatch may not help. When close() returns, it must past the line closeFinishedLatch.countDown();. The new code seems the same as the existing code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

In fact, there is a difference because lifeCycle.checkStateAndClose is a CAS (Compare-And-Swap) operation, meaning only the thread that successfully performs the CAS can execute this function.

In our reproduced case, the election thread successfully performed the CAS to set the state to closing and was executing the shutdown code inside. Then, when the thread responsible for deleting the raftgroup called this function and found that the state was closing, it essentially did nothing and returned to delete the file directory, which subsequently caused an error in StateMachineUpdater.

I checked the contents of this lambda expression, and except for FollowerState and LeaderElection which are asynchronous, the rest should be synchronous, including the StateMachineUpdater that we discovered this time. Therefore, after adding this countDownLatch, at least the error we found will not occur again.

As for whether it's necessary to also synchronize the waiting for FollowerState and LeaderElection, I think it could be either way. Even if they clean up asynchronously, they will ultimately just transition to Leader, and the underlying shutdownLeaderState and state.close can prevent any impact from occurring.

Additionally, I found that we can't directly add a synchronized signature to the close function, because this lambda function will still only be executed by the thread that successfully performs the CAS.

Overall, I feel that the current changes should not introduce any side effects. What do you think? @szetszwo

Copy link
Contributor

Choose a reason for hiding this comment

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

@OneSizeFitsQuorum , thanks for the detailed explanation! I understand the change now.

Copy link
Contributor

@szetszwo szetszwo left a comment

Choose a reason for hiding this comment

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

+1 the change looks good.

@@ -463,6 +465,13 @@ void groupRemove(boolean deleteDirectory, boolean renameDirectory) {
/* Shutdown is triggered here inorder to avoid any locked files. */
state.getStateMachineUpdater().setRemoving();
close();
try {
closeFinishedLatch.await();
Copy link
Contributor

Choose a reason for hiding this comment

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

@OneSizeFitsQuorum , thanks for the detailed explanation! I understand the change now.

@szetszwo szetszwo merged commit 8f5159d into apache:master Sep 3, 2024
12 checks passed
@OneSizeFitsQuorum OneSizeFitsQuorum deleted the ratis-2146 branch September 3, 2024 07:56
OneSizeFitsQuorum added a commit that referenced this pull request Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants