Skip to content

Commit

Permalink
bugfix: 执行方案步骤引用命名空间变量,如果目标主机包含ipv6主机,Job任务卡住 TencentBlueKing#1687
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Jan 10, 2023
1 parent 36ee071 commit 75f2159
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 60 deletions.
Expand Up @@ -324,6 +324,7 @@ Response<List<FileDistributionDetailVO>> getFileLogContentByFileTaskIds(

@ApiOperation(value = "获取执行步骤-主机对应的变量列表", produces = "application/json")
@GetMapping(value = {"/step-execution-result/variable/{stepInstanceId}/{ip}"})
@CompatibleImplementation(name = "ipv6", explain = "兼容IPv6版本之前的使用并保存ip的执行历史数据")
Response<List<ExecuteVariableVO>> getStepVariableByIp(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
Expand All @@ -345,6 +346,29 @@ Response<List<ExecuteVariableVO>> getStepVariableByIp(
String ip
);

@ApiOperation(value = "获取执行步骤-主机对应的变量列表", produces = "application/json")
@GetMapping(value = {"/step-execution-result/step/{stepInstanceId}/host/{hostId}/variables"})
Response<List<ExecuteVariableVO>> getStepVariableByHostId(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
String username,
@ApiIgnore
@RequestAttribute(value = "appResourceScope")
AppResourceScope appResourceScope,
@ApiParam(value = "资源范围类型", required = true)
@PathVariable(value = "scopeType")
String scopeType,
@ApiParam(value = "资源范围ID", required = true)
@PathVariable(value = "scopeId")
String scopeId,
@ApiParam(value = "步骤实例ID", name = "stepInstanceId", required = true)
@PathVariable("stepInstanceId")
Long stepInstanceId,
@ApiParam(value = "hostId", name = "hostId", required = true)
@PathVariable("hostId")
Long hostId
);

@ApiOperation(value = "获取执行结果分组下的主机列表", produces = "application/json")
@GetMapping(value = {"/step-execution-result/hosts/{stepInstanceId}/{executeCount}"})
Response<List<HostDTO>> getHostsByResultType(
Expand Down
Expand Up @@ -69,7 +69,8 @@ void testSaveVariableValues() {
variableValues.setGlobalParams(globalParams);
List<HostVariableValuesDTO> hostParamValuesList = new ArrayList<>();
HostVariableValuesDTO hostParamValues = new HostVariableValuesDTO();
hostParamValues.setIp("1.1.1.1");
hostParamValues.setHostId(1L);
hostParamValues.setCloudIpv4("0:1.1.1.1");
List<VariableValueDTO> namespaceParamValues = new ArrayList<>();
namespaceParamValues.add(new VariableValueDTO("param11", 2, "value11"));
namespaceParamValues.add(new VariableValueDTO("param12", 2, "value12"));
Expand All @@ -91,7 +92,8 @@ void testSaveVariableValues() {
assertThat(actual.getGlobalParams()).extracting("value").containsOnly("value11", "value12");

assertThat(actual.getNamespaceParams()).hasSize(1);
assertThat(actual.getNamespaceParams().get(0).getIp()).isEqualTo("1.1.1.1");
assertThat(actual.getNamespaceParams().get(0).getCloudIpv4()).isEqualTo("0:1.1.1.1");
assertThat(actual.getNamespaceParams().get(0).getHostId()).isEqualTo(1L);
assertThat(actual.getNamespaceParams().get(0).getValues()).hasSize(2);
assertThat(actual.getNamespaceParams().get(0).getValues()).extracting("name")
.containsOnly("param11", "param12");
Expand Down
Expand Up @@ -649,6 +649,13 @@ public Response<List<ExecuteVariableVO>> getStepVariableByIp(String username,
String scopeId,
Long stepInstanceId,
String ip) {
return getStepVariableByHost(username, appResourceScope, stepInstanceId, HostDTO.fromCloudIp(ip));
}

private Response<List<ExecuteVariableVO>> getStepVariableByHost(String username,
AppResourceScope appResourceScope,
Long stepInstanceId,
HostDTO queryHost) {
StepInstanceDTO stepInstance = taskInstanceService.getStepInstanceDetail(stepInstanceId);
if (stepInstance == null) {
return Response.buildSuccessResp(Collections.emptyList());
Expand Down Expand Up @@ -700,9 +707,9 @@ public Response<List<ExecuteVariableVO>> getStepVariableByIp(String username,
ExecuteVariableVO vo = new ExecuteVariableVO();
vo.setName(paramName);
String paramValue = (inputStepInstanceValues.getNamespaceParamsMap() != null
&& inputStepInstanceValues.getNamespaceParamsMap().get(ip) != null
&& inputStepInstanceValues.getNamespaceParamsMap().get(ip).get(paramName) != null)
? inputStepInstanceValues.getNamespaceParamsMap().get(ip).get(paramName).getValue()
&& inputStepInstanceValues.getNamespaceParamsMap().get(queryHost) != null
&& inputStepInstanceValues.getNamespaceParamsMap().get(queryHost).get(paramName) != null)
? inputStepInstanceValues.getNamespaceParamsMap().get(queryHost).get(paramName).getValue()
: taskVariablesMap.get(paramName).getValue();
vo.setValue(paramValue);
vo.setChangeable(1);
Expand Down Expand Up @@ -751,6 +758,16 @@ private ExecuteVariableVO convertToTaskVariableVO(TaskVariableDTO taskVariable)
return vo;
}

@Override
public Response<List<ExecuteVariableVO>> getStepVariableByHostId(String username,
AppResourceScope appResourceScope,
String scopeType,
String scopeId,
Long stepInstanceId,
Long hostId) {
return getStepVariableByHost(username, appResourceScope, stepInstanceId, HostDTO.fromHostId(hostId));
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Response<IpFileLogContentVO> getFileLogContentByHost(String username,
Expand Down
Expand Up @@ -543,11 +543,11 @@ private List<HostVariableValuesDTO> buildNamespaceVariableValues() {
for (Map.Entry<String, Map<String, String>> entry : namespaceParamValues.entrySet()) {
HostVariableValuesDTO hostVariableValues = new HostVariableValuesDTO();
List<VariableValueDTO> paramValues = toVariableValuesList(entry.getValue());
AgentTaskDTO agentTask = targetAgentTasks.get(entry.getKey());
hostVariableValues.setHostId(agentTask.getHostId());
hostVariableValues.setAgentId(entry.getKey());
HostDTO host = agentIdHostMap.get(entry.getKey());
hostVariableValues.setHostId(host.getHostId());
hostVariableValues.setCloudIpv4(host.toCloudIp());
hostVariableValues.setCloudIpv6(host.toCloudIpv6());
hostVariableValues.setValues(paramValues);
hostVariableValues.setIp(agentTask.getCloudIp());
hostVariableValuesList.add(hostVariableValues);
}
return hostVariableValuesList;
Expand Down
Expand Up @@ -60,12 +60,11 @@ public String resolve(VariableResolveContext context, String variableName) {
Map<String, Map<String, String>> namespaceVarsValues = new HashMap<>();
List<HostVariableValuesDTO> namespaceValues = stepInputVariables.getNamespaceParams();
namespaceValues.forEach(namespaceValue -> {
String ip = namespaceValue.getIp();
if (CollectionUtils.isNotEmpty(namespaceValue.getValues())) {
namespaceValue.getValues().forEach(variableValue -> {
Map<String, String> ipAndValueMap = namespaceVarsValues.computeIfAbsent(variableValue.getName(),
k -> new HashMap<>());
ipAndValueMap.put(ip, variableValue.getValue());
ipAndValueMap.put(namespaceValue.getPrimaryCloudIp(), variableValue.getValue());
});

}
Expand Down
Expand Up @@ -24,9 +24,11 @@

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

import com.tencent.bk.job.common.annotation.CompatibleImplementation;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.annotation.PersistenceObject;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

Expand All @@ -37,20 +39,33 @@
@Data
public class HostVariableValuesDTO {
/**
* 主机IP
* 主机云区域ID:IPv4
*/
@CompatibleImplementation(explain = "由于ip不再唯一,使用hostId/agentId替代该参数", version = "3.7.x")
private String ip;
@JsonProperty("ip")
private String cloudIpv4;
/**
* 主机ID
* 主机云区域ID:ipv6
*/
private Long hostId;
@JsonProperty("ipv6")
private String cloudIpv6;
/**
* bk_agent_id
* 主机ID
*/
private String agentId;
@JsonProperty("hostId")
private Long hostId;
/**
* 变量值
*/
@JsonProperty("values")
private List<VariableValueDTO> values;

/**
* 获取主机的ip,优先返回ipv4
*
* @return 主机ipv4/ipv6, ipv4 优先
*/
@JsonIgnore
public String getPrimaryCloudIp() {
return StringUtils.isNotEmpty(cloudIpv4) ? cloudIpv4 : cloudIpv6;
}
}
Expand Up @@ -28,6 +28,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.annotation.PersistenceObject;
import com.tencent.bk.job.common.model.dto.HostDTO;
import com.tencent.bk.job.execute.constants.VariableValueTypeEnum;
import lombok.Data;

Expand Down Expand Up @@ -82,8 +83,10 @@ public class StepInstanceVariableValuesDTO {
private Map<String, VariableValueDTO> globalParamsMap;

/**
* Map<Ip, Map<paramName, paramValue>>
* Map<Host, Map<paramName, paramValue>>
*/
@JsonIgnore
private Map<String, Map<String, VariableValueDTO>> namespaceParamsMap;
private Map<HostDTO, Map<String, VariableValueDTO>> namespaceParamsMap;


}
Expand Up @@ -53,7 +53,7 @@ public interface StepInstanceVariableValueService {
*
* @param taskInstanceId 作业实例ID
* @param stepInstanceId 当前步骤ID
* @param taskVariables 全局变量
* @param taskVariables 全局变量初始值
* @return 变量值
*/
StepInstanceVariableValuesDTO computeInputStepInstanceVariableValues(long taskInstanceId, long stepInstanceId,
Expand Down

0 comments on commit 75f2159

Please sign in to comment.