Skip to content

Commit

Permalink
Merge pull request #3368 from wangyu096/issue_3305
Browse files Browse the repository at this point in the history
feat: 定时任务兼容不合法主机 #3305
  • Loading branch information
wangyu096 authored Dec 27, 2024
2 parents 31667b1 + 246a3dc commit cbb392e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,17 @@ public class HostDTO implements Cloneable {
*/
private String cloudVendorName;

/**
* 管控区域:IPv4
*/
@JsonIgnore
private String cloudIp;

@Deprecated
public HostDTO(Long bkCloudId, String ip) {
public HostDTO(Long bkCloudId, String ipv4) {
this.bkCloudId = bkCloudId;
this.ip = ip;
this.ip = ipv4;
this.cloudIp = buildCloudIp(bkCloudId, ipv4);
}

public HostDTO(Long hostId) {
Expand Down Expand Up @@ -173,21 +180,29 @@ public static HostDTO fromHostIdOrCloudIp(Long hostId, String cloudIp) {
* 返回主机 云区域:ipv4
*/
public String toCloudIp() {
if (StringUtils.isNotEmpty(cloudIp)) {
return cloudIp;
}
if (StringUtils.isEmpty(ip)) {
return null;
} else {
return bkCloudId + ":" + ip;
cloudIp = buildCloudIp(bkCloudId, ip);
return cloudIp;
}
}

private String buildCloudIp(Long bkCloudId, String ip) {
return bkCloudId + ":" + ip;
}

/**
* 返回主机 云区域:ipv6
*/
public String toCloudIpv6() {
if (StringUtils.isEmpty(ipv6)) {
return null;
} else {
return bkCloudId + ":" + ipv6;
return buildCloudIp(bkCloudId, ipv6);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

package com.tencent.bk.job.execute.model;

import com.tencent.bk.job.common.model.HostCompositeKey;
import com.tencent.bk.job.common.model.dto.Container;
import com.tencent.bk.job.common.model.dto.HostDTO;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
Expand All @@ -44,6 +44,7 @@
*/
@Getter
@ToString
@Slf4j
public class TaskInstanceExecuteObjects {

/**
Expand Down Expand Up @@ -86,9 +87,13 @@ public class TaskInstanceExecuteObjects {
@Setter
private Map<Long, List<String>> whiteHostAllowActions;
/**
* 全量主机 Map
* 全量主机 Map - key: hostId
*/
private final Map<HostCompositeKey, HostDTO> hostMap = new HashMap<>();
private final Map<Long, HostDTO> hostsByHostId = new HashMap<>();
/**
* 全量主机 Map - key: cloudIp
*/
private final Map<String, HostDTO> hostsByCloudIp = new HashMap<>();

public void addContainers(Collection<Container> containers) {
if (validContainers == null) {
Expand Down Expand Up @@ -121,7 +126,25 @@ public void setValidHosts(List<HostDTO> validHosts) {

private void putHostMap(List<HostDTO> hosts) {
if (CollectionUtils.isNotEmpty(hosts)) {
hosts.forEach(host -> hostMap.put(host.getUniqueKey(), host));
hosts.forEach(host -> {
if (host.getHostId() != null) {
hostsByHostId.put(host.getHostId(), host);
}
if (host.toCloudIp() != null) {
hostsByCloudIp.put(host.toCloudIp(), host);
}
});
}
}

public HostDTO queryByHostKey(HostDTO host) {
if (host.getHostId() != null) {
return hostsByHostId.get(host.getHostId());
} else if (host.toCloudIp() != null) {
return hostsByCloudIp.get(host.toCloudIp());
} else {
log.info("Host not found, host: {}", host);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -468,22 +468,20 @@ private void fillTaskInstanceHostDetail(TaskInstanceDTO taskInstance,

fillHostAgent(taskInstance, taskInstanceExecuteObjects);

Map<HostCompositeKey, HostDTO> hostMap = taskInstanceExecuteObjects.getHostMap();

for (StepInstanceDTO stepInstance : stepInstanceList) {
if (!stepInstance.isStepContainsExecuteObject()) {
continue;
}
// 目标主机设置主机详情
fillTargetHostDetail(stepInstance, hostMap);
fillTargetHostDetail(stepInstance, taskInstanceExecuteObjects);
// 文件源设置主机详情
fillFileSourceHostDetail(stepInstance, hostMap);
fillFileSourceHostDetail(stepInstance, taskInstanceExecuteObjects);
}

if (CollectionUtils.isNotEmpty(variables)) {
variables.forEach(variable -> {
if (variable.getType() == TaskVariableTypeEnum.HOST_LIST.getType()) {
fillHostsDetail(variable.getExecuteTarget(), hostMap);
fillHostsDetail(variable.getExecuteTarget(), taskInstanceExecuteObjects);
}
});
}
Expand Down Expand Up @@ -528,37 +526,41 @@ private void setHostAgentId(boolean isUsingGseV2, HostDTO host, Set<HostDTO> inv
}
}

private void fillTargetHostDetail(StepInstanceDTO stepInstance, Map<HostCompositeKey, HostDTO> hostMap) {
fillHostsDetail(stepInstance.getTargetExecuteObjects(), hostMap);
private void fillTargetHostDetail(StepInstanceDTO stepInstance,
TaskInstanceExecuteObjects taskInstanceExecuteObjects) {
fillHostsDetail(stepInstance.getTargetExecuteObjects(), taskInstanceExecuteObjects);
}

private void fillFileSourceHostDetail(StepInstanceDTO stepInstance, Map<HostCompositeKey, HostDTO> hostMap) {
private void fillFileSourceHostDetail(StepInstanceDTO stepInstance,
TaskInstanceExecuteObjects taskInstanceExecuteObjects) {
if (stepInstance.getExecuteType() == SEND_FILE) {
List<FileSourceDTO> fileSourceList = stepInstance.getFileSourceList();
if (fileSourceList != null) {
for (FileSourceDTO fileSource : fileSourceList) {
fillHostsDetail(fileSource.getServers(), hostMap);
fillHostsDetail(fileSource.getServers(), taskInstanceExecuteObjects);
}
}
}
}

private void fillHostsDetail(ExecuteTargetDTO executeTargetDTO, Map<HostCompositeKey, HostDTO> hostMap) {
private void fillHostsDetail(ExecuteTargetDTO executeTargetDTO,
TaskInstanceExecuteObjects taskInstanceExecuteObjects) {
if (executeTargetDTO != null) {
fillHostsDetail(executeTargetDTO.getStaticIpList(), hostMap);
fillHostsDetail(executeTargetDTO.getStaticIpList(), taskInstanceExecuteObjects);
if (CollectionUtils.isNotEmpty(executeTargetDTO.getDynamicServerGroups())) {
executeTargetDTO.getDynamicServerGroups()
.forEach(group -> fillHostsDetail(group.getIpList(), hostMap));
.forEach(group -> fillHostsDetail(group.getIpList(), taskInstanceExecuteObjects));
}
if (CollectionUtils.isNotEmpty(executeTargetDTO.getTopoNodes())) {
executeTargetDTO.getTopoNodes().forEach(topoNode -> fillHostsDetail(topoNode.getIpList(), hostMap));
executeTargetDTO.getTopoNodes().forEach(
topoNode -> fillHostsDetail(topoNode.getIpList(), taskInstanceExecuteObjects));
}
}
}

private void fillHostsDetail(Collection<HostDTO> hosts, Map<HostCompositeKey, HostDTO> hostMap) {
private void fillHostsDetail(Collection<HostDTO> hosts, TaskInstanceExecuteObjects taskInstanceExecuteObjects) {
if (CollectionUtils.isNotEmpty(hosts)) {
hosts.forEach(host -> host.updateByHost(hostMap.get(host.getUniqueKey())));
hosts.forEach(host -> host.updateByHost(taskInstanceExecuteObjects.queryByHostKey(host)));
}
}

Expand Down

0 comments on commit cbb392e

Please sign in to comment.