Search in sources :

Example 1 with FailoverClosure

use of com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure in project nacos by alibaba.

the class JRaftServer method applyOperation.

public void applyOperation(Node node, Message data, FailoverClosure closure) {
    final Task task = new Task();
    task.setDone(new NacosClosure(data, status -> {
        NacosClosure.NacosStatus nacosStatus = (NacosClosure.NacosStatus) status;
        closure.setThrowable(nacosStatus.getThrowable());
        closure.setResponse(nacosStatus.getResponse());
        closure.run(nacosStatus);
    }));
    // add request type field at the head of task data.
    byte[] requestTypeFieldBytes = new byte[2];
    requestTypeFieldBytes[0] = ProtoMessageUtil.REQUEST_TYPE_FIELD_TAG;
    if (data instanceof ReadRequest) {
        requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ;
    } else {
        requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE;
    }
    byte[] dataBytes = data.toByteArray();
    task.setData((ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes).put(dataBytes).position(0));
    node.apply(task);
}
Also used : LoggerUtils(com.alibaba.nacos.common.utils.LoggerUtils) FailoverClosure(com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) Random(java.util.Random) ThreadUtils(com.alibaba.nacos.common.utils.ThreadUtils) ByteBuffer(java.nio.ByteBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetricsMonitor(com.alibaba.nacos.core.monitor.MetricsMonitor) Map(java.util.Map) Endpoint(com.alipay.sofa.jraft.util.Endpoint) JRaftUtils(com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils) FailoverClosureImpl(com.alibaba.nacos.core.distributed.raft.utils.FailoverClosureImpl) RestResult(com.alibaba.nacos.common.model.RestResult) SerializeFactory(com.alibaba.nacos.consistency.SerializeFactory) PeerId(com.alipay.sofa.jraft.entity.PeerId) EnvUtil(com.alibaba.nacos.sys.env.EnvUtil) RouteTable(com.alipay.sofa.jraft.RouteTable) Configuration(com.alipay.sofa.jraft.conf.Configuration) ProtoMessageUtil(com.alibaba.nacos.consistency.ProtoMessageUtil) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) Collection(java.util.Collection) InternetAddressUtil(com.alibaba.nacos.common.utils.InternetAddressUtil) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) Objects(java.util.Objects) List(java.util.List) JRaftException(com.alibaba.nacos.core.distributed.raft.exception.JRaftException) CliOptions(com.alipay.sofa.jraft.option.CliOptions) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) JRaftConstants(com.alibaba.nacos.core.distributed.raft.utils.JRaftConstants) StringUtils(com.alibaba.nacos.common.utils.StringUtils) CollectionUtils(org.springframework.util.CollectionUtils) RpcProcessor(com.alipay.sofa.jraft.rpc.RpcProcessor) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) Optional(java.util.Optional) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) JustForTest(com.alibaba.nacos.common.JustForTest) Loggers(com.alibaba.nacos.core.utils.Loggers) HashSet(java.util.HashSet) BiConsumer(java.util.function.BiConsumer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) RaftError(com.alipay.sofa.jraft.error.RaftError) ConvertUtils(com.alibaba.nacos.common.utils.ConvertUtils) 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) Serializer(com.alibaba.nacos.consistency.Serializer) Executor(java.util.concurrent.Executor) RequestProcessor(com.alibaba.nacos.consistency.RequestProcessor) RaftServiceFactory(com.alipay.sofa.jraft.RaftServiceFactory) Status(com.alipay.sofa.jraft.Status) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) RaftOptionsBuilder(com.alibaba.nacos.core.distributed.raft.utils.RaftOptionsBuilder) Response(com.alibaba.nacos.consistency.entity.Response) TimeUnit(java.util.concurrent.TimeUnit) RaftExecutor(com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor) CliServiceImpl(com.alipay.sofa.jraft.core.CliServiceImpl) Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) Paths(java.nio.file.Paths) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback) CliService(com.alipay.sofa.jraft.CliService) Message(com.google.protobuf.Message) Collections(java.util.Collections) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) Task(com.alipay.sofa.jraft.entity.Task) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest)

Example 2 with FailoverClosure

use of com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure 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)

Aggregations

Response (com.alibaba.nacos.consistency.entity.Response)2 FailoverClosure (com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure)2 Status (com.alipay.sofa.jraft.Status)2 JustForTest (com.alibaba.nacos.common.JustForTest)1 RestResult (com.alibaba.nacos.common.model.RestResult)1 ConvertUtils (com.alibaba.nacos.common.utils.ConvertUtils)1 InternetAddressUtil (com.alibaba.nacos.common.utils.InternetAddressUtil)1 LoggerUtils (com.alibaba.nacos.common.utils.LoggerUtils)1 StringUtils (com.alibaba.nacos.common.utils.StringUtils)1 ThreadUtils (com.alibaba.nacos.common.utils.ThreadUtils)1 ProtoMessageUtil (com.alibaba.nacos.consistency.ProtoMessageUtil)1 RequestProcessor (com.alibaba.nacos.consistency.RequestProcessor)1 SerializeFactory (com.alibaba.nacos.consistency.SerializeFactory)1 Serializer (com.alibaba.nacos.consistency.Serializer)1 RequestProcessor4CP (com.alibaba.nacos.consistency.cp.RequestProcessor4CP)1 ReadRequest (com.alibaba.nacos.consistency.entity.ReadRequest)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