use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceImpl method getPeers.
private List<PeerId> getPeers(final String groupId, final Configuration conf, final boolean returnLearners, final boolean onlyGetAlive) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireNonNull(conf, "Null conf");
final PeerId leaderId = new PeerId();
final Status st = getLeader(groupId, conf, leaderId);
if (!st.isOk()) {
throw new IllegalStateException(st.getErrorMsg());
}
if (!this.cliClientService.connect(leaderId.getEndpoint())) {
throw new IllegalStateException("Fail to init channel to leader " + leaderId);
}
final GetPeersRequest.Builder rb = //
GetPeersRequest.newBuilder().setGroupId(//
groupId).setLeaderId(//
leaderId.toString()).setOnlyAlive(onlyGetAlive);
try {
final Message result = this.cliClientService.getPeers(leaderId.getEndpoint(), rb.build(), null).get(this.cliOptions.getTimeoutMs() <= 0 ? this.cliOptions.getRpcDefaultTimeout() : this.cliOptions.getTimeoutMs(), TimeUnit.MILLISECONDS);
if (result instanceof GetPeersResponse) {
final GetPeersResponse resp = (GetPeersResponse) result;
final List<PeerId> peerIdList = new ArrayList<>();
final ProtocolStringList responsePeers = returnLearners ? resp.getLearnersList() : resp.getPeersList();
for (final String peerIdStr : responsePeers) {
final PeerId newPeer = new PeerId();
newPeer.parse(peerIdStr);
peerIdList.add(newPeer);
}
return peerIdList;
} else {
final ErrorResponse resp = (ErrorResponse) result;
throw new JRaftException(resp.getErrorMsg());
}
} catch (final JRaftException e) {
throw e;
} catch (final Exception e) {
throw new JRaftException(e);
}
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceImpl method removeLearners.
@Override
public Status removeLearners(final String groupId, final Configuration conf, final List<PeerId> learners) {
checkLearnersOpParams(groupId, conf, learners);
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 RemoveLearnersRequest.Builder rb = //
RemoveLearnersRequest.newBuilder().setGroupId(//
groupId).setLeaderId(leaderId.toString());
for (final PeerId peer : learners) {
rb.addLearners(peer.toString());
}
try {
final Message result = this.cliClientService.removeLearners(leaderId.getEndpoint(), rb.build(), null).get();
return processLearnersOpResponse(groupId, result, "removing learners: %s", learners);
} 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 processLearnersOpResponse.
private Status processLearnersOpResponse(final String groupId, final Message result, final String fmt, final Object... formatArgs) {
if (result instanceof LearnersOpResponse) {
final LearnersOpResponse resp = (LearnersOpResponse) result;
final Configuration oldConf = new Configuration();
for (final String peerIdStr : resp.getOldLearnersList()) {
final PeerId oldPeer = new PeerId();
oldPeer.parse(peerIdStr);
oldConf.addLearner(oldPeer);
}
final Configuration newConf = new Configuration();
for (final String peerIdStr : resp.getNewLearnersList()) {
final PeerId newPeer = new PeerId();
newPeer.parse(peerIdStr);
newConf.addLearner(newPeer);
}
LOG.info("Learners of replication group {} changed from {} to {} after {}.", groupId, oldConf, newConf, String.format(fmt, formatArgs));
return Status.OK();
} else {
return statusFromResponse(result);
}
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceImpl method recordConfigurationChange.
private void recordConfigurationChange(final String groupId, final List<String> oldPeersList, final List<String> newPeersList) {
final Configuration oldConf = new Configuration();
for (final String peerIdStr : oldPeersList) {
final PeerId oldPeer = new PeerId();
oldPeer.parse(peerIdStr);
oldConf.addPeer(oldPeer);
}
final Configuration newConf = new Configuration();
for (final String peerIdStr : newPeersList) {
final PeerId newPeer = new PeerId();
newPeer.parse(peerIdStr);
newConf.addPeer(newPeer);
}
LOG.info("Configuration of replication group {} changed from {} to {}.", groupId, oldConf, newConf);
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class CliServiceImpl method resetLearners.
@Override
public Status resetLearners(final String groupId, final Configuration conf, final List<PeerId> learners) {
checkLearnersOpParams(groupId, conf, learners);
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 ResetLearnersRequest.Builder rb = //
ResetLearnersRequest.newBuilder().setGroupId(//
groupId).setLeaderId(leaderId.toString());
for (final PeerId peer : learners) {
rb.addLearners(peer.toString());
}
try {
final Message result = this.cliClientService.resetLearners(leaderId.getEndpoint(), rb.build(), null).get();
return processLearnersOpResponse(groupId, result, "resetting learners: %s", learners);
} catch (final Exception e) {
return new Status(-1, e.getMessage());
}
}
Aggregations