forked from sofastack/sofa-jraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/2020.09.22 changes (sofastack#511)
* (feat) Don't update disk id when setSnapshot to avoid some corner cases. * (feat) Send heartbeat response directly, don't go through pipeline processing
- Loading branch information
1 parent
3a1bdcf
commit 7fdf7ec
Showing
8 changed files
with
77 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,7 @@ | |
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.Executor; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import com.alipay.sofa.jraft.JRaftUtils; | ||
import com.alipay.sofa.jraft.Node; | ||
import com.alipay.sofa.jraft.NodeManager; | ||
|
@@ -48,7 +46,7 @@ | |
* | ||
* @author boyan ([email protected]) | ||
* | ||
* 2018-Apr-04 3:00:13 PM | ||
* 2018-Apr-04 3:00:13 PM | ||
*/ | ||
public class AppendEntriesRequestProcessor extends NodeRequestProcessor<AppendEntriesRequest> implements | ||
ConnectionClosedEventListener { | ||
|
@@ -57,6 +55,7 @@ public class AppendEntriesRequestProcessor extends NodeRequestProcessor<AppendEn | |
|
||
/** | ||
* Peer executor selector. | ||
* | ||
* @author dennis | ||
*/ | ||
final class PeerExecutorSelector implements RpcProcessor.ExecutorSelector { | ||
|
@@ -99,34 +98,42 @@ public Executor select(final String reqClass, final Object reqHeader) { | |
*/ | ||
class SequenceRpcRequestClosure extends RpcRequestClosure { | ||
|
||
private final int reqSequence; | ||
private final String groupId; | ||
private final String peerId; | ||
private final int reqSequence; | ||
private final String groupId; | ||
private final String peerId; | ||
private final boolean isHeartbeat; | ||
|
||
public SequenceRpcRequestClosure(RpcRequestClosure parent, int sequence, String groupId, String peerId, | ||
Message defaultResp) { | ||
public SequenceRpcRequestClosure(final RpcRequestClosure parent, final Message defaultResp, | ||
final String groupId, final String peerId, final int sequence, | ||
final boolean isHeartbeat) { | ||
super(parent.getRpcCtx(), defaultResp); | ||
this.reqSequence = sequence; | ||
this.groupId = groupId; | ||
this.peerId = peerId; | ||
this.isHeartbeat = isHeartbeat; | ||
} | ||
|
||
@Override | ||
public void sendResponse(final Message msg) { | ||
sendSequenceResponse(this.groupId, this.peerId, this.reqSequence, getRpcCtx(), msg); | ||
if (this.isHeartbeat) { | ||
super.sendResponse(msg); | ||
} else { | ||
sendSequenceResponse(this.groupId, this.peerId, this.reqSequence, getRpcCtx(), msg); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Response message wrapper with a request sequence number and asyncContext.done | ||
* | ||
* @author dennis | ||
*/ | ||
static class SequenceMessage implements Comparable<SequenceMessage> { | ||
public final Message msg; | ||
private final int sequence; | ||
private final RpcContext rpcCtx; | ||
|
||
public SequenceMessage(RpcContext rpcCtx, Message msg, int sequence) { | ||
public SequenceMessage(final RpcContext rpcCtx, final Message msg, final int sequence) { | ||
super(); | ||
this.rpcCtx = rpcCtx; | ||
this.msg = msg; | ||
|
@@ -302,8 +309,7 @@ void removePeerRequestContext(final String groupId, final String peerId) { | |
} | ||
|
||
/** | ||
* RAFT group peer request contexts | ||
* Map<groupId, <peerId, ctx>> | ||
* RAFT group peer request contexts Map<groupId, <peerId, ctx>> | ||
*/ | ||
private final ConcurrentMap<String, ConcurrentMap<String, PeerRequestContext>> peerRequestContexts = new ConcurrentHashMap<>(); | ||
|
||
|
@@ -312,7 +318,7 @@ void removePeerRequestContext(final String groupId, final String peerId) { | |
*/ | ||
private final ExecutorSelector executorSelector; | ||
|
||
public AppendEntriesRequestProcessor(Executor executor) { | ||
public AppendEntriesRequestProcessor(final Executor executor) { | ||
super(executor, RpcRequests.AppendEntriesResponse.getDefaultInstance()); | ||
this.executorSelector = new PeerExecutorSelector(); | ||
} | ||
|
@@ -339,6 +345,12 @@ private int getAndIncrementNextRequiredSequence(final String groupId, final Stri | |
return getPeerRequestContext(groupId, peerId, conn).getAndIncrementNextRequiredSequence(); | ||
} | ||
|
||
private boolean isHeartbeatRequest(final AppendEntriesRequest request) { | ||
// No entries and no data means a true heartbeat request. | ||
// TODO(boyan) refactor, adds a new flag field? | ||
return request.getEntriesCount() == 0 && !request.hasData(); | ||
} | ||
|
||
@Override | ||
public Message processRequest0(final RaftServerService service, final AppendEntriesRequest request, | ||
final RpcRequestClosure done) { | ||
|
@@ -349,11 +361,19 @@ public Message processRequest0(final RaftServerService service, final AppendEntr | |
final String groupId = request.getGroupId(); | ||
final String peerId = request.getPeerId(); | ||
|
||
final int reqSequence = getAndIncrementSequence(groupId, peerId, done.getRpcCtx().getConnection()); | ||
boolean isHeartbeat = isHeartbeatRequest(request); | ||
int reqSequence = -1; | ||
if (!isHeartbeat) { | ||
reqSequence = getAndIncrementSequence(groupId, peerId, done.getRpcCtx().getConnection()); | ||
} | ||
final Message response = service.handleAppendEntriesRequest(request, new SequenceRpcRequestClosure(done, | ||
reqSequence, groupId, peerId, defaultResp())); | ||
defaultResp(), groupId, peerId, reqSequence, isHeartbeat)); | ||
if (response != null) { | ||
sendSequenceResponse(groupId, peerId, reqSequence, done.getRpcCtx(), response); | ||
if (isHeartbeat) { | ||
done.getRpcCtx().sendResponse(response); | ||
} else { | ||
sendSequenceResponse(groupId, peerId, reqSequence, done.getRpcCtx(), response); | ||
} | ||
} | ||
return null; | ||
} else { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters