Search in sources :

Example 1 with NoLeaderException

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

the class JRaftServer method invokeToLeader.

private void invokeToLeader(final String group, final Message request, final int timeoutMillis, FailoverClosure closure) {
    try {
        final Endpoint leaderIp = Optional.ofNullable(getLeader(group)).orElseThrow(() -> new NoLeaderException(group)).getEndpoint();
        cliClientService.getRpcClient().invokeAsync(leaderIp, request, new InvokeCallback() {

            @Override
            public void complete(Object o, Throwable ex) {
                if (Objects.nonNull(ex)) {
                    closure.setThrowable(ex);
                    closure.run(new Status(RaftError.UNKNOWN, ex.getMessage()));
                    return;
                }
                if (!((Response) o).getSuccess()) {
                    closure.setThrowable(new IllegalStateException(((Response) o).getErrMsg()));
                    closure.run(new Status(RaftError.UNKNOWN, ((Response) o).getErrMsg()));
                    return;
                }
                closure.setResponse((Response) o);
                closure.run(Status.OK());
            }

            @Override
            public Executor executor() {
                return RaftExecutor.getRaftCliServiceExecutor();
            }
        }, timeoutMillis);
    } catch (Exception e) {
        closure.setThrowable(e);
        closure.run(new Status(RaftError.UNKNOWN, e.toString()));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Response(com.alibaba.nacos.consistency.entity.Response) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback) Executor(java.util.concurrent.Executor) RaftExecutor(com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor) Endpoint(com.alipay.sofa.jraft.util.Endpoint) NoLeaderException(com.alibaba.nacos.core.distributed.raft.exception.NoLeaderException) JRaftException(com.alibaba.nacos.core.distributed.raft.exception.JRaftException) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) DuplicateRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException) NoLeaderException(com.alibaba.nacos.core.distributed.raft.exception.NoLeaderException) NoSuchRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)

Aggregations

Response (com.alibaba.nacos.consistency.entity.Response)1 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1 DuplicateRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException)1 JRaftException (com.alibaba.nacos.core.distributed.raft.exception.JRaftException)1 NoLeaderException (com.alibaba.nacos.core.distributed.raft.exception.NoLeaderException)1 NoSuchRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)1 RaftExecutor (com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor)1 Status (com.alipay.sofa.jraft.Status)1 InvokeCallback (com.alipay.sofa.jraft.rpc.InvokeCallback)1 Endpoint (com.alipay.sofa.jraft.util.Endpoint)1 Executor (java.util.concurrent.Executor)1