runzhiwang commented on a change in pull request #372:
URL: https://github.com/apache/incubator-ratis/pull/372#discussion_r547923629
##########
File path:
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
##########
@@ -866,18 +880,112 @@ RaftClientReply waitForReply(RaftClientRequest request,
CompletableFuture<RaftCl
@Override
public RaftClientReply transferLeadership(TransferLeadershipRequest request)
throws IOException {
- //TODO(runzhiwang): implement transfer leadership in server
- return null;
+ return waitForReply(request, transferLeadershipAsync(request));
+ }
+
+ private CompletableFuture<RaftClientReply>
logAndReturnTransferLeadershipFail(
+ TransferLeadershipRequest request, String msg) {
+ LOG.warn(msg);
+ return CompletableFuture.completedFuture(newExceptionReply(request, new
StateMachineException(msg)));
+ }
+
+ boolean isSteppingDown() {
+ return finishTransferLeader != null;
+ }
+
+ private Consumer<RaftPeerId> finishTransferLeader;
+
+ public Consumer<RaftPeerId> finishTransferLeader() {
+ return finishTransferLeader;
+ }
+
+ public Consumer<RaftPeerId> setFinishTransferLeader(Consumer<RaftPeerId>
consumer) {
+ return finishTransferLeader = consumer;
+ }
+
+ private void timeoutTransferLeadership(
+ TransferLeadershipRequest request, CompletableFuture<RaftClientReply>
replyFuture)
+ throws StateMachineException {
+ synchronized (replyFuture) {
Review comment:
Add `synchroized `is to avoid multi-thread compete at the same time,
replyFuture also complete in the following lambda code called by
ServerState#setLeader. If without synchroized, the replyFuture maybe complete
two times at the same time, even though does not cause error, but maybe we
should avoid this.
```
currLeader -> {
synchronized (replyFuture) {
if (currLeader == null || replyFuture.isDone()) {
return;
}
if (currLeader.equals(request.getNewLeader())) {
replyFuture.complete(newSuccessReply(request));
setFinishTransferLeader(null);
}
}
};
```
##########
File path:
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java
##########
@@ -866,18 +880,112 @@ RaftClientReply waitForReply(RaftClientRequest request,
CompletableFuture<RaftCl
@Override
public RaftClientReply transferLeadership(TransferLeadershipRequest request)
throws IOException {
- //TODO(runzhiwang): implement transfer leadership in server
- return null;
+ return waitForReply(request, transferLeadershipAsync(request));
+ }
+
+ private CompletableFuture<RaftClientReply>
logAndReturnTransferLeadershipFail(
+ TransferLeadershipRequest request, String msg) {
+ LOG.warn(msg);
+ return CompletableFuture.completedFuture(newExceptionReply(request, new
StateMachineException(msg)));
+ }
+
+ boolean isSteppingDown() {
+ return finishTransferLeader != null;
+ }
+
+ private Consumer<RaftPeerId> finishTransferLeader;
+
+ public Consumer<RaftPeerId> finishTransferLeader() {
+ return finishTransferLeader;
+ }
+
+ public Consumer<RaftPeerId> setFinishTransferLeader(Consumer<RaftPeerId>
consumer) {
+ return finishTransferLeader = consumer;
+ }
+
+ private void timeoutTransferLeadership(
+ TransferLeadershipRequest request, CompletableFuture<RaftClientReply>
replyFuture)
+ throws StateMachineException {
+ synchronized (replyFuture) {
Review comment:
@szetszwo Add `synchroized `is to avoid multi-thread compete at the same
time, replyFuture also complete in the following lambda code called by
ServerState#setLeader. If without synchroized, the replyFuture maybe complete
two times at the same time, even though does not cause error, but maybe we
should avoid this.
```
currLeader -> {
synchronized (replyFuture) {
if (currLeader == null || replyFuture.isDone()) {
return;
}
if (currLeader.equals(request.getNewLeader())) {
replyFuture.complete(newSuccessReply(request));
setFinishTransferLeader(null);
}
}
};
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]