Search in sources :

Example 6 with NamedThreadFactory

use of io.dingodb.raft.util.NamedThreadFactory in project dingo by dingodb.

the class DefaultFixedThreadsExecutorGroupFactory method newExecutorGroup.

@Override
public FixedThreadsExecutorGroup newExecutorGroup(final int nThreads, final String poolName, final int maxPendingTasksPerThread, final boolean useMpscQueue) {
    Requires.requireTrue(nThreads > 0, "nThreads must > 0");
    final boolean mpsc = useMpscQueue && Utils.USE_MPSC_SINGLE_THREAD_EXECUTOR;
    final SingleThreadExecutor[] children = new SingleThreadExecutor[nThreads];
    final ThreadFactory threadFactory = mpsc ? new NamedThreadFactory(poolName, true) : null;
    for (int i = 0; i < nThreads; i++) {
        if (mpsc) {
            children[i] = new MpscSingleThreadExecutor(maxPendingTasksPerThread, threadFactory);
        } else {
            children[i] = new DefaultSingleThreadExecutor(poolName, maxPendingTasksPerThread);
        }
    }
    return new DefaultFixedThreadsExecutorGroup(children);
}
Also used : NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory)

Example 7 with NamedThreadFactory

use of io.dingodb.raft.util.NamedThreadFactory in project dingo by dingodb.

the class RegionHeartbeatSender method start.

public synchronized void start() {
    if (heartbeatTimer == null) {
        this.heartbeatTimer = new HashedWheelTimer(new NamedThreadFactory(TIMER_THREAD_NAME, true), 50, TimeUnit.MILLISECONDS, 4096);
    }
    this.heartbeatTimer.start();
    final RegionHeartbeatTask regionHeartbeatTask = new RegionHeartbeatTask(-1);
    this.heartbeatTimer.newTimeout(regionHeartbeatTask, 0, TimeUnit.SECONDS);
}
Also used : NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) HashedWheelTimer(io.dingodb.raft.util.timer.HashedWheelTimer)

Example 8 with NamedThreadFactory

use of io.dingodb.raft.util.NamedThreadFactory in project dingo by dingodb.

the class NodeImpl method init.

@Override
public boolean init(final NodeOptions opts) {
    Requires.requireNonNull(opts, "Null node options");
    Requires.requireNonNull(opts.getRaftOptions(), "Null raft options");
    Requires.requireNonNull(opts.getServiceFactory(), "Null jraft service factory");
    this.serviceFactory = opts.getServiceFactory();
    this.options = opts;
    this.raftOptions = opts.getRaftOptions();
    this.metrics = new NodeMetrics(opts.isEnableMetrics());
    this.serverId.setPriority(opts.getElectionPriority());
    this.electionTimeoutCounter = 0;
    if (this.serverId.getIp().equals(Utils.IP_ANY)) {
        LOG.error("Node can't started from IP_ANY.");
        return false;
    }
    if (!NodeManager.getInstance().serverExists(this.serverId.getEndpoint())) {
        LOG.error("No RPC server attached to, did you forget to call addService?");
        return false;
    }
    this.timerManager = TIMER_FACTORY.getRaftScheduler(this.options.isSharedTimerPool(), this.options.getTimerPoolSize(), "JRaft-Node-ScheduleThreadPool");
    // Init timers
    final String suffix = getNodeId().toString();
    String name = "JRaft-VoteTimer-" + suffix;
    this.voteTimer = new RepeatedTimer(name, this.options.getElectionTimeoutMs(), TIMER_FACTORY.getVoteTimer(this.options.isSharedVoteTimer(), name)) {

        @Override
        protected void onTrigger() {
            handleVoteTimeout();
        }

        @Override
        protected int adjustTimeout(final int timeoutMs) {
            return randomTimeout(timeoutMs);
        }
    };
    name = "JRaft-ElectionTimer-" + suffix;
    this.electionTimer = new RepeatedTimer(name, this.options.getElectionTimeoutMs(), TIMER_FACTORY.getElectionTimer(this.options.isSharedElectionTimer(), name)) {

        @Override
        protected void onTrigger() {
            handleElectionTimeout();
        }

        @Override
        protected int adjustTimeout(final int timeoutMs) {
            return randomTimeout(timeoutMs);
        }
    };
    name = "JRaft-StepDownTimer-" + suffix;
    this.stepDownTimer = new RepeatedTimer(name, this.options.getElectionTimeoutMs() >> 1, TIMER_FACTORY.getStepDownTimer(this.options.isSharedStepDownTimer(), name)) {

        @Override
        protected void onTrigger() {
            handleStepDownTimeout();
        }
    };
    name = "JRaft-SnapshotTimer-" + suffix;
    this.snapshotTimer = new RepeatedTimer(name, this.options.getSnapshotIntervalSecs() * 1000, TIMER_FACTORY.getSnapshotTimer(this.options.isSharedSnapshotTimer(), name)) {

        private volatile boolean firstSchedule = true;

        @Override
        protected void onTrigger() {
            handleSnapshotTimeout();
        }

        @Override
        protected int adjustTimeout(final int timeoutMs) {
            if (!this.firstSchedule) {
                return timeoutMs;
            }
            // Randomize the first snapshot trigger timeout
            this.firstSchedule = false;
            if (timeoutMs > 0) {
                int half = timeoutMs / 2;
                return half + ThreadLocalRandom.current().nextInt(half);
            } else {
                return timeoutMs;
            }
        }
    };
    this.configManager = new ConfigurationManager();
    this.applyDisruptor = // 
    DisruptorBuilder.<LogEntryAndClosure>newInstance().setRingBufferSize(// 
    this.raftOptions.getDisruptorBufferSize()).setEventFactory(// 
    new LogEntryAndClosureFactory()).setThreadFactory(// 
    new NamedThreadFactory("JRaft-NodeImpl-Disruptor-", true)).setProducerType(// 
    ProducerType.MULTI).setWaitStrategy(// 
    new BlockingWaitStrategy()).build();
    this.applyDisruptor.handleEventsWith(new LogEntryAndClosureHandler());
    this.applyDisruptor.setDefaultExceptionHandler(new LogExceptionHandler<Object>(getClass().getSimpleName()));
    this.applyQueue = this.applyDisruptor.start();
    if (this.metrics.getMetricRegistry() != null) {
        this.metrics.getMetricRegistry().register("jraft-node-impl-disruptor", new DisruptorMetricSet(this.applyQueue));
    }
    this.fsmCaller = new FSMCallerImpl();
    if (!initLogStorage()) {
        LOG.error("Node {} initLogStorage failed.", getNodeId());
        return false;
    }
    if (!initMetaStorage()) {
        LOG.error("Node {} initMetaStorage failed.", getNodeId());
        return false;
    }
    if (!initFSMCaller(new LogId(0, 0))) {
        LOG.error("Node {} initFSMCaller failed.", getNodeId());
        return false;
    }
    this.ballotBox = new BallotBox();
    final BallotBoxOptions ballotBoxOpts = new BallotBoxOptions();
    ballotBoxOpts.setWaiter(this.fsmCaller);
    ballotBoxOpts.setClosureQueue(this.closureQueue);
    if (!this.ballotBox.init(ballotBoxOpts)) {
        LOG.error("Node {} init ballotBox failed.", getNodeId());
        return false;
    }
    if (!initSnapshotStorage()) {
        LOG.error("Node {} initSnapshotStorage failed.", getNodeId());
        return false;
    }
    final Status st = this.logManager.checkConsistency();
    if (!st.isOk()) {
        LOG.error("Node {} is initialized with inconsistent log, status={}.", getNodeId(), st);
        return false;
    }
    this.conf = new ConfigurationEntry();
    this.conf.setId(new LogId());
    // if have log using conf in log, else using conf in options
    if (this.logManager.getLastLogIndex() > 0) {
        checkAndSetConfiguration(false);
    } else {
        this.conf.setConf(this.options.getInitialConf());
        // initially set to max(priority of all nodes)
        this.targetPriority = getMaxPriorityOfNodes(this.conf.getConf().getPeers());
    }
    if (!this.conf.isEmpty()) {
        Requires.requireTrue(this.conf.isValid(), "Invalid conf: %s", this.conf);
    } else {
        LOG.info("Init node {} with empty conf.", this.serverId);
    }
    // TODO RPC service and ReplicatorGroup is in cycle dependent, refactor it
    this.replicatorGroup = new ReplicatorGroupImpl();
    this.rpcService = new DefaultRaftClientService(this.replicatorGroup);
    final ReplicatorGroupOptions rgOpts = new ReplicatorGroupOptions();
    rgOpts.setHeartbeatTimeoutMs(heartbeatTimeout(this.options.getElectionTimeoutMs()));
    rgOpts.setElectionTimeoutMs(this.options.getElectionTimeoutMs());
    rgOpts.setLogManager(this.logManager);
    rgOpts.setBallotBox(this.ballotBox);
    rgOpts.setNode(this);
    rgOpts.setRaftRpcClientService(this.rpcService);
    rgOpts.setSnapshotStorage(this.snapshotExecutor != null ? this.snapshotExecutor.getSnapshotStorage() : null);
    rgOpts.setRaftOptions(this.raftOptions);
    rgOpts.setTimerManager(this.timerManager);
    // Adds metric registry to RPC service.
    this.options.setMetricRegistry(this.metrics.getMetricRegistry());
    if (!this.rpcService.init(this.options)) {
        LOG.error("Fail to init rpc service.");
        return false;
    }
    this.replicatorGroup.init(new NodeId(this.groupId, this.serverId), rgOpts);
    this.readOnlyService = new ReadOnlyServiceImpl();
    final ReadOnlyServiceOptions rosOpts = new ReadOnlyServiceOptions();
    rosOpts.setFsmCaller(this.fsmCaller);
    rosOpts.setNode(this);
    rosOpts.setRaftOptions(this.raftOptions);
    if (!this.readOnlyService.init(rosOpts)) {
        LOG.error("Fail to init readOnlyService.");
        return false;
    }
    // set state to follower
    this.state = State.STATE_FOLLOWER;
    if (LOG.isInfoEnabled()) {
        LOG.info("Node {} init, term={}, lastLogId={}, conf={}, oldConf={}.", getNodeId(), this.currTerm, this.logManager.getLastLogId(false), this.conf.getConf(), this.conf.getOldConf());
    }
    if (this.snapshotExecutor != null && this.options.getSnapshotIntervalSecs() > 0) {
        LOG.debug("Node {} start snapshot timer, term={}.", getNodeId(), this.currTerm);
        this.snapshotTimer.start();
    }
    if (!this.conf.isEmpty()) {
        stepDown(this.currTerm, false, new Status());
    }
    if (!NodeManager.getInstance().add(this)) {
        LOG.error("NodeManager add {} failed.", getNodeId());
        return false;
    }
    // Now the raft node is started , have to acquire the writeLock to avoid race
    // conditions
    this.writeLock.lock();
    if (this.conf.isStable() && this.conf.getConf().size() == 1 && this.conf.getConf().contains(this.serverId)) {
        // The group contains only this server which must be the LEADER, trigger
        // the timer immediately.
        electSelf();
    } else {
        this.writeLock.unlock();
    }
    return true;
}
Also used : BallotBoxOptions(io.dingodb.raft.option.BallotBoxOptions) ReadOnlyServiceOptions(io.dingodb.raft.option.ReadOnlyServiceOptions) RepeatedTimer(io.dingodb.raft.util.RepeatedTimer) ConfigurationManager(io.dingodb.raft.conf.ConfigurationManager) ConfigurationEntry(io.dingodb.raft.conf.ConfigurationEntry) Status(io.dingodb.raft.Status) BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) DisruptorMetricSet(io.dingodb.raft.util.DisruptorMetricSet) ReplicatorGroupOptions(io.dingodb.raft.option.ReplicatorGroupOptions) NodeId(io.dingodb.raft.entity.NodeId) DefaultRaftClientService(io.dingodb.raft.rpc.impl.core.DefaultRaftClientService) LogId(io.dingodb.raft.entity.LogId)

Example 9 with NamedThreadFactory

use of io.dingodb.raft.util.NamedThreadFactory in project dingo by dingodb.

the class FSMCallerImpl method init.

@Override
public boolean init(final FSMCallerOptions opts) {
    this.logManager = opts.getLogManager();
    this.fsm = opts.getFsm();
    this.closureQueue = opts.getClosureQueue();
    this.afterShutdown = opts.getAfterShutdown();
    this.node = opts.getNode();
    this.nodeMetrics = this.node.getNodeMetrics();
    this.lastAppliedIndex.set(opts.getBootstrapId().getIndex());
    notifyLastAppliedIndexUpdated(this.lastAppliedIndex.get());
    this.lastAppliedTerm = opts.getBootstrapId().getTerm();
    this.disruptor = // 
    DisruptorBuilder.<ApplyTask>newInstance().setEventFactory(// 
    new ApplyTaskFactory()).setRingBufferSize(// 
    opts.getDisruptorBufferSize()).setThreadFactory(// 
    new NamedThreadFactory("JRaft-FSMCaller-Disruptor-", true)).setProducerType(// 
    ProducerType.MULTI).setWaitStrategy(// 
    new BlockingWaitStrategy()).build();
    this.disruptor.handleEventsWith(new ApplyTaskHandler());
    this.disruptor.setDefaultExceptionHandler(new LogExceptionHandler<Object>(getClass().getSimpleName()));
    this.taskQueue = this.disruptor.start();
    if (this.nodeMetrics.getMetricRegistry() != null) {
        this.nodeMetrics.getMetricRegistry().register("jraft-fsm-caller-disruptor", new DisruptorMetricSet(this.taskQueue));
    }
    this.error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_NONE);
    LOG.info("Starts FSMCaller successfully.");
    return true;
}
Also used : RaftException(io.dingodb.raft.error.RaftException) BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) DisruptorMetricSet(io.dingodb.raft.util.DisruptorMetricSet)

Example 10 with NamedThreadFactory

use of io.dingodb.raft.util.NamedThreadFactory in project dingo by dingodb.

the class DefaultDingoRowStore method init.

@Override
public synchronized boolean init(final DingoRowStoreOptions opts) {
    if (this.started) {
        LOG.info("[DefaultDingoRowStore] already started.");
        return true;
    }
    DescriberManager.getInstance().addDescriber(RouteTable.getInstance());
    this.opts = opts;
    // init placement driver
    final PlacementDriverOptions pdOpts = opts.getPlacementDriverOptions();
    final String clusterName = opts.getClusterName();
    Requires.requireNonNull(pdOpts, "opts.placementDriverOptions");
    Requires.requireNonNull(clusterName, "opts.clusterName");
    if (Strings.isBlank(pdOpts.getInitialServerList())) {
        // if blank, extends parent's value
        pdOpts.setInitialServerList(opts.getInitialServerList());
    }
    if (pdOpts.isFake()) {
        this.pdClient = new FakePlacementDriverClient(opts.getClusterId(), clusterName);
    } else {
        this.pdClient = new RemotePlacementDriverClient(opts.getClusterId(), clusterName);
    }
    if (!this.pdClient.init(pdOpts)) {
        LOG.error("Fail to init [PlacementDriverClient].");
        return false;
    }
    // init compress strategies
    ZipStrategyManager.init(opts);
    // init store engine
    final StoreEngineOptions stOpts = opts.getStoreEngineOptions();
    if (stOpts != null) {
        stOpts.setInitialServerList(opts.getInitialServerList());
        this.storeEngine = new StoreEngine(this.pdClient, this.stateListenerContainer);
        if (!this.storeEngine.init(stOpts)) {
            LOG.error("Fail to init [StoreEngine].");
            return false;
        }
    }
    final Endpoint selfEndpoint = this.storeEngine == null ? null : this.storeEngine.getSelfEndpoint();
    final RpcOptions rpcOpts = opts.getRpcOptions();
    Requires.requireNonNull(rpcOpts, "opts.rpcOptions");
    this.dingoRowStoreRpcService = new DefaultDingoRowStoreRpcService(this.pdClient, selfEndpoint) {

        @Override
        public Endpoint getLeader(final String regionId, final boolean forceRefresh, final long timeoutMillis) {
            final Endpoint leader = getLeaderByRegionEngine(regionId);
            if (leader != null) {
                return leader;
            }
            return super.getLeader(regionId, forceRefresh, timeoutMillis);
        }
    };
    if (!this.dingoRowStoreRpcService.init(rpcOpts)) {
        LOG.error("Fail to init [DingoRowStoreRpcService].");
        return false;
    }
    this.failoverRetries = opts.getFailoverRetries();
    this.futureTimeoutMillis = opts.getFutureTimeoutMillis();
    this.onlyLeaderRead = opts.isOnlyLeaderRead();
    if (opts.isUseParallelKVExecutor()) {
        final int numWorkers = Utils.cpus();
        final int bufSize = numWorkers << 4;
        final String name = "parallel-kv-executor";
        final ThreadFactory threadFactory = Constants.THREAD_AFFINITY_ENABLED ? new AffinityNamedThreadFactory(name, true) : new NamedThreadFactory(name, true);
        this.kvDispatcher = new TaskDispatcher(bufSize, numWorkers, WaitStrategyType.LITE_BLOCKING_WAIT, threadFactory);
    }
    this.batchingOpts = opts.getBatchingOptions();
    if (this.batchingOpts.isAllowBatching()) {
        this.getBatching = new GetBatching(KeyEvent::new, "get_batching", new GetBatchingHandler("get", false));
        this.getBatchingOnlySafe = new GetBatching(KeyEvent::new, "get_batching_only_safe", new GetBatchingHandler("get_only_safe", true));
        this.putBatching = new PutBatching(KVEvent::new, "put_batching", new PutBatchingHandler("put"));
    }
    LOG.info("[DefaultDingoRowStore] start successfully, options: {}.", opts);
    return this.started = true;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AffinityNamedThreadFactory(io.dingodb.store.row.util.concurrent.AffinityNamedThreadFactory) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) PlacementDriverOptions(io.dingodb.store.row.options.PlacementDriverOptions) AffinityNamedThreadFactory(io.dingodb.store.row.util.concurrent.AffinityNamedThreadFactory) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) RemotePlacementDriverClient(io.dingodb.store.row.client.pd.RemotePlacementDriverClient) Endpoint(io.dingodb.raft.util.Endpoint) StoreEngine(io.dingodb.store.row.StoreEngine) RpcOptions(io.dingodb.store.row.options.RpcOptions) Endpoint(io.dingodb.raft.util.Endpoint) TaskDispatcher(io.dingodb.store.row.util.concurrent.disruptor.TaskDispatcher) FakePlacementDriverClient(io.dingodb.store.row.client.pd.FakePlacementDriverClient) StoreEngineOptions(io.dingodb.store.row.options.StoreEngineOptions) AffinityNamedThreadFactory(io.dingodb.store.row.util.concurrent.AffinityNamedThreadFactory)

Aggregations

NamedThreadFactory (io.dingodb.raft.util.NamedThreadFactory)12 DisruptorMetricSet (io.dingodb.raft.util.DisruptorMetricSet)4 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)3 Endpoint (io.dingodb.raft.util.Endpoint)3 HashedWheelTimer (io.dingodb.raft.util.timer.HashedWheelTimer)3 Status (io.dingodb.raft.Status)2 ConfigurationEntry (io.dingodb.raft.conf.ConfigurationEntry)2 ConfigurationManager (io.dingodb.raft.conf.ConfigurationManager)2 LogId (io.dingodb.raft.entity.LogId)2 RaftException (io.dingodb.raft.error.RaftException)2 CallerRunsPolicyWithReport (io.dingodb.store.row.util.concurrent.CallerRunsPolicyWithReport)2 DiscardOldPolicyWithReport (io.dingodb.store.row.util.concurrent.DiscardOldPolicyWithReport)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 ThreadFactory (java.util.concurrent.ThreadFactory)2 com.lmax.disruptor (com.lmax.disruptor)1 Disruptor (com.lmax.disruptor.dsl.Disruptor)1 ProducerType (com.lmax.disruptor.dsl.ProducerType)1 FSMCaller (io.dingodb.raft.FSMCaller)1 Configuration (io.dingodb.raft.conf.Configuration)1 NodeMetrics (io.dingodb.raft.core.NodeMetrics)1