Search in sources :

Example 36 with NodeOptions

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

the class StoreEngine method init.

@Override
public synchronized boolean init(final StoreEngineOptions opts) {
    if (this.started) {
        LOG.info("[StoreEngine] already started.");
        return true;
    }
    DescriberManager.getInstance().addDescriber(this);
    this.storeOpts = Requires.requireNonNull(opts, "opts");
    Endpoint serverAddress = Requires.requireNonNull(opts.getServerAddress(), "opts.serverAddress");
    final int port = serverAddress.getPort();
    final String ip = serverAddress.getIp();
    if (ip == null || Utils.IP_ANY.equals(ip)) {
        serverAddress = new Endpoint(NetUtil.getLocalCanonicalHostName(), port);
        opts.setServerAddress(serverAddress);
    }
    final long metricsReportPeriod = opts.getMetricsReportPeriod();
    // init region options
    List<RegionEngineOptions> rOptsList = opts.getRegionEngineOptionsList();
    if (rOptsList == null || rOptsList.isEmpty()) {
        // -1 region
        final RegionEngineOptions rOpts = new RegionEngineOptions();
        rOpts.setRegionId(Constants.DEFAULT_REGION_ID);
        rOptsList = Lists.newArrayList();
        rOptsList.add(rOpts);
        opts.setRegionEngineOptionsList(rOptsList);
    }
    final String clusterName = this.pdClient.getClusterName();
    for (final RegionEngineOptions rOpts : rOptsList) {
        rOpts.setRaftGroupId(JRaftHelper.getJRaftGroupId(clusterName, rOpts.getRegionId()));
        rOpts.setServerAddress(serverAddress);
        if (Strings.isBlank(rOpts.getInitialServerList())) {
            // if blank, extends parent's value
            rOpts.setInitialServerList(opts.getInitialServerList());
        }
        if (rOpts.getNodeOptions() == null) {
            // copy common node options
            rOpts.setNodeOptions(opts.getCommonNodeOptions() == null ? new NodeOptions() : opts.getCommonNodeOptions().copy());
        }
        if (rOpts.getMetricsReportPeriod() <= 0 && metricsReportPeriod > 0) {
            // extends store opts
            rOpts.setMetricsReportPeriod(metricsReportPeriod);
        }
    }
    // init store
    final Store store = this.pdClient.getStoreMetadata(opts);
    if (store == null || store.getRegions() == null || store.getRegions().isEmpty()) {
        LOG.error("Empty store metadata: {}.", store);
        return false;
    }
    this.storeId = store.getId();
    // init executors
    if (this.readIndexExecutor == null) {
        this.readIndexExecutor = StoreEngineHelper.createReadIndexExecutor(opts.getReadIndexCoreThreads());
    }
    if (this.raftStateTrigger == null) {
        this.raftStateTrigger = StoreEngineHelper.createRaftStateTrigger(opts.getLeaderStateTriggerCoreThreads());
    }
    if (this.snapshotExecutor == null) {
        this.snapshotExecutor = StoreEngineHelper.createSnapshotExecutor(opts.getSnapshotCoreThreads(), opts.getSnapshotMaxThreads());
    }
    // init rpc executors
    final boolean useSharedRpcExecutor = opts.isUseSharedRpcExecutor();
    if (!useSharedRpcExecutor) {
        if (this.cliRpcExecutor == null) {
            this.cliRpcExecutor = StoreEngineHelper.createCliRpcExecutor(opts.getCliRpcCoreThreads());
        }
        if (this.raftRpcExecutor == null) {
            this.raftRpcExecutor = StoreEngineHelper.createRaftRpcExecutor(opts.getRaftRpcCoreThreads());
        }
        if (this.kvRpcExecutor == null) {
            this.kvRpcExecutor = StoreEngineHelper.createKvRpcExecutor(opts.getKvRpcCoreThreads());
        }
    }
    // init metrics
    startMetricReporters(metricsReportPeriod);
    // init rpc server
    this.rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverAddress, this.raftRpcExecutor, this.cliRpcExecutor);
    StoreEngineHelper.addKvStoreRequestProcessor(this.rpcServer, this);
    if (!this.rpcServer.init(null)) {
        LOG.error("Fail to init [RpcServer].");
        return false;
    }
    // init db store
    if (!initRawKVStore(opts)) {
        return false;
    }
    if (this.rawKVStore instanceof Describer) {
        DescriberManager.getInstance().addDescriber((Describer) this.rawKVStore);
    }
    // init all region engine
    if (!initAllRegionEngine(opts, store)) {
        LOG.error("Fail to init all [RegionEngine].");
        return false;
    }
    // heartbeat sender
    if (this.pdClient instanceof RemotePlacementDriverClient) {
        HeartbeatOptions heartbeatOpts = opts.getHeartbeatOptions();
        if (heartbeatOpts == null) {
            heartbeatOpts = new HeartbeatOptions();
        }
        this.heartbeatSender = new HeartbeatSender(this);
        if (!this.heartbeatSender.init(heartbeatOpts)) {
            LOG.error("Fail to init [HeartbeatSender].");
            return false;
        }
    }
    this.startTime = System.currentTimeMillis();
    LOG.info("[StoreEngine] start successfully: {}.", this);
    return this.started = true;
}
Also used : BatchRawKVStore(com.alipay.sofa.jraft.rhea.storage.BatchRawKVStore) Store(com.alipay.sofa.jraft.rhea.metadata.Store) MemoryRawKVStore(com.alipay.sofa.jraft.rhea.storage.MemoryRawKVStore) RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) RegionEngineOptions(com.alipay.sofa.jraft.rhea.options.RegionEngineOptions) RemotePlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.RemotePlacementDriverClient) Endpoint(com.alipay.sofa.jraft.util.Endpoint) HeartbeatSender(com.alipay.sofa.jraft.rhea.client.pd.HeartbeatSender) Endpoint(com.alipay.sofa.jraft.util.Endpoint) HeartbeatOptions(com.alipay.sofa.jraft.rhea.options.HeartbeatOptions) Describer(com.alipay.sofa.jraft.util.Describer)

Example 37 with NodeOptions

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

the class NodeImpl method bootstrap.

public boolean bootstrap(final BootstrapOptions opts) throws InterruptedException {
    if (opts.getLastLogIndex() > 0 && (opts.getGroupConf().isEmpty() || opts.getFsm() == null)) {
        LOG.error("Invalid arguments for bootstrap, groupConf={}, fsm={}, lastLogIndex={}.", opts.getGroupConf(), opts.getFsm(), opts.getLastLogIndex());
        return false;
    }
    if (opts.getGroupConf().isEmpty()) {
        LOG.error("Bootstrapping an empty node makes no sense.");
        return false;
    }
    Requires.requireNonNull(opts.getServiceFactory(), "Null jraft service factory");
    this.serviceFactory = opts.getServiceFactory();
    // Term is not an option since changing it is very dangerous
    final long bootstrapLogTerm = opts.getLastLogIndex() > 0 ? 1 : 0;
    final LogId bootstrapId = new LogId(opts.getLastLogIndex(), bootstrapLogTerm);
    this.options = new NodeOptions();
    this.raftOptions = this.options.getRaftOptions();
    this.metrics = new NodeMetrics(opts.isEnableMetrics());
    this.options.setFsm(opts.getFsm());
    this.options.setLogUri(opts.getLogUri());
    this.options.setRaftMetaUri(opts.getRaftMetaUri());
    this.options.setSnapshotUri(opts.getSnapshotUri());
    this.configManager = new ConfigurationManager();
    // Create fsmCaller at first as logManager needs it to report error
    this.fsmCaller = new FSMCallerImpl();
    if (!initLogStorage()) {
        LOG.error("Fail to init log storage.");
        return false;
    }
    if (!initMetaStorage()) {
        LOG.error("Fail to init meta storage.");
        return false;
    }
    if (this.currTerm == 0) {
        this.currTerm = 1;
        if (!this.metaStorage.setTermAndVotedFor(1, new PeerId())) {
            LOG.error("Fail to set term.");
            return false;
        }
    }
    if (opts.getFsm() != null && !initFSMCaller(bootstrapId)) {
        LOG.error("Fail to init fsm caller.");
        return false;
    }
    final LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    entry.getId().setTerm(this.currTerm);
    entry.setPeers(opts.getGroupConf().listPeers());
    entry.setLearners(opts.getGroupConf().listLearners());
    final List<LogEntry> entries = new ArrayList<>();
    entries.add(entry);
    final BootstrapStableClosure bootstrapDone = new BootstrapStableClosure();
    this.logManager.appendEntries(entries, bootstrapDone);
    if (!bootstrapDone.await().isOk()) {
        LOG.error("Fail to append configuration.");
        return false;
    }
    if (opts.getLastLogIndex() > 0) {
        if (!initSnapshotStorage()) {
            LOG.error("Fail to init snapshot storage.");
            return false;
        }
        final SynchronizedClosure snapshotDone = new SynchronizedClosure();
        this.snapshotExecutor.doSnapshot(snapshotDone);
        if (!snapshotDone.await().isOk()) {
            LOG.error("Fail to save snapshot, status={}.", snapshotDone.getStatus());
            return false;
        }
    }
    if (this.logManager.getFirstLogIndex() != opts.getLastLogIndex() + 1) {
        throw new IllegalStateException("First and last log index mismatch");
    }
    if (opts.getLastLogIndex() > 0) {
        if (this.logManager.getLastLogIndex() != opts.getLastLogIndex()) {
            throw new IllegalStateException("Last log index mismatch");
        }
    } else {
        if (this.logManager.getLastLogIndex() != opts.getLastLogIndex() + 1) {
            throw new IllegalStateException("Last log index mismatch");
        }
    }
    return true;
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) LogId(com.alipay.sofa.jraft.entity.LogId) ConfigurationManager(com.alipay.sofa.jraft.conf.ConfigurationManager) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 38 with NodeOptions

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

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)

Example 39 with NodeOptions

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

the class RaftNode method initRaftNode.

private Node initRaftNode() throws IOException {
    NodeOptions nodeOptions = this.context.nodeOptions();
    nodeOptions.setFsm(this.stateMachine);
    // TODO: When support sharding, groupId needs to be bound to shard Id
    String groupId = this.context.group();
    PeerId endpoint = this.context.endpoint();
    /*
         * Start raft node with shared rpc server:
         * return new RaftGroupService(groupId, endpoint, nodeOptions,
         *                             this.context.rpcServer(), true)
         *        .start(false)
         */
    return RaftServiceFactory.createAndInitRaftNode(groupId, endpoint, nodeOptions);
}
Also used : NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId)

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