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

fix#37 #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand All @@ -27,35 +29,13 @@ public class ExampleServiceImpl implements ExampleService {

private RaftNode raftNode;
private ExampleStateMachine stateMachine;
private int leaderId = -1;
private RpcClient leaderRpcClient = null;
private Lock leaderLock = new ReentrantLock();
private Map<Peer, ExampleService> serviceMap = new HashMap<>();

public ExampleServiceImpl(RaftNode raftNode, ExampleStateMachine stateMachine) {
this.raftNode = raftNode;
this.stateMachine = stateMachine;
}

private void onLeaderChangeEvent() {
if (raftNode.getLeaderId() != -1
&& raftNode.getLeaderId() != raftNode.getLocalServer().getServerId()
&& leaderId != raftNode.getLeaderId()) {
leaderLock.lock();
if (leaderId != -1 && leaderRpcClient != null) {
leaderRpcClient.stop();
leaderRpcClient = null;
leaderId = -1;
}
leaderId = raftNode.getLeaderId();
Peer peer = raftNode.getPeerMap().get(leaderId);
Endpoint endpoint = new Endpoint(peer.getServer().getEndpoint().getHost(),
peer.getServer().getEndpoint().getPort());
RpcClientOptions rpcClientOptions = new RpcClientOptions();
rpcClientOptions.setGlobalThreadPoolSharing(true);
leaderRpcClient = new RpcClient(endpoint, rpcClientOptions);
leaderLock.unlock();
}
}

@Override
public ExampleProto.SetResponse set(ExampleProto.SetRequest request) {
Expand All @@ -64,8 +44,14 @@ public ExampleProto.SetResponse set(ExampleProto.SetRequest request) {
if (raftNode.getLeaderId() <= 0) {
responseBuilder.setSuccess(false);
} else if (raftNode.getLeaderId() != raftNode.getLocalServer().getServerId()) {
onLeaderChangeEvent();
ExampleService exampleService = BrpcProxy.getProxy(leaderRpcClient, ExampleService.class);
Peer leaderPeer = raftNode.getPeerMap().get(raftNode.getLeaderId());
String host = leaderPeer.getServer().getEndpoint().getHost();
int port = leaderPeer.getServer().getEndpoint().getPort();
ExampleService exampleService = serviceMap.get(leaderPeer);
if (exampleService == null) {
exampleService = BrpcProxy.getProxy(new RpcClient(new Endpoint(host, port)), ExampleService.class);
serviceMap.put(leaderPeer, exampleService);
}
ExampleProto.SetResponse responseFromLeader = exampleService.set(request);
responseBuilder.mergeFrom(responseFromLeader);
} else {
Expand Down