Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 定时任务触发延迟优化 #2073 #2647

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 com.tencent.bk.job.crontab.listener.CrontabEventListener;
import com.tencent.bk.job.crontab.listener.event.CrontabEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Consumer;

/**
* spring cloud function 定义
* <p>
* 注意:方法名与配置文件中的spring.cloud.function.definition对应,修改需要注意!!!
*/
@Configuration
@Slf4j
public class JobFunctionConfiguration {
@Bean
public Consumer<CrontabEvent> handleCrontabFanoutEvent(@Autowired CrontabEventListener crontabEventListener) {
log.info("Init handleCrontabFanoutEvent consumer");
return crontabEventListener::handleEvent;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.constant;

/**
* 定时任务相关操作
*/
public enum CrontabActionEnum {
/**
* 添加定时任务
*/
ADD_CRON(1),
/**
* 删除定时任务
*/
DELETE_CRON(2);

private final int value;

CrontabActionEnum(int val) {
this.value = val;
}

public static CrontabActionEnum valueOf(int value) {
for (CrontabActionEnum crontabAction : values()) {
if (crontabAction.getValue() == value) {
return crontabAction;
}
}
throw new IllegalArgumentException("No CrontabActionEnum constant: " + value);
}

public int getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.tencent.bk.job.common.model.BaseSearchCondition;
import com.tencent.bk.job.common.model.PageData;
import com.tencent.bk.job.crontab.model.dto.CronJobBasicInfoDTO;
import com.tencent.bk.job.crontab.model.dto.CronJobInfoDTO;
import com.tencent.bk.job.crontab.model.dto.CronJobWithVarsDTO;

Expand Down Expand Up @@ -137,6 +138,7 @@ PageData<CronJobInfoDTO> listPageCronJobsByCondition(CronJobInfoDTO cronJobCondi

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

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

// 新增

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.tencent.bk.job.common.model.dto.UserRoleInfoDTO;
import com.tencent.bk.job.common.util.json.JsonUtils;
import com.tencent.bk.job.crontab.dao.CronJobDAO;
import com.tencent.bk.job.crontab.model.dto.CronJobBasicInfoDTO;
import com.tencent.bk.job.crontab.model.dto.CronJobInfoDTO;
import com.tencent.bk.job.crontab.model.dto.CronJobVariableDTO;
import com.tencent.bk.job.crontab.model.dto.CronJobWithVarsDTO;
Expand All @@ -45,6 +46,7 @@
import org.jooq.OrderField;
import org.jooq.Record1;
import org.jooq.Record21;
import org.jooq.Record3;
import org.jooq.Record4;
import org.jooq.Record5;
import org.jooq.Result;
Expand Down Expand Up @@ -527,6 +529,28 @@ assert record != null;
return record.value1();
}

@Override
public List<CronJobBasicInfoDTO> listEnabledCronBasicInfoForUpdate(int start, int limit) {
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
.select(TABLE.ID, TABLE.APP_ID, TABLE.NAME)
.from(TABLE)
.where(conditions)
.orderBy(TABLE.ID)
.limit(start, limit)
.forUpdate()
.fetch();
return records.map(record -> {
CronJobBasicInfoDTO cronJobBasicInfoDTO = new CronJobBasicInfoDTO();
cronJobBasicInfoDTO.setId(record.get(TABLE.ID).longValue());
cronJobBasicInfoDTO.setAppId(record.get(TABLE.APP_ID).longValue());
cronJobBasicInfoDTO.setName(record.get(TABLE.NAME));
return cronJobBasicInfoDTO;
});
}

private CronJobInfoDTO convertToCronJobDTO(Record21<ULong, ULong, String, String, ULong, ULong, String, ULong,
String, ULong, String, UByte, UByte, UByte, ULong, String, ULong, ULong, ULong, String, String> record) {
if (record == null) {
Expand Down
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.listener;

import com.tencent.bk.job.crontab.constant.CrontabActionEnum;
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 lombok.extern.slf4j.Slf4j;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* 定时任务事件处理
*/
@Component("crontabEventListener")
@Slf4j
public class CrontabEventListener {

private final CronJobService cronJobService;

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


/**
* 处理定时任务相关的事件
*
* @param crontabEvent 定时任务相关的事件
*/
public void handleEvent(CrontabEvent crontabEvent) {
log.info("Handle crontab event, event: {}, duration: {}ms", crontabEvent, crontabEvent.duration());
long appId = crontabEvent.getAppId();
long cronJobId = crontabEvent.getCronJobId();
CrontabActionEnum action = CrontabActionEnum.valueOf(crontabEvent.getAction());
try {
switch (action) {
case ADD_CRON:
CronJobInfoDTO cronJobInfoDTO = cronJobService.getCronJobInfoById(cronJobId);
refreshCronJobInQuartz(cronJobInfoDTO);
break;
case DELETE_CRON:
deleteCronJobFromQuartz(appId, cronJobId);
break;
default:
log.error("Invalid crontabEvent action: {}", action);
}
} catch (Throwable e) {
String errorMsg = MessageFormatter.format(
"Handle crontab event error, appId={}, cronJobId={}",
appId,
cronJobId
).getMessage();
log.error(errorMsg, e);
}
}

private void refreshCronJobInQuartz(CronJobInfoDTO cronJobInfoDTO) {
if (cronJobInfoDTO == null) {
return;
}
if (cronJobInfoDTO.getEnable()) {
// 开启定时任务
boolean result = cronJobService.addJobToQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
log.info(
"add cronJob({},{}) to quartz, result={}",
cronJobInfoDTO.getAppId(),
cronJobInfoDTO.getId(),
result
);
} else {
// 关闭定时任务
boolean result = cronJobService.deleteJobFromQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
log.info(
"delete cronJob({},{}) from quartz, result={}",
cronJobInfoDTO.getAppId(),
cronJobInfoDTO.getId(),
result
);
}
}

private void deleteCronJobFromQuartz(long appId, long cronJobId) {
// 删除定时任务
boolean result = cronJobService.deleteJobFromQuartz(appId, cronJobId);
log.info(
"delete cronJob({},{}) from quartz, result={}",
appId,
cronJobId,
result
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.listener.event;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.tencent.bk.job.crontab.constant.CrontabActionEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.StringJoiner;

/**
* 定时任务事件
*/
@Getter
@Setter
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CrontabEvent extends Event {
/**
* 定时任务操作
*
* @see CrontabActionEnum
*/
private int action;
/**
* Job业务ID
*/
private Long appId;
/**
* 定时任务ID
*/
private Long cronJobId;

/**
* 构造添加定时任务事件
*
* @param appId Job业务ID
* @param cronJobId 定时任务ID
* @return 事件
*/
public static CrontabEvent addCron(long appId, long cronJobId) {
CrontabEvent crontabEvent = new CrontabEvent();
crontabEvent.setAppId(appId);
crontabEvent.setCronJobId(cronJobId);
crontabEvent.setAction(CrontabActionEnum.ADD_CRON.getValue());
crontabEvent.setTime(LocalDateTime.now());
return crontabEvent;
}

/**
* 构造删除定时任务事件
*
* @param appId Job业务ID
* @param cronJobId 定时任务ID
* @return 事件
*/
public static CrontabEvent deleteCron(long appId, long cronJobId) {
CrontabEvent crontabEvent = new CrontabEvent();
crontabEvent.setAppId(appId);
crontabEvent.setCronJobId(cronJobId);
crontabEvent.setAction(CrontabActionEnum.DELETE_CRON.getValue());
crontabEvent.setTime(LocalDateTime.now());
return crontabEvent;
}

@Override
public String toString() {
return new StringJoiner(", ", CrontabEvent.class.getSimpleName() + "[", "]")
.add("action=" + action)
.add("appId=" + appId)
.add("cronJobId=" + cronJobId)
.add("time=" + time)
.toString();
}
}
Loading
Loading