Skip to content

Commit

Permalink
feat: 执行引擎任务调度配额限制 TencentBlueKing#261
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Jun 24, 2024
1 parent 5bdffc2 commit 4b076d7
Show file tree
Hide file tree
Showing 20 changed files with 371 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package com.tencent.bk.job.common.service.quota;

import com.tencent.bk.job.common.refreshable.config.ConfigRefreshHandler;
import com.tencent.bk.job.common.resource.quota.QuotaResourceId;
import com.tencent.bk.job.common.resource.quota.ResourceQuotaLimit;
import com.tencent.bk.job.common.service.quota.config.ResourceQuotaLimitProperties;
import com.tencent.bk.job.common.service.quota.config.parser.ResourceQuotaConfigParser;
import com.tencent.bk.job.common.service.quota.config.parser.RunningJobQuotaConfigParser;
Expand Down Expand Up @@ -53,14 +55,23 @@ public class ResourceQuotaStore implements ConfigRefreshHandler {


public ResourceQuotaLimit getResourceQuota(String resourceId) {
checkLoad();
return resourceQuotas.get(resourceId);
}

public Map<String, ResourceQuotaLimit> getAll() {
checkLoad();
return resourceQuotas;
}

private void checkLoad() {
if (!isInitial) {
synchronized (this) {
if (!isInitial) {
load(true);
}
}
}
return resourceQuotas.get(resourceId);
}

public boolean load(boolean ignoreException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
package com.tencent.bk.job.common.service.quota;

import com.tencent.bk.job.common.model.dto.ResourceScope;
import com.tencent.bk.job.common.resource.quota.QuotaResourceId;
import com.tencent.bk.job.common.resource.quota.ResourceQuotaLimit;
import com.tencent.bk.job.common.resource.quota.RunningJobResourceQuotaLimit;

/**
* 资源配额存储-正在执行中的作业数量配额限制
Expand Down Expand Up @@ -68,4 +71,19 @@ public long getQuotaLimitByAppCode(String appCode) {
RunningJobResourceQuotaLimit runningJobResourceQuotaLimit = (RunningJobResourceQuotaLimit) resourceQuotaLimit;
return runningJobResourceQuotaLimit.getLimitByBkAppCode(appCode);
}

/**
* 获取整个Job 系统 正在执行作业配额限制
*
* @return 最大正在执行作业限制
*/
public long getSystemQuotaLimit() {
ResourceQuotaLimit resourceQuotaLimit = resourceQuotaStore.getResourceQuota(QuotaResourceId.JOB_INSTANCE);
if (resourceQuotaLimit == null) {
// 不限制
return Long.MAX_VALUE;
}
RunningJobResourceQuotaLimit runningJobResourceQuotaLimit = (RunningJobResourceQuotaLimit) resourceQuotaLimit;
return runningJobResourceQuotaLimit.getCapacity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.tencent.bk.job.common.service.quota.config;

import com.tencent.bk.job.common.resource.quota.QuotaResourceId;
import com.tencent.bk.job.common.util.json.JsonUtils;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -44,7 +45,7 @@ public class ResourceQuotaLimitProperties {
/**
* key: 资源 ID ; value: 资源对应的配额限制
*
* @see com.tencent.bk.job.common.service.quota.QuotaResourceId
* @see QuotaResourceId
*/
private Map<String, ResourceQuotaLimitProp> resources;

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

package com.tencent.bk.job.common.service.quota.config.parser;

import com.tencent.bk.job.common.service.quota.AppQuotaLimit;
import com.tencent.bk.job.common.service.quota.QuotaLimit;
import com.tencent.bk.job.common.service.quota.ResourceScopeQuotaLimit;
import com.tencent.bk.job.common.resource.quota.AppQuotaLimit;
import com.tencent.bk.job.common.resource.quota.QuotaLimit;
import com.tencent.bk.job.common.resource.quota.ResourceScopeQuotaLimit;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
Expand All @@ -40,34 +40,28 @@ public abstract class AbstractResourceQuotaConfigParser implements ResourceQuota

protected abstract Long parseCapacity(String capacityExpr);

protected ResourceScopeQuotaLimit parseResourceScopeQuotaLimit(String capacityExpr,
protected ResourceScopeQuotaLimit parseResourceScopeQuotaLimit(Long capacity,
String globalLimitExpr,
String customLimitExpr) {

ResourceScopeQuotaLimit resourceScopeQuotaLimit = new ResourceScopeQuotaLimit(
capacityExpr,
globalLimitExpr,
customLimitExpr
);
Long capacity = parseCapacity(capacityExpr);
resourceScopeQuotaLimit.setCapacity(capacity);

parseGlobalLimit(resourceScopeQuotaLimit, capacity, globalLimitExpr);
resourceScopeQuotaLimit.setCustomLimits(parseCustomLimit(capacity, customLimitExpr, key -> key));

return resourceScopeQuotaLimit;
}

protected AppQuotaLimit parseAppQuotaLimit(String capacityExpr,
protected AppQuotaLimit parseAppQuotaLimit(Long capacity,
String globalLimitExpr,
String customLimitExpr) {
AppQuotaLimit appQuotaLimit = new AppQuotaLimit(
capacityExpr,
globalLimitExpr,
customLimitExpr
);
Long capacity = parseCapacity(capacityExpr);
appQuotaLimit.setCapacity(capacity);

parseGlobalLimit(appQuotaLimit, capacity, globalLimitExpr);
appQuotaLimit.setCustomLimits(parseCustomLimit(capacity, customLimitExpr, key -> key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

package com.tencent.bk.job.common.service.quota.config.parser;

import com.tencent.bk.job.common.resource.quota.ResourceQuotaLimit;
import com.tencent.bk.job.common.service.quota.ResourceQuotaConfigParseException;
import com.tencent.bk.job.common.service.quota.ResourceQuotaLimit;
import com.tencent.bk.job.common.service.quota.config.ResourceQuotaLimitProperties;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

package com.tencent.bk.job.common.service.quota.config.parser;

import com.tencent.bk.job.common.service.quota.AppQuotaLimit;
import com.tencent.bk.job.common.resource.quota.AppQuotaLimit;
import com.tencent.bk.job.common.resource.quota.ResourceQuotaLimit;
import com.tencent.bk.job.common.resource.quota.ResourceScopeQuotaLimit;
import com.tencent.bk.job.common.resource.quota.RunningJobResourceQuotaLimit;
import com.tencent.bk.job.common.service.quota.ResourceQuotaConfigParseException;
import com.tencent.bk.job.common.service.quota.ResourceQuotaLimit;
import com.tencent.bk.job.common.service.quota.ResourceScopeQuotaLimit;
import com.tencent.bk.job.common.service.quota.RunningJobResourceQuotaLimit;
import com.tencent.bk.job.common.service.quota.config.ResourceQuotaLimitProperties;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -43,11 +43,15 @@ public ResourceQuotaLimit parse(ResourceQuotaLimitProperties.ResourceQuotaLimitP

RunningJobResourceQuotaLimit resourceQuota = new RunningJobResourceQuotaLimit();
try {
resourceQuota.setCapacityExpr(resourceQuotaLimitProp.getCapacity());
Long capacity = parseCapacity(resourceQuotaLimitProp.getCapacity());
resourceQuota.setCapacity(capacity);

ResourceQuotaLimitProperties.QuotaLimitProp resourceScopeQuotaLimitProp
= resourceQuotaLimitProp.getResourceScopeQuotaLimit();
if (resourceScopeQuotaLimitProp != null) {
ResourceScopeQuotaLimit resourceScopeQuotaLimit = parseResourceScopeQuotaLimit(
resourceQuotaLimitProp.getCapacity(),
capacity,
resourceScopeQuotaLimitProp.getGlobal(),
resourceScopeQuotaLimitProp.getCustom()
);
Expand All @@ -58,7 +62,7 @@ public ResourceQuotaLimit parse(ResourceQuotaLimitProperties.ResourceQuotaLimitP
= resourceQuotaLimitProp.getAppQuotaLimit();
if (resourceQuotaLimitProp.getAppQuotaLimit() != null) {
AppQuotaLimit appQuotaLimit = parseAppQuotaLimit(
resourceQuotaLimitProp.getCapacity(),
capacity,
appQuotaLimitProp.getGlobal(),
appQuotaLimitProp.getCustom()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.tencent.bk.job.common.service.quota;

import com.tencent.bk.job.common.resource.quota.RunningJobResourceQuotaLimit;
import com.tencent.bk.job.common.service.quota.config.ResourceQuotaLimitProperties;
import com.tencent.bk.job.common.service.quota.config.parser.RunningJobQuotaConfigParser;
import org.junit.jupiter.api.Test;
Expand All @@ -46,19 +47,19 @@ void parseByPercentageLimitValue() {

RunningJobResourceQuotaLimit resourceQuota =
(RunningJobResourceQuotaLimit) parser.parse(resourceQuotaLimitProp);
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCapacityExpr()).isEqualTo("1000");

assertThat(resourceQuota.getCapacityExpr()).isEqualTo("1000");
assertThat(resourceQuota.getCapacity()).isEqualTo(1000L);

assertThat(resourceQuota.getResourceScopeQuotaLimit().getGlobalLimitExpr()).isEqualTo("1%");
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCustomLimitExpr()).isEqualTo("biz:2=10%,biz:3=15%");
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCapacity()).isEqualTo(1000L);
assertThat(resourceQuota.getResourceScopeQuotaLimit().getGlobalLimit()).isEqualTo(10L);
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCustomLimits()).isNotEmpty();
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCustomLimits().get("biz:2")).isEqualTo(100L);
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCustomLimits().get("biz:3")).isEqualTo(150L);

assertThat(resourceQuota.getAppQuotaLimit().getCapacityExpr()).isEqualTo("1000");
assertThat(resourceQuota.getAppQuotaLimit().getGlobalLimitExpr()).isEqualTo("2%");
assertThat(resourceQuota.getAppQuotaLimit().getCustomLimitExpr()).isEqualTo("bk-soap=20%,bk-nodeman=10%");
assertThat(resourceQuota.getAppQuotaLimit().getCapacity()).isEqualTo(1000L);
assertThat(resourceQuota.getAppQuotaLimit().getGlobalLimit()).isEqualTo(20L);
assertThat(resourceQuota.getAppQuotaLimit().getCustomLimits()).isNotEmpty();
assertThat(resourceQuota.getAppQuotaLimit().getCustomLimits().get("bk-soap")).isEqualTo(200L);
Expand All @@ -79,19 +80,19 @@ void parseByExplicitLimitValue() {

RunningJobResourceQuotaLimit resourceQuota =
(RunningJobResourceQuotaLimit) parser.parse(resourceQuotaLimitProp);
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCapacityExpr()).isNull();

assertThat(resourceQuota.getCapacityExpr()).isNull();
assertThat(resourceQuota.getCapacity()).isNull();

assertThat(resourceQuota.getResourceScopeQuotaLimit().getGlobalLimitExpr()).isEqualTo("10");
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCustomLimitExpr()).isEqualTo("biz:2=100,biz:3=150");
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCapacity()).isNull();
assertThat(resourceQuota.getResourceScopeQuotaLimit().getGlobalLimit()).isEqualTo(10L);
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCustomLimits()).isNotEmpty();
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCustomLimits().get("biz:2")).isEqualTo(100L);
assertThat(resourceQuota.getResourceScopeQuotaLimit().getCustomLimits().get("biz:3")).isEqualTo(150L);

assertThat(resourceQuota.getAppQuotaLimit().getCapacityExpr()).isNull();
assertThat(resourceQuota.getAppQuotaLimit().getGlobalLimitExpr()).isEqualTo("20");
assertThat(resourceQuota.getAppQuotaLimit().getCustomLimitExpr()).isEqualTo("bk-soap=100,bk-nodeman=150");
assertThat(resourceQuota.getAppQuotaLimit().getCapacity()).isNull();
assertThat(resourceQuota.getAppQuotaLimit().getGlobalLimit()).isEqualTo(20L);
assertThat(resourceQuota.getAppQuotaLimit().getCustomLimits()).isNotEmpty();
assertThat(resourceQuota.getAppQuotaLimit().getCustomLimits().get("bk-soap")).isEqualTo(100L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public boolean isBizSet() {
return type == ResourceScopeTypeEnum.BIZ_SET;
}

public String getResourceScopeUniqueId() {
public String toResourceScopeUniqueId() {
return type.getValue() + ":" + id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.service.quota;
package com.tencent.bk.job.common.resource.quota;

import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -44,8 +44,8 @@ public class AppQuotaLimit extends QuotaLimit {
*/
private Map<String, Long> customLimits = new HashMap<>();

public AppQuotaLimit(String capacityExpr, String globalLimitExpr, String customLimitExpr) {
super(capacityExpr, globalLimitExpr, customLimitExpr);
public AppQuotaLimit(String globalLimitExpr, String customLimitExpr) {
super(globalLimitExpr, customLimitExpr);
}

public long getLimit(String bkAppCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.service.quota;
package com.tencent.bk.job.common.resource.quota;

import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -33,11 +33,6 @@
@Data
@NoArgsConstructor
public class QuotaLimit {
/**
* 配额容量表达式
*/
protected String capacityExpr;

/**
* 全局配额表达式
*/
Expand All @@ -48,18 +43,12 @@ public class QuotaLimit {
*/
protected String customLimitExpr;

/**
* 解析之后的配额总量限制
*/
private Long capacity;

/**
* 解析之后的全局业务配额限制
*/
private Long globalLimit;

public QuotaLimit(String capacityExpr, String globalLimitExpr, String customLimitExpr) {
this.capacityExpr = capacityExpr;
public QuotaLimit(String globalLimitExpr, String customLimitExpr) {
this.globalLimitExpr = globalLimitExpr;
this.customLimitExpr = customLimitExpr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.service.quota;
package com.tencent.bk.job.common.resource.quota;

/**
* 配额限制的资源 ID 定义
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.service.quota;
package com.tencent.bk.job.common.resource.quota;

import lombok.Data;

/**
* 资源配额限制-基础类
*/
@Data
public class ResourceQuotaLimit {
/**
* 配额容量表达式
*/
private String capacityExpr;
/**
* 解析后的配额总量限制
*/
private Long capacity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.service.quota;
package com.tencent.bk.job.common.resource.quota;

import com.tencent.bk.job.common.model.dto.ResourceScope;
import lombok.Data;
Expand All @@ -45,12 +45,12 @@ public class ResourceScopeQuotaLimit extends QuotaLimit {
*/
private Map<String, Long> customLimits = new HashMap<>();

public ResourceScopeQuotaLimit(String capacityExpr, String globalLimitExpr, String customLimitExpr) {
super(capacityExpr, globalLimitExpr, customLimitExpr);
public ResourceScopeQuotaLimit(String globalLimitExpr, String customLimitExpr) {
super(globalLimitExpr, customLimitExpr);
}

public long getLimit(ResourceScope resourceScope) {
String resourceScopeUniqueId = resourceScope.getResourceScopeUniqueId();
String resourceScopeUniqueId = resourceScope.toResourceScopeUniqueId();
Long limit = customLimits.get(resourceScopeUniqueId);
if (limit == null) {
limit = getGlobalLimit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.service.quota;
package com.tencent.bk.job.common.resource.quota;

import com.tencent.bk.job.common.model.dto.ResourceScope;
import lombok.Data;
Expand Down
Loading

0 comments on commit 4b076d7

Please sign in to comment.