use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class CliServiceImpl method addLearners.
@Override
public Status addLearners(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 CliRequests.AddLearnersRequest.Builder rb = //
CliRequests.AddLearnersRequest.newBuilder().setGroupId(//
groupId).setLeaderId(leaderId.toString());
for (final PeerId peer : learners) {
rb.addLearners(peer.toString());
}
try {
final Message result = this.cliClientService.addLearners(leaderId.getEndpoint(), rb.build(), null).get();
return processLearnersOpResponse(groupId, result, "adding learners: %s", learners);
} catch (final Exception e) {
return new Status(-1, e.getMessage());
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
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 CliRequests.GetPeersRequest.Builder rb = //
CliRequests.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 CliRequests.GetPeersResponse) {
final CliRequests.GetPeersResponse resp = (CliRequests.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 RpcRequests.ErrorResponse resp = (RpcRequests.ErrorResponse) result;
throw new JRaftException(resp.getErrorMsg());
}
} catch (final JRaftException e) {
throw e;
} catch (final Exception e) {
throw new JRaftException(e);
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class CliServiceImpl method addPeer.
@Override
public Status addPeer(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 CliRequests.AddPeerRequest.Builder rb = //
CliRequests.AddPeerRequest.newBuilder().setGroupId(//
groupId).setLeaderId(//
leaderId.toString()).setPeerId(peer.toString());
try {
final Message result = this.cliClientService.addPeer(leaderId.getEndpoint(), rb.build(), null).get();
if (result instanceof CliRequests.AddPeerResponse) {
final CliRequests.AddPeerResponse resp = (CliRequests.AddPeerResponse) result;
recordConfigurationChange(groupId, resp.getOldPeersList(), resp.getNewPeersList());
return Status.OK();
} else {
return statusFromResponse(result);
}
} catch (final Exception e) {
return new Status(-1, e.getMessage());
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
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 CliRequests.GetLeaderRequest.Builder rb = //
CliRequests.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 RpcRequests.ErrorResponse) {
if (st.isOk()) {
st.setError(-1, ((RpcRequests.ErrorResponse) msg).getErrorMsg());
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, %s", savedMsg, ((RpcRequests.ErrorResponse) msg).getErrorMsg());
}
} else {
final CliRequests.GetLeaderResponse response = (CliRequests.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();
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class FSMCallerImpl method doSnapshotLoad.
private void doSnapshotLoad(final LoadSnapshotClosure done) {
Requires.requireNonNull(done, "LoadSnapshotClosure is null");
final SnapshotReader reader = done.start();
if (reader == null) {
done.run(new Status(RaftError.EINVAL, "open SnapshotReader failed"));
return;
}
final RaftOutter.SnapshotMeta meta = reader.load();
if (meta == null) {
done.run(new Status(RaftError.EINVAL, "SnapshotReader load meta failed"));
if (reader.getRaftError() == RaftError.EIO) {
final RaftException err = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT, RaftError.EIO, "Fail to load snapshot meta");
setError(err);
}
return;
}
final LogId lastAppliedId = new LogId(this.lastAppliedIndex.get(), this.lastAppliedTerm);
final LogId snapshotId = new LogId(meta.getLastIncludedIndex(), meta.getLastIncludedTerm());
if (lastAppliedId.compareTo(snapshotId) > 0) {
done.run(new Status(RaftError.ESTALE, "Loading a stale snapshot last_applied_index=%d " + "last_applied_term=%d snapshot_index=%d snapshot_term=%d", lastAppliedId.getIndex(), lastAppliedId.getTerm(), snapshotId.getIndex(), snapshotId.getTerm()));
return;
}
if (!this.fsm.onSnapshotLoad(reader)) {
done.run(new Status(-1, "StateMachine onSnapshotLoad failed"));
final RaftException e = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_STATE_MACHINE, RaftError.ESTATEMACHINE, "StateMachine onSnapshotLoad failed");
setError(e);
return;
}
if (meta.getOldPeersCount() == 0) {
// Joint stage is not supposed to be noticeable by end users.
final Configuration conf = new Configuration();
for (int i = 0, size = meta.getPeersCount(); i < size; i++) {
final PeerId peer = new PeerId();
Requires.requireTrue(peer.parse(meta.getPeers(i)), "Parse peer failed");
conf.addPeer(peer);
}
this.fsm.onConfigurationCommitted(conf);
}
this.lastAppliedIndex.set(meta.getLastIncludedIndex());
this.lastAppliedTerm = meta.getLastIncludedTerm();
done.run(Status.OK());
}
Aggregations