Search in sources :

Example 1 with SofaThreadPoolExecutor

use of com.alipay.sofa.common.thread.SofaThreadPoolExecutor in project sofa-boot by sofastack.

the class SpringContextInstallStage method refreshSpringContextParallel.

/**
 * start sofa module parallel
 *
 * @param application
 */
private void refreshSpringContextParallel(ApplicationRuntimeModel application) {
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    List<DeploymentDescriptor> coreRoots = new ArrayList<>();
    int coreSize = (int) (CPU_COUNT * sofaModuleProperties.getParallelRefreshCoreCountFactor());
    long taskTimeout = sofaModuleProperties.getParallelRefreshTimeout();
    long period = sofaModuleProperties.getParallelRefreshCheckPeriod();
    ThreadPoolExecutor executor = new SofaThreadPoolExecutor(coreSize, coreSize, 60, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(DEFAULT_REFRESH_TASK_QUEUE_SIZE), new NamedThreadFactory("sofa-module-start"), new ThreadPoolExecutor.CallerRunsPolicy(), "sofa-module-start", SofaBootConstants.SOFABOOT_SPACE_NAME, taskTimeout, period, TimeUnit.SECONDS);
    try {
        for (DeploymentDescriptor deployment : application.getResolvedDeployments()) {
            DependencyTree.Entry entry = application.getDeployRegistry().getEntry(deployment.getModuleName());
            if (entry != null && entry.getDependencies() == null) {
                coreRoots.add(deployment);
            }
        }
        refreshSpringContextParallel(coreRoots, application.getResolvedDeployments().size(), application, executor);
    } finally {
        executor.shutdown();
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : NamedThreadFactory(com.alipay.sofa.boot.util.NamedThreadFactory) DeploymentDescriptor(com.alipay.sofa.isle.deployment.DeploymentDescriptor) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) SofaThreadPoolExecutor(com.alipay.sofa.common.thread.SofaThreadPoolExecutor) DependencyTree(com.alipay.sofa.isle.deployment.DependencyTree) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SofaThreadPoolExecutor(com.alipay.sofa.common.thread.SofaThreadPoolExecutor)

Example 2 with SofaThreadPoolExecutor

use of com.alipay.sofa.common.thread.SofaThreadPoolExecutor in project sofa-boot by sofastack.

the class AsyncTaskExecutor method createThreadPoolExecutor.

/**
 * Create thread pool to execute async init task
 * @return
 */
private static ThreadPoolExecutor createThreadPoolExecutor(Environment environment) {
    int threadPoolCoreSize = CPU_COUNT + 1;
    String coreSizeStr = environment.getProperty(SofaBootConstants.ASYNC_INIT_BEAN_CORE_SIZE);
    if (coreSizeStr != null) {
        threadPoolCoreSize = Integer.parseInt(coreSizeStr);
    }
    int threadPoolMaxSize = CPU_COUNT + 1;
    String maxSizeStr = environment.getProperty(SofaBootConstants.ASYNC_INIT_BEAN_MAX_SIZE);
    if (maxSizeStr != null) {
        threadPoolMaxSize = Integer.parseInt(maxSizeStr);
    }
    SofaLogger.info(String.format("create async-init-bean thread pool, corePoolSize: %d, maxPoolSize: %d.", threadPoolCoreSize, threadPoolMaxSize));
    return new SofaThreadPoolExecutor(threadPoolCoreSize, threadPoolMaxSize, 30, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new NamedThreadFactory("async-init-bean"), new ThreadPoolExecutor.CallerRunsPolicy(), "async-init-bean", "sofa-boot");
}
Also used : NamedThreadFactory(com.alipay.sofa.boot.util.NamedThreadFactory) SofaThreadPoolExecutor(com.alipay.sofa.common.thread.SofaThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SofaThreadPoolExecutor(com.alipay.sofa.common.thread.SofaThreadPoolExecutor)

Example 3 with SofaThreadPoolExecutor

use of com.alipay.sofa.common.thread.SofaThreadPoolExecutor in project sofa-boot by alipay.

the class AsyncTaskExecutor method createThreadPoolExecutor.

/**
 * Create thread pool to execute async init task
 * @return
 */
private static ThreadPoolExecutor createThreadPoolExecutor(Environment environment) {
    int threadPoolCoreSize = CPU_COUNT + 1;
    String coreSizeStr = environment.getProperty(SofaBootConstants.ASYNC_INIT_BEAN_CORE_SIZE);
    if (coreSizeStr != null) {
        threadPoolCoreSize = Integer.parseInt(coreSizeStr);
    }
    int threadPoolMaxSize = CPU_COUNT + 1;
    String maxSizeStr = environment.getProperty(SofaBootConstants.ASYNC_INIT_BEAN_MAX_SIZE);
    if (maxSizeStr != null) {
        threadPoolMaxSize = Integer.parseInt(maxSizeStr);
    }
    SofaLogger.info(String.format("create async-init-bean thread pool, corePoolSize: %d, maxPoolSize: %d.", threadPoolCoreSize, threadPoolMaxSize));
    return new SofaThreadPoolExecutor(threadPoolCoreSize, threadPoolMaxSize, 30, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new NamedThreadFactory("async-init-bean"), new ThreadPoolExecutor.CallerRunsPolicy(), "async-init-bean", "sofa-boot");
}
Also used : NamedThreadFactory(com.alipay.sofa.boot.util.NamedThreadFactory) SofaThreadPoolExecutor(com.alipay.sofa.common.thread.SofaThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SofaThreadPoolExecutor(com.alipay.sofa.common.thread.SofaThreadPoolExecutor)

Example 4 with SofaThreadPoolExecutor

use of com.alipay.sofa.common.thread.SofaThreadPoolExecutor in project sofa-boot by alipay.

the class SpringContextInstallStage method refreshSpringContextParallel.

/**
 * start sofa module parallel
 *
 * @param application
 */
private void refreshSpringContextParallel(ApplicationRuntimeModel application) {
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    List<DeploymentDescriptor> coreRoots = new ArrayList<>();
    int coreSize = (int) (CPU_COUNT * sofaModuleProperties.getParallelRefreshCoreCountFactor());
    long taskTimeout = sofaModuleProperties.getParallelRefreshTimeout();
    long period = sofaModuleProperties.getParallelRefreshCheckPeriod();
    ThreadPoolExecutor executor = new SofaThreadPoolExecutor(coreSize, coreSize, 60, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(DEFAULT_REFRESH_TASK_QUEUE_SIZE), new NamedThreadFactory("sofa-module-start"), new ThreadPoolExecutor.CallerRunsPolicy(), "sofa-module-start", SofaBootConstants.SOFABOOT_SPACE_NAME, taskTimeout, period, TimeUnit.SECONDS);
    try {
        for (DeploymentDescriptor deployment : application.getResolvedDeployments()) {
            DependencyTree.Entry entry = application.getDeployRegistry().getEntry(deployment.getModuleName());
            if (entry != null && entry.getDependencies() == null) {
                coreRoots.add(deployment);
            }
        }
        refreshSpringContextParallel(coreRoots, application.getResolvedDeployments().size(), application, executor);
    } finally {
        executor.shutdown();
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : NamedThreadFactory(com.alipay.sofa.boot.util.NamedThreadFactory) DeploymentDescriptor(com.alipay.sofa.isle.deployment.DeploymentDescriptor) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) SofaThreadPoolExecutor(com.alipay.sofa.common.thread.SofaThreadPoolExecutor) DependencyTree(com.alipay.sofa.isle.deployment.DependencyTree) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SofaThreadPoolExecutor(com.alipay.sofa.common.thread.SofaThreadPoolExecutor)

Aggregations

NamedThreadFactory (com.alipay.sofa.boot.util.NamedThreadFactory)4 SofaThreadPoolExecutor (com.alipay.sofa.common.thread.SofaThreadPoolExecutor)4 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)4 DependencyTree (com.alipay.sofa.isle.deployment.DependencyTree)2 DeploymentDescriptor (com.alipay.sofa.isle.deployment.DeploymentDescriptor)2 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2