From 1dfcb6de1c2d7ef514cd9ad4f90d4cc24e536d06 Mon Sep 17 00:00:00 2001 From: carlyin <449036249@qq.com> Date: Fri, 21 Jun 2024 11:48:10 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=AF=A6=E6=83=85=E9=A1=B5=E7=9A=84?= =?UTF-8?q?=E6=97=A7=E6=95=B0=E6=8D=AE=E5=85=BC=E5=AE=B9=E4=B8=8B=E6=9E=B6?= =?UTF-8?q?=20#9522?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/record/BaseBuildRecordService.kt | 47 +++++++++++++ .../record/PipelineBuildRecordService.kt | 69 ++++++++++--------- 2 files changed, 84 insertions(+), 32 deletions(-) diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/record/BaseBuildRecordService.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/record/BaseBuildRecordService.kt index 4ea5f9fd5a5..6069a4291e1 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/record/BaseBuildRecordService.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/record/BaseBuildRecordService.kt @@ -50,6 +50,7 @@ import com.tencent.devops.common.redis.RedisOperation import com.tencent.devops.common.service.utils.LogUtils import com.tencent.devops.common.web.utils.I18nUtil import com.tencent.devops.common.websocket.enum.RefreshType +import com.tencent.devops.process.constant.ProcessMessageCode import com.tencent.devops.process.dao.record.BuildRecordModelDao import com.tencent.devops.process.engine.control.lock.PipelineBuildRecordLock import com.tencent.devops.process.engine.dao.PipelineBuildDao @@ -68,6 +69,7 @@ import com.tencent.devops.process.utils.KEY_PIPELINE_ID import com.tencent.devops.process.utils.KEY_PROJECT_ID import org.jooq.DSLContext import org.slf4j.LoggerFactory +import javax.ws.rs.core.Response @Suppress("LongParameterList", "MagicNumber", "ReturnCount", "ComplexMethod") open class BaseBuildRecordService( @@ -151,6 +153,51 @@ open class BaseBuildRecordService( return } + fun getRecordModel( + projectId: String, + pipelineId: String, + version: Int, + buildId: String, + executeCount: Int? = null, + queryDslContext: DSLContext? = null, + debug: Boolean? = false + ): Model? { + val fixedExecuteCount = if (executeCount == null) { + val buildInfo = pipelineBuildDao.getBuildInfo( + dslContext = queryDslContext ?: dslContext, projectId = projectId, buildId = buildId + ) ?: throw ErrorCodeException( + statusCode = Response.Status.NOT_FOUND.statusCode, + errorCode = ProcessMessageCode.ERROR_NO_BUILD_EXISTS_BY_ID, + params = arrayOf(buildId) + ) + buildInfo.executeCount ?: 1 + } else { + executeCount + } + val buildRecordModel = buildRecordModelDao.getRecord( + dslContext = queryDslContext ?: dslContext, + projectId = projectId, + pipelineId = pipelineId, + buildId = buildId, + executeCount = fixedExecuteCount + ) + return if (buildRecordModel != null) { + getRecordModel( + projectId = projectId, + pipelineId = pipelineId, + version = version, + buildId = buildId, + fixedExecuteCount = fixedExecuteCount, + buildRecordModel = buildRecordModel, + executeCount = executeCount, + queryDslContext = queryDslContext, + debug = debug + ) + } else { + null + } + } + fun getRecordModel( projectId: String, pipelineId: String, diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/record/PipelineBuildRecordService.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/record/PipelineBuildRecordService.kt index 747dbc034eb..dabec44ef4c 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/record/PipelineBuildRecordService.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/record/PipelineBuildRecordService.kt @@ -27,7 +27,9 @@ package com.tencent.devops.process.engine.service.record +import com.tencent.devops.common.api.exception.ErrorCodeException import com.tencent.devops.common.api.pojo.ErrorInfo +import com.tencent.devops.common.api.util.JsonUtil import com.tencent.devops.common.api.util.Watcher import com.tencent.devops.common.api.util.timestampmilli import com.tencent.devops.common.event.dispatcher.pipeline.PipelineEventDispatcher @@ -52,8 +54,10 @@ import com.tencent.devops.common.pipeline.utils.ModelUtils import com.tencent.devops.common.redis.RedisOperation import com.tencent.devops.common.service.utils.LogUtils import com.tencent.devops.common.web.utils.I18nUtil +import com.tencent.devops.process.constant.ProcessMessageCode import com.tencent.devops.process.constant.ProcessMessageCode.BK_EVENT import com.tencent.devops.process.constant.ProcessMessageCode.BK_WAREHOUSE_EVENTS +import com.tencent.devops.process.dao.BuildDetailDao import com.tencent.devops.process.dao.record.BuildRecordContainerDao import com.tencent.devops.process.dao.record.BuildRecordModelDao import com.tencent.devops.process.dao.record.BuildRecordStageDao @@ -87,6 +91,7 @@ import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.stereotype.Service +import javax.ws.rs.core.Response @Suppress( "LongParameterList", @@ -97,7 +102,6 @@ import org.springframework.stereotype.Service ) @Service class PipelineBuildRecordService @Autowired constructor( - private val pipelineBuildDetailService: PipelineBuildDetailService, private val pipelineRepositoryService: PipelineRepositoryService, private val pipelineBuildSummaryDao: PipelineBuildSummaryDao, private val pipelineTriggerReviewDao: PipelineTriggerReviewDao, @@ -106,6 +110,7 @@ class PipelineBuildRecordService @Autowired constructor( private val recordStageDao: BuildRecordStageDao, private val recordContainerDao: BuildRecordContainerDao, private val recordTaskDao: BuildRecordTaskDao, + private val buildDetailDao: BuildDetailDao, recordModelService: PipelineRecordModelService, pipelineResourceDao: PipelineResourceDao, pipelineBuildDao: PipelineBuildDao, @@ -206,7 +211,8 @@ class PipelineBuildRecordService @Autowired constructor( "fixedExecuteCount=$fixedExecuteCount" ) watcher.start("getDetailModel") - val detail = pipelineBuildDetailService.getBuildModel(projectId, buildId, queryDslContext) ?: return null + val record = buildDetailDao.get(queryDslContext ?: dslContext, projectId, buildId) ?: return null + val detail = JsonUtil.to(record.model, Model::class.java) fixDetailTimeCost(buildInfo, detail) detail } @@ -463,12 +469,6 @@ class PipelineBuildRecordService @Autowired constructor( cancelUser: String, executeCount: Int ) { - pipelineBuildDetailService.buildCancel( - projectId = projectId, - buildId = buildId, - buildStatus = buildStatus, - cancelUser = cancelUser - ) logger.info("[$buildId]|BUILD_CANCEL|cancelUser=$cancelUser|buildStatus=$buildStatus") dslContext.transaction { configuration -> val context = DSL.using(configuration) @@ -552,18 +552,26 @@ class PipelineBuildRecordService @Autowired constructor( errorMsg: String? ): Triple, BuildRecordTimeCost?> { logger.info("[$buildId]|BUILD_END|buildStatus=$buildStatus") -// var allStageStatus: List = emptyList() var timeCost: BuildRecordTimeCost? = null + val recordModel = recordModelDao.getRecord( + dslContext = dslContext, + projectId = projectId, + pipelineId = pipelineId, + buildId = buildId, + executeCount = executeCount + ) ?: run { + logger.warn( + "ENGINE|$buildId|buildEnd| get model($buildId) record failed." + ) + throw ErrorCodeException( + statusCode = Response.Status.NOT_FOUND.statusCode, + errorCode = ProcessMessageCode.ERROR_NO_BUILD_EXISTS_BY_ID, + params = arrayOf(buildId) + ) + } + var allStageStatus: List = emptyList() dslContext.transaction { configuration -> val context = DSL.using(configuration) - val recordModel = recordModelDao.getRecord( - context, projectId, pipelineId, buildId, executeCount - ) ?: run { - logger.warn( - "ENGINE|$buildId|buildEnd| get model($buildId) record failed." - ) - return@transaction - } val now = LocalDateTime.now() val runningStatusSet = enumValues().filter { it.isRunning() }.toSet() // 刷新运行中stage状态,取出所有stage记录还需用于耗时计算 @@ -602,12 +610,9 @@ class PipelineBuildRecordService @Autowired constructor( recordTaskDao.batchSave(context, recordTasks) recordContainerDao.batchSave(context, recordContainers) recordStageDao.batchSave(context, recordStages) - -// allStageStatus = fetchHistoryStageStatus( -// recordStages = recordStages, -// buildStatus = buildStatus, -// errorMsg = errorMsg -// ) + allStageStatus = fetchHistoryStageStatus( + recordStages = recordStages, buildStatus = buildStatus, errorMsg = errorMsg + ) val modelVar = mutableMapOf() timeCost = recordModel.generateBuildTimeCost(recordStages) @@ -625,13 +630,18 @@ class PipelineBuildRecordService @Autowired constructor( executeCount = executeCount ) } - val detail = pipelineBuildDetailService.buildEnd( + val model = getRecordModel( projectId = projectId, + pipelineId = pipelineId, + version = recordModel.resourceVersion, buildId = buildId, - buildStatus = buildStatus, - errorMsg = errorMsg + executeCount = executeCount + ) ?: throw ErrorCodeException( + statusCode = Response.Status.NOT_FOUND.statusCode, + errorCode = ProcessMessageCode.ERROR_NO_BUILD_EXISTS_BY_ID, + params = arrayOf(buildId) ) - return Triple(detail.first, detail.second, timeCost) + return Triple(model, allStageStatus, timeCost) } fun updateBuildCancelUser( @@ -640,11 +650,6 @@ class PipelineBuildRecordService @Autowired constructor( executeCount: Int, cancelUserId: String ) { - pipelineBuildDetailService.updateBuildCancelUser( - projectId = projectId, - buildId = buildId, - cancelUserId = cancelUserId - ) recordModelDao.updateBuildCancelUser( dslContext = dslContext, projectId = projectId,