Search in sources :

Example 41 with PeerId

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

Example 42 with PeerId

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

Example 43 with PeerId

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

Example 44 with PeerId

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;
}
Also used : PBLogEntry(io.dingodb.raft.entity.codec.v2.LogOutter.PBLogEntry) LogId(io.dingodb.raft.entity.LogId) PeerId(io.dingodb.raft.entity.PeerId)

Example 45 with PeerId

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);
}
Also used : Status(io.dingodb.raft.Status) Configuration(io.dingodb.raft.conf.Configuration) RoundRobinLoadBalancer(io.dingodb.store.row.client.RoundRobinLoadBalancer) RouteTableException(io.dingodb.store.row.errors.RouteTableException) Endpoint(io.dingodb.raft.util.Endpoint) RegionRouteTable(io.dingodb.store.row.client.RegionRouteTable) RouteTable(io.dingodb.raft.RouteTable) Endpoint(io.dingodb.raft.util.Endpoint) TimeoutException(java.util.concurrent.TimeoutException) 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