Skip to content

Commit

Permalink
Merge pull request #1014 from yaoxuwan/issue_997
Browse files Browse the repository at this point in the history
fix: 包版本删除时,统计剩余版本个数然后删除包存在并发问题 #997
  • Loading branch information
owenlxu authored Aug 3, 2023
2 parents aa3f683 + b975650 commit 4398fd7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.dao.DuplicateKeyException
import org.springframework.data.mongodb.MongoCollectionUtils.getPreferredCollectionName
import org.springframework.data.mongodb.core.FindAndModifyOptions
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.aggregation.Aggregation
import org.springframework.data.mongodb.core.aggregation.AggregationResults
Expand Down Expand Up @@ -172,6 +173,13 @@ abstract class AbstractMongoDao<E> : MongoDao<E> {
return determineMongoTemplate().aggregate(aggregation, determineCollectionName(aggregation), outputType)
}

override fun <T> findAndModify(query: Query, update: Update, options: FindAndModifyOptions, clazz: Class<T>): T? {
if (logger.isDebugEnabled) {
logger.debug("Mongo Dao findAndModify: [$query], [$update]")
}
return determineMongoTemplate().findAndModify(query, update, options, clazz)
}

protected open fun determineCollectionName(): String {
var collectionName: String? = null
if (classType.isAnnotationPresent(Document::class.java)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ package com.tencent.bkrepo.common.mongo.dao

import com.mongodb.client.result.DeleteResult
import com.mongodb.client.result.UpdateResult
import org.springframework.data.mongodb.core.FindAndModifyOptions
import org.springframework.data.mongodb.core.aggregation.Aggregation
import org.springframework.data.mongodb.core.aggregation.AggregationResults
import org.springframework.data.mongodb.core.query.Query
Expand Down Expand Up @@ -107,4 +108,9 @@ interface MongoDao<E> {
* 文档聚合操作
*/
fun <O> aggregate(aggregation: Aggregation, outputType: Class<O>): AggregationResults<O>

/**
* 查询并更新操作
*/
fun <T> findAndModify(query: Query, update: Update, options: FindAndModifyOptions, clazz: Class<T>): T?
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import com.mongodb.client.result.UpdateResult
import com.tencent.bkrepo.common.mongo.dao.simple.SimpleMongoDao
import com.tencent.bkrepo.repository.model.TPackage
import com.tencent.bkrepo.repository.util.PackageQueryHelper
import org.springframework.data.mongodb.core.FindAndModifyOptions
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query
import org.springframework.data.mongodb.core.query.Update
import org.springframework.data.mongodb.core.query.isEqualTo
import org.springframework.stereotype.Repository

/**
Expand Down Expand Up @@ -84,4 +88,18 @@ class PackageDao : SimpleMongoDao<TPackage>() {
val update = Update().pull(TPackage::clusterNames.name, clusterName)
return updateFirst(query, update)
}

fun decreaseVersions(packageId: String): TPackage? {
val query = Query(Criteria.where(ID).isEqualTo(packageId))
val update = Update().inc(TPackage::versions.name, -1)
val options = FindAndModifyOptions()
options.returnNew(true)
return this.findAndModify(query, update, options, TPackage::class.java)
}

fun updateLatestVersion(packageId: String, latestVersion: String) {
val query = Query(Criteria.where(ID).isEqualTo(packageId))
val update = Update().set(TPackage::latest.name, latestVersion)
this.updateFirst(query, update)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,20 @@ class PackageServiceImpl(
versionName: String,
realIpAddress: String?
) {
val tPackage = packageDao.findByKey(projectId, repoName, packageKey) ?: return
val tPackageVersion = packageVersionDao.findByName(tPackage.id.orEmpty(), versionName) ?: return
var tPackage = packageDao.findByKey(projectId, repoName, packageKey) ?: return
val packageId = tPackage.id!!
val tPackageVersion = packageVersionDao.findByName(packageId, versionName) ?: return
checkCluster(tPackageVersion)
packageVersionDao.deleteByName(tPackageVersion.packageId, tPackageVersion.name)
tPackage.versions -= 1
packageVersionDao.deleteByName(packageId, tPackageVersion.name)
tPackage = packageDao.decreaseVersions(packageId) ?: return
if (tPackage.versions <= 0L) {
packageDao.removeById(tPackage.id.orEmpty())
packageDao.removeById(packageId)
logger.info("Delete package [$projectId/$repoName/$packageKey-$versionName] because no version exist")
} else {
if (tPackage.latest == tPackageVersion.name) {
val latestVersion = packageVersionDao.findLatest(tPackage.id.orEmpty())
tPackage.latest = latestVersion?.name.orEmpty()
val latestVersion = packageVersionDao.findLatest(packageId)
packageDao.updateLatestVersion(packageId, latestVersion?.name.orEmpty())
}
packageDao.save(tPackage)
}
publishEvent(
PackageEventFactory.buildDeletedEvent(
Expand Down

0 comments on commit 4398fd7

Please sign in to comment.