Skip to content

Commit

Permalink
perf:misc多线程清理构建数据优化 TencentBlueKing#4645
Browse files Browse the repository at this point in the history
  • Loading branch information
carlyin0801 committed Jul 8, 2021
1 parent 54831fd commit 6b7a679
Showing 1 changed file with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy
import java.util.concurrent.TimeUnit

@Component
Expand All @@ -76,29 +77,36 @@ class PipelineBuildHistoryDataClearJob @Autowired constructor(
"pipeline:build:history:data:clear:project:id"
private const val PIPELINE_BUILD_HISTORY_DATA_CLEAR_PROJECT_LIST_KEY =
"pipeline:build:history:data:clear:project:list"
private var executor:ThreadPoolExecutor? = null
}

@Value("\${process.deletedPipelineStoreDays:30}")
private val deletedPipelineStoreDays: Long = 30 // 回收站已删除流水线保存天数

// 创建带有边界队列的线程池,防止内存爆掉
private val executor = ThreadPoolExecutor(
miscBuildDataClearConfig.maxThreadHandleProjectNum,
miscBuildDataClearConfig.maxThreadHandleProjectNum,
0L,
TimeUnit.MILLISECONDS,
LinkedBlockingQueue(10)
)

@Scheduled(initialDelay = 10000, fixedDelay = 12000)
fun pipelineBuildHistoryDataClear() {
if (!miscBuildDataClearConfig.switch.toBoolean()) {
// 如果清理构建历史数据开关关闭,则不清理
return
}
logger.info("pipelineBuildHistoryDataClear start")
val lock = RedisLock(redisOperation,
LOCK_KEY, 3000)
if (executor == null) {
// 创建带有边界队列的线程池,防止内存爆掉
logger.info("pipelineBuildHistoryDataClear create executor")
executor = ThreadPoolExecutor(
miscBuildDataClearConfig.maxThreadHandleProjectNum,
miscBuildDataClearConfig.maxThreadHandleProjectNum,
0L,
TimeUnit.MILLISECONDS,
LinkedBlockingQueue(10),
Executors.defaultThreadFactory(),
ThreadPoolExecutor.DiscardPolicy()
)
}
val lock = RedisLock(
redisOperation,
LOCK_KEY, 3000
)
try {
if (!lock.tryLock()) {
logger.info("get lock failed, skip")
Expand Down Expand Up @@ -147,7 +155,7 @@ class PipelineBuildHistoryDataClearJob @Autowired constructor(
maxThreadProjectPrimaryId: Long
): Future<Boolean> {
val threadName = "Thread-$threadNo"
return executor.submit(Callable<Boolean> {
return executor!!.submit(Callable<Boolean> {
var handleProjectPrimaryId =
redisOperation.get("$threadName:$PIPELINE_BUILD_HISTORY_DATA_CLEAR_PROJECT_ID_KEY")?.toLong()
if (handleProjectPrimaryId == null) {
Expand Down

0 comments on commit 6b7a679

Please sign in to comment.