use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class GetPeersRequestProcessor method processRequest0.
@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.GetPeersRequest request, final RpcRequestClosure done) {
final List<PeerId> peers;
final List<PeerId> learners;
if (request.hasOnlyAlive() && request.getOnlyAlive()) {
peers = ctx.node.listAlivePeers();
learners = ctx.node.listAliveLearners();
} else {
peers = ctx.node.listPeers();
learners = ctx.node.listLearners();
}
final CliRequests.GetPeersResponse.Builder builder = CliRequests.GetPeersResponse.newBuilder();
for (final PeerId peerId : peers) {
builder.addPeers(peerId.toString());
}
for (final PeerId peerId : learners) {
builder.addLearners(peerId.toString());
}
return builder.build();
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class LogManagerImpl method confFromMeta.
private Configuration confFromMeta(final SnapshotMeta meta) {
final Configuration conf = new Configuration();
for (int i = 0; i < meta.getPeersCount(); i++) {
final PeerId peer = new PeerId();
peer.parse(meta.getPeers(i));
conf.addPeer(peer);
}
for (int i = 0; i < meta.getLearnersCount(); i++) {
final PeerId peer = new PeerId();
peer.parse(meta.getLearners(i));
conf.addLearner(peer);
}
return conf;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class NodeRequestProcessor method processRequest.
@Override
public Message processRequest(final T request, final RpcRequestClosure done) {
final PeerId peer = new PeerId();
final String peerIdStr = getPeerId(request);
if (peer.parse(peerIdStr)) {
final String groupId = getGroupId(request);
final Node node = NodeManager.getInstance().get(groupId, peer);
if (node != null) {
return processRequest0((RaftServerService) node, request, done);
} else {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.ENOENT, "Peer id not found: %s, group: %s", peerIdStr, groupId);
}
} else {
return //
RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peerId: %s", peerIdStr);
}
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class V2Encoder method encode.
@Override
public byte[] encode(final LogEntry log) {
Requires.requireNonNull(log, "Null log");
final LogId logId = log.getId();
final PBLogEntry.Builder builder = //
PBLogEntry.newBuilder().setType(//
log.getType()).setIndex(//
logId.getIndex()).setTerm(logId.getTerm());
final List<PeerId> peers = log.getPeers();
if (hasPeers(peers)) {
encodePeers(builder, peers);
}
final List<PeerId> oldPeers = log.getOldPeers();
if (hasPeers(oldPeers)) {
encodeOldPeers(builder, oldPeers);
}
final List<PeerId> learners = log.getLearners();
if (hasPeers(learners)) {
encodeLearners(builder, learners);
}
final List<PeerId> oldLearners = log.getOldLearners();
if (hasPeers(oldLearners)) {
encodeOldLearners(builder, oldLearners);
}
if (log.hasChecksum()) {
builder.setChecksum(log.getChecksum());
}
builder.setData(log.getData() != null ? ZeroByteStringHelper.wrap(log.getData()) : ByteString.EMPTY);
final PBLogEntry pbLogEntry = builder.build();
final int bodyLen = pbLogEntry.getSerializedSize();
final byte[] ret = new byte[LogEntryV2CodecFactory.HEADER_SIZE + bodyLen];
// write header
int i = 0;
for (; i < LogEntryV2CodecFactory.MAGIC_BYTES.length; i++) {
ret[i] = LogEntryV2CodecFactory.MAGIC_BYTES[i];
}
ret[i++] = LogEntryV2CodecFactory.VERSION;
// avoid memory copy for only 3 bytes
for (; i < LogEntryV2CodecFactory.HEADER_SIZE; i++) {
ret[i] = LogEntryV2CodecFactory.RESERVED[i - LogEntryV2CodecFactory.MAGIC_BYTES.length - 1];
}
// write body
writeToByteArray(pbLogEntry, ret, i, bodyLen);
return ret;
}
use of io.dingodb.raft.entity.PeerId in project dingo by dingodb.
the class AbstractPlacementDriverClient method getLuckyPeer.
@Override
public Endpoint getLuckyPeer(final String regionId, final boolean forceRefresh, final long timeoutMillis, final Endpoint unExpect) {
final String raftGroupId = JRaftHelper.getJRaftGroupId(this.clusterName, regionId);
final RouteTable routeTable = RouteTable.getInstance();
if (forceRefresh) {
final long deadline = System.currentTimeMillis() + timeoutMillis;
final StringBuilder error = new StringBuilder();
// or in the 'leader-transfer' state, it needs to be re-tried
for (; ; ) {
try {
final Status st = routeTable.refreshConfiguration(this.cliClientService, raftGroupId, 5000);
if (st.isOk()) {
break;
}
error.append(st.toString());
} catch (final InterruptedException e) {
ThrowUtil.throwException(e);
} catch (final TimeoutException e) {
error.append(e.getMessage());
}
if (System.currentTimeMillis() < deadline) {
LOG.debug("Fail to get peers, retry again, {}.", error);
error.append(", ");
try {
Thread.sleep(5);
} catch (final InterruptedException e) {
ThrowUtil.throwException(e);
}
} else {
throw new RouteTableException(error.toString());
}
}
}
final Configuration configs = routeTable.getConfiguration(raftGroupId);
if (configs == null) {
throw new RouteTableException("empty configs in group: " + raftGroupId);
}
final List<PeerId> peerList = configs.getPeers();
if (peerList == null || peerList.isEmpty()) {
throw new RouteTableException("empty peers in group: " + raftGroupId);
}
final int size = peerList.size();
if (size == 1) {
return peerList.get(0).getEndpoint();
}
final RoundRobinLoadBalancer balancer = RoundRobinLoadBalancer.getInstance(regionId);
for (int i = 0; i < size; i++) {
final PeerId candidate = balancer.select(peerList);
final Endpoint luckyOne = candidate.getEndpoint();
if (!luckyOne.equals(unExpect)) {
return luckyOne;
}
}
throw new RouteTableException("have no choice in group(peers): " + raftGroupId);
}
Aggregations