|
@@ -5,10 +5,9 @@ import com.xxl.job.admin.core.trigger.XxlJobTrigger;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-import java.util.concurrent.LinkedBlockingQueue;
|
|
|
-import java.util.concurrent.ThreadFactory;
|
|
|
-import java.util.concurrent.ThreadPoolExecutor;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
/**
|
|
|
* job trigger thread pool helper
|
|
@@ -21,32 +20,91 @@ public class JobTriggerPoolHelper {
|
|
|
|
|
|
// ---------------------- trigger pool ----------------------
|
|
|
|
|
|
- private ThreadPoolExecutor triggerPool = new ThreadPoolExecutor(
|
|
|
- 32,
|
|
|
- 256,
|
|
|
+ // fast/slow thread pool
|
|
|
+ private ThreadPoolExecutor fastTriggerPool = new ThreadPoolExecutor(
|
|
|
+ 8,
|
|
|
+ 200,
|
|
|
60L,
|
|
|
TimeUnit.SECONDS,
|
|
|
new LinkedBlockingQueue<Runnable>(1000),
|
|
|
new ThreadFactory() {
|
|
|
@Override
|
|
|
public Thread newThread(Runnable r) {
|
|
|
- return new Thread(r, "xxl-job, admin JobTriggerPoolHelper-triggerPool-" + r.hashCode());
|
|
|
+ return new Thread(r, "xxl-job, admin JobTriggerPoolHelper-fastTriggerPool-" + r.hashCode());
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+ private ThreadPoolExecutor slowTriggerPool = new ThreadPoolExecutor(
|
|
|
+ 0,
|
|
|
+ 100,
|
|
|
+ 60L,
|
|
|
+ TimeUnit.SECONDS,
|
|
|
+ new LinkedBlockingQueue<Runnable>(2000),
|
|
|
+ new ThreadFactory() {
|
|
|
+ @Override
|
|
|
+ public Thread newThread(Runnable r) {
|
|
|
+ return new Thread(r, "xxl-job, admin JobTriggerPoolHelper-slowTriggerPool-" + r.hashCode());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ // job timeout count
|
|
|
+ private volatile long minTim = System.currentTimeMillis()/60000; // ms > min
|
|
|
+ private volatile Map<Integer, AtomicInteger> jobTimeoutCountMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * add trigger
|
|
|
+ */
|
|
|
public void addTrigger(final int jobId, final TriggerTypeEnum triggerType, final int failRetryCount, final String executorShardingParam, final String executorParam) {
|
|
|
- triggerPool.execute(new Runnable() {
|
|
|
+
|
|
|
+ // choose thread pool
|
|
|
+ ThreadPoolExecutor triggerPool_ = fastTriggerPool;
|
|
|
+ AtomicInteger jobTimeoutCount = jobTimeoutCountMap.get(jobId);
|
|
|
+ if (jobTimeoutCount!=null && jobTimeoutCount.get() > 10) { // job-timeout 10 times in 1 min
|
|
|
+ triggerPool_ = slowTriggerPool;
|
|
|
+ }
|
|
|
+
|
|
|
+ // trigger
|
|
|
+ triggerPool_.execute(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
- XxlJobTrigger.trigger(jobId, triggerType, failRetryCount, executorShardingParam, executorParam);
|
|
|
+
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // do trigger
|
|
|
+ XxlJobTrigger.trigger(jobId, triggerType, failRetryCount, executorShardingParam, executorParam);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ } finally {
|
|
|
+
|
|
|
+ // check timeout-count-map
|
|
|
+ long minTim_now = System.currentTimeMillis()/60000;
|
|
|
+ if (minTim != minTim_now) {
|
|
|
+ minTim = minTim_now;
|
|
|
+ jobTimeoutCountMap.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ // incr timeout-count-map
|
|
|
+ long cost = System.currentTimeMillis()-start;
|
|
|
+ if (cost > 500) { // ob-timeout threshold 500ms
|
|
|
+ AtomicInteger timeoutCount = jobTimeoutCountMap.put(jobId, new AtomicInteger(1));
|
|
|
+ if (timeoutCount != null) {
|
|
|
+ timeoutCount.incrementAndGet();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
public void stop() {
|
|
|
//triggerPool.shutdown();
|
|
|
- triggerPool.shutdownNow();
|
|
|
+ fastTriggerPool.shutdownNow();
|
|
|
+ slowTriggerPool.shutdownNow();
|
|
|
logger.info(">>>>>>>>> xxl-job trigger thread pool shutdown success.");
|
|
|
}
|
|
|
|