Skip to content

Commit

Permalink
feat: 空目录清理调整 TencentBlueKing#2313
Browse files Browse the repository at this point in the history
  • Loading branch information
zacYL committed Jul 5, 2024
1 parent d5ea46e commit a83145f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class ActiveProjectEmptyFolderCleanupJob(
override fun beforeRunProject(projectId: String) {
if (properties.userMemory) return
// 每次任务启动前要将redis上对应的key清理, 避免干扰
val key = KEY_PREFIX + FolderUtils.buildCacheKey(projectId = projectId, repoName = StringPool.EMPTY)
val key = KEY_PREFIX + StringPool.COLON +
FolderUtils.buildCacheKey(projectId = projectId, repoName = StringPool.EMPTY)
emptyFolderCleanup.removeRedisKey(key)
}

Expand Down Expand Up @@ -141,6 +142,6 @@ class ActiveProjectEmptyFolderCleanupJob(

companion object {
private val logger = LoggerFactory.getLogger(ActiveProjectEmptyFolderCleanupJob::class.java)
private val KEY_PREFIX = "activeEmptyFolder"
private const val KEY_PREFIX = "activeEmptyFolder"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class EmptyFolderCleanup(
if (context.folders.isEmpty()) return
val movedToRedis: MutableList<String> = mutableListOf()
val storedFolderPrefix = if (collectionName.isNullOrEmpty()) {
FolderUtils.buildCacheKey(collectionName = collectionName, projectId = projectId)
FolderUtils.buildCacheKey(collectionName = collectionName, projectId = projectId)+StringPool.COLON
} else {
FolderUtils.buildCacheKey(collectionName = collectionName, projectId = StringPool.EMPTY)
}
Expand All @@ -154,7 +154,7 @@ class EmptyFolderCleanup(
fullPath = folderInfo.fullPath, tag = NODE_NUM
)

val key = keyPrefix + FolderUtils.buildCacheKey(collectionName = collectionName, projectId = projectId)
val key = keyPrefix + StringPool.COLON + FolderUtils.buildCacheKey(collectionName = collectionName, projectId = projectId)
hashCommands.hIncrBy(key.toByteArray(), nodeNumHKey.toByteArray(), entry.value.nodeNum.toLong())
entry.value.id?.let {
val idHKey = FolderUtils.buildCacheKey(
Expand Down Expand Up @@ -189,25 +189,14 @@ class EmptyFolderCleanup(
for (entry in context.folders) {
if (!entry.key.startsWith(prefix) || entry.value.nodeNum.toLong() > 0) continue
val folderInfo = extractFolderInfoFromCacheKey(entry.key, runCollection) ?: continue
if (emptyFolderDoubleCheck(
projectId = folderInfo.projectId,
repoName = folderInfo.repoName,
path = folderInfo.fullPath,
collectionName = collection
)) {
val deletedFlag = deletedFolderFlag(
repoName = folderInfo.repoName,
deletedEmptyFolder = deletedEmptyFolder,
deleteFolderRepos = deleteFolderRepos
)
logger.info(
"will delete empty folder ${folderInfo.fullPath}" +
" in repo ${folderInfo.projectId}|${folderInfo.repoName} " +
"with config deletedFlag: $deletedFlag"
)
doEmptyFolderDelete(entry.value.id, collection, deletedFlag)
context.totalDeletedNum.increment()
}
emptyFolderHandler(
folderInfo = folderInfo,
context = context,
collection = collection,
deleteFolderRepos = deleteFolderRepos,
deletedEmptyFolder = deletedEmptyFolder,
id = entry.value.id!!,
)
}
clearContextCache(projectId, context, collection, runCollection)
}
Expand All @@ -227,7 +216,7 @@ class EmptyFolderCleanup(
} else {
FolderUtils.buildCacheKey(collectionName = null, projectId = projectId)
}
val key = keyPrefix + keySuffix
val key = keyPrefix + StringPool.COLON + keySuffix
val hashOps = redisTemplate.opsForHash<String, String>()
val options = ScanOptions.scanOptions().build()
redisTemplate.execute { connection ->
Expand All @@ -240,30 +229,48 @@ class EmptyFolderCleanup(
key, entry, folderInfo, hashOps
)
if (statInfo.nodeNum > 0) continue
if (emptyFolderDoubleCheck(
projectId = folderInfo.projectId,
repoName = folderInfo.repoName,
path = folderInfo.fullPath,
collectionName = collection
)) {
val deletedFlag = deletedFolderFlag(
repoName = folderInfo.repoName,
deletedEmptyFolder = deletedEmptyFolder,
deleteFolderRepos = deleteFolderRepos
)
logger.info(
"will delete empty folder ${folderInfo.fullPath}" +
" in repo ${folderInfo.projectId}|${folderInfo.repoName} " +
"with config deletedFlag: $deletedFlag"
)
doEmptyFolderDelete(statInfo.id, collection, deletedFlag)
context.totalDeletedNum.increment()
}
emptyFolderHandler(
folderInfo = folderInfo,
context = context,
collection = collection,
deleteFolderRepos = deleteFolderRepos,
deletedEmptyFolder = deletedEmptyFolder,
id = statInfo.id!!,
)
}
}
redisTemplate.delete(key)
}

private fun emptyFolderHandler(
folderInfo: FolderInfo,
collection: String,
deletedEmptyFolder: Boolean,
deleteFolderRepos: List<String>,
id: String,
context: EmptyFolderCleanupJobContext,
) {
if (emptyFolderDoubleCheck(
projectId = folderInfo.projectId,
repoName = folderInfo.repoName,
path = folderInfo.fullPath,
collectionName = collection
)) {
val deletedFlag = deletedFolderFlag(
repoName = folderInfo.repoName,
deletedEmptyFolder = deletedEmptyFolder,
deleteFolderRepos = deleteFolderRepos
)
logger.info(
"will delete empty folder ${folderInfo.fullPath}" +
" in repo ${folderInfo.projectId}|${folderInfo.repoName} " +
"with config deletedFlag: $deletedFlag"
)
doEmptyFolderDelete(id, collection, deletedFlag)
context.totalDeletedNum.increment()
}
}

/**
* 从redis中获取对应目录的统计信息
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class InactiveProjectEmptyFolderCleanupJob(
if (properties.userMemory) return
// 每次任务启动前要将redis上对应的key清理, 避免干扰
collectionNames().forEach {
val key = KEY_PREFIX + FolderUtils.buildCacheKey(
val key = KEY_PREFIX + StringPool.COLON + FolderUtils.buildCacheKey(
collectionName = it, projectId = StringPool.EMPTY
)
emptyFolderCleanup.removeRedisKey(key)
Expand Down

0 comments on commit a83145f

Please sign in to comment.