Skip to content

Commit 8e27120

Browse files
committed
Seperate job executor pools to avoid thread starvation situation.
1 parent 6ad245e commit 8e27120

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

framework/jobs/src/org/apache/cloudstack/framework/jobs/AsyncJobManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
public interface AsyncJobManager extends Manager {
2929

30-
public static final String JOB_POOL_THREAD_PREFIX = "Job-Executor";
30+
public static final String API_JOB_POOL_THREAD_PREFIX = "API-Job-Executor";
31+
public static final String WORK_JOB_POOL_THREAD_PREFIX = "Work-Job-Executor";
3132

3233
AsyncJobVO getAsyncJob(long jobId);
3334

framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
117117
private volatile long _executionRunNumber = 1;
118118

119119
private final ScheduledExecutorService _heartbeatScheduler = Executors.newScheduledThreadPool(1, new NamedThreadFactory("AsyncJobMgr-Heartbeat"));
120-
private ExecutorService _executor;
120+
private ExecutorService _apiJobExecutor;
121+
private ExecutorService _workerJobExecutor;
121122

122123
@Override
123124
public String getConfigComponentName() {
@@ -390,7 +391,10 @@ private void scheduleExecution(final AsyncJob job, boolean executeInContext) {
390391
if (executeInContext) {
391392
runnable.run();
392393
} else {
393-
_executor.submit(runnable);
394+
if (job.getDispatcher() == null || job.getDispatcher().equalsIgnoreCase("ApiAsyncJobDispatcher"))
395+
_apiJobExecutor.submit(runnable);
396+
else
397+
_workerJobExecutor.submit(runnable);
394398
}
395399
}
396400

@@ -855,10 +859,14 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
855859
final Properties dbProps = DbProperties.getDbProperties();
856860
final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive"));
857861

858-
int poolSize = (cloudMaxActive * 2) / 3;
862+
int apiPoolSize = cloudMaxActive / 2;
863+
int workPoolSize = (cloudMaxActive * 2) / 3;
859864

860-
s_logger.info("Start AsyncJobManager thread pool in size " + poolSize);
861-
_executor = Executors.newFixedThreadPool(poolSize, new NamedThreadFactory(AsyncJobManager.JOB_POOL_THREAD_PREFIX));
865+
s_logger.info("Start AsyncJobManager API executor thread pool in size " + apiPoolSize);
866+
_apiJobExecutor = Executors.newFixedThreadPool(apiPoolSize, new NamedThreadFactory(AsyncJobManager.API_JOB_POOL_THREAD_PREFIX));
867+
868+
s_logger.info("Start AsyncJobManager Work executor thread pool in size " + workPoolSize);
869+
_workerJobExecutor = Executors.newFixedThreadPool(workPoolSize, new NamedThreadFactory(AsyncJobManager.WORK_JOB_POOL_THREAD_PREFIX));
862870
} catch (final Exception e) {
863871
throw new ConfigurationException("Unable to load db.properties to configure AsyncJobManagerImpl");
864872
}
@@ -941,7 +949,8 @@ public boolean start() {
941949
@Override
942950
public boolean stop() {
943951
_heartbeatScheduler.shutdown();
944-
_executor.shutdown();
952+
_apiJobExecutor.shutdown();
953+
_workerJobExecutor.shutdown();
945954
return true;
946955
}
947956

framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobMonitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void registerActiveTask(long runNumber, long jobId) {
115115
assert (_activeTasks.get(runNumber) == null);
116116

117117
long threadId = Thread.currentThread().getId();
118-
boolean fromPoolThread = Thread.currentThread().getName().contains(AsyncJobManager.JOB_POOL_THREAD_PREFIX);
118+
boolean fromPoolThread = Thread.currentThread().getName().contains(AsyncJobManager.API_JOB_POOL_THREAD_PREFIX);
119119
ActiveTaskRecord record = new ActiveTaskRecord(jobId, threadId, fromPoolThread);
120120
_activeTasks.put(runNumber, record);
121121
if (fromPoolThread)

0 commit comments

Comments
 (0)