-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 修复ddc_blob references字段过大导致慢查询问题 #2181
* fix: 修复ddc_blob references字段过大导致慢查询问题 #2181 * fix: 增加ExpiredDdcRefCleanupJob测试 #2181 * fix: 增加ExpiredDdcRefCleanupJob测试 #2181 * fix: 增加DdcBlobCleanupJob测试 #2181 * fix: 增加ref对应的blob数量超过限制日志输出 #2181
- Loading branch information
Showing
12 changed files
with
375 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/backend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/model/TDdcBlobRef.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.tencent.bkrepo.ddc.model | ||
|
||
import org.springframework.data.mongodb.core.index.CompoundIndex | ||
import org.springframework.data.mongodb.core.index.CompoundIndexes | ||
import org.springframework.data.mongodb.core.mapping.Document | ||
|
||
|
||
@Document("ddc_blob_ref") | ||
@CompoundIndexes( | ||
CompoundIndex( | ||
name = "projectId_repoName_blobId_ref_idx", | ||
def = "{'projectId': 1, 'repoName': 1, 'blobId': 1, 'ref': 1}", | ||
unique = true, | ||
background = true | ||
), | ||
CompoundIndex( | ||
name = "projectId_repoName_ref_blobId_idx", | ||
def = "{'projectId': 1, 'repoName': 1, 'ref': 1, 'blobId': 1}", | ||
unique = true, | ||
background = true | ||
), | ||
) | ||
data class TDdcBlobRef( | ||
var id: String? = null, | ||
var projectId: String, | ||
var repoName: String, | ||
/** | ||
* blob blake3 hash | ||
*/ | ||
var blobId: String, | ||
/** | ||
* 引用了该blob的ref或blob,ref的inline blob中直接或间接引用的所有blob都会关联到ref | ||
* ref类型引用 ref/{bucket}/{key} | ||
* blob类型引用 blob/{blobId} | ||
*/ | ||
var ref: String, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ackend/ddc/biz-ddc/src/main/kotlin/com/tencent/bkrepo/ddc/repository/BlobRefRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.tencent.bkrepo.ddc.repository | ||
|
||
import com.tencent.bkrepo.common.mongo.dao.simple.SimpleMongoDao | ||
import com.tencent.bkrepo.ddc.model.TDdcBlobRef | ||
import com.tencent.bkrepo.ddc.utils.DdcUtils.buildRef | ||
import org.springframework.dao.DuplicateKeyException | ||
import org.springframework.data.mongodb.core.query.Criteria | ||
import org.springframework.data.mongodb.core.query.Query | ||
import org.springframework.data.mongodb.core.query.isEqualTo | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class BlobRefRepository : SimpleMongoDao<TDdcBlobRef>() { | ||
fun addRefToBlob(projectId: String, repoName: String, bucket: String, refKey: String, blobIds: Set<String>) { | ||
if (blobIds.size > DEFAULT_BLOB_SIZE_LIMIT) { | ||
val ref = "$projectId/$repoName/${buildRef(bucket, refKey)}" | ||
logger.error("blobs of ref[$ref] exceed size limit, size[${blobIds.size}]]") | ||
} | ||
blobIds.forEach { | ||
try { | ||
insert( | ||
TDdcBlobRef( | ||
id = null, | ||
projectId = projectId, | ||
repoName = repoName, | ||
blobId = it, | ||
ref = buildRef(bucket, refKey) | ||
) | ||
) | ||
} catch (ignore: DuplicateKeyException) { | ||
} | ||
} | ||
} | ||
|
||
fun removeRefFromBlob(projectId: String, repoName: String, bucket: String, refKey: String): List<TDdcBlobRef> { | ||
val criteria = Criteria | ||
.where(TDdcBlobRef::projectId.name).isEqualTo(projectId) | ||
.and(TDdcBlobRef::repoName.name).isEqualTo(repoName) | ||
.and(TDdcBlobRef::ref.name).isEqualTo(buildRef(bucket, refKey)) | ||
return determineMongoTemplate().findAllAndRemove(Query(criteria), TDdcBlobRef::class.java) | ||
} | ||
|
||
companion object { | ||
private const val DEFAULT_BLOB_SIZE_LIMIT = 20 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...ob/biz-job/src/test/kotlin/com/tencent/bkrepo/job/batch/task/ddc/DdcBlobCleanupJobTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.tencent.bkrepo.job.batch.task.ddc | ||
|
||
import com.tencent.bkrepo.common.metadata.service.log.OperateLogService | ||
import com.tencent.bkrepo.common.metadata.service.node.NodeService | ||
import com.tencent.bkrepo.common.mongo.constant.ID | ||
import com.tencent.bkrepo.job.batch.JobBaseTest | ||
import com.tencent.bkrepo.job.batch.task.ddc.DdcTestUtils.insertBlob | ||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.times | ||
import org.mockito.kotlin.verify | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest | ||
import org.springframework.boot.test.mock.mockito.MockBean | ||
import org.springframework.data.mongodb.core.MongoTemplate | ||
import org.springframework.data.mongodb.core.query.Criteria | ||
import org.springframework.data.mongodb.core.query.Query | ||
import org.springframework.data.mongodb.core.query.isEqualTo | ||
|
||
|
||
@DisplayName("DDC Blob清理测试") | ||
@DataMongoTest | ||
class DdcBlobCleanupJobTest @Autowired constructor( | ||
private val blobCleanupJob: DdcBlobCleanupJob, | ||
private val mongoTemplate: MongoTemplate, | ||
) : JobBaseTest() { | ||
|
||
@MockBean | ||
private lateinit var nodeService: NodeService | ||
|
||
@MockBean | ||
lateinit var operateLogService: OperateLogService | ||
|
||
@Test | ||
fun test() { | ||
generateData() | ||
blobCleanupJob.start() | ||
listOf(blobIds[0], blobIds[2], blobIds[6]).forEach { assertFalse(blobExists(it)) } | ||
listOf(blobIds[1], blobIds[3], blobIds[4], blobIds[5], blobIds[7]).forEach { assertTrue(blobExists(it)) } | ||
verify(nodeService, times(3)).deleteNode(any()) | ||
} | ||
|
||
private fun blobExists(id: String): Boolean { | ||
return mongoTemplate.exists(Query(Criteria.where(ID).isEqualTo(id)), DdcBlobCleanupJob.COLLECTION_NAME) | ||
} | ||
|
||
private fun generateData() { | ||
val ref = "ref/legacytexture/366f955a863296d36ce99868d015752ad0e29f81" | ||
// 旧数据 | ||
mongoTemplate.insertBlob(blobIds[0], "1", emptySet()) | ||
mongoTemplate.insertBlob(blobIds[1], "2", setOf(ref)) | ||
|
||
// 混合 | ||
mongoTemplate.insertBlob(blobIds[2], "3", emptySet(), 0L) | ||
mongoTemplate.insertBlob(blobIds[3], "4", setOf(ref), 1L) | ||
mongoTemplate.insertBlob(blobIds[4], "7", setOf(ref), 0L) | ||
mongoTemplate.insertBlob(blobIds[5], "8", emptySet(), 1L) | ||
|
||
// 新数据 | ||
mongoTemplate.insertBlob(blobIds[6], "5", null, 0L) | ||
mongoTemplate.insertBlob(blobIds[7], "6", null, 1L) | ||
} | ||
|
||
companion object { | ||
private val blobIds = Array(8) { | ||
"11bb03c690c9fab0c5e3ed9" + it | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...backend/job/biz-job/src/test/kotlin/com/tencent/bkrepo/job/batch/task/ddc/DdcTestUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.tencent.bkrepo.job.batch.task.ddc | ||
|
||
import com.tencent.bkrepo.common.mongo.constant.ID | ||
import com.tencent.bkrepo.job.UT_PROJECT_ID | ||
import com.tencent.bkrepo.job.UT_REPO_NAME | ||
import com.tencent.bkrepo.job.batch.task.ddc.ExpiredDdcRefCleanupJob.Companion.COLLECTION_NAME_BLOB_REF | ||
import org.bson.types.ObjectId | ||
import org.springframework.data.mongodb.core.MongoTemplate | ||
import java.time.LocalDateTime | ||
|
||
object DdcTestUtils { | ||
fun MongoTemplate.insertBlob(id: String, blobId: String, ref: Set<String>?, refCount: Long? = null) { | ||
val blob = mutableMapOf<String, Any>( | ||
ID to ObjectId(id), | ||
"projectId" to UT_PROJECT_ID, | ||
"repoName" to UT_REPO_NAME, | ||
"blobId" to blobId, | ||
"lastModifiedDate" to LocalDateTime.now().minusHours(2L) | ||
) | ||
ref?.let { blob["references"] = it } | ||
refCount?.let { blob["refCount"] = it } | ||
insert(blob, DdcBlobCleanupJob.COLLECTION_NAME) | ||
} | ||
|
||
fun MongoTemplate.insertBlobRef(id: String, blobId: String, ref: String): ExpiredDdcRefCleanupJob.BlobRef { | ||
val blobRef = ExpiredDdcRefCleanupJob.BlobRef( | ||
id = id, | ||
projectId = UT_PROJECT_ID, | ||
repoName = UT_REPO_NAME, | ||
blobId = blobId, | ||
ref = ref, | ||
) | ||
insert(blobRef, COLLECTION_NAME_BLOB_REF) | ||
return blobRef | ||
} | ||
} |
Oops, something went wrong.