use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class NodeImpl method addLearners.
@Override
public void addLearners(final List<PeerId> learners, final Closure done) {
checkPeers(learners);
this.writeLock.lock();
try {
final Configuration newConf = new Configuration(this.conf.getConf());
for (final PeerId peer : learners) {
newConf.addLearner(peer);
}
unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
} finally {
this.writeLock.unlock();
}
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class NodeImpl method checkPeers.
private void checkPeers(final List<PeerId> peers) {
Requires.requireNonNull(peers, "Null peers");
Requires.requireTrue(!peers.isEmpty(), "Empty peers");
for (final PeerId peer : peers) {
Requires.requireNonNull(peer, "Null peer");
}
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class ReplicatorGroupImpl method stopAllAndFindTheNextCandidate.
@Override
public ThreadId stopAllAndFindTheNextCandidate(final ConfigurationEntry conf) {
ThreadId candidate = null;
final PeerId candidateId = findTheNextCandidate(conf);
if (candidateId != null) {
candidate = this.replicatorMap.get(candidateId);
} else {
LOG.info("Fail to find the next candidate.");
}
for (final ThreadId r : this.replicatorMap.values()) {
if (r != candidate) {
Replicator.stop(r);
}
}
this.replicatorMap.clear();
this.failureReplicators.clear();
return candidate;
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceImpl method transferLeader.
@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 = checkLeaderAndConnect(groupId, conf, leaderId);
if (!st.isOk()) {
return st;
}
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());
}
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceImpl method getLeader.
@Override
public Status getLeader(final String groupId, final Configuration conf, final PeerId leaderId) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireNonNull(leaderId, "Null leader id");
if (conf == null || conf.isEmpty()) {
return new Status(RaftError.EINVAL, "Empty group configuration");
}
final Status st = new Status(-1, "Fail to get leader of group %s", groupId);
for (final PeerId peer : conf) {
if (!this.cliClientService.connect(peer.getEndpoint())) {
LOG.error("Fail to connect peer {} to get leader for group {}.", peer, groupId);
continue;
}
final GetLeaderRequest.Builder rb = //
GetLeaderRequest.newBuilder().setGroupId(//
groupId).setPeerId(peer.toString());
final Future<Message> result = this.cliClientService.getLeader(peer.getEndpoint(), rb.build(), null);
try {
final Message msg = result.get(this.cliOptions.getTimeoutMs() <= 0 ? this.cliOptions.getRpcDefaultTimeout() : this.cliOptions.getTimeoutMs(), TimeUnit.MILLISECONDS);
if (msg instanceof ErrorResponse) {
if (st.isOk()) {
st.setError(-1, ((ErrorResponse) msg).getErrorMsg());
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, %s", savedMsg, ((ErrorResponse) msg).getErrorMsg());
}
} else {
final GetLeaderResponse response = (GetLeaderResponse) msg;
if (leaderId.parse(response.getLeaderId())) {
break;
}
}
} catch (final Exception e) {
if (st.isOk()) {
st.setError(-1, e.getMessage());
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, %s", savedMsg, e.getMessage());
}
}
}
if (leaderId.isEmpty()) {
return st;
}
return Status.OK();
}
Aggregations