Skip to content

Commit

Permalink
feat: 兼容UE4.26-4.27 DDC #1256 (#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl authored Oct 18, 2023
1 parent c0632e1 commit 710dd38
Show file tree
Hide file tree
Showing 23 changed files with 661 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class ReferenceArtifactInfo(
repoName: String,
val bucket: String,
val refKey: RefKey,
/**
* 是通过旧接口调用
*/
var legacy: Boolean = false,
var inlineBlobHash: String? = null,
) : ArtifactInfo(projectId, repoName, StringPool.EMPTY) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import com.tencent.bkrepo.common.api.exception.BadRequestException
import com.tencent.bkrepo.common.api.exception.ErrorCodeException
import com.tencent.bkrepo.common.api.message.CommonMessageCode
import com.tencent.bkrepo.common.api.util.toJsonString
import com.tencent.bkrepo.common.artifact.api.ArtifactFile
import com.tencent.bkrepo.common.artifact.api.ArtifactInfo
import com.tencent.bkrepo.common.artifact.message.ArtifactMessageCode
import com.tencent.bkrepo.common.artifact.repository.context.ArtifactDownloadContext
import com.tencent.bkrepo.common.artifact.repository.context.ArtifactUploadContext
Expand All @@ -52,6 +54,7 @@ import com.tencent.bkrepo.ddc.exception.NotImplementedException
import com.tencent.bkrepo.ddc.exception.ReferenceIsMissingBlobsException
import com.tencent.bkrepo.ddc.metrics.DdcMeterBinder
import com.tencent.bkrepo.ddc.pojo.Blob
import com.tencent.bkrepo.ddc.pojo.ContentHash
import com.tencent.bkrepo.ddc.pojo.Reference
import com.tencent.bkrepo.ddc.pojo.UploadCompressedBlobResponse
import com.tencent.bkrepo.ddc.serialization.CbObject
Expand All @@ -60,6 +63,7 @@ import com.tencent.bkrepo.ddc.service.ReferenceResolver
import com.tencent.bkrepo.ddc.service.ReferenceService
import com.tencent.bkrepo.ddc.utils.BlakeUtils.blake3
import com.tencent.bkrepo.ddc.utils.BlakeUtils.hex
import com.tencent.bkrepo.ddc.utils.DdcUtils
import com.tencent.bkrepo.ddc.utils.MEDIA_TYPE_JUPITER_INLINED_PAYLOAD
import com.tencent.bkrepo.ddc.utils.MEDIA_TYPE_UNREAL_COMPACT_BINARY
import com.tencent.bkrepo.ddc.utils.MEDIA_TYPE_UNREAL_UNREAL_COMPRESSED_BUFFER
Expand Down Expand Up @@ -88,7 +92,7 @@ class DdcLocalRepository(
super.onUploadBefore(context)
var uploadBlake3: String? = null
val artifactInfo = context.artifactInfo
if (artifactInfo is ReferenceArtifactInfo) {
if (artifactInfo is ReferenceArtifactInfo && !artifactInfo.legacy) {
uploadBlake3 = artifactInfo.inlineBlobHash!!
}
if (uploadBlake3 != null && uploadBlake3 != context.getStreamArtifactFile().blake3().hex()) {
Expand All @@ -98,8 +102,11 @@ class DdcLocalRepository(

override fun onUpload(context: ArtifactUploadContext) {
val artifactInfo = context.artifactInfo
if (artifactInfo is ReferenceArtifactInfo) {
val startTime = System.nanoTime()
val startTime = System.nanoTime()
if (artifactInfo is ReferenceArtifactInfo && artifactInfo.legacy) {
onUploadLegacyReference(context)
ddcMeterBinder.legacyRefStoreTimer.record(System.nanoTime() - startTime, NANOSECONDS)
} else if (artifactInfo is ReferenceArtifactInfo) {
onUploadReference(context)
ddcMeterBinder.refStoreTimer.record(System.nanoTime() - startTime, NANOSECONDS)
} else {
Expand All @@ -117,7 +124,9 @@ class DdcLocalRepository(

override fun onDownload(context: ArtifactDownloadContext): ArtifactResource? {
val artifactInfo = context.artifactInfo
return if (artifactInfo is ReferenceArtifactInfo) {
return if (artifactInfo is ReferenceArtifactInfo && artifactInfo.legacy) {
onDownloadLegacyReference(context)
} else if (artifactInfo is ReferenceArtifactInfo) {
onDownloadReference(context)
} else {
val startTime = System.nanoTime()
Expand All @@ -139,6 +148,64 @@ class DdcLocalRepository(
}
}

private fun onUploadLegacyReference(context: ArtifactUploadContext) {
with(context) {
val artifactInfo = context.artifactInfo as ReferenceArtifactInfo
val sha1 = context.request.getHeader(HEADER_NAME_HASH_SHA1)
?: throw BadRequestException(
CommonMessageCode.PARAMETER_INVALID, "Missing expected header $HEADER_NAME_HASH_SHA1"
)
val artifactFileSha1 = getArtifactSha1()
if (sha1.toLowerCase() != artifactFileSha1) {
throw BadRequestException(
CommonMessageCode.PARAMETER_INVALID,
"Incorrect hash, got hash \"${sha1}\" " +
"but hash of content was determined to be \"${artifactFileSha1}\""
)
}
val blobIdByteArray = getStreamArtifactFile().blake3()
val blobId = blobIdByteArray.hex()
val blobIdContentHash = ContentHash(blobIdByteArray)
// 创建blob
val blobFullPath = "/${DdcUtils.DIR_BLOBS}/$blobId"
val createRequest = buildBlobNodeCreateRequest(
blobId,
blobId,
artifactInfo,
blobFullPath,
getArtifactFile(),
context.userId
)
storageManager.storeArtifactFile(createRequest, getArtifactFile(), storageCredentials)
val blob = Blob(
projectId = projectId,
repoName = repoName,
sha256 = getArtifactSha256(),
fullPath = blobFullPath,
size = getArtifactFile().getSize(),
blobId = blobIdContentHash,
contentId = blobIdContentHash,
sha1 = artifactFileSha1,
)
blobService.create(blob)

// 创建Ref
val ref = referenceService.createLegacyReference(
Reference(
projectId = projectId,
repoName = repoName,
bucket = artifactInfo.bucket,
key = artifactInfo.refKey,
finalized = true,
blobId = blobIdContentHash,
legacy = true
)
)

blobService.addRefToBlobs(ref, setOf(blobId))
}
}

private fun onUploadReference(context: ArtifactUploadContext) {
val contentType = context.request.contentType
val artifactInfo = context.artifactInfo as ReferenceArtifactInfo
Expand Down Expand Up @@ -187,7 +254,15 @@ class DdcLocalRepository(
// TODO 改为读取流时直接计算blake3,避免重复读流
artifactInfo.compressedContentId = getStreamArtifactFile().blake3().hex()

storageManager.storeArtifactFile(buildBlobNodeCreateRequest(context), getArtifactFile(), storageCredentials)
val createRequest = buildBlobNodeCreateRequest(
artifactInfo.compressedContentId!!,
artifactInfo.contentId,
artifactInfo,
artifactInfo.getArtifactFullPath(),
getArtifactFile(),
userId
)
storageManager.storeArtifactFile(createRequest, getArtifactFile(), storageCredentials)
blobService.create(Blob.from(artifactInfo, getArtifactSha256(), getArtifactFile().getSize()))
HttpContextHolder
.getResponse()
Expand All @@ -196,30 +271,61 @@ class DdcLocalRepository(
}
}

private fun buildBlobNodeCreateRequest(context: ArtifactUploadContext): NodeCreateRequest {
val artifactInfo = context.artifactInfo as CompressedBlobArtifactInfo
private fun buildBlobNodeCreateRequest(
blobId: String,
contentId: String,
artifactInfo: ArtifactInfo,
fullPath: String,
artifactFile: ArtifactFile,
userId: String
): NodeCreateRequest {
val metadata = ArrayList<MetadataModel>()
metadata.add(
MetadataModel(
key = NODE_METADATA_KEY_BLOB_ID,
value = artifactInfo.compressedContentId!!,
value = blobId,
system = true
)
)
metadata.add(
MetadataModel(
key = NODE_METADATA_KEY_CONTENT_ID,
value = artifactInfo.contentId,
value = contentId,
system = true
)
)

return buildNodeCreateRequest(context).copy(
return NodeCreateRequest(
projectId = artifactInfo.projectId,
repoName = artifactInfo.repoName,
folder = false,
fullPath = fullPath,
size = artifactFile.getSize(),
sha256 = artifactFile.getFileSha256(),
md5 = artifactFile.getFileMd5(),
operator = userId,
overwrite = true,
nodeMetadata = metadata
nodeMetadata = metadata,
)
}

private fun onDownloadLegacyReference(context: ArtifactDownloadContext): ArtifactResource? {
with(context) {
val startTime = System.nanoTime()
val artifactInfo = context.artifactInfo as ReferenceArtifactInfo
val ref = referenceService.getLegacyReference(
artifactInfo.projectId, artifactInfo.repoName, artifactInfo.bucket, artifactInfo.refKey.toString()
) ?: return null

refDownloadListener.onRefDownloaded(RefDownloadedEvent(ref, SecurityUtils.getUserId()))
val blob = blobService.findBlob(ref.projectId, ref.repoName, ref.blobId.toString()) ?: return null
response.addHeader(HEADER_NAME_HASH, ref.blobId.toString())
response.addHeader(HEADER_NAME_HASH_SHA1, blob.sha1!!)
ddcMeterBinder.legacyRefLoadTimer.record(System.nanoTime() - startTime, NANOSECONDS)
return blobToArtifactResource(context, blob, MediaTypes.APPLICATION_OCTET_STREAM)
}
}

private fun onDownloadReference(context: ArtifactDownloadContext): ArtifactResource? {
with(context) {
val startTime = System.nanoTime()
Expand Down Expand Up @@ -325,6 +431,7 @@ class DdcLocalRepository(
val artifactInfo = context.artifactInfo as ReferenceArtifactInfo
val blobs = refResolver.getReferencedBlobs(context.projectId, context.repoName, cb)
if (blobs.size == 1) {
context.response.addHeader(HEADER_NAME_INLINE_PAYLOAD_HASH, blobs[0].toString())
blobToArtifactResource(context, blobs[0], responseType)
} else if (blobs.isEmpty()) {
null
Expand All @@ -347,7 +454,6 @@ class DdcLocalRepository(
responseType: String
): ArtifactResource? {
with(context) {
response.addHeader(HEADER_NAME_INLINE_PAYLOAD_HASH, blob.toString())
return try {
val blobInputStream = blobService.loadBlob(blob)
ArtifactResource(blobInputStream, artifactInfo.getResponseName()).apply { contentType = responseType }
Expand Down Expand Up @@ -376,6 +482,7 @@ class DdcLocalRepository(
private val logger = LoggerFactory.getLogger(DdcLocalRepository::class.java)
private val DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss")
const val HEADER_NAME_HASH = "X-Jupiter-IoHash"
private const val HEADER_NAME_HASH_SHA1 = "X-Jupiter-Sha1"
private const val HEADER_NAME_LAST_ACCESS = "X-Jupiter-LastAccess"
private const val HEADER_NAME_INLINE_PAYLOAD_HASH = "X-Jupiter-InlinePayloadHash"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.tencent.bkrepo.common.artifact.resolve.path.Resolver
import com.tencent.bkrepo.ddc.artifact.ReferenceArtifactInfo
import com.tencent.bkrepo.ddc.artifact.ReferenceArtifactInfo.Companion.PATH_VARIABLE_BUCKET
import com.tencent.bkrepo.ddc.artifact.ReferenceArtifactInfo.Companion.PATH_VARIABLE_REF_ID
import com.tencent.bkrepo.ddc.controller.LegacyReferencesController.Companion.LEGACY_PREFIX
import com.tencent.bkrepo.ddc.pojo.RefKey
import org.springframework.stereotype.Component
import org.springframework.web.servlet.HandlerMapping
Expand All @@ -46,12 +47,14 @@ class ReferenceArtifactInfoResolver : ArtifactInfoResolver {
artifactUri: String,
request: HttpServletRequest
): ReferenceArtifactInfo {
val legacy = request.requestURI.startsWith(LEGACY_PREFIX)
val attributes = request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE) as Map<*, *>
return ReferenceArtifactInfo(
projectId = projectId,
repoName = repoName,
bucket = attributes[PATH_VARIABLE_BUCKET].toString(),
refKey = RefKey.create(attributes[PATH_VARIABLE_REF_ID].toString())
refKey = RefKey.create(attributes[PATH_VARIABLE_REF_ID].toString(), legacy),
legacy = legacy
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class RefDownloadListener(

fun onRefDownloaded(event: RefDownloadedEvent) {
with(event.ref) {
cache.put(RefId(projectId, repoName, bucket, key.toString()), LocalDateTime.now())
cache.put(RefId(projectId, repoName, bucket, key.toString(), legacy), LocalDateTime.now())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ import com.tencent.bkrepo.common.api.constant.HttpStatus
import com.tencent.bkrepo.common.service.util.HttpContextHolder
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import com.tencent.bkrepo.ddc.controller.LegacyReferencesController.Companion.LEGACY_PREFIX

@RestController
class HealthController {
@GetMapping("/{projectId}/health/ready")
@GetMapping(
"/{projectId}/health/ready",
"$LEGACY_PREFIX{projectId}/health/ready"
)
fun ready() {
HttpContextHolder.getResponse().apply {
status = HttpStatus.OK.value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2023 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.ddc.controller

import com.tencent.bkrepo.auth.pojo.enums.PermissionAction
import com.tencent.bkrepo.auth.pojo.enums.ResourceType
import com.tencent.bkrepo.common.artifact.api.ArtifactFile
import com.tencent.bkrepo.common.artifact.api.ArtifactPathVariable
import com.tencent.bkrepo.common.security.permission.Permission
import com.tencent.bkrepo.ddc.artifact.ReferenceArtifactInfo
import com.tencent.bkrepo.ddc.artifact.ReferenceArtifactInfo.Companion.PATH_VARIABLE_BUCKET
import com.tencent.bkrepo.ddc.artifact.ReferenceArtifactInfo.Companion.PATH_VARIABLE_REF_ID
import com.tencent.bkrepo.ddc.controller.LegacyReferencesController.Companion.LEGACY_PREFIX
import com.tencent.bkrepo.ddc.service.ReferenceArtifactService
import com.tencent.bkrepo.ddc.utils.MEDIA_TYPE_UNREAL_COMPACT_BINARY
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("$LEGACY_PREFIX{projectId}/api/v1/c")
class LegacyReferencesController(
private val referenceArtifactService: ReferenceArtifactService,
) {
@ApiOperation("获取ref")
@Permission(ResourceType.REPO, PermissionAction.READ)
@GetMapping(
"/ddc/{repoName}/{${PATH_VARIABLE_BUCKET}}/{${PATH_VARIABLE_REF_ID}}.raw",
"/ddc/{repoName}/{${PATH_VARIABLE_BUCKET}}/{${PATH_VARIABLE_REF_ID}}",
produces = [
MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_OCTET_STREAM_VALUE,
MEDIA_TYPE_UNREAL_COMPACT_BINARY
]
)
fun get(
@ApiParam(value = "ddc ref", required = true)
@ArtifactPathVariable
artifactInfo: ReferenceArtifactInfo
) {
referenceArtifactService.downloadRef(artifactInfo)
}

@PutMapping(
"/ddc/{repoName}/{${PATH_VARIABLE_BUCKET}}/{${PATH_VARIABLE_REF_ID}}",
consumes = [MediaType.APPLICATION_OCTET_STREAM_VALUE]
)
@Permission(ResourceType.REPO, PermissionAction.WRITE)
fun put(
@ApiParam(value = "ddc ref", required = true)
@ArtifactPathVariable
artifactInfo: ReferenceArtifactInfo,
file: ArtifactFile
) {
referenceArtifactService.createRef(artifactInfo, file)
}

@DeleteMapping("/ddc/{repoName}/{${PATH_VARIABLE_BUCKET}}/{${PATH_VARIABLE_REF_ID}}")
@Permission(ResourceType.REPO, PermissionAction.WRITE)
fun delete(
@ApiParam(value = "ddc ref", required = true)
@ArtifactPathVariable
artifactInfo: ReferenceArtifactInfo
) {
referenceArtifactService.deleteRef(artifactInfo)
}

companion object {
const val LEGACY_PREFIX = "/legacy/"
}
}
Loading

0 comments on commit 710dd38

Please sign in to comment.