Java Code Examples for com.alipay.sofa.jraft.entity.PeerId#isEmpty()
The following examples show how to use
com.alipay.sofa.jraft.entity.PeerId#isEmpty() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetLeaderRequestProcessor.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public Message processRequest(final GetLeaderRequest request, final RpcRequestClosure done) { List<Node> nodes = new ArrayList<>(); final String groupId = getGroupId(request); if (request.hasPeerId()) { final String peerIdStr = getPeerId(request); final PeerId peer = new PeerId(); if (peer.parse(peerIdStr)) { final Status st = new Status(); nodes.add(getNode(groupId, peer, st)); if (!st.isOk()) { return RpcFactoryHelper // .responseFactory() // .newResponse(defaultResp(), st); } } else { return RpcFactoryHelper // .responseFactory() // .newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr); } } else { nodes = NodeManager.getInstance().getNodesByGroupId(groupId); } if (nodes == null || nodes.isEmpty()) { return RpcFactoryHelper // .responseFactory() // .newResponse(defaultResp(), RaftError.ENOENT, "No nodes in group %s", groupId); } for (final Node node : nodes) { final PeerId leader = node.getLeaderId(); if (leader != null && !leader.isEmpty()) { return GetLeaderResponse.newBuilder().setLeaderId(leader.toString()).build(); } } return RpcFactoryHelper // .responseFactory() // .newResponse(defaultResp(), RaftError.EAGAIN, "Unknown leader"); }
Example 2
Source File: CliServiceImpl.java From sofa-jraft with Apache License 2.0 | 5 votes |
@Override public Status transferLeader(final String groupId, final Configuration conf, final PeerId peer) { Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id"); Requires.requireNonNull(conf, "Null configuration"); Requires.requireNonNull(peer, "Null peer"); final PeerId leaderId = new PeerId(); final Status st = getLeader(groupId, conf, leaderId); if (!st.isOk()) { return st; } if (!this.cliClientService.connect(leaderId.getEndpoint())) { return new Status(-1, "Fail to init channel to leader %s", leaderId); } final TransferLeaderRequest.Builder rb = TransferLeaderRequest.newBuilder() // .setGroupId(groupId) // .setLeaderId(leaderId.toString()); if (!peer.isEmpty()) { rb.setPeerId(peer.toString()); } try { final Message result = this.cliClientService.transferLeader(leaderId.getEndpoint(), rb.build(), null).get(); return statusFromResponse(result); } catch (final Exception e) { return new Status(-1, e.getMessage()); } }
Example 3
Source File: NodeImpl.java From sofa-jraft with Apache License 2.0 | 5 votes |
private void resetLeaderId(final PeerId newLeaderId, final Status status) { if (newLeaderId.isEmpty()) { if (!this.leaderId.isEmpty() && this.state.compareTo(State.STATE_TRANSFERRING) > 0) { this.fsmCaller.onStopFollowing(new LeaderChangeContext(this.leaderId.copy(), this.currTerm, status)); } this.leaderId = PeerId.emptyPeer(); } else { if (this.leaderId == null || this.leaderId.isEmpty()) { this.fsmCaller.onStartFollowing(new LeaderChangeContext(newLeaderId, this.currTerm, status)); } this.leaderId = newLeaderId.copy(); } }
Example 4
Source File: CliServiceImpl.java From sofa-jraft with Apache License 2.0 | 4 votes |
@Override public Status rebalance(final Set<String> balanceGroupIds, final Configuration conf, final Map<String, PeerId> rebalancedLeaderIds) { Requires.requireNonNull(balanceGroupIds, "Null balance group ids"); Requires.requireTrue(!balanceGroupIds.isEmpty(), "Empty balance group ids"); Requires.requireNonNull(conf, "Null configuration"); Requires.requireTrue(!conf.isEmpty(), "No peers of configuration"); LOG.info("Rebalance start with raft groups={}.", balanceGroupIds); final long start = Utils.monotonicMs(); int transfers = 0; Status failedStatus = null; final Queue<String> groupDeque = new ArrayDeque<>(balanceGroupIds); final LeaderCounter leaderCounter = new LeaderCounter(balanceGroupIds.size(), conf.size()); for (;;) { final String groupId = groupDeque.poll(); if (groupId == null) { // well done break; } final PeerId leaderId = new PeerId(); final Status leaderStatus = getLeader(groupId, conf, leaderId); if (!leaderStatus.isOk()) { failedStatus = leaderStatus; break; } if (rebalancedLeaderIds != null) { rebalancedLeaderIds.put(groupId, leaderId); } if (leaderCounter.incrementAndGet(leaderId) <= leaderCounter.getExpectedAverage()) { // The num of leaders is less than the expected average, we are going to deal with others continue; } // Find the target peer and try to transfer the leader to this peer final PeerId targetPeer = findTargetPeer(leaderId, groupId, conf, leaderCounter); if (!targetPeer.isEmpty()) { final Status transferStatus = transferLeader(groupId, conf, targetPeer); transfers++; if (!transferStatus.isOk()) { // The failure of `transfer leader` usually means the node is busy, // so we return failure status and should try `rebalance` again later. failedStatus = transferStatus; break; } LOG.info("Group {} transfer leader to {}.", groupId, targetPeer); leaderCounter.decrementAndGet(leaderId); groupDeque.add(groupId); if (rebalancedLeaderIds != null) { rebalancedLeaderIds.put(groupId, targetPeer); } } } final Status status = failedStatus != null ? failedStatus : Status.OK(); if (LOG.isInfoEnabled()) { LOG.info( "Rebalanced raft groups={}, status={}, number of transfers={}, elapsed time={} ms, rebalanced result={}.", balanceGroupIds, status, transfers, Utils.monotonicMs() - start, rebalancedLeaderIds); } return status; }