Skip to content

Commit

Permalink
Merge pull request #2650 from jsonwan/github_perf/crontab
Browse files Browse the repository at this point in the history
perf: 定时任务触发延迟优化 #2073
  • Loading branch information
jsonwan authored Dec 1, 2023
2 parents 756fc3c + 6594816 commit 78148a5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.crontab.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ScheduledThreadPoolExecutor;

//设定一个长度5的定时任务线程池
@Slf4j
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(
new ScheduledThreadPoolExecutor(
5, (r, executor) ->
log.error("ScheduledThreadPoolExecutor rejected a runnable")
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.tencent.bk.job.crontab.util.DbRecordMapper;
import com.tencent.bk.job.crontab.util.DbUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
Expand All @@ -52,6 +53,7 @@
import org.jooq.Result;
import org.jooq.TableField;
import org.jooq.UpdateSetMoreStep;
import org.jooq.conf.ParamType;
import org.jooq.types.UByte;
import org.jooq.types.UInteger;
import org.jooq.types.ULong;
Expand Down Expand Up @@ -534,14 +536,22 @@ public List<CronJobBasicInfoDTO> listEnabledCronBasicInfoForUpdate(int start, in
List<Condition> conditions = new ArrayList<>();
conditions.add(TABLE.IS_DELETED.eq(UByte.valueOf(0)));
conditions.add(TABLE.IS_ENABLE.eq(UByte.valueOf(1)));
Result<Record3<ULong, ULong, String>> records = context
val query = context
.select(TABLE.ID, TABLE.APP_ID, TABLE.NAME)
.from(TABLE)
.where(conditions)
.orderBy(TABLE.ID)
.limit(start, limit)
.forUpdate()
.fetch();
.forUpdate();
if (log.isDebugEnabled()) {
log.debug(
"start={},limit={},SQL={}",
start,
limit,
query.getSQL(ParamType.INLINED)
);
}
Result<Record3<ULong, ULong, String>> records = query.fetch();
return records.map(record -> {
CronJobBasicInfoDTO cronJobBasicInfoDTO = new CronJobBasicInfoDTO();
cronJobBasicInfoDTO.setId(record.get(TABLE.ID).longValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CronJobBatchLoadServiceImpl(CronJobService cronJobService) {
}

@Override
@Transactional(rollbackFor = {Exception.class, Error.class})
@Transactional(rollbackFor = {Exception.class, Error.class}, timeout = 30)
public CronLoadResult batchLoadCronToQuartz(int start, int limit) {
int successNum = 0;
int failedNum = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public void loadAllCronJob() {
private void loadAllCronJobToQuartz() {
int start = 0;
int limit = 100;
int fetchNum = 0;
int currentFetchNum;
int allFetchNum = 0;
int successNum = 0;
int failedNum = 0;
List<CronJobBasicInfoDTO> failedCronList = new ArrayList<>();
Expand All @@ -75,17 +76,18 @@ private void loadAllCronJobToQuartz() {
start,
limit
);
fetchNum += loadResult.getFetchNum();
currentFetchNum = loadResult.getFetchNum();
allFetchNum += currentFetchNum;
successNum += loadResult.getSuccessNum();
failedNum += loadResult.getFailedNum();
if (CollectionUtils.isNotEmpty(loadResult.getFailedCronList())) {
failedCronList.addAll(loadResult.getFailedCronList());
}
start += limit;
} while (fetchNum > 0);
} while (currentFetchNum > 0);
log.info(
"CronJobs load from db finished: fetchNum={}, successNum={}, failedNum={}, failedCronList={}",
fetchNum,
allFetchNum,
successNum,
failedNum,
failedCronList
Expand Down

0 comments on commit 78148a5

Please sign in to comment.