Skip to content

Commit

Permalink
perf: file-gateway调度逻辑优化 TencentBlueKing#2977
Browse files Browse the repository at this point in the history
调度指标数据增加请求来源标签。
  • Loading branch information
jsonwan committed May 15, 2024
1 parent 3a15f07 commit 1119b1b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class MetricsConstants {

// tag
public static final String TAG_KEY_MODULE = "module";
public static final String TAG_KEY_REQUEST_SOURCE = "requestSource";
public static final String TAG_KEY_DISPATCH_RESULT = "dispatchResult";

// value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public interface DispatchService {
* 根据文件源找到一个最适合的FileWorker
*
* @param fileSourceDTO 文件源对象
* @param requestSource 请求来源
* @return 选中的对接文件源的FileWorker对象
*/
FileWorkerDTO findBestFileWorker(FileSourceDTO fileSourceDTO);
FileWorkerDTO findBestFileWorker(FileSourceDTO fileSourceDTO, String requestSource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private List<FileWorkerDTO> getFileWorkerByScopeAndAbilityTag(Long appId,
}

@Override
public FileWorkerDTO findBestFileWorker(FileSourceDTO fileSourceDTO) {
public FileWorkerDTO findBestFileWorker(FileSourceDTO fileSourceDTO, String requestSource) {
Timer.Sample sample = Timer.start(meterRegistry);
FileWorkerDTO fileWorkerDTO = null;
try {
Expand All @@ -157,7 +157,7 @@ public FileWorkerDTO findBestFileWorker(FileSourceDTO fileSourceDTO) {
long nanoSeconds = sample.stop(
meterRegistry.timer(
MetricsConstants.NAME_FILE_GATEWAY_DISPATCH_TIME,
buildDispatchResult(fileWorkerDTO)
buildDispatchResult(fileWorkerDTO, requestSource)
)
);
long millis = TimeUnit.NANOSECONDS.toMillis(nanoSeconds);
Expand All @@ -168,9 +168,10 @@ public FileWorkerDTO findBestFileWorker(FileSourceDTO fileSourceDTO) {
return fileWorkerDTO;
}

private Iterable<Tag> buildDispatchResult(FileWorkerDTO fileWorkerDTO) {
private Iterable<Tag> buildDispatchResult(FileWorkerDTO fileWorkerDTO, String requestSource) {
List<Tag> tagList = new ArrayList<>();
tagList.add(Tag.of(MetricsConstants.TAG_KEY_MODULE, MetricsConstants.TAG_VALUE_MODULE_FILE_GATEWAY));
tagList.add(Tag.of(MetricsConstants.TAG_KEY_REQUEST_SOURCE, requestSource));
if (fileWorkerDTO == null) {
tagList.add(
Tag.of(MetricsConstants.TAG_KEY_DISPATCH_RESULT, MetricsConstants.TAG_VALUE_DISPATCH_RESULT_FALSE)
Expand Down Expand Up @@ -229,7 +230,6 @@ private FileWorkerDTO findWorkerByAuto(FileSourceDTO fileSourceDTO) {
if (!fileWorkerDTOList.isEmpty()) {
// 按策略调度:RoundRobin
int index = (int) (dispatchCount.get() % fileWorkerDTOList.size());
log.debug("choose fileWorker index: {}", index);
fileWorkerDTO = fileWorkerDTOList.get(index);
} else {
log.error("Cannot find available file worker, abilityTagList={}", abilityTagList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ public FileServiceImpl(FileSourceService fileSourceService,
this.jobHttpClient = jobHttpClient;
}

private FileWorkerDTO getFileWorker(FileSourceDTO fileSourceDTO) {
private FileWorkerDTO getFileWorker(FileSourceDTO fileSourceDTO, String requestSource) {
if (fileSourceDTO == null) {
throw new InternalException(ErrorCode.FILE_SOURCE_NOT_EXIST);
}
return dispatchService.findBestFileWorker(fileSourceDTO);
return dispatchService.findBestFileWorker(fileSourceDTO, requestSource);
}

@Override
public boolean isFileAvailable(String username, Long appId, Integer fileSourceId) {
FileSourceDTO fileSourceDTO = fileSourceService.getFileSourceById(appId, fileSourceId);
FileWorkerDTO fileWorkerDTO = getFileWorker(fileSourceDTO);
FileWorkerDTO fileWorkerDTO = getFileWorker(fileSourceDTO, "isFileAvailable");
if (fileWorkerDTO == null) {
throw new InternalException(ErrorCode.CAN_NOT_FIND_AVAILABLE_FILE_WORKER);
}
Expand All @@ -103,7 +103,7 @@ public FileNodesVO listFileNode(String username, Long appId, Integer fileSourceI
if (name == null) name = "";
final String finalName = name;
FileSourceDTO fileSourceDTO = fileSourceService.getFileSourceById(appId, fileSourceId);
FileWorkerDTO fileWorkerDTO = getFileWorker(fileSourceDTO);
FileWorkerDTO fileWorkerDTO = getFileWorker(fileSourceDTO, "listFileNode");
if (fileWorkerDTO == null) {
throw new InternalException(ErrorCode.CAN_NOT_FIND_AVAILABLE_FILE_WORKER);
}
Expand All @@ -129,7 +129,10 @@ public FileNodesVO listFileNode(String username, Long appId, Integer fileSourceI
@Override
public Boolean executeAction(String username, Long appId, Integer fileSourceId, ExecuteActionReq executeActionReq) {
FileSourceDTO fileSourceDTO = fileSourceService.getFileSourceById(appId, fileSourceId);
FileWorkerDTO fileWorkerDTO = getFileWorker(fileSourceDTO);
FileWorkerDTO fileWorkerDTO = getFileWorker(
fileSourceDTO,
"executeAction" + executeActionReq.getActionCode()
);
if (fileWorkerDTO == null) {
throw new InternalException(ErrorCode.CAN_NOT_FIND_AVAILABLE_FILE_WORKER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ public TaskInfoDTO startFileSourceDownloadTaskWithId(String username, Long appId
if (fileSourceDTO == null) {
throw new RuntimeException("FileSource not exist, fileSourceId=" + fileSourceId.toString());
}
FileWorkerDTO fileWorkerDTO = dispatchService.findBestFileWorker(fileSourceDTO);
FileWorkerDTO fileWorkerDTO = dispatchService.findBestFileWorker(
fileSourceDTO,
"DownloadTask(appId=" + appId + ")"
);
if (fileWorkerDTO == null) {
throw new RuntimeException(String.format("Cannot match fileWorker for FileSourceTask,appId=%d," +
"stepInstanceId=%d,fileSourceId=%d,filePathList=%s", appId, stepInstanceId, fileSourceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void run() {
// 2.删除现有FileSourceTask任务
fileSourceTaskService.deleteFileSourceTaskById(fileSourceTaskId);
log.debug("delete fileSourceTask {}", fileSourceTaskId);
FileWorkerDTO fileWorkerDTO = dispatchService.findBestFileWorker(fileSourceDTO);
FileWorkerDTO fileWorkerDTO = dispatchService.findBestFileWorker(fileSourceDTO, "ReDispatch");
log.debug("found bestWorker:{}", fileSourceDTO);
if (fileWorkerDTO != null) {
// 3.重新派发任务
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public void run() {
pageSize
);
for (FileSourceDTO fileSourceDTO : fileSourceDTOList) {
FileWorkerDTO fileWorkerDTO = dispatchService.findBestFileWorker(fileSourceDTO);
FileWorkerDTO fileWorkerDTO = dispatchService.findBestFileWorker(
fileSourceDTO, "FileSourceStatusUpdateTask"
);
int status;
if (fileWorkerDTO == null) {
log.info(
Expand Down

0 comments on commit 1119b1b

Please sign in to comment.