Search in sources :

Example 46 with Node

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

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 47 with Node

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

the class JRaftServer method shutdown.

synchronized void shutdown() {
    if (isShutdown) {
        return;
    }
    isShutdown = true;
    try {
        Loggers.RAFT.info("========= The raft protocol is starting to close =========");
        for (Map.Entry<String, RaftGroupTuple> entry : multiRaftGroup.entrySet()) {
            final RaftGroupTuple tuple = entry.getValue();
            final Node node = tuple.getNode();
            tuple.node.shutdown();
            tuple.raftGroupService.shutdown();
        }
        cliService.shutdown();
        cliClientService.shutdown();
        Loggers.RAFT.info("========= The raft protocol has been closed =========");
    } catch (Throwable t) {
        Loggers.RAFT.error("There was an error in the raft protocol shutdown, cause: ", t);
    }
}
Also used : Node(com.alipay.sofa.jraft.Node) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 48 with Node

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

the class JRaftServer method createMultiRaftGroup.

synchronized void createMultiRaftGroup(Collection<RequestProcessor4CP> processors) {
    // There is no reason why the LogProcessor cannot be processed because of the synchronization
    if (!this.isStarted) {
        this.processors.addAll(processors);
        return;
    }
    final String parentPath = Paths.get(EnvUtil.getNacosHome(), "data/protocol/raft").toString();
    for (RequestProcessor4CP processor : processors) {
        final String groupName = processor.group();
        if (multiRaftGroup.containsKey(groupName)) {
            throw new DuplicateRaftGroupException(groupName);
        }
        // Ensure that each Raft Group has its own configuration and NodeOptions
        Configuration configuration = conf.copy();
        NodeOptions copy = nodeOptions.copy();
        JRaftUtils.initDirectory(parentPath, groupName, copy);
        // Here, the LogProcessor is passed into StateMachine, and when the StateMachine
        // triggers onApply, the onApply of the LogProcessor is actually called
        NacosStateMachine machine = new NacosStateMachine(this, processor);
        copy.setFsm(machine);
        copy.setInitialConf(configuration);
        // Set snapshot interval, default 1800 seconds
        int doSnapshotInterval = ConvertUtils.toInt(raftConfig.getVal(RaftSysConstants.RAFT_SNAPSHOT_INTERVAL_SECS), RaftSysConstants.DEFAULT_RAFT_SNAPSHOT_INTERVAL_SECS);
        // If the business module does not implement a snapshot processor, cancel the snapshot
        doSnapshotInterval = CollectionUtils.isEmpty(processor.loadSnapshotOperate()) ? 0 : doSnapshotInterval;
        copy.setSnapshotIntervalSecs(doSnapshotInterval);
        Loggers.RAFT.info("create raft group : {}", groupName);
        RaftGroupService raftGroupService = new RaftGroupService(groupName, localPeerId, copy, rpcServer, true);
        // Because BaseRpcServer has been started before, it is not allowed to start again here
        Node node = raftGroupService.start(false);
        machine.setNode(node);
        RouteTable.getInstance().updateConfiguration(groupName, configuration);
        RaftExecutor.executeByCommon(() -> registerSelfToCluster(groupName, localPeerId, configuration));
        // Turn on the leader auto refresh for this group
        Random random = new Random();
        long period = nodeOptions.getElectionTimeoutMs() + random.nextInt(5 * 1000);
        RaftExecutor.scheduleRaftMemberRefreshJob(() -> refreshRouteTable(groupName), nodeOptions.getElectionTimeoutMs(), period, TimeUnit.MILLISECONDS);
        multiRaftGroup.put(groupName, new RaftGroupTuple(node, processor, raftGroupService, machine));
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) Random(java.util.Random) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Node(com.alipay.sofa.jraft.Node) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) DuplicateRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 49 with Node

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

the class JRaftServer method commit.

public CompletableFuture<Response> commit(final String group, final Message data, final CompletableFuture<Response> future) {
    LoggerUtils.printIfDebugEnabled(Loggers.RAFT, "data requested this time : {}", data);
    final RaftGroupTuple tuple = findTupleByGroup(group);
    if (tuple == null) {
        future.completeExceptionally(new IllegalArgumentException("No corresponding Raft Group found : " + group));
        return future;
    }
    FailoverClosureImpl closure = new FailoverClosureImpl(future);
    final Node node = tuple.node;
    if (node.isLeader()) {
        // The leader node directly applies this request
        applyOperation(node, data, closure);
    } else {
        // Forward to Leader for request processing
        invokeToLeader(group, data, rpcRequestTimeoutMs, closure);
    }
    return future;
}
Also used : FailoverClosureImpl(com.alibaba.nacos.core.distributed.raft.utils.FailoverClosureImpl) Node(com.alipay.sofa.jraft.Node)

Example 50 with Node

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

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