Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:第三方构建机构建资源锁定策略优化 #10449 #10534

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ abstract class ThirdPartyAgentDispatch(
// 类型为REUSE_JOB时,被复用的job的value,防止同一个stage并发下拿不到agent,启动时填充
open var reusedInfo: ReusedInfo?
) : DispatchType(value) {
fun idType(): Boolean = (agentType == AgentType.ID) || (reusedInfo?.agentType == AgentType.ID)
// 本身是 id,和被复用对象在同一JOB且被复用对象也是 id,是复用但是位于后面的 JOB
fun idType(): Boolean =
(agentType == AgentType.ID) || (reusedInfo?.agentType == AgentType.ID) ||
(agentType == AgentType.REUSE_JOB_ID && reusedInfo == null)

// 是否在复用锁定链上
fun hasReuseMutex(): Boolean = this.agentType.isReuse() || this.reusedInfo != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class ThirdPartyDispatchService @Autowired constructor(
ThirdPartyAgentIDDispatchType(
displayName = agentId,
workspace = dispatchType.workspace,
agentType = AgentType.ID,
agentType = AgentType.REUSE_JOB_ID,
dockerInfo = dispatchType.dockerInfo,
reusedInfo = dispatchType.reusedInfo
)
Expand Down Expand Up @@ -206,7 +206,6 @@ class ThirdPartyDispatchService @Autowired constructor(
dispatchMessage: DispatchMessage,
dispatchType: ThirdPartyAgentIDDispatchType
) {

val agentResult = if (dispatchType.idType()) {
client.get(ServiceThirdPartyAgentResource::class)
.getAgentById(dispatchMessage.event.projectId, dispatchType.displayName)
Expand Down Expand Up @@ -303,8 +302,8 @@ class ThirdPartyDispatchService @Autowired constructor(
}

// #10082 对于复用的机器和被复用的,需要加锁校验看看这台机器能不能使用
val lockKey = AgentReuseMutex.genAgentReuseMutexLockKey(event.projectId, agent.agentId)
if (hasReuseMutex) {
val lockKey = AgentReuseMutex.genAgentReuseMutexLockKey(event.projectId, agent.agentId)
val lock = RedisLockByValue(
redisOperation = redisOperation,
lockKey = lockKey,
Expand Down Expand Up @@ -348,6 +347,28 @@ class ThirdPartyDispatchService @Autowired constructor(
} catch (e: Exception) {
logger.error("inQueue|doAgentInQueue|error", e)
}
} else if (redisOperation.get(lockKey) != null) {
// 没有复用逻辑的需要检查下如果这个机器剩一个可调度空间且有复用锁那么不能进行调度
val checkRes = if (dockerInfo != null) {
((agent.dockerParallelTaskCount ?: 4) -
thirdPartyAgentBuildService.getDockerRunningBuilds(agent.agentId)) <= 1
} else {
((agent.parallelTaskCount ?: 4) -
thirdPartyAgentBuildService.getRunningBuilds(agent.agentId)) <= 1
}
if (checkRes) {
log(
dispatchMessage.event,
I18nUtil.getCodeLanMessage(
messageCode = AGENT_REUSE_MUTEX_REDISPATCH,
language = I18nUtil.getDefaultLocaleLanguage(),
params = arrayOf(
"${agent.agentId}|${agent.hostname}/${agent.ip}", redisOperation.get(lockKey) ?: ""
)
)
)
return false
}
}

// #5806 入库失败就不再写Redis
Expand Down
Loading