Skip to content

Commit

Permalink
fix: 修复迁移任务job每次只触发一个任务 TencentBlueKing#2058
Browse files Browse the repository at this point in the history
* fix: 修复迁移任务job每次只触发一个任务 TencentBlueKing#2058

* fix: 修复迁移任务job每次只触发一个任务 TencentBlueKing#2058

* fix: 设置仓库在迁移时抛出RepoMigratingException TencentBlueKing#2058
  • Loading branch information
cnlkl authored Apr 24, 2024
1 parent ed5739b commit b47fc36
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.tencent.bkrepo.job.batch.context.FileJobContext
import com.tencent.bkrepo.job.batch.utils.NodeCommonUtils
import com.tencent.bkrepo.job.config.properties.FileReferenceCleanupJobProperties
import com.tencent.bkrepo.job.exception.JobExecuteException
import com.tencent.bkrepo.job.exception.RepoMigratingException
import com.tencent.bkrepo.repository.api.StorageCredentialsClient
import com.tencent.bkrepo.repository.constant.SYSTEM_USER
import org.springframework.boot.context.properties.EnableConfigurationProperties
Expand Down Expand Up @@ -145,7 +146,11 @@ class FileReferenceCleanupJob(
}
mongoTemplate.remove(Query(Criteria(ID).isEqualTo(id)), collectionName)
} catch (e: Exception) {
throw JobExecuteException("Failed to delete file[$sha256] on [$storageCredentials].", e)
if (e is RepoMigratingException) {
logger.info(e.message)
} else {
throw JobExecuteException("Failed to delete file[$sha256] on [${storageCredentials?.key}].", e)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ class MigrateRepoStorageJob(
override fun doStart0(jobContext: JobContext) {
migrateRepoStorageService.rollbackInterruptedTaskState()
while (true) {
migrateRepoStorageService.tryExecuteTask()
?.let { logger.info("execute migrate task[$it]") }
?: break
val executedTask = migrateRepoStorageService.tryExecuteTask() ?: break
logger.info("execute migrate task[$executedTask]")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.tencent.bkrepo.common.mongo.constant.MIN_OBJECT_ID
import com.tencent.bkrepo.common.mongo.dao.util.sharding.HashShardingUtils
import com.tencent.bkrepo.job.BATCH_SIZE
import com.tencent.bkrepo.job.SHARDING_COUNT
import com.tencent.bkrepo.job.exception.RepoMigratingException
import com.tencent.bkrepo.job.migrate.MigrateRepoStorageService
import org.bson.types.ObjectId
import org.springframework.data.domain.Sort
Expand Down Expand Up @@ -75,7 +76,7 @@ class NodeCommonUtils(
.any {
// node正在迁移时无法判断是否存在于存储[storageCredentialsKey]上
if (migrateRepoStorageService.migrating(it.projectId, it.repoName)) {
throw IllegalStateException("repo[${it.projectId}/${it.repoName}] was migrating")
throw RepoMigratingException("repo[${it.projectId}/${it.repoName}] was migrating")
}
val repo = RepositoryCommonUtils.getRepositoryDetail(it.projectId, it.repoName)
repo.storageCredentials?.key == storageCredentialsKey
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.job.exception

class RepoMigratingException(msg: String): IllegalStateException(msg)
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ class MigrateExecutorTest @Autowired constructor(
val task = migrateTaskService.findTask(UT_PROJECT_ID, UT_REPO_NAME)!!
assertEquals(MigrateRepoStorageTaskState.MIGRATING.name, task.state)
assertNotNull(task.startDate)
assertTrue(executingTaskRecorder.executing(task.id!!))

// 等待任务执行结束
Thread.sleep(2000L)
Thread.sleep(500L)
assertTrue(executingTaskRecorder.executing(task.id!!))
context.waitAllTransferFinished()
assertTaskFinished(task.id!!, nodeCount)
}
Expand Down

0 comments on commit b47fc36

Please sign in to comment.