Skip to content

Commit

Permalink
Merge pull request #1304 from yaoxuwan/issue_1302
Browse files Browse the repository at this point in the history
feat: 缓存文件过期时间配置改为Duration #1302
  • Loading branch information
owenlxu authored Oct 19, 2023
2 parents bc93a58 + 06a9422 commit 7feda6e
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

package com.tencent.bkrepo.common.storage.config

import java.time.Duration

/**
* 本地文件缓存属性
*/
Expand All @@ -50,5 +52,10 @@ data class CacheProperties(
/**
* 缓存文件时间,单位天。小于或等于0则永久存储
*/
var expireDays: Int = -1
@Deprecated(message = "replace with expireDuration")
var expireDays: Int = -1,
/**
* 缓存文件时间,小于或等于0则永久存储
*/
var expireDuration: Duration = Duration.ofHours(12)
)
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ class CleanupFileVisitor(
) : ArtifactFileVisitor() {

val result = CleanupResult()
private val expireDays = credentials.cache.expireDays
private val expireDuration = credentials.cache.expireDuration
private val rateLimiter = RateLimiter.create(permitsPerSecond)

@Throws(IOException::class)
override fun visitFile(filePath: Path, attributes: BasicFileAttributes): FileVisitResult {
val size = attributes.size()
try {
if (isExpired(attributes, expireDays) && !isNFSTempFile(filePath)) {
if (isExpired(attributes, expireDuration) && !isNFSTempFile(filePath)) {
if (isTempFile(filePath) || existInStorage(filePath)) {
rateLimiter.acquire()
Files.delete(filePath)
Expand Down Expand Up @@ -106,17 +106,17 @@ class CleanupFileVisitor(
}

override fun needWalk(): Boolean {
return expireDays > 0
return expireDuration.seconds > 0
}

/**
* 判断文件是否过期
* 根据上次访问时间和上次修改时间判断
*/
private fun isExpired(attributes: BasicFileAttributes, expireDays: Int): Boolean {
private fun isExpired(attributes: BasicFileAttributes, expireDuration: Duration): Boolean {
val lastAccessTime = attributes.lastAccessTime().toMillis()
val lastModifiedTime = attributes.lastModifiedTime().toMillis()
val expiredTime = System.currentTimeMillis() - Duration.ofDays(expireDays.toLong()).toMillis()
val expiredTime = System.currentTimeMillis() - expireDuration.toMillis()
return lastAccessTime < expiredTime && lastModifiedTime < expiredTime
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.test.context.TestPropertySource
import org.springframework.test.context.junit.jupiter.SpringExtension
import java.time.Duration

@ExtendWith(SpringExtension::class)
@EnableConfigurationProperties(value = [StorageProperties::class])
Expand All @@ -65,7 +66,7 @@ internal class StorageCredentialsTest {
Assertions.assertEquals(1.0F, storageProperties.innercos.timeout)
Assertions.assertTrue(storageProperties.innercos.cache.enabled)
Assertions.assertEquals("/data/cached", storageProperties.innercos.cache.path)
Assertions.assertEquals(10, storageProperties.innercos.cache.expireDays)
Assertions.assertEquals(Duration.ofDays(1), storageProperties.innercos.cache.expireDuration)
Assertions.assertEquals("/data/temp", storageProperties.innercos.upload.location)
}

Expand All @@ -75,7 +76,7 @@ internal class StorageCredentialsTest {
originalCredentials.path = "test"
originalCredentials.cache.enabled = true
originalCredentials.cache.path = "cache-test"
originalCredentials.cache.expireDays = 10
originalCredentials.cache.expireDuration = Duration.ofDays(1)
val jsonString = originalCredentials.toJsonString()

val serializedCredentials = jsonString.readJsonString<StorageCredentials>()
Expand All @@ -84,7 +85,7 @@ internal class StorageCredentialsTest {
Assertions.assertEquals(originalCredentials.path, serializedCredentials.path)
Assertions.assertEquals(originalCredentials.cache.enabled, serializedCredentials.cache.enabled)
Assertions.assertEquals(originalCredentials.cache.path, serializedCredentials.cache.path)
Assertions.assertEquals(originalCredentials.cache.expireDays, serializedCredentials.cache.expireDays)
Assertions.assertEquals(originalCredentials.cache.expireDuration, serializedCredentials.cache.expireDuration)
Assertions.assertEquals(originalCredentials.upload.location, serializedCredentials.upload.location)
Assertions.assertEquals(jsonString, serializedCredentials.toJsonString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ storage.innercos.cmdId=2002
storage.innercos.timeout=1.0
storage.innercos.cache.enabled=true
storage.innercos.cache.path=/data/cached
storage.innercos.cache.expireDays=10
storage.innercos.cache.expireDuration=24h
storage.innercos.upload.location=/data/temp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class StorageCredentialServiceImpl(
storageCredentials.apply {
cache = cache.copy(
loadCacheFirst = request.credentials.cache.loadCacheFirst,
expireDays = request.credentials.cache.expireDays
expireDays = request.credentials.cache.expireDays,
expireDuration = request.credentials.cache.expireDuration
)
upload = upload.copy(localPath = request.credentials.upload.localPath)
credentialUpdaters[StorageCredentialsUpdater.name(this::class.java)]?.update(this, request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest
import org.springframework.boot.test.mock.mockito.MockBean
import java.time.Duration

@DisplayName("仓库服务测试")
@DataMongoTest
Expand All @@ -93,7 +94,7 @@ class RepositoryServiceTest @Autowired constructor(
path = "test"
cache.enabled = true
cache.path = "cache-test"
cache.expireDays = 10
cache.expireDuration = Duration.ofHours(10)
}

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import org.junit.jupiter.api.assertThrows
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest
import org.springframework.context.annotation.Import
import java.time.Duration

@Import(NodeDao::class, FileReferenceDao::class)
@DisplayName("存储身份凭证服务测试")
Expand All @@ -83,7 +84,7 @@ internal class StorageCredentialServiceTest @Autowired constructor(
assertEquals(credential.path, dbCredentials.path)
assertEquals(credential.cache.enabled, dbCredentials.cache.enabled)
assertEquals(credential.cache.path, dbCredentials.cache.path)
assertEquals(credential.cache.expireDays, dbCredentials.cache.expireDays)
assertEquals(credential.cache.expireDuration, dbCredentials.cache.expireDuration)

assertThrows<ErrorCodeException> {
createCredential()
Expand All @@ -108,29 +109,29 @@ internal class StorageCredentialServiceTest @Autowired constructor(
fun testUpdateCredential() {
val storageCredentials = createCredential()
assertEquals(true, storageCredentials.cache.loadCacheFirst)
assertEquals(10, storageCredentials.cache.expireDays)
assertEquals(Duration.ofHours(10), storageCredentials.cache.expireDuration)
assertEquals(UT_STORAGE_CREDENTIALS_KEY, storageCredentials.key)

var updateCredentialsPayload = storageCredentials.apply {
cache = cache.copy(loadCacheFirst = false, expireDays = -1)
cache = cache.copy(loadCacheFirst = false, expireDuration = Duration.ZERO)
}
var updateReq = StorageCredentialsUpdateRequest(updateCredentialsPayload, UT_STORAGE_CREDENTIALS_KEY)
var updatedStorageCredentials = storageCredentialService.update(UT_USER, updateReq)
assertEquals(false, updatedStorageCredentials.cache.loadCacheFirst)
assertEquals(-1, updatedStorageCredentials.cache.expireDays)
assertEquals(Duration.ZERO, updatedStorageCredentials.cache.expireDuration)
assertEquals(storageCredentials.upload.localPath, updatedStorageCredentials.upload.localPath)
assertEquals(UT_STORAGE_CREDENTIALS_KEY, updatedStorageCredentials.key)

val localPath = "/test"
updateCredentialsPayload = storageCredentials.apply {
cache = cache.copy(loadCacheFirst = true, expireDays = 10)
cache = cache.copy(loadCacheFirst = true, expireDuration = Duration.ofHours(10))
upload = upload.copy(localPath = localPath)
}
updateReq = StorageCredentialsUpdateRequest(updateCredentialsPayload, UT_STORAGE_CREDENTIALS_KEY)
updatedStorageCredentials = storageCredentialService.update(UT_USER, updateReq)
assertEquals(localPath, updatedStorageCredentials.upload.localPath)
assertEquals(true, updatedStorageCredentials.cache.loadCacheFirst)
assertEquals(10, updatedStorageCredentials.cache.expireDays)
assertEquals(Duration.ofHours(10), updatedStorageCredentials.cache.expireDuration)
assertEquals(localPath, updatedStorageCredentials.upload.localPath)
assertEquals(UT_STORAGE_CREDENTIALS_KEY, updatedStorageCredentials.key)
}
Expand Down Expand Up @@ -216,7 +217,7 @@ internal class StorageCredentialServiceTest @Autowired constructor(
credential.apply {
cache.enabled = true
cache.path = "cache-test"
cache.expireDays = 10
cache.expireDuration = Duration.ofHours(10)
cache.loadCacheFirst = true
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/devops-op/src/api/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ export function updateCredential(key, credential, defaultCredential = false) {
if (defaultCredential) {
const prefix = `${STORAGE_CONFIG_PREFIX}.${credential.type}`
const expireDaysKey = `${prefix}.${STORAGE_CACHE_CONFIG_PREFIX}.expireDays`
const expireDurationKey = `${prefix}.${STORAGE_CACHE_CONFIG_PREFIX}.expireDuration`
const loadCacheFirstKey = `${prefix}.${STORAGE_CACHE_CONFIG_PREFIX}.loadCacheFirst`
const values = [
{
'key': expireDaysKey,
'value': credential.cache.expireDays
},
{
'key': expireDurationKey,
'value': credential.cache.expireDuration * 1000 // consul上Duration不带单位时默认是毫秒
},
{
'key': loadCacheFirstKey,
'value': credential.cache.loadCacheFirst
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/devops-op/src/views/job/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
width="100"
>
<template slot-scope="scope">
{{scope.row.running ? "":""}}
{{ scope.row.running ? "":"" }}
</template>
</el-table-column>
<el-table-column
Expand Down
20 changes: 19 additions & 1 deletion src/frontend/devops-op/src/views/storage/Credential.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@
</template>
</el-table-column>
<el-table-column
label="缓存天数"
label="缓存天数(已废弃)"
width="180"
>
<template v-if="scope.row.cache.enabled" slot-scope="scope">
<span>{{ expireDays(scope.row.cache.expireDays) }}</span>
</template>
</el-table-column>
<el-table-column
label="缓存时间"
width="180"
>
<template v-if="scope.row.cache.enabled" slot-scope="scope">
<span>{{ formatSeconds(scope.row.cache.expireDuration) }}</span>
</template>
</el-table-column>
<el-table-column
label="优先读缓存"
width="180"
Expand Down Expand Up @@ -142,6 +150,16 @@ export default {
},
expireDays(expireDays) {
return parseInt(expireDays) <= 0 ? '永久' : expireDays
},
formatSeconds(seconds) {
if (seconds <= 0) {
return '永久'
}
const hours = Math.floor(seconds / 3600)
const minutes = Math.floor((seconds % 3600) / 60)
const remainingSeconds = seconds % 60
return `${hours}小时 ${minutes}分钟 ${remainingSeconds}`
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,12 @@
<el-form-item v-if="credential.cache.enabled" label="优先加载缓存" prop="cache.loadCacheFirst">
<el-switch v-model="credential.cache.loadCacheFirst" />
</el-form-item>
<el-form-item v-if="credential.cache.enabled" label="缓存天数" prop="cache.expireDays">
<el-form-item v-if="credential.cache.enabled" label="缓存天数(已废弃)" prop="cache.expireDays">
<el-input-number v-model="credential.cache.expireDays" controls-position="right" :min="0" :max="30" />
</el-form-item>
<el-form-item v-if="credential.cache.enabled" label="缓存时间(秒)" prop="cache.expireDuration">
<el-input-number v-model="credential.cache.expireDuration" controls-position="right" :min="0" />
</el-form-item>
</el-form>
<div slot="footer">
<el-button @click="close">取 消</el-button>
Expand Down

0 comments on commit 7feda6e

Please sign in to comment.