Search in sources :

Example 21 with NodeOptions

use of com.alipay.sofa.jraft.option.NodeOptions in project incubator-hugegraph by apache.

the class RaftSharedContext method nodeOptions.

public NodeOptions nodeOptions() throws IOException {
    HugeConfig config = this.config();
    PeerId selfId = new PeerId();
    selfId.parse(config.get(CoreOptions.RAFT_ENDPOINT));
    NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setEnableMetrics(false);
    nodeOptions.setRpcProcessorThreadPoolSize(config.get(CoreOptions.RAFT_RPC_THREADS));
    nodeOptions.setRpcConnectTimeoutMs(config.get(CoreOptions.RAFT_RPC_CONNECT_TIMEOUT));
    nodeOptions.setRpcDefaultTimeout(config.get(CoreOptions.RAFT_RPC_TIMEOUT));
    int electionTimeout = config.get(CoreOptions.RAFT_ELECTION_TIMEOUT);
    nodeOptions.setElectionTimeoutMs(electionTimeout);
    nodeOptions.setDisableCli(false);
    int snapshotInterval = config.get(CoreOptions.RAFT_SNAPSHOT_INTERVAL);
    nodeOptions.setSnapshotIntervalSecs(snapshotInterval);
    Configuration groupPeers = new Configuration();
    String groupPeersStr = config.get(CoreOptions.RAFT_GROUP_PEERS);
    if (!groupPeers.parse(groupPeersStr)) {
        throw new HugeException("Failed to parse group peers %s", groupPeersStr);
    }
    nodeOptions.setInitialConf(groupPeers);
    String raftPath = config.get(CoreOptions.RAFT_PATH);
    String logUri = Paths.get(raftPath, "log").toString();
    FileUtils.forceMkdir(new File(logUri));
    nodeOptions.setLogUri(logUri);
    String metaUri = Paths.get(raftPath, "meta").toString();
    FileUtils.forceMkdir(new File(metaUri));
    nodeOptions.setRaftMetaUri(metaUri);
    if (config.get(CoreOptions.RAFT_USE_SNAPSHOT)) {
        String snapshotUri = Paths.get(raftPath, "snapshot").toString();
        FileUtils.forceMkdir(new File(snapshotUri));
        nodeOptions.setSnapshotUri(snapshotUri);
    }
    RaftOptions raftOptions = nodeOptions.getRaftOptions();
    /*
         * NOTE: if buffer size is too small(<=1024), will throw exception
         * "LogManager is busy, disk queue overload"
         */
    raftOptions.setApplyBatch(config.get(CoreOptions.RAFT_APPLY_BATCH));
    raftOptions.setDisruptorBufferSize(config.get(CoreOptions.RAFT_QUEUE_SIZE));
    raftOptions.setDisruptorPublishEventWaitTimeoutSecs(config.get(CoreOptions.RAFT_QUEUE_PUBLISH_TIMEOUT));
    raftOptions.setReplicatorPipeline(config.get(CoreOptions.RAFT_REPLICATOR_PIPELINE));
    raftOptions.setOpenStatistics(false);
    raftOptions.setReadOnlyOptions(ReadOnlyOption.valueOf(config.get(CoreOptions.RAFT_READ_STRATEGY)));
    return nodeOptions;
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Configuration(com.alipay.sofa.jraft.conf.Configuration) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) HugeConfig(com.baidu.hugegraph.config.HugeConfig) HugeException(com.baidu.hugegraph.HugeException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 22 with NodeOptions

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

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.getMmqHome(), "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
        MmqStateMachine machine = new MmqStateMachine(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) RequestProcessor4CP(org.monkey.mmq.core.consistency.cp.RequestProcessor4CP) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 23 with NodeOptions

use of com.alipay.sofa.jraft.option.NodeOptions in project RICE by gaojiayi.

the class ElectionNode method init.

@Override
public boolean init(final ElectionNodeOptions opts) {
    if (this.started) {
        LOG.info("[ElectionNode: {}] already started.", opts.getServerAddress());
        return true;
    }
    // node options
    NodeOptions nodeOpts = opts.getNodeOptions();
    if (nodeOpts == null) {
        nodeOpts = new NodeOptions();
    }
    this.fsm = new ElectionOnlyStateMachine(this.listeners);
    nodeOpts.setFsm(this.fsm);
    final Configuration initialConf = new Configuration();
    if (!initialConf.parse(opts.getInitialServerAddressList())) {
        throw new IllegalArgumentException("Fail to parse initConf: " + opts.getInitialServerAddressList());
    }
    // Set the initial cluster configuration
    nodeOpts.setInitialConf(initialConf);
    final String dataPath = opts.getDataPath();
    try {
        FileUtils.forceMkdir(new File(dataPath));
    } catch (final IOException e) {
        LOG.error("Fail to make dir for dataPath {}.", dataPath);
        return false;
    }
    // Set the data path
    // Log, required
    nodeOpts.setLogUri(Paths.get(dataPath, "log").toString());
    // Metadata, required
    nodeOpts.setRaftMetaUri(Paths.get(dataPath, "meta").toString());
    // nodeOpts.setSnapshotUri(Paths.get(dataPath, "snapshot").toString());
    final String groupId = opts.getGroupId();
    final PeerId serverId = new PeerId();
    if (!serverId.parse(opts.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId: " + opts.getServerAddress());
    }
    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOpts, rpcServer);
    this.node = this.raftGroupService.start();
    if (this.node != null) {
        this.started = true;
    }
    return this.started;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) IOException(java.io.IOException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 24 with NodeOptions

use of com.alipay.sofa.jraft.option.NodeOptions 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 25 with NodeOptions

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

the class JRaftServer method init.

void init(RaftConfig config) {
    this.raftConfig = config;
    this.serializer = SerializeFactory.getDefault();
    Loggers.RAFT.info("Initializes the Raft protocol, raft-config info : {}", config);
    RaftExecutor.init(config);
    final String self = config.getSelfMember();
    String[] info = InternetAddressUtil.splitIPPortStr(self);
    selfIp = info[0];
    selfPort = Integer.parseInt(info[1]);
    localPeerId = PeerId.parsePeer(self);
    nodeOptions = new NodeOptions();
    // Set the election timeout time. The default is 5 seconds.
    int electionTimeout = Math.max(ConvertUtils.toInt(config.getVal(RaftSysConstants.RAFT_ELECTION_TIMEOUT_MS), RaftSysConstants.DEFAULT_ELECTION_TIMEOUT), RaftSysConstants.DEFAULT_ELECTION_TIMEOUT);
    rpcRequestTimeoutMs = ConvertUtils.toInt(raftConfig.getVal(RaftSysConstants.RAFT_RPC_REQUEST_TIMEOUT_MS), RaftSysConstants.DEFAULT_RAFT_RPC_REQUEST_TIMEOUT_MS);
    nodeOptions.setSharedElectionTimer(true);
    nodeOptions.setSharedVoteTimer(true);
    nodeOptions.setSharedStepDownTimer(true);
    nodeOptions.setSharedSnapshotTimer(true);
    nodeOptions.setElectionTimeoutMs(electionTimeout);
    RaftOptions raftOptions = RaftOptionsBuilder.initRaftOptions(raftConfig);
    nodeOptions.setRaftOptions(raftOptions);
    // open jraft node metrics record function
    nodeOptions.setEnableMetrics(true);
    CliOptions cliOptions = new CliOptions();
    this.cliService = RaftServiceFactory.createAndInitCliService(cliOptions);
    this.cliClientService = (CliClientServiceImpl) ((CliServiceImpl) this.cliService).getCliClientService();
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) CliServiceImpl(com.alipay.sofa.jraft.core.CliServiceImpl) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) CliOptions(com.alipay.sofa.jraft.option.CliOptions)

Aggregations

NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)39 PeerId (com.alipay.sofa.jraft.entity.PeerId)24 Endpoint (com.alipay.sofa.jraft.util.Endpoint)22 Configuration (com.alipay.sofa.jraft.conf.Configuration)18 Test (org.junit.Test)16 Node (com.alipay.sofa.jraft.Node)13 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)9 File (java.io.File)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 Before (org.junit.Before)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)3 NodeId (com.alipay.sofa.jraft.entity.NodeId)3 BootstrapOptions (com.alipay.sofa.jraft.option.BootstrapOptions)3 SnapshotCopierOptions (com.alipay.sofa.jraft.option.SnapshotCopierOptions)3 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Iterator (com.alipay.sofa.jraft.Iterator)2 StateMachine (com.alipay.sofa.jraft.StateMachine)2