-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Proxy支持删除未同步的文件存储 #1836 * feat: Proxy支持删除未同步的文件存储 #1836 * feat: Proxy支持删除未同步的文件存储 #1836 * feat: Proxy支持删除未同步的文件存储 #1836 * feat: Proxy支持删除未同步的文件存储 #1836 * feat: Proxy支持删除未同步的文件存储 #1836
- Loading branch information
Showing
12 changed files
with
410 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...itory/src/main/kotlin/com/tencent/bkrepo/repository/api/proxy/ProxyFileReferenceClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.repository.api.proxy | ||
|
||
import com.tencent.bkrepo.common.api.constant.REPOSITORY_SERVICE_NAME | ||
import com.tencent.bkrepo.common.api.pojo.Response | ||
import io.swagger.annotations.Api | ||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RequestParam | ||
|
||
@Api("文件引用服务接口") | ||
@FeignClient(REPOSITORY_SERVICE_NAME, contextId = "ProxyFileReferenceClient", primary = false) | ||
@RequestMapping("/proxy/fileReference") | ||
interface ProxyFileReferenceClient { | ||
|
||
@GetMapping("/count") | ||
fun count(@RequestParam sha256: String, @RequestParam credentialsKey: String?): Response<Long> | ||
} |
43 changes: 43 additions & 0 deletions
43
...ain/kotlin/com/tencent/bkrepo/repository/controller/proxy/ProxyFileReferenceController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.repository.controller.proxy | ||
|
||
import com.tencent.bkrepo.common.api.pojo.Response | ||
import com.tencent.bkrepo.common.service.util.ResponseBuilder | ||
import com.tencent.bkrepo.repository.api.proxy.ProxyFileReferenceClient | ||
import com.tencent.bkrepo.repository.service.file.FileReferenceService | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
class ProxyFileReferenceController( | ||
private val fileReferenceService: FileReferenceService | ||
) : ProxyFileReferenceClient { | ||
override fun count(sha256: String, credentialsKey: String?): Response<Long> { | ||
return ResponseBuilder.success(fileReferenceService.count(sha256, credentialsKey)) | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...-proxy/src/main/kotlin/com/tencent/bkrepo/proxy/artifact/storage/ProxyCleanFileVisitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.proxy.artifact.storage | ||
|
||
import com.tencent.bkrepo.common.service.exception.RemoteErrorCodeException | ||
import com.tencent.bkrepo.common.storage.filesystem.ArtifactFileVisitor | ||
import com.tencent.bkrepo.repository.api.proxy.ProxyFileReferenceClient | ||
import org.slf4j.LoggerFactory | ||
import java.io.File | ||
import java.nio.file.FileVisitResult | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.attribute.BasicFileAttributes | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* 不同步时,清理本地存储的文件遍历器 | ||
*/ | ||
class ProxyCleanFileVisitor( | ||
private val proxyFileReferenceClient: ProxyFileReferenceClient, | ||
private val cacheExpireDays: Int | ||
) : ArtifactFileVisitor() { | ||
override fun needWalk(): Boolean { | ||
return true | ||
} | ||
|
||
override fun visitFile(filePath: Path, attrs: BasicFileAttributes?): FileVisitResult { | ||
try { | ||
if (filePath.toString().endsWith(".sync")) { | ||
val syncFile = filePath.toFile() | ||
val dataFile = File(filePath.toString().removeSuffix(".sync")) | ||
cleanFile(dataFile, syncFile) | ||
} else { | ||
ProxyStorageUtils.deleteCacheFile(filePath, cacheExpireDays) | ||
} | ||
} catch (e: Exception) { | ||
logger.error("clean file error: ", e) | ||
} | ||
return FileVisitResult.CONTINUE | ||
} | ||
|
||
/** | ||
* 当文件索引为0时,删除本地存储 | ||
*/ | ||
private fun cleanFile(dataFile: File, syncFile: File) { | ||
val attrs = Files.readAttributes(dataFile.toPath(), BasicFileAttributes::class.java) | ||
val creationTime = attrs.creationTime().toMillis() | ||
// 防止刚存储,未创建文件引用时被误删 | ||
if (System.currentTimeMillis() - creationTime < TimeUnit.MINUTES.toMillis(1)) { | ||
return | ||
} | ||
val sha256 = dataFile.name | ||
val credentialKeys = ProxyStorageUtils.readStorageCredentialKeys(syncFile) | ||
try { | ||
credentialKeys.forEach { | ||
val fileReference = proxyFileReferenceClient.count(sha256, it).data!! | ||
if (fileReference > 0) { | ||
logger.info("skip clean file[$sha256] which file reference is $fileReference on storage[$it]") | ||
return | ||
} | ||
} | ||
} catch (e: RemoteErrorCodeException) { | ||
logger.warn("get sha256[$sha256] file reference failed: ${e.errorMessage}") | ||
return | ||
} | ||
dataFile.delete() | ||
syncFile.delete() | ||
logger.info("clean file[$sha256] success") | ||
} | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(ProxyCleanFileVisitor::class.java) | ||
} | ||
} |
Oops, something went wrong.