Search in sources :

Example 41 with Node

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

the class GetLeaderRequestProcessor method processRequest.

@Override
public Message processRequest(final GetLeaderRequest request, final RpcRequestClosure done) {
    List<Node> nodes = new ArrayList<>();
    final String groupId = getGroupId(request);
    if (request.hasPeerId()) {
        final String peerIdStr = getPeerId(request);
        final PeerId peer = new PeerId();
        if (peer.parse(peerIdStr)) {
            final Status st = new Status();
            nodes.add(getNode(groupId, peer, st));
            if (!st.isOk()) {
                return // 
                RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st);
            }
        } else {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer id %s", peerIdStr);
        }
    } else {
        nodes = NodeManager.getInstance().getNodesByGroupId(groupId);
    }
    if (nodes == null || nodes.isEmpty()) {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.ENOENT, "No nodes in group %s", groupId);
    }
    for (final Node node : nodes) {
        final PeerId leader = node.getLeaderId();
        if (leader != null && !leader.isEmpty()) {
            return GetLeaderResponse.newBuilder().setLeaderId(leader.toString()).build();
        }
    }
    return // 
    RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EAGAIN, "Unknown leader");
}
Also used : Status(com.alipay.sofa.jraft.Status) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 42 with Node

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

the class NodeRequestProcessor method processRequest.

@Override
public Message processRequest(final T request, final RpcRequestClosure done) {
    final PeerId peer = new PeerId();
    final String peerIdStr = getPeerId(request);
    if (peer.parse(peerIdStr)) {
        final String groupId = getGroupId(request);
        final Node node = NodeManager.getInstance().get(groupId, peer);
        if (node != null) {
            return processRequest0((RaftServerService) node, request, done);
        } else {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.ENOENT, "Peer id not found: %s, group: %s", peerIdStr, groupId);
        }
    } else {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peerId: %s", peerIdStr);
    }
}
Also used : Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 43 with Node

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

the class JRaftMaintainService method execute.

/**
 * Execute relevant commands.
 *
 * @param args {@link Map}
 * @return {@link RestResult}
 */
public RestResult<String> execute(Map<String, String> args) {
    final CliService cliService = raftServer.getCliService();
    if (args.containsKey(JRaftConstants.GROUP_ID)) {
        final String groupId = args.get(JRaftConstants.GROUP_ID);
        final Node node = raftServer.findNodeByGroup(groupId);
        return single(cliService, groupId, node, args);
    }
    Map<String, JRaftServer.RaftGroupTuple> tupleMap = raftServer.getMultiRaftGroup();
    for (Map.Entry<String, JRaftServer.RaftGroupTuple> entry : tupleMap.entrySet()) {
        final String group = entry.getKey();
        final Node node = entry.getValue().getNode();
        RestResult<String> result = single(cliService, group, node, args);
        if (!result.ok()) {
            return result;
        }
    }
    return RestResultUtils.success();
}
Also used : Node(com.alipay.sofa.jraft.Node) CliService(com.alipay.sofa.jraft.CliService) Map(java.util.Map)

Example 44 with Node

use of com.alipay.sofa.jraft.Node 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 45 with Node

use of com.alipay.sofa.jraft.Node in project nacos by alibaba.

the class NacosStateMachine method onApply.

@Override
public void onApply(Iterator iter) {
    int index = 0;
    int applied = 0;
    Message message;
    NacosClosure closure = null;
    try {
        while (iter.hasNext()) {
            Status status = Status.OK();
            try {
                if (iter.done() != null) {
                    closure = (NacosClosure) iter.done();
                    message = closure.getMessage();
                } else {
                    final ByteBuffer data = iter.getData();
                    message = ProtoMessageUtil.parse(data.array());
                    if (message instanceof ReadRequest) {
                        // 'iter.done() == null' means current node is follower, ignore read operation
                        applied++;
                        index++;
                        iter.next();
                        continue;
                    }
                }
                LoggerUtils.printIfDebugEnabled(Loggers.RAFT, "receive log : {}", message);
                if (message instanceof WriteRequest) {
                    Response response = processor.onApply((WriteRequest) message);
                    postProcessor(response, closure);
                }
                if (message instanceof ReadRequest) {
                    Response response = processor.onRequest((ReadRequest) message);
                    postProcessor(response, closure);
                }
            } catch (Throwable e) {
                index++;
                status.setError(RaftError.UNKNOWN, e.toString());
                Optional.ofNullable(closure).ifPresent(closure1 -> closure1.setThrowable(e));
                throw e;
            } finally {
                Optional.ofNullable(closure).ifPresent(closure1 -> closure1.run(status));
            }
            applied++;
            index++;
            iter.next();
        }
    } catch (Throwable t) {
        Loggers.RAFT.error("processor : {}, stateMachine meet critical error: {}.", processor, t);
        iter.setErrorAndRollback(index - applied, new Status(RaftError.ESTATEMACHINE, "StateMachine meet critical error: %s.", ExceptionUtil.getStackTrace(t)));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Response(com.alibaba.nacos.consistency.entity.Response) NotifyCenter(com.alibaba.nacos.common.notify.NotifyCenter) Arrays(java.util.Arrays) LoggerUtils(com.alibaba.nacos.common.utils.LoggerUtils) SnapshotOperation(com.alibaba.nacos.consistency.snapshot.SnapshotOperation) StateMachineAdapter(com.alipay.sofa.jraft.core.StateMachineAdapter) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) LeaderChangeContext(com.alipay.sofa.jraft.entity.LeaderChangeContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Loggers(com.alibaba.nacos.core.utils.Loggers) ArrayList(java.util.ArrayList) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) Closure(com.alipay.sofa.jraft.Closure) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) RaftError(com.alipay.sofa.jraft.error.RaftError) Iterator(com.alipay.sofa.jraft.Iterator) JRaftUtils(com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils) JacksonUtils(com.alibaba.nacos.common.utils.JacksonUtils) Writer(com.alibaba.nacos.consistency.snapshot.Writer) ExceptionUtil(com.alibaba.nacos.common.utils.ExceptionUtil) RouteTable(com.alipay.sofa.jraft.RouteTable) Configuration(com.alipay.sofa.jraft.conf.Configuration) ProtoMessageUtil(com.alibaba.nacos.consistency.ProtoMessageUtil) Collection(java.util.Collection) RequestProcessor(com.alibaba.nacos.consistency.RequestProcessor) Status(com.alipay.sofa.jraft.Status) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) Response(com.alibaba.nacos.consistency.entity.Response) Objects(java.util.Objects) List(java.util.List) Node(com.alipay.sofa.jraft.Node) Reader(com.alibaba.nacos.consistency.snapshot.Reader) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) Message(com.google.protobuf.Message) Optional(java.util.Optional) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Collections(java.util.Collections) RaftException(com.alipay.sofa.jraft.error.RaftException) Message(com.google.protobuf.Message) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteBuffer(java.nio.ByteBuffer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest)

Aggregations

Node (com.alipay.sofa.jraft.Node)90 PeerId (com.alipay.sofa.jraft.entity.PeerId)74 Test (org.junit.Test)64 Endpoint (com.alipay.sofa.jraft.util.Endpoint)41 CountDownLatch (java.util.concurrent.CountDownLatch)32 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 ArrayList (java.util.ArrayList)22 Status (com.alipay.sofa.jraft.Status)21 Task (com.alipay.sofa.jraft.entity.Task)17 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)17 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)14 ByteBuffer (java.nio.ByteBuffer)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)10 RaftException (com.alipay.sofa.jraft.error.RaftException)8 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)7 LinkedHashSet (java.util.LinkedHashSet)7 NodeId (com.alipay.sofa.jraft.entity.NodeId)6 LogIndexOutOfBoundsException (com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException)6 LogNotFoundException (com.alipay.sofa.jraft.error.LogNotFoundException)6