Search in sources :

Example 1 with RequestProcessor4CP

use of com.alibaba.nacos.consistency.cp.RequestProcessor4CP in project nacos by alibaba.

the class JRaftServerTest method testCommit.

@Test
public void testCommit() throws NoSuchFieldException, IllegalAccessException, TimeoutException, InterruptedException {
    WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder();
    WriteRequest writeRequest = writeRequestBuilder.build();
    // No group is set, and make sure that an IllegalArgumentException will be thrown.
    CompletableFuture<Response> future = server.commit(groupId, writeRequest, this.future);
    verify(future).completeExceptionally(any(IllegalArgumentException.class));
    // Set an group.
    Collection<RequestProcessor4CP> processors = Collections.singletonList(mockProcessor4CP);
    server.createMultiRaftGroup(processors);
    Field cliClientServiceField = JRaftServer.class.getDeclaredField("cliClientService");
    cliClientServiceField.setAccessible(true);
    cliClientServiceField.set(server, cliClientServiceMock);
    // Make the node leader and verify the invokeToLeader is never called.
    NodeImpl node = (NodeImpl) server.findNodeByGroup(groupId);
    Field stateField = NodeImpl.class.getDeclaredField("state");
    stateField.setAccessible(true);
    stateField.set(node, State.STATE_LEADER);
    server.commit(groupId, writeRequest, future);
    verify(cliClientServiceMock, never()).getRpcClient();
    // make the node follower and verify the invokeToLeader is called.
    node = (NodeImpl) server.findNodeByGroup(groupId);
    stateField.setAccessible(true);
    stateField.set(node, State.STATE_FOLLOWER);
    RouteTable.getInstance().updateLeader(groupId, peerId1);
    server.commit(groupId, writeRequest, future);
    verify(cliClientServiceMock).getRpcClient();
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Field(java.lang.reflect.Field) NodeImpl(com.alipay.sofa.jraft.core.NodeImpl) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) Test(org.junit.Test)

Example 2 with RequestProcessor4CP

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

Aggregations

RequestProcessor4CP (com.alibaba.nacos.consistency.cp.RequestProcessor4CP)2 Response (com.alibaba.nacos.consistency.entity.Response)1 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)1 DuplicateRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException)1 Node (com.alipay.sofa.jraft.Node)1 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 NodeImpl (com.alipay.sofa.jraft.core.NodeImpl)1 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)1 Endpoint (com.alipay.sofa.jraft.util.Endpoint)1 Field (java.lang.reflect.Field)1 Random (java.util.Random)1 Test (org.junit.Test)1