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 21, 2024
1 parent 1f4d10e commit 94d369b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@

public enum ResourceQuotaCheckResultEnum {

NO_LIMIT(1),
RESOURCE_SCOPE_LIMIT(2),
APP_LIMIT(3);
NO_LIMIT("no_limit"),
RESOURCE_SCOPE_LIMIT("resource_scope_quota_limit"),
APP_LIMIT("app_quota_limit");

private final Integer value;
private final String value;

ResourceQuotaCheckResultEnum(Integer value) {
ResourceQuotaCheckResultEnum(String value) {
this.value = value;
}

public static ResourceQuotaCheckResultEnum valOf(Integer value) {
public static ResourceQuotaCheckResultEnum valOf(String value) {
if (value == null) return null;
for (ResourceQuotaCheckResultEnum resultEnum : values()) {
if (resultEnum.value.equals(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ public class RunningJobResourceQuotaManager {
private static final String CHECK_QUOTA_LUA_SCRIPT =
"local resource_scope_key = KEYS[1]\n" +
"local resource_scope_limit = tonumber(ARGV[1])\n" +
"\n" +
"local running_job_count_resource_scope = tonumber(redis.call('zcard', resource_scope_key) or \"0\")\n" +
"redis.call('expire', resource_scope_key, 86400)\n" +
"if running_job_count_resource_scope >= resource_scope_limit then\n" +
" return 2\n" +
" return \"resource_scope_quota_limit\"\n" +
"end\n" +
"\n" +
"if KEYS[2] ~= \"None\" then\n" +
" local app_key = KEYS[2]\n" +
" local app_limit = tonumber(ARGV[2])\n" +
Expand All @@ -65,10 +67,11 @@ public class RunningJobResourceQuotaManager {
" redis.call('expire', app_key, 86400)\n" +
" \n" +
" if running_job_count_app >= app_limit then\n" +
" return 3\n" +
" return \"app_quota_limit\"\n" +
" end\n" +
"end\n" +
"return 1";
"\n" +
"return \"no_limit\"";


public RunningJobResourceQuotaManager(StringRedisTemplate redisTemplate,
Expand Down Expand Up @@ -104,7 +107,7 @@ public void removeJob(String appCode, ResourceScope resourceScope, long jobInsta
*/
public ResourceQuotaCheckResultEnum checkResourceQuotaLimit(String appCode, ResourceScope resourceScope) {
long startTime = System.currentTimeMillis();
RedisScript<Integer> script = RedisScript.of(CHECK_QUOTA_LUA_SCRIPT, Integer.class);
RedisScript<String> script = RedisScript.of(CHECK_QUOTA_LUA_SCRIPT, String.class);

// 是否通过第三方应用方式调用作业平台产生的作业
boolean isJobFrom3rdApp = StringUtils.isNotEmpty(appCode);
Expand All @@ -115,7 +118,7 @@ public ResourceQuotaCheckResultEnum checkResourceQuotaLimit(String appCode, Reso

long resourceScopeLimit = runningJobResourceQuotaStore.getQuotaLimitByResourceScope(resourceScope);

Integer checkResourceQuotaResult;
String checkResourceQuotaResult;
if (isJobFrom3rdApp) {
long appLimit = runningJobResourceQuotaStore.getQuotaLimitByAppCode(appCode);
checkResourceQuotaResult = redisTemplate.execute(script, keyList, String.valueOf(resourceScopeLimit),
Expand Down

0 comments on commit 94d369b

Please sign in to comment.