Skip to content

Commit

Permalink
Merge pull request #1139 from zacYL/issue_1067
Browse files Browse the repository at this point in the history
feat: 节点缓存逻辑调整 #1067
  • Loading branch information
owenlxu authored Oct 13, 2023
2 parents 6b83110 + b595627 commit c6ad6ef
Show file tree
Hide file tree
Showing 26 changed files with 119 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ open class ArtifactInfo(

private val normalizedUri = PathUtils.normalizeFullPath(artifactUri)

/**
* 构件实际映射路径, 默认情况为空
* 存在着特殊场景下传入构件url与实际存储路径需要进行转换的场景
* 如构建路径为/archive/file/tmp.data 实际映射路径可能为/archive/file/tmp/tmp.data
*/
private var artifactMappingUri: String? = null


/**
* 设置构件实际映射路径
*/
open fun setArtifactMappingUri(artifactMappingUri: String) {
this.artifactMappingUri = PathUtils.normalizeFullPath(artifactMappingUri)
}

fun getArtifactMappingUri(): String? = artifactMappingUri

/**
* 构件名称,不同依赖源解析规则不一样,可以override
*
Expand All @@ -75,7 +92,11 @@ open class ArtifactInfo(
*
* 默认使用传入的artifactUri作为名称
*/
open fun getArtifactFullPath(): String = normalizedUri
open fun getArtifactFullPath(): String = if (artifactMappingUri.isNullOrEmpty()) {
normalizedUri
} else {
artifactMappingUri!!
}

/**
* 构件下载显示名称,不同依赖源解析规则不一样,可以override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class ArtifactDownloadListener(
val repoName = event.context.repoName
val fullPath = event.context.artifactInfo.getArtifactFullPath()
val userId = event.context.userId
val node = ArtifactContextHolder.getNodeDetail()
val node = ArtifactContextHolder.getNodeDetail(event.context.artifactInfo)
if (node == null) {
val downloadedEvent = NodeDownloadedEvent(
projectId = projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,11 @@ class ArtifactContextHolder(
return otherRepo ?: throw RepoNotFoundException(repoName)
}

fun getNodeDetail(projectId: String? = null, repoName: String? = null, fullPath: String? = null): NodeDetail? {
fun getNodeDetail(artifactInfo: ArtifactInfo): NodeDetail? {
val request = HttpContextHolder.getRequestOrNull() ?: return null
val artifactInfo = getArtifactInfo(request) ?: return null
val finalProjectId = projectId ?: artifactInfo.projectId
val finalRepoName = repoName ?: artifactInfo.repoName
val finalFullPath = fullPath ?: artifactInfo.getArtifactFullPath()
val finalProjectId = artifactInfo.projectId
val finalRepoName = artifactInfo.repoName
val finalFullPath = artifactInfo.getArtifactFullPath()

val attrKey = "$NODE_DETAIL_KEY:$finalProjectId:$finalRepoName$finalFullPath"
val nodeDetailAttribute = request.getAttribute(attrKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class LocalRepository : AbstractArtifactRepository() {

override fun onDownload(context: ArtifactDownloadContext): ArtifactResource? {
with(context) {
val node = ArtifactContextHolder.getNodeDetail()
val node = ArtifactContextHolder.getNodeDetail(artifactInfo)
node?.let { downloadIntercept(context, it) }
val inputStream = storageManager.loadArtifactInputStream(node, storageCredentials) ?: return null
val responseName = artifactInfo.getResponseName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ class CosRedirectService(
return false
}

val node = ArtifactContextHolder.getNodeDetail(
context.repositoryDetail.projectId,
context.repositoryDetail.name
)
val node = ArtifactContextHolder.getNodeDetail(context.artifactInfo)
// 从request uri中获取artifact信息,artifact为null时表示非单制品下载请求,此时不支持重定向
val artifact = ArtifactContextHolder.getArtifactInfo()
// node为null时表示制品不存在,或者是Remote仓库的制品尚未被缓存,此时不支持重定向
Expand Down Expand Up @@ -115,10 +112,7 @@ class CosRedirectService(
override fun redirect(context: ArtifactDownloadContext) {
val credentials = context.repositoryDetail.storageCredentials ?: storageProperties.defaultStorageCredentials()
require(credentials is InnerCosCredentials)
val node = ArtifactContextHolder.getNodeDetail(
projectId = context.repositoryDetail.projectId,
repoName = context.repositoryDetail.name
)!!
val node = ArtifactContextHolder.getNodeDetail(context.artifactInfo)!!

// 创建请求并签名
val clientConfig = ClientConfig(credentials).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ class EdgeNodeRedirectService(
}

override fun shouldRedirect(context: ArtifactDownloadContext): Boolean {
val node = ArtifactContextHolder.getNodeDetail(
context.repositoryDetail.projectId,
context.repositoryDetail.name
)
val node = ArtifactContextHolder.getNodeDetail(context.artifactInfo)
val selfClusterName = clusterProperties.self.name
if (logger.isDebugEnabled) {
logger.debug("node cluster: ${node?.clusterNames.orEmpty().toJsonString()},in cluster $selfClusterName")
Expand Down Expand Up @@ -102,10 +99,7 @@ class EdgeNodeRedirectService(
* 获取边缘节点名称
* */
private fun getEdgeClusterName(context: ArtifactDownloadContext): String? {
val node = ArtifactContextHolder.getNodeDetail(
context.repositoryDetail.projectId,
context.repositoryDetail.name
)
val node = ArtifactContextHolder.getNodeDetail(context.artifactInfo)
return node?.clusterNames?.firstOrNull()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ class ComposerLocalRepository(private val stageClient: StageClient) : LocalRepos

override fun onDownload(context: ArtifactDownloadContext): ArtifactResource? {
with(context) {
val artifactPath = artifactInfo.getArtifactFullPath().removePrefix("/$DIRECT_DISTS")
val node = ArtifactContextHolder.getNodeDetail(fullPath = artifactPath)
artifactInfo.setArtifactMappingUri(artifactInfo.getArtifactFullPath().removePrefix("/$DIRECT_DISTS"))
val node = ArtifactContextHolder.getNodeDetail(artifactInfo)
node?.let {
downloadIntercept(context, it)
packageVersion(it)?.let { packageVersion -> downloadIntercept(context, packageVersion) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class CompressedBlobArtifactInfo(

override fun getArtifactName() = "/$DIR_BLOBS/$compressedContentId"

override fun getArtifactFullPath() = "/$DIR_BLOBS/$compressedContentId"
override fun getArtifactFullPath() = if(getArtifactMappingUri().isNullOrEmpty()) {
"/$DIR_BLOBS/$compressedContentId"
} else {
getArtifactMappingUri()!!
}

companion object {
const val PATH_VARIABLE_CONTENT_ID = "id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ class ReferenceArtifactInfo(

override fun getArtifactName() = "/$bucket/$refKey"

override fun getArtifactFullPath() = "/$bucket/$refKey"
override fun getArtifactFullPath(): String {
return if(getArtifactMappingUri().isNullOrEmpty()) {
"/$bucket/$refKey"
} else getArtifactMappingUri()!!
}

companion object {
const val PATH_VARIABLE_BUCKET = "bucket"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,7 @@ class GenericLocalRepository(
private fun downloadSingleNode(context: ArtifactDownloadContext): ArtifactResource? {
with(context) {
val node = getNodeDetailsFromReq(true)?.firstOrNull()
?: ArtifactContextHolder.getNodeDetail(
projectId = projectId,
repoName = repoName,
fullPath = artifactInfo.getArtifactFullPath()
)
?: ArtifactContextHolder.getNodeDetail(artifactInfo)
?: return null
if (node.folder) {
return downloadFolder(this, node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DownloadService(

fun download(artifactInfo: GenericArtifactInfo) {
with(artifactInfo) {
val node = ArtifactContextHolder.getNodeDetail()
val node = ArtifactContextHolder.getNodeDetail(this)
?: throw NodeNotFoundException(getArtifactFullPath())
val download = HttpContextHolder.getRequest().getParameter(PARAM_DOWNLOAD)?.toBoolean() ?: false
val context = ArtifactDownloadContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class GitContentArtifactInfo(
) : GitRepositoryArtifactInfo(projectId, repoName, artifactUri) {

override fun getArtifactFullPath(): String {
return String.format("objects/%s/%s", commitId, path)
return if(getArtifactMappingUri().isNullOrEmpty()) {
String.format("objects/%s/%s", commitId, path)
} else getArtifactMappingUri()!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class GitPackFileArtifactInfo(
companion object {
const val PACKS = "packs"
}

override fun getArtifactFullPath(): String {
return "$PACKS/$fileName"
return if(getArtifactMappingUri().isNullOrEmpty()) {
"$PACKS/$fileName"
} else getArtifactMappingUri()!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class HelmLocalRepository(

override fun onDownload(context: ArtifactDownloadContext): ArtifactResource? {
val fullPath = context.getStringAttribute(FULL_PATH)!!
val node = ArtifactContextHolder.getNodeDetail(fullPath = fullPath)
context.artifactInfo.setArtifactMappingUri(fullPath)
val node = ArtifactContextHolder.getNodeDetail(context.artifactInfo)
node?.let {
node.metadata[NAME]?.let { context.putAttribute(NAME, it) }
node.metadata[VERSION]?.let { context.putAttribute(VERSION, it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class HelmDeleteArtifactInfo(

private val chartFullPath = HelmUtils.getChartFileFullPath(name, version)

override fun getArtifactFullPath(): String = chartFullPath
override fun getArtifactFullPath(): String {
return if(getArtifactMappingUri().isNullOrEmpty()) chartFullPath
else getArtifactMappingUri()!!
}

override fun getArtifactName(): String = name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,8 @@ class MavenLocalRepository(
checksumType: HashType? = null
): NodeDetail? {
with(context) {
var node = ArtifactContextHolder.getNodeDetail(fullPath = fullPath)
artifactInfo.setArtifactMappingUri(fullPath)
var node = ArtifactContextHolder.getNodeDetail(artifactInfo)
if (node != null || checksumType == null) {
return node
}
Expand All @@ -784,11 +785,13 @@ class MavenLocalRepository(
"in ${artifactInfo.getRepoIdentify()}"
)
val temPath = fullPath.removeSuffix(".${checksumType.ext}")
node = ArtifactContextHolder.getNodeDetail(fullPath = temPath)
artifactInfo.setArtifactMappingUri(temPath)
node = ArtifactContextHolder.getNodeDetail(artifactInfo)
// 源文件存在,但是对应checksum文件不存在,需要生成
if (node != null) {
verifyPath(context, temPath, checksumType)
node = ArtifactContextHolder.getNodeDetail(fullPath = fullPath)
artifactInfo.setArtifactMappingUri(fullPath)
node = ArtifactContextHolder.getNodeDetail(artifactInfo)
}
return node
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ open class NpmArtifactInfo(
*/
private val tarballFullPath: String = NpmUtils.formatTarballPath(packageName, version, delimiter)

override fun getArtifactFullPath() = tarballFullPath
override fun getArtifactFullPath(): String {
return if(getArtifactMappingUri().isNullOrEmpty()) {
tarballFullPath
} else getArtifactMappingUri()!!
}

override fun getArtifactVersion() = version

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class NugetDeleteArtifactInfo(

private val nugetFullPath = NugetUtils.getNupkgFullPath(packageName, version)

override fun getArtifactFullPath(): String = nugetFullPath
override fun getArtifactFullPath(): String {
return if(getArtifactMappingUri().isNullOrEmpty()) {
nugetFullPath
} else getArtifactMappingUri()!!
}

override fun getArtifactName(): String = packageName

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ class NugetDownloadArtifactInfo(
private val nupkgFullPath = NugetUtils.getNupkgFullPath(packageName, version)
private val nuspecFullPath = NugetUtils.getNuspecFullPath(packageName, version)

override fun getArtifactFullPath(): String = if (type == MANIFEST) nuspecFullPath else nupkgFullPath
override fun getArtifactFullPath(): String {
return if(getArtifactMappingUri().isNullOrEmpty()) {
if (type == MANIFEST) nuspecFullPath else nupkgFullPath
} else getArtifactMappingUri()!!
}

override fun getArtifactName(): String = packageName

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ class NugetPublishArtifactInfo(
) : NugetArtifactInfo(projectId, repoName, StringPool.EMPTY) {
lateinit var artifactFile: ArtifactFile

override fun getArtifactFullPath(): String = NugetUtils.getNupkgFullPath(packageName, version)
override fun getArtifactFullPath(): String {
return if(getArtifactMappingUri().isNullOrEmpty()) {
NugetUtils.getNupkgFullPath(packageName, version)
} else getArtifactMappingUri()!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ class OciBlobArtifactInfo(
}

override fun getArtifactFullPath(): String {
return if (digest.isNullOrBlank()) {
""
} else {
OciLocationUtils.buildDigestBlobsPath(packageName, ociDigest)
}
return if(getArtifactMappingUri().isNullOrEmpty()) {
if (digest.isNullOrBlank()) {
""
} else {
OciLocationUtils.buildDigestBlobsPath(packageName, ociDigest)
}
} else getArtifactMappingUri()!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ class OciManifestArtifactInfo(
val reference: String,
val isValidDigest: Boolean
) : OciArtifactInfo(projectId, repoName, packageName, version) {

override fun getArtifactFullPath(): String {
return if (isValidDigest) {
OciLocationUtils.buildDigestManifestPathWithReference(packageName, reference)
} else {
OciLocationUtils.buildManifestPath(packageName, reference)
}
return if(getArtifactMappingUri().isNullOrEmpty()) {
if (isValidDigest) {
OciLocationUtils.buildDigestManifestPathWithReference(packageName, reference)
} else {
OciLocationUtils.buildManifestPath(packageName, reference)
}
} else getArtifactMappingUri()!!

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,12 @@ class OciRegistryLocalRepository(
}

private fun getNodeDetail(artifactInfo: OciArtifactInfo, fullPath: String): NodeDetail? {
return ArtifactContextHolder.getNodeDetail(fullPath = fullPath) ?: run {
artifactInfo.setArtifactMappingUri(fullPath)
return ArtifactContextHolder.getNodeDetail(artifactInfo) ?: run {
val oldDockerPath = ociOperationService.getDockerNode(artifactInfo)
?: return null
ArtifactContextHolder.getNodeDetail(fullPath = oldDockerPath)
artifactInfo.setArtifactMappingUri(oldDockerPath)
ArtifactContextHolder.getNodeDetail(artifactInfo)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class ShareServiceImpl(
* 加固签名的apk包,不允许匿名下载
*/
private fun checkAlphaApkDownloadUser(userId: String, artifactInfo: ArtifactInfo) {
val nodeDetail = ArtifactContextHolder.getNodeDetail()
val nodeDetail = ArtifactContextHolder.getNodeDetail(artifactInfo)
?: throw NodeNotFoundException(artifactInfo.getArtifactFullPath())
val appStageKey = nodeDetail.metadata.keys.find { it.equals(BK_CI_APP_STAGE_KEY, true) }
?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,19 @@ class RpmArtifactInfo(
}

override fun getArtifactFullPath(): String {
val action = HttpContextHolder.getRequest().method
return if (action.equals("delete", ignoreCase = true)) {
val packageKey = HttpContextHolder.getRequest().getParameter("packageKey")
val version = HttpContextHolder.getRequest().getParameter("version")
if (StringUtils.isBlank(packageKey)) {
super.getArtifactFullPath()
return if(getArtifactMappingUri().isNullOrEmpty()) {
val action = HttpContextHolder.getRequest().method
if (action.equals("delete", ignoreCase = true)) {
val packageKey = HttpContextHolder.getRequest().getParameter("packageKey")
val version = HttpContextHolder.getRequest().getParameter("version")
if (StringUtils.isBlank(packageKey)) {
super.getArtifactFullPath()
} else {
"/${PackageKeys.resolveRpm(packageKey)}-$version.rpm"
}
} else {
"/${PackageKeys.resolveRpm(packageKey)}-$version.rpm"
super.getArtifactFullPath()
}
} else {
super.getArtifactFullPath()
}
} else getArtifactMappingUri()!!
}
}
Loading

0 comments on commit c6ad6ef

Please sign in to comment.