use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class BaseCliRequestProcessor method processRequest.
@Override
public Message processRequest(T request, RpcRequestClosure done) {
String groupId = getGroupId(request);
String peerIdStr = getPeerId(request);
PeerId peerId = null;
if (!StringUtils.isBlank(peerIdStr)) {
peerId = new PeerId();
if (!peerId.parse(peerIdStr)) {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer: %s", peerIdStr);
}
}
Status st = new Status();
Node node = getNode(groupId, peerId, st);
if (!st.isOk()) {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st.getCode(), st.getErrorMsg());
} else {
return processRequest0(new CliRequestContext(node, groupId, peerId), request, done);
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class ChangePeersRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.ChangePeersRequest request, final RpcRequestClosure done) {
final List<PeerId> oldConf = ctx.node.listPeers();
final Configuration conf = new Configuration();
for (final String peerIdStr : request.getNewPeersList()) {
final PeerId peer = new PeerId();
if (peer.parse(peerIdStr)) {
conf.addPeer(peer);
} else {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
}
}
LOG.info("Receive ChangePeersRequest to {} from {}, new conf is {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), conf);
ctx.node.changePeers(conf, status -> {
if (!status.isOk()) {
done.run(status);
} else {
CliRequests.ChangePeersResponse.Builder rb = CliRequests.ChangePeersResponse.newBuilder();
for (final PeerId peer : oldConf) {
rb.addOldPeers(peer.toString());
}
for (final PeerId peer : conf) {
rb.addNewPeers(peer.toString());
}
done.sendResponse(rb.build());
}
});
return null;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class V2Decoder method decode.
@Override
public LogEntry decode(final byte[] bs) {
if (bs == null || bs.length < LogEntryV2CodecFactory.HEADER_SIZE) {
return null;
}
int i = 0;
for (; i < LogEntryV2CodecFactory.MAGIC_BYTES.length; i++) {
if (bs[i] != LogEntryV2CodecFactory.MAGIC_BYTES[i]) {
return null;
}
}
if (bs[i++] != LogEntryV2CodecFactory.VERSION) {
return null;
}
// Ignored reserved
i += LogEntryV2CodecFactory.RESERVED.length;
try {
final LogOutter.PBLogEntry entry = LogOutter.PBLogEntry.parseFrom(ZeroByteStringHelper.wrap(bs, i, bs.length - i));
final LogEntry log = new LogEntry();
log.setType(entry.getType());
log.getId().setIndex(entry.getIndex());
log.getId().setTerm(entry.getTerm());
if (entry.hasChecksum()) {
log.setChecksum(entry.getChecksum());
}
if (entry.getPeersCount() > 0) {
final List<PeerId> peers = new ArrayList<>(entry.getPeersCount());
for (final ByteString bstring : entry.getPeersList()) {
peers.add(JRaftUtils.getPeerId(AsciiStringUtil.unsafeDecode(bstring)));
}
log.setPeers(peers);
}
if (entry.getOldPeersCount() > 0) {
final List<PeerId> peers = new ArrayList<>(entry.getOldPeersCount());
for (final ByteString bstring : entry.getOldPeersList()) {
peers.add(JRaftUtils.getPeerId(AsciiStringUtil.unsafeDecode(bstring)));
}
log.setOldPeers(peers);
}
if (entry.getLearnersCount() > 0) {
final List<PeerId> peers = new ArrayList<>(entry.getLearnersCount());
for (final ByteString bstring : entry.getLearnersList()) {
peers.add(JRaftUtils.getPeerId(AsciiStringUtil.unsafeDecode(bstring)));
}
log.setLearners(peers);
}
if (entry.getOldLearnersCount() > 0) {
final List<PeerId> peers = new ArrayList<>(entry.getOldLearnersCount());
for (final ByteString bstring : entry.getOldLearnersList()) {
peers.add(JRaftUtils.getPeerId(AsciiStringUtil.unsafeDecode(bstring)));
}
log.setOldLearners(peers);
}
final ByteString data = entry.getData();
if (!data.isEmpty()) {
log.setData(ByteBuffer.wrap(ZeroByteStringHelper.getByteArray(data)));
}
return log;
} catch (final InvalidProtocolBufferException e) {
LOG.error("Fail to decode pb log entry", e);
return null;
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class LogManagerImpl method oldConfFromMeta.
private Configuration oldConfFromMeta(final SnapshotMeta meta) {
final Configuration oldConf = new Configuration();
for (int i = 0; i < meta.getOldPeersCount(); i++) {
final PeerId peer = new PeerId();
peer.parse(meta.getOldPeers(i));
oldConf.addPeer(peer);
}
for (int i = 0; i < meta.getOldLearnersCount(); i++) {
final PeerId peer = new PeerId();
peer.parse(meta.getOldLearners(i));
oldConf.addLearner(peer);
}
return oldConf;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class DefaultDingoRowStore method getLeaderByRegionEngine.
private Endpoint getLeaderByRegionEngine(final String regionId) {
final RegionEngine regionEngine = getRegionEngine(regionId);
if (regionEngine != null) {
final PeerId leader = regionEngine.getLeaderId();
if (leader != null) {
final String raftGroupId = JRaftHelper.getJRaftGroupId(this.pdClient.getClusterName(), regionId);
RouteTable.getInstance().updateLeader(raftGroupId, leader);
return leader.getEndpoint();
}
}
return null;
}
Aggregations