Search in sources :

Example 16 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

the class AtomicRangeGroup method readFromQuorum.

public void readFromQuorum(final String key, RpcContext asyncContext) {
    final byte[] reqContext = new byte[4];
    Bits.putInt(reqContext, 0, requestId.incrementAndGet());
    this.node.readIndex(reqContext, new ReadIndexClosure() {

        @Override
        public void run(Status status, long index, byte[] reqCtx) {
            if (status.isOk()) {
                try {
                    asyncContext.sendResponse(new ValueCommand(fsm.getValue(key)));
                } catch (final KeyNotFoundException e) {
                    asyncContext.sendResponse(GetCommandProcessor.createKeyNotFoundResponse());
                }
            } else {
                asyncContext.sendResponse(new BooleanCommand(false, status.getErrorMsg()));
            }
        }
    });
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) ValueCommand(com.alipay.sofa.jraft.test.atomic.command.ValueCommand) KeyNotFoundException(com.alipay.sofa.jraft.test.atomic.KeyNotFoundException)

Example 17 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

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();
        };
        switch(this.node.getOptions().getApplyTaskMode()) {
            case Blocking:
                this.readIndexQueue.publishEvent(translator);
                break;
            case NonBlocking:
            default:
                if (!this.readIndexQueue.tryPublishEvent(translator)) {
                    final String errorMsg = "Node is busy, has too many read-index requests, queue is full and bufferSize=" + this.readIndexQueue.getBufferSize();
                    Utils.runClosureInThread(closure, new Status(RaftError.EBUSY, errorMsg));
                    this.nodeMetrics.recordTimes("read-index-overload-times", 1);
                    LOG.warn("Node {} ReadOnlyServiceImpl readIndexQueue is overload.", this.node.getNodeId());
                    if (closure == null) {
                        throw new OverloadException(errorMsg);
                    }
                }
                break;
        }
    } catch (final Exception e) {
        Utils.runClosureInThread(closure, new Status(RaftError.EPERM, "Node is down."));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) OverloadException(com.alipay.sofa.jraft.error.OverloadException) ReadIndexState(com.alipay.sofa.jraft.entity.ReadIndexState) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) ReadIndexResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexResponse) LoggerFactory(org.slf4j.LoggerFactory) ReadIndexRequest(com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest) LastAppliedLogIndexListener(com.alipay.sofa.jraft.FSMCaller.LastAppliedLogIndexListener) Utils(com.alipay.sofa.jraft.util.Utils) ArrayList(java.util.ArrayList) BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RaftError(com.alipay.sofa.jraft.error.RaftError) EventHandler(com.lmax.disruptor.EventHandler) LogExceptionHandler(com.alipay.sofa.jraft.util.LogExceptionHandler) OnlyForTest(com.alipay.sofa.jraft.util.OnlyForTest) ZeroByteStringHelper(com.google.protobuf.ZeroByteStringHelper) Logger(org.slf4j.Logger) DisruptorMetricSet(com.alipay.sofa.jraft.util.DisruptorMetricSet) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) RingBuffer(com.lmax.disruptor.RingBuffer) DisruptorBuilder(com.alipay.sofa.jraft.util.DisruptorBuilder) ProducerType(com.lmax.disruptor.dsl.ProducerType) Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ReadIndexStatus(com.alipay.sofa.jraft.entity.ReadIndexStatus) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ReadOnlyServiceOptions(com.alipay.sofa.jraft.option.ReadOnlyServiceOptions) List(java.util.List) Lock(java.util.concurrent.locks.Lock) TreeMap(java.util.TreeMap) EventTranslator(com.lmax.disruptor.EventTranslator) FSMCaller(com.alipay.sofa.jraft.FSMCaller) Bytes(com.alipay.sofa.jraft.util.Bytes) ReadOnlyService(com.alipay.sofa.jraft.ReadOnlyService) EventFactory(com.lmax.disruptor.EventFactory) RaftException(com.alipay.sofa.jraft.error.RaftException) Disruptor(com.lmax.disruptor.dsl.Disruptor) RpcResponseClosureAdapter(com.alipay.sofa.jraft.rpc.RpcResponseClosureAdapter) Bytes(com.alipay.sofa.jraft.util.Bytes) OverloadException(com.alipay.sofa.jraft.error.OverloadException) OverloadException(com.alipay.sofa.jraft.error.OverloadException) RaftException(com.alipay.sofa.jraft.error.RaftException)

Example 18 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project sofa-jraft by sofastack.

the class ReadOnlyServiceImpl method reportError.

private void reportError(final ReadIndexStatus status, final Status st) {
    final long nowMs = Utils.monotonicMs();
    final List<ReadIndexState> states = status.getStates();
    final int taskCount = states.size();
    for (int i = 0; i < taskCount; i++) {
        final ReadIndexState task = states.get(i);
        final ReadIndexClosure done = task.getDone();
        if (done != null) {
            this.nodeMetrics.recordLatency("read-index", nowMs - task.getStartTimeMs());
            done.run(st);
        }
    }
}
Also used : ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ReadIndexState(com.alipay.sofa.jraft.entity.ReadIndexState)

Example 19 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project mmqtt by MrHKing.

the class JRaftServer method get.

CompletableFuture<Response> get(final ReadRequest request) {
    final String group = request.getGroup();
    CompletableFuture<Response> future = new CompletableFuture<>();
    final RaftGroupTuple tuple = findTupleByGroup(group);
    if (Objects.isNull(tuple)) {
        future.completeExceptionally(new NoSuchRaftGroupException(group));
        return future;
    }
    final Node node = tuple.node;
    final RequestProcessor processor = tuple.processor;
    try {
        node.readIndex(BytesUtil.EMPTY_BYTES, new ReadIndexClosure() {

            @Override
            public void run(Status status, long index, byte[] reqCtx) {
                if (status.isOk()) {
                    try {
                        Response response = processor.onRequest(request);
                        future.complete(response);
                    } catch (Throwable t) {
                        MetricsMonitor.raftReadIndexFailed();
                        future.completeExceptionally(new ConsistencyException("The conformance protocol is temporarily unavailable for reading", t));
                    }
                    return;
                }
                MetricsMonitor.raftReadIndexFailed();
                Loggers.RAFT.error("ReadIndex has error : {}", status.getErrorMsg());
                future.completeExceptionally(new ConsistencyException("The conformance protocol is temporarily unavailable for reading, " + status.getErrorMsg()));
            }
        });
        return future;
    } catch (Throwable e) {
        MetricsMonitor.raftReadFromLeader();
        Loggers.RAFT.warn("Raft linear read failed, go to Leader read logic : {}", e.toString());
        // run raft read
        readFromLeader(request, future);
        return future;
    }
}
Also used : Response(org.monkey.mmq.core.entity.Response) CompletableFuture(java.util.concurrent.CompletableFuture) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) RequestProcessor(org.monkey.mmq.core.consistency.RequestProcessor)

Example 20 with ReadIndexClosure

use of com.alipay.sofa.jraft.closure.ReadIndexClosure in project incubator-hugegraph by apache.

the class RaftNode method waitStarted.

protected void waitStarted(int timeout) {
    String group = this.context.group();
    ReadIndexClosure readIndexClosure = new ReadIndexClosure() {

        @Override
        public void run(Status status, long index, byte[] reqCtx) {
            RaftNode.this.started.set(status.isOk());
        }
    };
    long beginTime = System.currentTimeMillis();
    while (true) {
        this.node.readIndex(BytesUtil.EMPTY_BYTES, readIndexClosure);
        if (this.started.get()) {
            break;
        }
        try {
            Thread.sleep(RaftSharedContext.POLL_INTERVAL);
        } catch (InterruptedException e) {
            LOG.info("Waiting for heartbeat is interrupted: {}", e);
        }
        long consumedTime = System.currentTimeMillis() - beginTime;
        if (timeout > 0 && consumedTime >= timeout) {
            throw new BackendException("Waiting for raft group '%s' heartbeat timeout(%sms)", group, consumedTime);
        }
        LOG.warn("Waiting for raft group '{}' heartbeat cost {}s", group, consumedTime / 1000.0);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) BackendException(com.baidu.hugegraph.backend.BackendException)

Aggregations

ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)20 Status (com.alipay.sofa.jraft.Status)17 CountDownLatch (java.util.concurrent.CountDownLatch)11 Test (org.junit.Test)10 ReadIndexStatus (com.alipay.sofa.jraft.entity.ReadIndexStatus)7 ReadIndexRequest (com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest)6 Node (com.alipay.sofa.jraft.Node)5 ArgumentMatcher (org.mockito.ArgumentMatcher)5 PeerId (com.alipay.sofa.jraft.entity.PeerId)4 ReadIndexState (com.alipay.sofa.jraft.entity.ReadIndexState)4 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)4 Bytes (com.alipay.sofa.jraft.util.Bytes)2 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 RequestProcessor (com.alibaba.nacos.consistency.RequestProcessor)1 Response (com.alibaba.nacos.consistency.entity.Response)1 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1 NoSuchRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)1 FSMCaller (com.alipay.sofa.jraft.FSMCaller)1 LastAppliedLogIndexListener (com.alipay.sofa.jraft.FSMCaller.LastAppliedLogIndexListener)1