Skip to content

Commit 2335fc5

Browse files
author
Kelven Yang
committed
bug 10918: cap async-job execution thread pool to be 2/3 of maximum DB connection limit
1 parent 50a2fc7 commit 2335fc5

3 files changed

Lines changed: 63 additions & 8 deletions

File tree

‎server/src/com/cloud/async/AsyncJobManagerImpl.java‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818

1919
package com.cloud.async;
2020

21+
import java.io.File;
22+
import java.io.FileInputStream;
2123
import java.lang.reflect.Type;
2224
import java.util.Date;
2325
import java.util.List;
2426
import java.util.Map;
27+
import java.util.Properties;
2528
import java.util.Random;
2629
import java.util.concurrent.ExecutorService;
2730
import java.util.concurrent.Executors;
31+
import java.util.concurrent.RejectedExecutionException;
2832
import java.util.concurrent.ScheduledExecutorService;
2933
import java.util.concurrent.TimeUnit;
3034

@@ -58,6 +62,7 @@
5862
import com.cloud.user.dao.AccountDao;
5963
import com.cloud.utils.DateUtil;
6064
import com.cloud.utils.NumbersUtil;
65+
import com.cloud.utils.PropertiesUtil;
6166
import com.cloud.utils.component.ComponentLocator;
6267
import com.cloud.utils.concurrency.NamedThreadFactory;
6368
import com.cloud.utils.db.DB;
@@ -93,7 +98,7 @@ public class AsyncJobManagerImpl implements AsyncJobManager, ClusterManagerListe
9398

9499
private final ScheduledExecutorService _heartbeatScheduler =
95100
Executors.newScheduledThreadPool(1, new NamedThreadFactory("AsyncJobMgr-Heartbeat"));
96-
private final ExecutorService _executor = Executors.newCachedThreadPool(new NamedThreadFactory("Job-Executor"));
101+
private ExecutorService _executor;
97102

98103
@Override
99104
public AsyncJobExecutorContext getExecutorContext() {
@@ -370,7 +375,7 @@ private void scheduleExecution(final AsyncJobVO job, boolean executeInContext) {
370375
if (executeInContext) {
371376
runnable.run();
372377
} else {
373-
_executor.submit(runnable);
378+
_executor.submit(runnable);
374379
}
375380
}
376381

@@ -511,7 +516,14 @@ private void executeQueueItem(SyncQueueItemVO item, boolean fromPreviousSession)
511516

512517
job.setCompleteMsid(getMsid());
513518
_jobDao.update(job.getId(), job);
514-
scheduleExecution(job);
519+
520+
try {
521+
scheduleExecution(job);
522+
} catch(RejectedExecutionException e) {
523+
s_logger.warn("Execution for job-" + job.getId() + " is rejected, return it to the queue for next turn");
524+
_queueMgr.returnItem(item.getId());
525+
}
526+
515527
} else {
516528
if(s_logger.isDebugEnabled()) {
517529
s_logger.debug("Unable to find related job for queue item: " + item.toString());
@@ -559,14 +571,14 @@ private Runnable getHeartbeatTask() {
559571
return new Runnable() {
560572
@Override
561573
public void run() {
562-
try {
574+
try {
563575
List<SyncQueueItemVO> l = _queueMgr.dequeueFromAny(getMsid(), MAX_ONETIME_SCHEDULE_SIZE);
564576
if(l != null && l.size() > 0) {
565577
for(SyncQueueItemVO item: l) {
566578
if(s_logger.isDebugEnabled()) {
567579
s_logger.debug("Execute sync-queue item: " + item.toString());
568-
}
569-
executeQueueItem(item, false);
580+
}
581+
executeQueueItem(item, false);
570582
}
571583
}
572584
} catch(Throwable e) {
@@ -709,8 +721,24 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
709721
_accountMgr = locator.getManager(AccountManager.class);
710722

711723
_dispatcher = ApiDispatcher.getInstance();
712-
713-
return true;
724+
725+
726+
try {
727+
final File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
728+
final Properties dbProps = new Properties();
729+
dbProps.load(new FileInputStream(dbPropsFile));
730+
731+
final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive"));
732+
733+
int poolSize = (cloudMaxActive * 2) / 3;
734+
735+
s_logger.info("Start AsyncJobManager thread pool in size " + poolSize);
736+
_executor = Executors.newFixedThreadPool(poolSize, new NamedThreadFactory("Job-Executor"));
737+
} catch (final Exception e) {
738+
throw new ConfigurationException("Unable to load db.properties to configure AsyncJobManagerImpl");
739+
}
740+
741+
return true;
714742
}
715743

716744
@Override

‎server/src/com/cloud/async/SyncQueueManager.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface SyncQueueManager extends Manager {
2727
public SyncQueueItemVO dequeueFromOne(long queueId, Long msid);
2828
public List<SyncQueueItemVO> dequeueFromAny(Long msid, int maxItems);
2929
public void purgeItem(long queueItemId);
30+
public void returnItem(long queueItemId);
3031

3132
public List<SyncQueueItemVO> getActiveQueueItems(Long msid, boolean exclusive);
3233
public List<SyncQueueItemVO> getBlockedQueueItems(long thresholdMs, boolean exclusive);

‎server/src/com/cloud/async/SyncQueueManagerImpl.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,32 @@ public void purgeItem(long queueItemId) {
195195
s_logger.error("Unexpected exception: ", e);
196196
txt.rollback();
197197
}
198+
}
199+
200+
@Override
201+
@DB
202+
public void returnItem(long queueItemId) {
203+
Transaction txt = Transaction.currentTxn();
204+
try {
205+
txt.start();
206+
207+
SyncQueueItemVO itemVO = _syncQueueItemDao.findById(queueItemId);
208+
if(itemVO != null) {
209+
SyncQueueVO queueVO = _syncQueueDao.lockRow(itemVO.getQueueId(), true);
210+
211+
itemVO.setLastProcessMsid(null);
212+
itemVO.setLastProcessNumber(null);
213+
_syncQueueItemDao.update(queueItemId, itemVO);
214+
215+
queueVO.setLastProcessTime(null);
216+
queueVO.setLastUpdated(DateUtil.currentGMTTime());
217+
_syncQueueDao.update(queueVO.getId(), queueVO);
218+
}
219+
txt.commit();
220+
} catch(Exception e) {
221+
s_logger.error("Unexpected exception: ", e);
222+
txt.rollback();
223+
}
198224
}
199225

200226
@Override

0 commit comments

Comments
 (0)