Skip to content

Commit

Permalink
Merge pull request #2536 from jsonwan/github_perf/agent_status
Browse files Browse the repository at this point in the history
perf: Job中的主机根据其对应业务的灰度情况展示Agent状态 #2492
  • Loading branch information
jsonwan authored Oct 24, 2023
2 parents 2aea7c6 + 91e00f8 commit f5b9713
Show file tree
Hide file tree
Showing 66 changed files with 1,660 additions and 2,336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
package com.tencent.bk.job.common.service.feature;

import com.tencent.bk.job.common.model.dto.ResourceScope;
import com.tencent.bk.job.common.service.feature.strategy.ToggleStrategyContextParams;
import com.tencent.bk.job.common.util.feature.Feature;
import com.tencent.bk.job.common.util.feature.FeatureExecutionContext;
import com.tencent.bk.job.common.util.feature.FeatureManager;
import com.tencent.bk.job.common.util.feature.FeatureStore;
import com.tencent.bk.job.common.util.feature.ToggleStrategy;
import com.tencent.bk.job.common.util.feature.ToggleStrategyContextParams;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -54,6 +54,17 @@ public DefaultFeatureManager(FeatureStore featureStore, MeterRegistry meterRegis
this.meterRegistry = meterRegistry;
}

public boolean isFeatureEnabled(String featureId) {
Feature feature = featureStore.getFeature(featureId);
if (feature == null) {
if (log.isDebugEnabled()) {
log.debug("Feature: {} is not exist!", featureId);
}
return false;
}
return feature.isEnabled();
}

/**
* 判断特性是否开启
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,12 @@ public void assertRequiredInitParam(String paramName) {
}



public void assertRequiredContextParam(FeatureExecutionContext context, String paramName) {
if (context.getParam(paramName) == null) {
String msg = MessageFormatter.format(
"Context param {} is required for evaluate", paramName).getMessage();
log.error(msg);
throw new FeatureConfigParseException(msg);
throw new FeatureParamMissingException(msg);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.common.service.feature.strategy;

/**
* 特性开关配置参数缺失异常
*/
public class FeatureParamMissingException extends RuntimeException {

public FeatureParamMissingException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,34 @@ public JobInstanceAttrToggleStrategy(Map<String, String> initParams) {
}
}

@SuppressWarnings("DuplicatedCode")
@Override
public boolean evaluate(String featureId, FeatureExecutionContext ctx) {
if (requireAllGseV2AgentAvailable != null && requireAllGseV2AgentAvailable) {
assertRequiredContextParam(ctx, CTX_PARAM_IS_ALL_GSE_V2_AGENT_AVAILABLE);
boolean isAllAgentV2Available = (boolean) ctx.getParam(CTX_PARAM_IS_ALL_GSE_V2_AGENT_AVAILABLE);
if (!isAllAgentV2Available) {
return false;
}
}
if (requireAnyGseV2AgentAvailable != null && requireAnyGseV2AgentAvailable) {
assertRequiredContextParam(ctx, CTX_PARAM_IS_ANY_GSE_V2_AGENT_AVAILABLE);
boolean isAnyAgentV2Available = (boolean) ctx.getParam(CTX_PARAM_IS_ANY_GSE_V2_AGENT_AVAILABLE);
if (!isAnyAgentV2Available) {
return false;
}
}
if (CollectionUtils.isNotEmpty(startupModes)) {
assertRequiredContextParam(ctx, CTX_PARAM_STARTUP_MODE);
String startupMode = (String) ctx.getParam(CTX_PARAM_STARTUP_MODE);
if (!startupModes.contains(startupMode)) {
return false;
}
}
if (CollectionUtils.isNotEmpty(operators)) {
assertRequiredContextParam(ctx, CTX_PARAM_OPERATOR);
String operator = (String) ctx.getParam(CTX_PARAM_OPERATOR);
if (!operators.contains(operator)) {
return false;
}
return operators.contains(operator);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.tencent.bk.job.common.model.dto.ResourceScope;
import com.tencent.bk.job.common.util.feature.FeatureExecutionContext;
import com.tencent.bk.job.common.util.feature.ToggleStrategyContextParams;

import java.util.Map;
import java.util.StringJoiner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.tencent.bk.job.common.model.dto.ResourceScope;
import com.tencent.bk.job.common.util.feature.FeatureExecutionContext;
import com.tencent.bk.job.common.util.feature.ToggleStrategyContextParams;

import java.util.Map;
import java.util.StringJoiner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

import java.io.InputStream;

import static com.tencent.bk.job.common.service.feature.strategy.ToggleStrategyContextParams.CTX_PARAM_RESOURCE_SCOPE;
import static com.tencent.bk.job.common.util.feature.ToggleStrategyContextParams.CTX_PARAM_RESOURCE_SCOPE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@

package com.tencent.bk.job.common.util;

import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -86,4 +92,28 @@ public static <E> List<List<E>> partitionCollection(Collection<E> collection, in
.collect(Collectors.toList()))
.collect(Collectors.toList());
}

/**
* 从集合元素中抽取两个字段构成Map
*
* @param entityCollection 实体集合
* @param keyFunc 抽取Key的方法
* @param valueFunc 抽取Value的方法
* @param <E> 实体类型
* @param <K> Key类型
* @param <V> Value类型
* @return HashMap<K, V>,null可以为key或value,entityCollection为null时返回空Map
*/
public static <E, K, V> Map<K, V> convertToMap(List<E> entityCollection,
Function<E, K> keyFunc,
Function<E, V> valueFunc) {
if (CollectionUtils.isEmpty(entityCollection)) {
return Collections.emptyMap();
}
Map<K, V> map = new HashMap<>();
for (E entity : entityCollection) {
map.put(keyFunc.apply(entity), valueFunc.apply(entity));
}
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,59 @@ void testPartitionHashSet() {
assertThat(mergedElements).hasSize(2009);
}

@Test
void testConvertToMap() {
assertThat(CollectionUtil.convertToMap(null, TestEntity::getKey, TestEntity::getStringValue)).isEmpty();
List<TestEntity> entityList = new ArrayList<>();
entityList.add(new TestEntity(null, null, null));
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).size()).isEqualTo(1);
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).containsKey(null)).isTrue();
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).containsValue(null)).isTrue();
Long key = 1L;
String stringValue = "StringValue";
Object objectValue = new Object();
entityList.add(new TestEntity(key, stringValue, objectValue));
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).size()).isEqualTo(2);
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).containsKey(key)).isTrue();
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).containsValue(stringValue)).isTrue();
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getObjectValue).size()).isEqualTo(2);
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getObjectValue).containsKey(key)).isTrue();
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getObjectValue).containsValue(objectValue)).isTrue();
String stringValue2 = "StringValue2";
Object objectValue2 = new Object();
entityList.add(new TestEntity(2L, stringValue2, objectValue2));
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).size()).isEqualTo(3);
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).containsKey(key)).isTrue();
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).containsValue(stringValue)).isTrue();
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getStringValue).containsValue(stringValue2)).isTrue();
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getObjectValue).size()).isEqualTo(3);
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getObjectValue).containsKey(key)).isTrue();
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getObjectValue).containsValue(objectValue)).isTrue();
assertThat(CollectionUtil.convertToMap(entityList, TestEntity::getKey, TestEntity::getObjectValue).containsValue(objectValue2)).isTrue();
}

class TestEntity {
private final Long key;
private final String stringValue;
private final Object objectValue;

public TestEntity(Long key, String stringValue, Object objectValue) {
this.key = key;
this.stringValue = stringValue;
this.objectValue = objectValue;
}

public Long getKey() {
return key;
}

public String getStringValue() {
return stringValue;
}

public Object getObjectValue() {
return objectValue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.tencent.bk.job.common.annotation.CompatibleImplementation;
import com.tencent.bk.job.common.annotation.PersistenceObject;
import com.tencent.bk.job.common.constant.JobConstants;
import com.tencent.bk.job.common.model.vo.CloudAreaInfoVO;
Expand Down Expand Up @@ -179,7 +178,6 @@ public void setGseAgentAlive(Boolean gseAgentAlive) {
}
}

@CompatibleImplementation(name = "ipv6", explain = "兼容方法,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
private Integer getAgentAliveValue() {
return gseAgentAlive == null ? 0 : (gseAgentAlive ? 1 : 0);
}
Expand Down Expand Up @@ -290,6 +288,14 @@ public int getAgentStatusValue() {
return JobConstants.GSE_AGENT_STATUS_VALUE_ALIVE;
}

@JsonIgnore
public String getHostIdOrCloudIp() {
if (hostId != null && hostId > 0) {
return String.valueOf(hostId);
}
return getCloudIp();
}

public HostDTO toHostDTO() {
HostDTO host = new HostDTO();
host.setHostId(hostId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ public String getFinalAgentId() {
return cloudIp;
}

@JsonIgnore
public String getHostIdOrCloudIp() {
if (hostId != null && hostId > 0) {
return String.valueOf(hostId);
}
return cloudIp;
}

public static List<String> buildAgentIdList(List<HostSimpleDTO> hosts) {
List<String> agentIdList = new ArrayList<>();
for (HostSimpleDTO host : hosts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.bk.job.common.annotation.CompatibleImplementation;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InvalidParamException;
import com.tencent.bk.job.common.util.JobContextUtil;
Expand Down Expand Up @@ -65,8 +64,6 @@ public class HostInfoVO {

@ApiModelProperty("主机名称")
private String hostName;
@CompatibleImplementation(name = "ipv6", explain = "兼容字段,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
private String ipDesc;

// agent状态:-2:未找到,-1:查询失败,0:初始安装,1:启动中,2:运行中,3:有损状态,4:繁忙,5:升级中,6:停止中,7:解除安装
@JsonIgnore
Expand All @@ -77,16 +74,12 @@ public class HostInfoVO {

@ApiModelProperty("云区域信息")
private CloudAreaInfoVO cloudArea;
@CompatibleImplementation(name = "ipv6", explain = "兼容字段,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
private CloudAreaInfoVO cloudAreaInfo;

/**
* 操作系统
*/
@ApiModelProperty("操作系统")
private String osName;
@CompatibleImplementation(name = "ipv6", explain = "兼容字段,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
private String os;

@ApiModelProperty("系统类型")
@JsonProperty("osType")
Expand All @@ -99,57 +92,6 @@ public class HostInfoVO {
@JsonProperty("cloudVendor")
private String cloudVendorName;

@CompatibleImplementation(name = "ipv6", explain = "兼容实现,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
public String getHostName() {
if (hostName != null) {
return hostName;
} else if (ipDesc != null) {
log.warn("Use compatible field:ipDesc={}", ipDesc);
return ipDesc;
}
return null;
}

@CompatibleImplementation(name = "ipv6", explain = "兼容实现,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
public void setHostName(String hostName) {
this.hostName = hostName;
this.ipDesc = hostName;
}

@CompatibleImplementation(name = "ipv6", explain = "兼容实现,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
public CloudAreaInfoVO getCloudArea() {
if (cloudArea != null) {
return cloudArea;
} else if (cloudAreaInfo != null) {
log.warn("Use compatible field:cloudAreaInfo={}", cloudAreaInfo);
return cloudAreaInfo;
}
return null;
}

@CompatibleImplementation(name = "ipv6", explain = "兼容实现,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
public void setCloudArea(CloudAreaInfoVO cloudArea) {
this.cloudArea = cloudArea;
this.cloudAreaInfo = cloudArea;
}

@CompatibleImplementation(name = "ipv6", explain = "兼容实现,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
public String getOsName() {
if (osName != null) {
return osName;
} else if (os != null) {
log.warn("Use compatible field:os={}", os);
return os;
}
return null;
}

@CompatibleImplementation(name = "ipv6", explain = "兼容实现,保证发布过程中无损变更,下个版本删除", deprecatedVersion = "3.8.0")
public void setOsName(String osName) {
this.osName = osName;
this.os = osName;
}

public void validate(boolean isCreate) throws InvalidParamException {
if (!JobContextUtil.isAllowMigration() && (hostId == null || hostId <= 0)) {
log.warn("Missing host_id!");
Expand Down
Loading

0 comments on commit f5b9713

Please sign in to comment.