Skip to content

Commit

Permalink
Merge pull request #6277 from carlyin0801/issue_6265_atom_env_query_fix
Browse files Browse the repository at this point in the history
pref:获取插件环境信息接口性能优化 #6265
  • Loading branch information
irwinsun authored Mar 8, 2022
2 parents 94ac06d + 3faacc0 commit ec365a9
Show file tree
Hide file tree
Showing 15 changed files with 327 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import com.tencent.devops.process.engine.dao.PipelineBuildDao
import com.tencent.devops.process.engine.pojo.PipelineTaskStatusInfo
import com.tencent.devops.process.service.BuildVariableService
import com.tencent.devops.process.util.TaskUtils
import com.tencent.devops.store.api.atom.ServiceMarketAtomEnvResource
import com.tencent.devops.store.api.atom.ServiceAtomResource
import org.jooq.DSLContext
import org.springframework.stereotype.Service
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -524,12 +524,12 @@ class TaskBuildDetailService(
}

private val atomCache = Caffeine.newBuilder()
.maximumSize(2000)
.expireAfterAccess(30, TimeUnit.MINUTES)
.maximumSize(20000)
.expireAfterAccess(6, TimeUnit.HOURS)
.build<String/*projectCode VS atomCode VS atomVersion*/, String/*true version*/> { mix ->
val keys = mix.split(" VS ")
client.get(ServiceMarketAtomEnvResource::class)
.getAtomEnv(projectCode = keys[0], atomCode = keys[1], version = keys[2]).data?.version
client.get(ServiceAtomResource::class)
.getAtomRealVersion(projectCode = keys[0], atomCode = keys[1], version = keys[2]).data
}

fun findTaskVersion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,19 @@ interface ServiceAtomResource {
@PathParam("version")
version: String
): Result<PipelineAtom?>

@ApiOperation("获取插件真实版本号")
@GET
@Path("/projects/{projectCode}/codes/{atomCode}/versions/{version}/real")
fun getAtomRealVersion(
@ApiParam("项目代码", required = true)
@PathParam("projectCode")
projectCode: String,
@ApiParam("插件代码", required = true)
@PathParam("atomCode")
atomCode: String,
@ApiParam("版本号", required = true)
@PathParam("version")
version: String
): Result<String?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,59 +250,57 @@ class AtomDao : AtomBaseDao() {
projectCode: String,
atomCode: String,
version: String,
defaultFlag: Boolean,
atomStatusList: List<Byte>? = null
): TAtomRecord? {
val a = TAtom.T_ATOM.`as`("a")
val b = TStoreProjectRel.T_STORE_PROJECT_REL.`as`("b")
val t = dslContext.selectFrom(a)
.where(
generateGetPipelineAtomCondition(
a = a,
atomCode = atomCode,
version = version,
defaultFlag = true,
atomStatusList = atomStatusList
)
val tAtom = TAtom.T_ATOM
val tStoreProjectRel = TStoreProjectRel.T_STORE_PROJECT_REL
return if (defaultFlag) {
val conditions = generateGetPipelineAtomCondition(
tAtom = tAtom,
atomCode = atomCode,
version = version,
defaultFlag = true,
atomStatusList = atomStatusList
)
.union(
dslContext.selectFrom(a)
.where(
generateGetPipelineAtomCondition(
a = a,
atomCode = atomCode,
version = version,
defaultFlag = false,
atomStatusList = atomStatusList
)
)
.andExists(
dslContext.selectOne().from(b).where(
a.ATOM_CODE.eq(b.STORE_CODE).and(b.STORE_TYPE.eq(StoreTypeEnum.ATOM.type.toByte()))
.and(b.PROJECT_CODE.eq(projectCode))
)
)
dslContext.selectFrom(tAtom).where(conditions).orderBy(tAtom.CREATE_TIME.desc()).limit(1).fetchOne()
} else {
val conditions = generateGetPipelineAtomCondition(
tAtom = tAtom,
atomCode = atomCode,
version = version,
defaultFlag = false,
atomStatusList = atomStatusList
)
.asTable("t")
return dslContext.selectFrom(t).orderBy(t.field("CREATE_TIME")!!.desc()).limit(1).fetchOne()
dslContext.selectFrom(tAtom).where(conditions)
.andExists(
dslContext.selectOne().from(tStoreProjectRel).where(
tAtom.ATOM_CODE.eq(tStoreProjectRel.STORE_CODE)
.and(tStoreProjectRel.STORE_TYPE.eq(StoreTypeEnum.ATOM.type.toByte()))
.and(tStoreProjectRel.PROJECT_CODE.eq(projectCode))
)
)
.orderBy(tAtom.CREATE_TIME.desc()).limit(1).fetchOne()
}
}

private fun generateGetPipelineAtomCondition(
a: TAtom,
tAtom: TAtom,
atomCode: String,
defaultFlag: Boolean? = null,
version: String? = null,
atomStatusList: List<Byte>? = null
): MutableList<Condition> {
val conditions = mutableListOf<Condition>()
conditions.add(a.ATOM_CODE.eq(atomCode))
conditions.add(tAtom.ATOM_CODE.eq(atomCode))
if (version != null) {
conditions.add(a.VERSION.like(VersionUtils.generateQueryVersion(version)))
conditions.add(tAtom.VERSION.like(VersionUtils.generateQueryVersion(version)))
}
if (defaultFlag != null) {
conditions.add(a.DEFAULT_FLAG.eq(defaultFlag))
conditions.add(tAtom.DEFAULT_FLAG.eq(defaultFlag))
}
if (atomStatusList != null) {
conditions.add(a.ATOM_STATUS.`in`(atomStatusList))
conditions.add(tAtom.ATOM_STATUS.`in`(atomStatusList))
}
return conditions
}
Expand Down Expand Up @@ -404,7 +402,7 @@ class AtomDao : AtomBaseDao() {
).from(this)
.where(
generateGetPipelineAtomCondition(
a = this,
tAtom = this,
atomCode = atomCode,
atomStatusList = atomStatusList
)
Expand All @@ -418,50 +416,40 @@ class AtomDao : AtomBaseDao() {
dslContext: DSLContext,
projectCode: String,
atomCode: String,
defaultFlag: Boolean,
atomStatusList: List<Byte>?
): Result<out Record>? {
val a = TAtom.T_ATOM.`as`("a")
val b = TStoreProjectRel.T_STORE_PROJECT_REL.`as`("b")
val t = dslContext.select(
a.VERSION.`as`(KEY_VERSION),
a.CREATE_TIME.`as`(KEY_CREATE_TIME),
a.ATOM_STATUS.`as`(KEY_ATOM_STATUS)
).from(a)
.where(
generateGetPipelineAtomCondition(
a = a,
atomCode = atomCode,
defaultFlag = true,
atomStatusList = atomStatusList
)
val tAtom = TAtom.T_ATOM
val tStoreProjectRel = TStoreProjectRel.T_STORE_PROJECT_REL
val baseStep = dslContext.select(
tAtom.VERSION.`as`(KEY_VERSION),
tAtom.CREATE_TIME.`as`(KEY_CREATE_TIME),
tAtom.ATOM_STATUS.`as`(KEY_ATOM_STATUS)
).from(tAtom)
val t = if (defaultFlag) {
val conditions = generateGetPipelineAtomCondition(
tAtom = tAtom,
atomCode = atomCode,
defaultFlag = true,
atomStatusList = atomStatusList
)
.union(
dslContext.select(
a.VERSION.`as`(KEY_VERSION),
a.CREATE_TIME.`as`(KEY_CREATE_TIME),
a.ATOM_STATUS.`as`(KEY_ATOM_STATUS)
).from(a).join(b).on(a.ATOM_CODE.eq(b.STORE_CODE))
.where(
generateGetPipelineAtomCondition(
a = a,
atomCode = atomCode,
defaultFlag = false,
atomStatusList = atomStatusList
)
)
.andExists(
dslContext.selectOne().from(b).where(
a.ATOM_CODE.eq(b.STORE_CODE).and(b.STORE_TYPE.eq(StoreTypeEnum.ATOM.type.toByte()))
.and(b.PROJECT_CODE.eq(projectCode))
)
)
baseStep.where(conditions)
} else {
val conditions = generateGetPipelineAtomCondition(
tAtom = tAtom,
atomCode = atomCode,
defaultFlag = false,
atomStatusList = atomStatusList
)
.asTable("t")
conditions.add(tStoreProjectRel.PROJECT_CODE.eq(projectCode))
conditions.add(tStoreProjectRel.STORE_TYPE.eq(StoreTypeEnum.ATOM.type.toByte()))
baseStep.join(tStoreProjectRel).on(tAtom.ATOM_CODE.eq(tStoreProjectRel.STORE_CODE)).where(conditions)
}
val firstVersion = JooqUtils.subStr(
str = t.field(KEY_VERSION) as Field<String>,
delim = ".",
count = 1
).`as`("firstVersion")
)
val secondVersion = JooqUtils.subStr(
str = JooqUtils.subStr(
str = t.field(KEY_VERSION) as Field<String>,
Expand All @@ -470,12 +458,12 @@ class AtomDao : AtomBaseDao() {
),
delim = ".",
count = 1
).`as`("secondVersion")
)
val thirdVersion = JooqUtils.subStr(
str = t.field(KEY_VERSION) as Field<String>,
delim = ".",
count = -1
).`as`("thirdVersion")
)
return dslContext.select(
t.field(KEY_VERSION),
t.field(KEY_CREATE_TIME),
Expand Down Expand Up @@ -1016,4 +1004,48 @@ class AtomDao : AtomBaseDao() {
.fetch()
}
}

fun getAtomRealVersion(
dslContext: DSLContext,
projectCode: String,
atomCode: String,
version: String,
defaultFlag: Boolean,
atomStatusList: List<Byte>? = null
): String? {
val tAtom = TAtom.T_ATOM
val tStoreProjectRel = TStoreProjectRel.T_STORE_PROJECT_REL
return if (defaultFlag) {
val conditions = generateGetPipelineAtomCondition(
tAtom = tAtom,
atomCode = atomCode,
version = version,
defaultFlag = true,
atomStatusList = atomStatusList
)
dslContext.select(tAtom.VERSION).from(tAtom)
.where(conditions)
.orderBy(tAtom.CREATE_TIME.desc()).limit(1).fetchOne(0, String::class.java)
} else {
val conditions = generateGetPipelineAtomCondition(
tAtom = tAtom,
atomCode = atomCode,
version = version,
defaultFlag = false,
atomStatusList = atomStatusList
)
conditions.add(tStoreProjectRel.PROJECT_CODE.eq(projectCode))
conditions.add(tStoreProjectRel.STORE_TYPE.eq(StoreTypeEnum.ATOM.type.toByte()))
dslContext.select(tAtom.VERSION).from(tAtom)
.where(conditions)
.andExists(
dslContext.selectOne().from(tStoreProjectRel).where(
tAtom.ATOM_CODE.eq(tStoreProjectRel.STORE_CODE)
.and(tStoreProjectRel.STORE_TYPE.eq(StoreTypeEnum.ATOM.type.toByte()))
.and(tStoreProjectRel.PROJECT_CODE.eq(projectCode))
)
)
.orderBy(tAtom.CREATE_TIME.desc()).limit(1).fetchOne(0, String::class.java)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ class MarketAtomDao : AtomBaseDao() {
.set(DEFAULT_FLAG, approveReq.defaultFlag)
.set(BUILD_LESS_RUN_FLAG, approveReq.buildLessRunFlag)
.set(SERVICE_SCOPE, JsonUtil.getObjectMapper().writeValueAsString(approveReq.serviceScope))
.set(LATEST_FLAG, latestFlag)
.set(MODIFIER, userId)
.set(UPDATE_TIME, LocalDateTime.now())
val weight = approveReq.weight
Expand Down
Loading

0 comments on commit ec365a9

Please sign in to comment.