Search in sources :

Example 1 with NamedThreadFactory

use of com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory in project sofa-jraft by sofastack.

the class PlacementDriverServer method createDefaultPdExecutor.

private ThreadPoolExecutor createDefaultPdExecutor() {
    final int corePoolSize = Math.max(Utils.cpus() << 2, 32);
    final String name = "rheakv-pd-executor";
    return // 
    ThreadPoolUtil.newBuilder().poolName(// 
    name).enableMetric(// 
    true).coreThreads(// 
    corePoolSize).maximumThreads(// 
    corePoolSize << 2).keepAliveSeconds(// 
    120L).workQueue(// 
    new ArrayBlockingQueue<>(4096)).threadFactory(// 
    new NamedThreadFactory(name, true)).rejectedHandler(// 
    new CallerRunsPolicyWithReport(name, name)).build();
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) NamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory) Endpoint(com.alipay.sofa.jraft.util.Endpoint) CallerRunsPolicyWithReport(com.alipay.sofa.jraft.rhea.util.concurrent.CallerRunsPolicyWithReport)

Example 2 with NamedThreadFactory

use of com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory in project sofa-jraft by sofastack.

the class DefaultRheaKVStore method init.

@Override
public synchronized boolean init(final RheaKVStoreOptions opts) {
    if (this.started) {
        LOG.info("[DefaultRheaKVStore] 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.rheaKVRpcService = new DefaultRheaKVRpcService(this.pdClient, selfEndpoint) {

        @Override
        public Endpoint getLeader(final long 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.rheaKVRpcService.init(rpcOpts)) {
        LOG.error("Fail to init [RheaKVRpcService].");
        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("[DefaultRheaKVStore] start successfully, options: {}.", opts);
    return this.started = true;
}
Also used : NamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) AffinityNamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.AffinityNamedThreadFactory) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions) NamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory) AffinityNamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.AffinityNamedThreadFactory) RemotePlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.RemotePlacementDriverClient) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngine(com.alipay.sofa.jraft.rhea.StoreEngine) RpcOptions(com.alipay.sofa.jraft.rhea.options.RpcOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) TaskDispatcher(com.alipay.sofa.jraft.rhea.util.concurrent.disruptor.TaskDispatcher) FakePlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.FakePlacementDriverClient) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions) AffinityNamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.AffinityNamedThreadFactory)

Example 3 with NamedThreadFactory

use of com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory in project sofa-jraft by sofastack.

the class DefaultPlacementDriverRpcService method createRpcCallbackExecutor.

private ThreadPoolExecutor createRpcCallbackExecutor(final RpcOptions opts) {
    final int callbackExecutorCorePoolSize = opts.getCallbackExecutorCorePoolSize();
    final int callbackExecutorMaximumPoolSize = opts.getCallbackExecutorMaximumPoolSize();
    if (callbackExecutorCorePoolSize <= 0 || callbackExecutorMaximumPoolSize <= 0) {
        return null;
    }
    final String name = "rheakv-pd-rpc-callback";
    return // 
    ThreadPoolUtil.newBuilder().poolName(// 
    name).enableMetric(// 
    true).coreThreads(// 
    callbackExecutorCorePoolSize).maximumThreads(// 
    callbackExecutorMaximumPoolSize).keepAliveSeconds(// 
    120L).workQueue(// 
    new ArrayBlockingQueue<>(opts.getCallbackExecutorQueueCapacity())).threadFactory(// 
    new NamedThreadFactory(name, true)).rejectedHandler(// 
    new CallerRunsPolicyWithReport(name)).build();
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) NamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory) Endpoint(com.alipay.sofa.jraft.util.Endpoint) CallerRunsPolicyWithReport(com.alipay.sofa.jraft.rhea.util.concurrent.CallerRunsPolicyWithReport)

Example 4 with NamedThreadFactory

use of com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory in project sofa-jraft by sofastack.

the class HeartbeatSender method init.

@Override
public synchronized boolean init(final HeartbeatOptions opts) {
    if (this.started) {
        LOG.info("[HeartbeatSender] already started.");
        return true;
    }
    this.statsCollector = new StatsCollector(this.storeEngine);
    this.instructionProcessor = new InstructionProcessor(this.storeEngine);
    this.heartbeatTimer = new HashedWheelTimer(new NamedThreadFactory("heartbeat-timer", true), 50, TimeUnit.MILLISECONDS, 4096);
    this.heartbeatRpcTimeoutMillis = opts.getHeartbeatRpcTimeoutMillis();
    if (this.heartbeatRpcTimeoutMillis <= 0) {
        throw new IllegalArgumentException("Heartbeat rpc timeout millis must > 0, " + this.heartbeatRpcTimeoutMillis);
    }
    final String name = "rheakv-heartbeat-callback";
    this.heartbeatRpcCallbackExecutor = // 
    ThreadPoolUtil.newBuilder().poolName(// 
    name).enableMetric(// 
    true).coreThreads(// 
    4).maximumThreads(// 
    4).keepAliveSeconds(// 
    120L).workQueue(// 
    new ArrayBlockingQueue<>(1024)).threadFactory(// 
    new NamedThreadFactory(name, true)).rejectedHandler(// 
    new DiscardOldPolicyWithReport(name)).build();
    final long storeHeartbeatIntervalSeconds = opts.getStoreHeartbeatIntervalSeconds();
    final long regionHeartbeatIntervalSeconds = opts.getRegionHeartbeatIntervalSeconds();
    if (storeHeartbeatIntervalSeconds <= 0) {
        throw new IllegalArgumentException("Store heartbeat interval seconds must > 0, " + storeHeartbeatIntervalSeconds);
    }
    if (regionHeartbeatIntervalSeconds <= 0) {
        throw new IllegalArgumentException("Region heartbeat interval seconds must > 0, " + regionHeartbeatIntervalSeconds);
    }
    final long now = System.currentTimeMillis();
    final StoreHeartbeatTask storeHeartbeatTask = new StoreHeartbeatTask(storeHeartbeatIntervalSeconds, now, false);
    final RegionHeartbeatTask regionHeartbeatTask = new RegionHeartbeatTask(regionHeartbeatIntervalSeconds, now, false);
    this.heartbeatTimer.newTimeout(storeHeartbeatTask, storeHeartbeatTask.getNextDelay(), TimeUnit.SECONDS);
    this.heartbeatTimer.newTimeout(regionHeartbeatTask, regionHeartbeatTask.getNextDelay(), TimeUnit.SECONDS);
    LOG.info("[HeartbeatSender] start successfully, options: {}.", opts);
    return this.started = true;
}
Also used : DiscardOldPolicyWithReport(com.alipay.sofa.jraft.rhea.util.concurrent.DiscardOldPolicyWithReport) NamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory) HashedWheelTimer(com.alipay.sofa.jraft.util.timer.HashedWheelTimer)

Example 5 with NamedThreadFactory

use of com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory in project sofa-jraft by sofastack.

the class DefaultPlacementDriverService method createPipelineExecutor.

private ThreadPoolExecutor createPipelineExecutor(final PlacementDriverServerOptions opts) {
    final int corePoolSize = opts.getPipelineCorePoolSize();
    final int maximumPoolSize = opts.getPipelineMaximumPoolSize();
    if (corePoolSize <= 0 || maximumPoolSize <= 0) {
        return null;
    }
    final String name = "rheakv-pipeline-executor";
    return // 
    ThreadPoolUtil.newBuilder().poolName(// 
    name).enableMetric(// 
    false).coreThreads(// 
    corePoolSize).maximumThreads(// 
    maximumPoolSize).keepAliveSeconds(// 
    120L).workQueue(// 
    new ArrayBlockingQueue<>(1024)).threadFactory(// 
    new NamedThreadFactory(name, true)).rejectedHandler(// 
    new CallerRunsPolicyWithReport(name)).build();
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) NamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory) CallerRunsPolicyWithReport(com.alipay.sofa.jraft.rhea.util.concurrent.CallerRunsPolicyWithReport)

Aggregations

NamedThreadFactory (com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory)6 CallerRunsPolicyWithReport (com.alipay.sofa.jraft.rhea.util.concurrent.CallerRunsPolicyWithReport)4 Endpoint (com.alipay.sofa.jraft.util.Endpoint)4 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)4 StoreEngine (com.alipay.sofa.jraft.rhea.StoreEngine)1 FakePlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.FakePlacementDriverClient)1 RemotePlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.RemotePlacementDriverClient)1 PlacementDriverOptions (com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions)1 RpcOptions (com.alipay.sofa.jraft.rhea.options.RpcOptions)1 StoreEngineOptions (com.alipay.sofa.jraft.rhea.options.StoreEngineOptions)1 AffinityNamedThreadFactory (com.alipay.sofa.jraft.rhea.util.concurrent.AffinityNamedThreadFactory)1 DiscardOldPolicyWithReport (com.alipay.sofa.jraft.rhea.util.concurrent.DiscardOldPolicyWithReport)1 TaskDispatcher (com.alipay.sofa.jraft.rhea.util.concurrent.disruptor.TaskDispatcher)1 HashedWheelTimer (com.alipay.sofa.jraft.util.timer.HashedWheelTimer)1 ThreadFactory (java.util.concurrent.ThreadFactory)1