Search in sources :

Example 21 with Status

use of io.dingodb.raft.Status in project dingo by dingodb.

the class ReadOnlyServiceImpl method addRequest.

@Override
public void addRequest(final byte[] reqCtx, final ReadIndexClosure closure) {
    if (this.shutdownLatch != null) {
        Utils.runClosureInThread(closure, new Status(RaftError.EHOSTDOWN, "Was stopped"));
        throw new IllegalStateException("Service already shutdown.");
    }
    try {
        EventTranslator<ReadIndexEvent> translator = (event, sequence) -> {
            event.done = closure;
            event.requestContext = new Bytes(reqCtx);
            event.startTime = Utils.monotonicMs();
        };
        int retryTimes = 0;
        while (true) {
            if (this.readIndexQueue.tryPublishEvent(translator)) {
                break;
            } else {
                retryTimes++;
                if (retryTimes > MAX_ADD_REQUEST_RETRY_TIMES) {
                    Utils.runClosureInThread(closure, new Status(RaftError.EBUSY, "Node is busy, has too many read-only requests."));
                    this.nodeMetrics.recordTimes("read-index-overload-times", 1);
                    LOG.warn("Node {} ReadOnlyServiceImpl readIndexQueue is overload.", this.node.getNodeId());
                    return;
                }
                ThreadHelper.onSpinWait();
            }
        }
    } catch (final Exception e) {
        Utils.runClosureInThread(closure, new Status(RaftError.EPERM, "Node is down."));
    }
}
Also used : Status(io.dingodb.raft.Status) ReadIndexStatus(io.dingodb.raft.entity.ReadIndexStatus) RaftException(io.dingodb.raft.error.RaftException) RaftError(io.dingodb.raft.error.RaftError) LoggerFactory(org.slf4j.LoggerFactory) LogExceptionHandler(io.dingodb.raft.util.LogExceptionHandler) ArrayList(java.util.ArrayList) Bytes(io.dingodb.raft.util.Bytes) BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ThreadHelper(io.dingodb.raft.util.ThreadHelper) ReadOnlyService(io.dingodb.raft.ReadOnlyService) OnlyForTest(io.dingodb.raft.util.OnlyForTest) EventHandler(com.lmax.disruptor.EventHandler) RaftOptions(io.dingodb.raft.option.RaftOptions) ReadIndexClosure(io.dingodb.raft.closure.ReadIndexClosure) ZeroByteStringHelper(com.google.protobuf.ZeroByteStringHelper) DisruptorMetricSet(io.dingodb.raft.util.DisruptorMetricSet) Logger(org.slf4j.Logger) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) RingBuffer(com.lmax.disruptor.RingBuffer) ProducerType(com.lmax.disruptor.dsl.ProducerType) Status(io.dingodb.raft.Status) Utils(io.dingodb.raft.util.Utils) Executors(java.util.concurrent.Executors) FSMCaller(io.dingodb.raft.FSMCaller) ReadIndexStatus(io.dingodb.raft.entity.ReadIndexStatus) DisruptorBuilder(io.dingodb.raft.util.DisruptorBuilder) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) RpcResponseClosureAdapter(io.dingodb.raft.rpc.RpcResponseClosureAdapter) List(java.util.List) Lock(java.util.concurrent.locks.Lock) EventTranslator(com.lmax.disruptor.EventTranslator) TreeMap(java.util.TreeMap) ReadOnlyServiceOptions(io.dingodb.raft.option.ReadOnlyServiceOptions) ReadIndexState(io.dingodb.raft.entity.ReadIndexState) RpcRequests(io.dingodb.raft.rpc.RpcRequests) EventFactory(com.lmax.disruptor.EventFactory) Disruptor(com.lmax.disruptor.dsl.Disruptor) Bytes(io.dingodb.raft.util.Bytes) RaftException(io.dingodb.raft.error.RaftException)

Example 22 with Status

use of io.dingodb.raft.Status in project dingo by dingodb.

the class ReadOnlyServiceImpl method join.

@Override
public void join() throws InterruptedException {
    if (this.shutdownLatch != null) {
        this.shutdownLatch.await();
    }
    this.readIndexDisruptor.shutdown();
    resetPendingStatusError(new Status(RaftError.ESTOP, "Node is quit."));
    this.scheduledExecutorService.awaitTermination(5, TimeUnit.SECONDS);
}
Also used : Status(io.dingodb.raft.Status) ReadIndexStatus(io.dingodb.raft.entity.ReadIndexStatus)

Example 23 with Status

use of io.dingodb.raft.Status in project dingo by dingodb.

the class Replicator method onHeartbeatReturned.

static void onHeartbeatReturned(final ThreadId id, final Status status, final RpcRequests.AppendEntriesRequest request, final RpcRequests.AppendEntriesResponse response, final long rpcSendTime) {
    if (id == null) {
        // replicator already was destroyed.
        return;
    }
    final long startTimeMs = Utils.nowMs();
    Replicator r;
    if ((r = (Replicator) id.lock()) == null) {
        return;
    }
    boolean doUnlock = true;
    try {
        final boolean isLogDebugEnabled = LOG.isDebugEnabled();
        StringBuilder sb = null;
        if (isLogDebugEnabled) {
            sb = // 
            new StringBuilder("Node ").append(// 
            r.options.getGroupId()).append(// 
            ':').append(// 
            r.options.getServerId()).append(// 
            " received HeartbeatResponse from ").append(// 
            r.options.getPeerId()).append(// 
            " prevLogIndex=").append(// 
            request.getPrevLogIndex()).append(// 
            " prevLogTerm=").append(request.getPrevLogTerm());
        }
        if (!status.isOk()) {
            if (isLogDebugEnabled) {
                // 
                sb.append(" fail, sleep, status=").append(status);
                LOG.debug(sb.toString());
            }
            r.setState(State.Probe);
            notifyReplicatorStatusListener(r, ReplicatorEvent.ERROR, status);
            if (++r.consecutiveErrorTimes % 10 == 0) {
                LOG.warn("Fail to issue RPC to {}, consecutiveErrorTimes={}, error={}", r.options.getPeerId(), r.consecutiveErrorTimes, status);
            }
            r.startHeartbeatTimer(startTimeMs);
            return;
        }
        r.consecutiveErrorTimes = 0;
        if (response.getTerm() > r.options.getTerm()) {
            if (isLogDebugEnabled) {
                // 
                sb.append(" fail, greater term ").append(// 
                response.getTerm()).append(// 
                " expect term ").append(r.options.getTerm());
                LOG.debug(sb.toString());
            }
            final NodeImpl node = r.options.getNode();
            r.notifyOnCaughtUp(RaftError.EPERM.getNumber(), true);
            r.destroy();
            node.increaseTermTo(response.getTerm(), new Status(RaftError.EHIGHERTERMRESPONSE, "Leader receives higher term heartbeat_response from peer:%s", r.options.getPeerId()));
            return;
        }
        if (!response.getSuccess() && response.hasLastLogIndex()) {
            if (isLogDebugEnabled) {
                // 
                sb.append(" fail, response term ").append(// 
                response.getTerm()).append(// 
                " lastLogIndex ").append(response.getLastLogIndex());
                LOG.debug(sb.toString());
            }
            LOG.warn("Heartbeat to peer {} failure, try to send a probe request.", r.options.getPeerId());
            doUnlock = false;
            r.sendProbeRequest();
            r.startHeartbeatTimer(startTimeMs);
            return;
        }
        if (isLogDebugEnabled) {
            LOG.debug(sb.toString());
        }
        if (rpcSendTime > r.lastRpcSendTimestamp) {
            r.lastRpcSendTimestamp = rpcSendTime;
        }
        r.startHeartbeatTimer(startTimeMs);
    } finally {
        if (doUnlock) {
            id.unlock();
        }
    }
}
Also used : Status(io.dingodb.raft.Status)

Example 24 with Status

use of io.dingodb.raft.Status in project dingo by dingodb.

the class ResetPeerRequestProcessor method processRequest0.

@Override
protected Message processRequest0(final CliRequestContext ctx, final CliRequests.ResetPeerRequest request, final RpcRequestClosure done) {
    final Configuration newConf = new Configuration();
    for (final String peerIdStr : request.getNewPeersList()) {
        final PeerId peer = new PeerId();
        if (peer.parse(peerIdStr)) {
            newConf.addPeer(peer);
        } else {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
        }
    }
    LOG.info("Receive ResetPeerRequest to {} from {}, new conf is {}", ctx.node.getNodeId(), done.getRpcCtx().getRemoteAddress(), newConf);
    final Status st = ctx.node.resetPeers(newConf);
    return // 
    RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st);
}
Also used : Status(io.dingodb.raft.Status) Configuration(io.dingodb.raft.conf.Configuration) PeerId(io.dingodb.raft.entity.PeerId)

Example 25 with Status

use of io.dingodb.raft.Status in project dingo by dingodb.

the class Replicator method onTimeoutNowReturned.

@SuppressWarnings("unused")
static void onTimeoutNowReturned(final ThreadId id, final Status status, final RpcRequests.TimeoutNowRequest request, final RpcRequests.TimeoutNowResponse response, final boolean stopAfterFinish) {
    final Replicator r = (Replicator) id.lock();
    if (r == null) {
        return;
    }
    final boolean isLogDebugEnabled = LOG.isDebugEnabled();
    StringBuilder sb = null;
    if (isLogDebugEnabled) {
        sb = // 
        new StringBuilder("Node ").append(r.options.getGroupId()).append(":").append(// 
        r.options.getServerId()).append(// 
        " received TimeoutNowResponse from ").append(r.options.getPeerId());
    }
    if (!status.isOk()) {
        if (isLogDebugEnabled) {
            sb.append(" fail:").append(status);
            LOG.debug(sb.toString());
        }
        notifyReplicatorStatusListener(r, ReplicatorEvent.ERROR, status);
        if (stopAfterFinish) {
            r.notifyOnCaughtUp(RaftError.ESTOP.getNumber(), true);
            r.destroy();
        } else {
            id.unlock();
        }
        return;
    }
    if (isLogDebugEnabled) {
        sb.append(response.getSuccess() ? " success" : " fail");
        LOG.debug(sb.toString());
    }
    if (response.getTerm() > r.options.getTerm()) {
        final NodeImpl node = r.options.getNode();
        r.notifyOnCaughtUp(RaftError.EPERM.getNumber(), true);
        r.destroy();
        node.increaseTermTo(response.getTerm(), new Status(RaftError.EHIGHERTERMRESPONSE, "Leader receives higher term timeout_now_response from peer:%s", r.options.getPeerId()));
        return;
    }
    if (stopAfterFinish) {
        r.notifyOnCaughtUp(RaftError.ESTOP.getNumber(), true);
        r.destroy();
    } else {
        id.unlock();
    }
}
Also used : Status(io.dingodb.raft.Status)

Aggregations

Status (io.dingodb.raft.Status)112 PeerId (io.dingodb.raft.entity.PeerId)31 BaseKVStoreClosure (io.dingodb.store.row.storage.BaseKVStoreClosure)21 Message (com.google.protobuf.Message)19 Configuration (io.dingodb.raft.conf.Configuration)12 RpcRequests (io.dingodb.raft.rpc.RpcRequests)12 JRaftException (io.dingodb.raft.error.JRaftException)11 LogId (io.dingodb.raft.entity.LogId)10 RaftException (io.dingodb.raft.error.RaftException)9 ArrayList (java.util.ArrayList)9 ConfigurationEntry (io.dingodb.raft.conf.ConfigurationEntry)7 LogEntry (io.dingodb.raft.entity.LogEntry)7 ByteBuffer (java.nio.ByteBuffer)7 RaftOutter (io.dingodb.raft.entity.RaftOutter)6 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)5 Disruptor (com.lmax.disruptor.dsl.Disruptor)5 ProducerType (com.lmax.disruptor.dsl.ProducerType)5 Closure (io.dingodb.raft.Closure)5 FSMCaller (io.dingodb.raft.FSMCaller)5 Node (io.dingodb.raft.Node)5