133tosakarin commented on PR #1168:
URL: https://github.com/apache/ratis/pull/1168#issuecomment-2421158186
> Suppose we have the following code:
>
> ```java
> synchronized(server) {
> ...
> final CompletableFuture<Void> future = state.shudown()
> f.join()
> }
> ```
>
> * I agree with you that calling `join()` at the end of
`synchronized(server)` block seems useless since it just waits for the `state`
thread to terminate. If anything needs to be synchronized, it should be done
inside the `shutdown()` method. Of course, the `state` thread could be
`synchronized(server)` in its `run()` method. The same synchronization will
happen also in the non-shutdown cases. It there is a bug, the non-shutdown
cases should also have the same bug.
> * I could see that calling `join()` at the middle of
`synchronized(server)` may be useful in some cases -- the caller wants to wait
of the `state` thread to complete for some reasons such as getting a final
result. However, we are not doing that in our shutdown code.
Thank you for your valuable advice! We have reached a consensus
Regarding the question raised by **OneSizeFitsQuorum**, the concern might be
that when thread A waits for the state to shut down outside the `synchronized
(server)` block, it is possible that the state thread could modify some data of
the server. When thread A is woken up, since some server data may have been
changed by the state thread before its shutdown, the question is whether this
could have any subsequent impact on thread A.
OneSizeFitsQuorum recommends writing it as follows:
For Thread A:
```
synchronized (server) {
future = state.shutdown();
while (!future.isDone()) {
server.wait();
}
}
```
For Thread State:
```
Future<Void> stopped;
...
try {
run();
} finally {
stopped.complete();
synchronized (server) {
server.notify();
}
}
```
In our code, the State thread may be LogAppender, FollowerState,
LeaderElection
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]