Skip to content

Commit

Permalink
Merge pull request #3379 from jsonwan/github_perf/cron
Browse files Browse the repository at this point in the history
perf: 定时任务全量加载速度优化 #3363
  • Loading branch information
jsonwan authored Jan 15, 2025
2 parents 51783d7 + a83f1ad commit a217c9c
Show file tree
Hide file tree
Showing 23 changed files with 833 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class JobApplicationAvailabilityBean extends ApplicationAvailabilityBean
public void onApplicationEvent(AvailabilityChangeEvent<?> event) {
super.onApplicationEvent(event);
if (ReadinessState.REFUSING_TRAFFIC == event.getState()) {
// SpringCloud负载均衡缓存默认为35s,等待调用方缓存刷新后再真正关闭Spring容器
int waitSeconds = 40;
// SpringCloud负载均衡缓存设置为20s,等待调用方缓存刷新后再真正关闭Spring容器
int waitSeconds = 30;
while (waitSeconds > 0) {
ThreadUtils.sleep(1000);
log.info("wait for GracefulShutdown, {}s left", waitSeconds--);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ PageData<CronJobInfoDTO> listPageCronJobsWithoutVarsByCondition(CronJobInfoDTO c
* @param cronJobIdList 定时任务 IDs
* @return 定时任务信息
*/
List<CronJobInfoDTO> getCronJobByIds(List<Long> cronJobIdList);
List<CronJobInfoDTO> listCronJobByIds(List<Long> cronJobIdList);

/**
* 根据定时任务 ID 查询定时任务信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public CronJobInfoDTO getCronJobById(long cronJobId) {
}

@Override
public List<CronJobInfoDTO> getCronJobByIds(List<Long> cronJobIdList) {
public List<CronJobInfoDTO> listCronJobByIds(List<Long> cronJobIdList) {
List<Condition> conditions = new ArrayList<>();
conditions.add(TABLE.ID.in(cronJobIdList.stream().map(ULong::valueOf).collect(Collectors.toList())));
conditions.add(TABLE.IS_DELETED.equal(UByte.valueOf(0)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.tencent.bk.job.crontab.listener.event.CrontabEvent;
import com.tencent.bk.job.crontab.model.dto.CronJobInfoDTO;
import com.tencent.bk.job.crontab.service.CronJobService;
import com.tencent.bk.job.crontab.service.QuartzService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -41,10 +42,12 @@
public class CrontabEventListener {

private final CronJobService cronJobService;
private final QuartzService quartzService;

@Autowired
public CrontabEventListener(CronJobService cronJobService) {
public CrontabEventListener(CronJobService cronJobService, QuartzService quartzService) {
this.cronJobService = cronJobService;
this.quartzService = quartzService;
}


Expand Down Expand Up @@ -86,7 +89,7 @@ private void refreshCronJobInQuartz(CronJobInfoDTO cronJobInfoDTO) {
}
if (cronJobInfoDTO.getEnable()) {
// 开启定时任务
boolean result = cronJobService.addJobToQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
boolean result = cronJobService.checkAndAddJobToQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
log.info(
"add cronJob({},{}) to quartz, result={}",
cronJobInfoDTO.getAppId(),
Expand All @@ -95,7 +98,7 @@ private void refreshCronJobInQuartz(CronJobInfoDTO cronJobInfoDTO) {
);
} else {
// 关闭定时任务
boolean result = cronJobService.deleteJobFromQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
boolean result = quartzService.deleteJobFromQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
log.info(
"delete cronJob({},{}) from quartz, result={}",
cronJobInfoDTO.getAppId(),
Expand All @@ -107,7 +110,7 @@ private void refreshCronJobInQuartz(CronJobInfoDTO cronJobInfoDTO) {

private void deleteCronJobFromQuartz(long appId, long cronJobId) {
// 删除定时任务
boolean result = cronJobService.deleteJobFromQuartz(appId, cronJobId);
boolean result = quartzService.deleteJobFromQuartz(appId, cronJobId);
log.info(
"delete cronJob({},{}) from quartz, result={}",
appId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.model.dto;

import lombok.Data;

/**
* 添加任务到Quartz的结果
*/
@Data
public class AddJobToQuartzResult {
/**
* 定时任务基本信息
*/
private CronJobBasicInfoDTO cronJobBasicInfo;
/**
* 是否成功
*/
private boolean success;
/**
* 提示信息
*/
private String message;
/**
* 异常信息
*/
private Exception exception;

/**
* 构造失败结果
*
* @param cronJobBasicInfo 定时任务基本信息
* @param message 提示信息
* @return 失败结果
*/
public static AddJobToQuartzResult failResult(CronJobBasicInfoDTO cronJobBasicInfo, String message) {
AddJobToQuartzResult result = new AddJobToQuartzResult();
result.setCronJobBasicInfo(cronJobBasicInfo);
result.setSuccess(false);
result.setMessage(message);
return result;
}

/**
* 构造失败结果
*
* @param cronJobBasicInfo 定时任务基本信息
* @param message 提示信息
* @param exception 异常信息
* @return 失败结果
*/
public static AddJobToQuartzResult failResult(CronJobBasicInfoDTO cronJobBasicInfo,
String message,
Exception exception) {
AddJobToQuartzResult result = new AddJobToQuartzResult();
result.setCronJobBasicInfo(cronJobBasicInfo);
result.setSuccess(false);
result.setMessage(message);
result.setException(exception);
return result;
}

/**
* 构造成功结果
*
* @param cronJobBasicInfo 定时任务基本信息
* @return 成功结果
*/
public static AddJobToQuartzResult successResult(CronJobBasicInfoDTO cronJobBasicInfo) {
AddJobToQuartzResult result = new AddJobToQuartzResult();
result.setCronJobBasicInfo(cronJobBasicInfo);
result.setSuccess(true);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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.model.dto;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 定时任务批量添加到Quartz的结果
*/
@Slf4j
@Data
public class BatchAddResult {
/**
* 添加结果Map,key为定时任务ID,value为添加结果
*/
private Map<Long, AddJobToQuartzResult> resultMap;
/**
* 添加成功的任务数量
*/
private int successNum = 0;
/**
* 添加失败的任务数量
*/
private int failNum = 0;

/**
* 添加一个结果数据至批量结果
*
* @param result 单个结果数据
*/
public void addResult(AddJobToQuartzResult result) {
if (result == null) {
return;
}
if (resultMap == null) {
resultMap = new HashMap<>();
}
CronJobBasicInfoDTO cronJobBasicInfo = result.getCronJobBasicInfo();
if (cronJobBasicInfo == null) {
log.info("cronJobBasicInfo is null, ignore");
return;
}
resultMap.put(cronJobBasicInfo.getId(), result);
if (result.isSuccess()) {
successNum++;
} else {
failNum++;
}
}

/**
* 合并批量结果
*
* @param batchAddResult 待合并的批量结果
*/
public void merge(BatchAddResult batchAddResult) {
if (batchAddResult == null) {
return;
}
if (CollectionUtils.isEmpty(batchAddResult.resultMap)) {
return;
}
if (resultMap == null) {
resultMap = new HashMap<>(batchAddResult.resultMap);
} else {
resultMap.putAll(batchAddResult.resultMap);
}
successNum += batchAddResult.successNum;
failNum += batchAddResult.failNum;
}

/**
* 获取批量添加失败的添加结果列表
*
* @return 批量添加失败的添加结果列表
*/
public List<AddJobToQuartzResult> getFailedResultList() {
if (failNum == 0) {
return Collections.emptyList();
}
List<AddJobToQuartzResult> failedResultList = new ArrayList<>();
for (Map.Entry<Long, AddJobToQuartzResult> entry : resultMap.entrySet()) {
if (!entry.getValue().isSuccess()) {
failedResultList.add(entry.getValue());
}
}
return failedResultList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,12 @@ public ServiceCronJobDTO toServiceCronJobDTO() {
cronJob.setLastModifyTime(lastModifyTime);
return cronJob;
}

public CronJobBasicInfoDTO toBasicInfoDTO() {
CronJobBasicInfoDTO cronJob = new CronJobBasicInfoDTO();
cronJob.setId(id);
cronJob.setAppId(appId);
cronJob.setName(name);
return cronJob;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.service;

import com.tencent.bk.job.crontab.model.BatchUpdateCronJobReq;
import com.tencent.bk.job.crontab.model.dto.BatchAddResult;
import com.tencent.bk.job.crontab.model.dto.CronJobBasicInfoDTO;
import com.tencent.bk.job.crontab.model.dto.NeedScheduleCronInfo;

import java.util.List;

public interface BatchCronJobService {

/**
* 批量添加定时任务到Quartz
*
* @param cronJobBasicInfoList 定时任务列表
* @return 批量添加结果
*/
BatchAddResult batchAddJobToQuartz(List<CronJobBasicInfoDTO> cronJobBasicInfoList);

/**
* 批量更新定时任务
*
* @param username 用户名
* @param appId Job业务ID
* @param batchUpdateCronJobReq 批量更新请求
* @return 更新结果数据
*/
NeedScheduleCronInfo batchUpdateCronJob(String username,
Long appId,
BatchUpdateCronJobReq batchUpdateCronJobReq);
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,7 @@ PageData<CronJobInfoDTO> listPageCronJobInfosWithoutVars(CronJobInfoDTO cronJobC

Integer countCronJob(Long appId, Boolean active, Boolean cron);

boolean addJobToQuartz(long appId, long cronJobId) throws ServiceException;

boolean deleteJobFromQuartz(long appId, long cronJobId);
boolean checkAndAddJobToQuartz(long appId, long cronJobId) throws ServiceException;

List<CronJobBasicInfoDTO> listEnabledCronBasicInfoForUpdate(int start, int limit);

Expand Down
Loading

0 comments on commit a217c9c

Please sign in to comment.