Search in sources :

Example 26 with PeerId

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);
    }
}
Also used : Status(io.dingodb.raft.Status) Node(io.dingodb.raft.Node) PeerId(io.dingodb.raft.entity.PeerId)

Example 27 with PeerId

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;
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) PeerId(io.dingodb.raft.entity.PeerId)

Example 28 with PeerId

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;
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) LogEntry(io.dingodb.raft.entity.LogEntry) PeerId(io.dingodb.raft.entity.PeerId)

Example 29 with PeerId

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;
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) PeerId(io.dingodb.raft.entity.PeerId)

Example 30 with PeerId

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;
}
Also used : RegionEngine(io.dingodb.store.row.RegionEngine) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

PeerId (io.dingodb.raft.entity.PeerId)72 Status (io.dingodb.raft.Status)27 Configuration (io.dingodb.raft.conf.Configuration)15 Message (com.google.protobuf.Message)12 ArrayList (java.util.ArrayList)12 JRaftException (io.dingodb.raft.error.JRaftException)10 LogId (io.dingodb.raft.entity.LogId)9 CliRequests (io.dingodb.raft.rpc.CliRequests)8 Node (io.dingodb.raft.Node)6 RpcRequests (io.dingodb.raft.rpc.RpcRequests)4 Endpoint (io.dingodb.raft.util.Endpoint)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 LogEntry (io.dingodb.raft.entity.LogEntry)3 NodeOptions (io.dingodb.raft.option.NodeOptions)3 ByteBuffer (java.nio.ByteBuffer)3 TimeoutException (java.util.concurrent.TimeoutException)3 RaftOutter (io.dingodb.raft.entity.RaftOutter)2 RaftException (io.dingodb.raft.error.RaftException)2 ThreadId (io.dingodb.raft.util.ThreadId)2 ExecutionException (java.util.concurrent.ExecutionException)2