Search in sources :

Example 16 with Response

use of com.alibaba.nacos.consistency.entity.Response 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)

Example 17 with Response

use of com.alibaba.nacos.consistency.entity.Response 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)

Example 18 with Response

use of com.alibaba.nacos.consistency.entity.Response in project nacos by alibaba.

the class AbstractProcessor method execute.

protected void execute(JRaftServer server, final RpcContext asyncCtx, final Message message, final JRaftServer.RaftGroupTuple tuple) {
    FailoverClosure closure = new FailoverClosure() {

        Response data;

        Throwable ex;

        @Override
        public void setResponse(Response data) {
            this.data = data;
        }

        @Override
        public void setThrowable(Throwable throwable) {
            this.ex = throwable;
        }

        @Override
        public void run(Status status) {
            if (Objects.nonNull(ex)) {
                Loggers.RAFT.error("execute has error : ", ex);
                asyncCtx.sendResponse(Response.newBuilder().setErrMsg(ex.toString()).setSuccess(false).build());
            } else {
                asyncCtx.sendResponse(data);
            }
        }
    };
    server.applyOperation(tuple.getNode(), message, closure);
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Status(com.alipay.sofa.jraft.Status) FailoverClosure(com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure)

Example 19 with Response

use of com.alibaba.nacos.consistency.entity.Response in project nacos by alibaba.

the class PersistentClientOperationServiceImplTest method testOnApply.

@Test
public void testOnApply() {
    PersistentClientOperationServiceImpl.InstanceStoreRequest request = new PersistentClientOperationServiceImpl.InstanceStoreRequest();
    Service service1 = Service.newService("A", "B", "C");
    request.setService(service1);
    request.setClientId("xxxx");
    request.setInstance(new Instance());
    Mockito.when(serializer.deserialize(Mockito.any())).thenReturn(request);
    Mockito.when(clientManager.contains(Mockito.anyString())).thenReturn(true);
    IpPortBasedClient ipPortBasedClient = Mockito.mock(IpPortBasedClient.class);
    Mockito.when(clientManager.getClient(Mockito.anyString())).thenReturn(ipPortBasedClient);
    WriteRequest writeRequest = WriteRequest.newBuilder().setOperation(DataOperation.ADD.name()).build();
    Response response = persistentClientOperationServiceImpl.onApply(writeRequest);
    Assert.assertTrue(response.getSuccess());
    writeRequest = WriteRequest.newBuilder().setOperation(DataOperation.DELETE.name()).build();
    response = persistentClientOperationServiceImpl.onApply(writeRequest);
    Assert.assertTrue(response.getSuccess());
    ServiceManager.getInstance().removeSingleton(service1);
    writeRequest = WriteRequest.newBuilder().setOperation(DataOperation.VERIFY.name()).build();
    response = persistentClientOperationServiceImpl.onApply(writeRequest);
    Assert.assertFalse(response.getSuccess());
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Instance(com.alibaba.nacos.api.naming.pojo.Instance) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) IpPortBasedClient(com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient) Test(org.junit.Test)

Aggregations

Response (com.alibaba.nacos.consistency.entity.Response)19 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)10 NacosException (com.alibaba.nacos.api.exception.NacosException)9 NacosRuntimeException (com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)7 NJdbcException (com.alibaba.nacos.config.server.exception.NJdbcException)7 TimeoutException (java.util.concurrent.TimeoutException)7 DataAccessException (org.springframework.dao.DataAccessException)7 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)7 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)7 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)5 Test (org.junit.Test)5 ReadRequest (com.alibaba.nacos.consistency.entity.ReadRequest)4 ArrayList (java.util.ArrayList)4 RequestProcessor4CP (com.alibaba.nacos.consistency.cp.RequestProcessor4CP)3 Status (com.alipay.sofa.jraft.Status)3 ByteString (com.google.protobuf.ByteString)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 NotifyCenter (com.alibaba.nacos.common.notify.NotifyCenter)2 ExceptionUtil (com.alibaba.nacos.common.utils.ExceptionUtil)2 JacksonUtils (com.alibaba.nacos.common.utils.JacksonUtils)2