Skip to content

Commit

Permalink
fix: 优化一下细节问题 #2813
Browse files Browse the repository at this point in the history
  • Loading branch information
zzdjx committed Jan 13, 2025
1 parent c3fb6fa commit dbd0fe9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ interface BlockNodeService {
/**
* 删除旧分块,即删除非指定的nodeCurrentSha256的分块。
* 如果未指定nodeCurrentSha256,则删除节点所有分块
* 如果指定uploadId,则删除该uploadId对应的分块,未指定则删除所有分块
* 如果指定uploadId,则删除该uploadId对应的分块,未指定则删除uploadId为null的所有分块
* @param projectId 项目id
* @param repoName 仓库名
* @param fullPath 文件路径
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,11 @@ open class NodeDeleteSupport(

if (deletedNum == 0L) return NodeDeleteResult(0L, 0L, deleteTime)

// 计算删除的文件大小
val deletedSize = nodeBaseService.aggregateComputeSize(criteria.apply {
and(TNode::deleted.name).isEqualTo(deleteTime)
})

logger.info("Delete old block base node: $fullPath, operator: $operator, delete num : $deletedNum, " +
"delete time: $deleteTime success")
return NodeDeleteResult(deletedNum, deletedSize, deleteTime)
logger.info(
"Delete old block base node: $fullPath, operator: $operator, delete num : $deletedNum, " +
"delete time: $deleteTime success"
)
return NodeDeleteResult(deletedNum, 0, deleteTime)
}

private fun delete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
package com.tencent.bkrepo.common.storage.config

import org.springframework.util.unit.DataSize
import java.time.Duration

/**
* 文件接收配置
Expand Down Expand Up @@ -73,8 +74,14 @@ data class ReceiveProperties(
* 每秒接收数据量
*/
var rateLimit: DataSize = DataSize.ofBytes(-1),

/**
* 限速熔断阈值,当仓库配置的rateLimit小于等于限速熔断阈值时则直接将请求断开
*/
var circuitBreakerThreshold: DataSize = DataSize.ofKilobytes(1),

/**
* 接受分块过期时间, 默认12小时
*/
var blockExpireTime: Duration = Duration.ofHours(12),
)
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import com.tencent.bkrepo.common.artifact.stream.Range
import com.tencent.bkrepo.common.artifact.util.chunked.ChunkedUploadUtils
import com.tencent.bkrepo.common.artifact.util.http.HttpRangeUtils
import com.tencent.bkrepo.common.metadata.model.TBlockNode
import com.tencent.bkrepo.common.metadata.service.blocknode.impl.BlockNodeServiceImpl
import com.tencent.bkrepo.common.metadata.service.blocknode.BlockNodeService
import com.tencent.bkrepo.common.metadata.service.node.PipelineNodeService
import com.tencent.bkrepo.common.query.model.Rule
import com.tencent.bkrepo.common.security.manager.ci.CIPermissionManager
Expand Down Expand Up @@ -146,7 +146,7 @@ class GenericLocalRepository(
private val clusterNodeClient: ClusterNodeClient,
private val pipelineNodeService: PipelineNodeService,
private val ciPermissionManager: CIPermissionManager,
private val blockNodeServiceImpl: BlockNodeServiceImpl,
private val blockNodeService: BlockNodeService,
private val storageProperties: StorageProperties,
) : LocalRepository() {

Expand Down Expand Up @@ -217,7 +217,7 @@ class GenericLocalRepository(
val sha256 = getArtifactSha256()

val offset = context.request.getHeader(HEADER_OFFSET)?.toLongOrNull()
val expires = storageProperties.filesystem.cache.expireDuration
val expires = storageProperties.receive.blockExpireTime

val blockNode = TBlockNode(
createdBy = userId,
Expand All @@ -234,7 +234,7 @@ class GenericLocalRepository(

storageService.store(sha256, blockArtifactFile, storageCredentials)

val blockNodeInfo = blockNodeServiceImpl.createBlock(blockNode, storageCredentials)
val blockNodeInfo = blockNodeService.createBlock(blockNode, storageCredentials)

// Set response content type and write success response
context.response.contentType = MediaTypes.APPLICATION_JSON
Expand All @@ -244,7 +244,8 @@ class GenericLocalRepository(
blockNodeInfo.size,
blockNodeInfo.sha256,
blockNodeInfo.startPos,
blockNodeInfo.uploadId)
blockNodeInfo.uploadId
)
).toJsonString()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ class UploadService(

fun startBlockUpload(userId: String, artifactInfo: GenericArtifactInfo): UploadTransactionInfo {
with(artifactInfo) {
val expires = getLongHeader(HEADER_EXPIRES)

val overwrite = getBooleanHeader(HEADER_OVERWRITE)
Preconditions.checkArgument(expires >= 0, "expires")

// 判断文件是否存在
if (!overwrite && nodeService.checkExist(this)) {
logger.warn(
"User[${SecurityUtils.getPrincipal()}] start block upload [$artifactInfo] failed: " +
"artifact already exists."
"artifact already exists."
)
throw ErrorCodeException(ArtifactMessageCode.NODE_EXISTED, getArtifactName())
}
Expand Down Expand Up @@ -152,12 +152,13 @@ class UploadService(
// 记录上传启动的日志
logger.info(
"User[${SecurityUtils.getPrincipal()}] start block upload [$artifactInfo] success, "
+ "version: $uploadId.")
+ "version: $uploadId."
)
return uploadTransaction
}
}

fun blockBaseNodeCreate(userId: String, artifactInfo: GenericArtifactInfo, uploadId: String){
fun blockBaseNodeCreate(userId: String, artifactInfo: GenericArtifactInfo, uploadId: String) {
val attributes = NodeAttribute(
uid = NodeAttribute.NOBODY,
gid = NodeAttribute.NOBODY,
Expand All @@ -172,7 +173,7 @@ class UploadService(
value = uploadId
)
val fileSize = getLongHeader(HEADER_FILE_SIZE).takeIf { it > 0L }
?: throw ErrorCodeException(GenericMessageCode.BLOCK_HEAD_NOT_FOUND)
?: throw ErrorCodeException(GenericMessageCode.BLOCK_HEAD_NOT_FOUND)
val request = NodeCreateRequest(
projectId = artifactInfo.projectId,
repoName = artifactInfo.repoName,
Expand Down Expand Up @@ -205,7 +206,8 @@ class UploadService(
artifactInfo.projectId,
artifactInfo.repoName,
artifactInfo.getArtifactFullPath(),
uploadId)
uploadId
)
}

fun completeBlockUpload(
Expand All @@ -222,7 +224,7 @@ class UploadService(
val fileInfo = if (!sha256.isNullOrEmpty() && !md5.isNullOrEmpty() && size != null) {
logger.info(
"sha256 $sha256, md5 $md5, size $size for " +
"fullPath ${artifactInfo.getArtifactFullPath()} with uploadId $uploadId"
"fullPath ${artifactInfo.getArtifactFullPath()} with uploadId $uploadId"
)
FileInfo(sha256, md5, size)
} else {
Expand Down Expand Up @@ -258,10 +260,11 @@ class UploadService(
// 创建新的基础节点(Base Node)
try {
blockBaseNodeCreate(userId, artifactInfo, uploadId)
}
catch (e: Exception) {
logger.error("Create block base node failed, file path [${artifactInfo.getArtifactFullPath()}], " +
"version : $uploadId")
} catch (e: Exception) {
logger.error(
"Create block base node failed, file path [${artifactInfo.getArtifactFullPath()}], " +
"version : $uploadId"
)
abortSeparateBlockUpload(userId, uploadId, artifactInfo)
throw e
}
Expand All @@ -273,16 +276,12 @@ class UploadService(
artifactInfo.getArtifactFullPath()
)

// 获取节点信息并验证节点是否存在
val node = ArtifactContextHolder.getNodeDetail(artifactInfo) ?: run {
logger.warn("Node not found for artifact: ${artifactInfo.getArtifactFullPath()}")
throw ErrorCodeException(GenericMessageCode.NODE_DATA_ERROR, artifactInfo)
}

// 获取节点信息(不再进行节点存在性检查)
val node = ArtifactContextHolder.getNodeDetail(artifactInfo)

// 获取并按起始位置排序块信息列表
val blockInfoList = blockNodeService.listBlocksInUploadId(
node.projectId,
node!!.projectId,
node.repoName,
node.fullPath,
uploadId = uploadId
Expand Down Expand Up @@ -310,8 +309,10 @@ class UploadService(
}

// 上传完成,记录日志
logger.info("User [$userId] successfully completed block upload [uploadId: $uploadId], " +
"file path [${artifactInfo.getArtifactFullPath()}].")
logger.info(
"User [$userId] successfully completed block upload [uploadId: $uploadId], " +
"file path [${artifactInfo.getArtifactFullPath()}]."
)

}

Expand Down

0 comments on commit dbd0fe9

Please sign in to comment.