Search in sources :

Example 1 with NoSuchRaftGroupException

use of com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException in project nacos by alibaba.

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 : {}, go to Leader read.", status.getErrorMsg());
                MetricsMonitor.raftReadFromLeader();
                readFromLeader(request, future);
            }
        });
        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 : Status(com.alipay.sofa.jraft.Status) Node(com.alipay.sofa.jraft.Node) NoSuchRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException) Response(com.alibaba.nacos.consistency.entity.Response) CompletableFuture(java.util.concurrent.CompletableFuture) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) RequestProcessor(com.alibaba.nacos.consistency.RequestProcessor)

Aggregations

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 Node (com.alipay.sofa.jraft.Node)1 Status (com.alipay.sofa.jraft.Status)1 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)1 CompletableFuture (java.util.concurrent.CompletableFuture)1