|
18 | 18 |
|
19 | 19 | package com.cloud.async; |
20 | 20 |
|
| 21 | +import java.io.File; |
| 22 | +import java.io.FileInputStream; |
21 | 23 | import java.lang.reflect.Type; |
22 | 24 | import java.util.Date; |
23 | 25 | import java.util.List; |
24 | 26 | import java.util.Map; |
| 27 | +import java.util.Properties; |
25 | 28 | import java.util.Random; |
26 | 29 | import java.util.concurrent.ExecutorService; |
27 | 30 | import java.util.concurrent.Executors; |
| 31 | +import java.util.concurrent.RejectedExecutionException; |
28 | 32 | import java.util.concurrent.ScheduledExecutorService; |
29 | 33 | import java.util.concurrent.TimeUnit; |
30 | 34 |
|
|
58 | 62 | import com.cloud.user.dao.AccountDao; |
59 | 63 | import com.cloud.utils.DateUtil; |
60 | 64 | import com.cloud.utils.NumbersUtil; |
| 65 | +import com.cloud.utils.PropertiesUtil; |
61 | 66 | import com.cloud.utils.component.ComponentLocator; |
62 | 67 | import com.cloud.utils.concurrency.NamedThreadFactory; |
63 | 68 | import com.cloud.utils.db.DB; |
@@ -93,7 +98,7 @@ public class AsyncJobManagerImpl implements AsyncJobManager, ClusterManagerListe |
93 | 98 |
|
94 | 99 | private final ScheduledExecutorService _heartbeatScheduler = |
95 | 100 | Executors.newScheduledThreadPool(1, new NamedThreadFactory("AsyncJobMgr-Heartbeat")); |
96 | | - private final ExecutorService _executor = Executors.newCachedThreadPool(new NamedThreadFactory("Job-Executor")); |
| 101 | + private ExecutorService _executor; |
97 | 102 |
|
98 | 103 | @Override |
99 | 104 | public AsyncJobExecutorContext getExecutorContext() { |
@@ -370,7 +375,7 @@ private void scheduleExecution(final AsyncJobVO job, boolean executeInContext) { |
370 | 375 | if (executeInContext) { |
371 | 376 | runnable.run(); |
372 | 377 | } else { |
373 | | - _executor.submit(runnable); |
| 378 | + _executor.submit(runnable); |
374 | 379 | } |
375 | 380 | } |
376 | 381 |
|
@@ -511,7 +516,14 @@ private void executeQueueItem(SyncQueueItemVO item, boolean fromPreviousSession) |
511 | 516 |
|
512 | 517 | job.setCompleteMsid(getMsid()); |
513 | 518 | _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 | + |
515 | 527 | } else { |
516 | 528 | if(s_logger.isDebugEnabled()) { |
517 | 529 | s_logger.debug("Unable to find related job for queue item: " + item.toString()); |
@@ -559,14 +571,14 @@ private Runnable getHeartbeatTask() { |
559 | 571 | return new Runnable() { |
560 | 572 | @Override |
561 | 573 | public void run() { |
562 | | - try { |
| 574 | + try { |
563 | 575 | List<SyncQueueItemVO> l = _queueMgr.dequeueFromAny(getMsid(), MAX_ONETIME_SCHEDULE_SIZE); |
564 | 576 | if(l != null && l.size() > 0) { |
565 | 577 | for(SyncQueueItemVO item: l) { |
566 | 578 | if(s_logger.isDebugEnabled()) { |
567 | 579 | s_logger.debug("Execute sync-queue item: " + item.toString()); |
568 | | - } |
569 | | - executeQueueItem(item, false); |
| 580 | + } |
| 581 | + executeQueueItem(item, false); |
570 | 582 | } |
571 | 583 | } |
572 | 584 | } catch(Throwable e) { |
@@ -709,8 +721,24 @@ public boolean configure(String name, Map<String, Object> params) throws Configu |
709 | 721 | _accountMgr = locator.getManager(AccountManager.class); |
710 | 722 |
|
711 | 723 | _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; |
714 | 742 | } |
715 | 743 |
|
716 | 744 | @Override |
|
0 commit comments