-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into dev-version-2
- Loading branch information
Showing
10 changed files
with
287 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
kotlin.code.style=official | ||
kotlin_version=1.8.22 | ||
release_version=1.0.0 | ||
release_version=1.1.0 | ||
|
||
description = 'th2 data processor for Zephyr synchronization' | ||
vcs_url=https://github.com/th2-net/th2-data-processor-zephyr.git |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/exactpro/th2/dataprocessor/zephyr/cfg/CachesConfiguration.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,33 @@ | ||
/* | ||
* Copyright 2020-2024 Exactpro (Exactpro Systems Limited) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.exactpro.th2.dataprocessor.zephyr.cfg | ||
|
||
import java.time.LocalTime | ||
import java.util.concurrent.TimeUnit | ||
|
||
class CachesConfiguration( | ||
val cycles: CacheConfiguration = CacheConfiguration( | ||
expireAfterSeconds = TimeUnit.DAYS.toSeconds(1), | ||
size = 100, | ||
) | ||
) | ||
|
||
class CacheConfiguration( | ||
val expireAfterSeconds: Long, | ||
val size: Int = 100, | ||
val invalidateAt: LocalTime? = null, | ||
) |
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
86 changes: 86 additions & 0 deletions
86
src/main/kotlin/com/exactpro/th2/dataprocessor/zephyr/impl/cache/LRUCache.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,86 @@ | ||
/* | ||
* Copyright 2020-2024 Exactpro (Exactpro Systems Limited) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.exactpro.th2.dataprocessor.zephyr.impl.cache | ||
|
||
import mu.KotlinLogging | ||
import org.apache.commons.collections4.map.LRUMap | ||
import java.time.Clock | ||
import java.time.Duration | ||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalTime | ||
import java.time.ZoneOffset | ||
import java.time.temporal.ChronoUnit | ||
|
||
internal class LRUCache<K, V>( | ||
size: Int, | ||
private val expireAfterMillis: Long, | ||
private val timeSource: Clock = Clock.systemUTC(), | ||
invalidateAt: LocalTime? = null, | ||
) { | ||
@Volatile | ||
private var nextInvalidateAt: Instant | ||
init { | ||
require(size > 0) { "size must be positive but was $size" } | ||
require(expireAfterMillis > 0) { "expireAfterMillis must be positive but was $expireAfterMillis" } | ||
nextInvalidateAt = if (invalidateAt == null) { | ||
Instant.MAX | ||
} else { | ||
val now = timeSource.instant() | ||
val next = invalidateAt.atDate(LocalDate.ofInstant(now, ZoneOffset.UTC)) | ||
.toInstant(ZoneOffset.UTC) | ||
if (next <= now) { | ||
next.plus(1, ChronoUnit.DAYS) | ||
} else { | ||
next | ||
} | ||
} | ||
} | ||
private class TimestampedValue<T>(val value: T, val createdAt: Instant) | ||
|
||
private val cache = LRUMap<K, TimestampedValue<V>>(size) | ||
|
||
operator fun get(key: K): V? { | ||
val now = timeSource.instant() | ||
if (now >= nextInvalidateAt) { | ||
invalidateAll() | ||
} | ||
return cache[key]?.takeUnless { | ||
Duration.between(it.createdAt, now).toMillis() > expireAfterMillis | ||
}?.value | ||
} | ||
|
||
operator fun set(key: K, value: V) { | ||
cache[key] = TimestampedValue(value, timeSource.instant()) | ||
} | ||
|
||
private fun invalidateAll() { | ||
cache.clear() | ||
val currentInvalidateAt = nextInvalidateAt | ||
var next: Instant = nextInvalidateAt | ||
val now = timeSource.instant() | ||
do { | ||
next = next.plus(1, ChronoUnit.DAYS) | ||
} while (next < now) | ||
nextInvalidateAt = next | ||
LOGGER.info { "Cache invalidated. Now: $now, Invalidate at: $currentInvalidateAt, Next invalidate at: $nextInvalidateAt" } | ||
} | ||
|
||
private companion object { | ||
private val LOGGER = KotlinLogging.logger { } | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
src/test/kotlin/com/exactpro/th2/dataprocessor/zephyr/impl/cache/LRUCacheTest.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,78 @@ | ||
/* | ||
* Copyright 2020-2024 Exactpro (Exactpro Systems Limited) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.exactpro.th2.dataprocessor.zephyr.impl.cache | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertNull | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.kotlin.doReturn | ||
import org.mockito.kotlin.doReturnConsecutively | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
import java.time.Clock | ||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalTime | ||
import java.time.ZoneOffset | ||
import java.time.temporal.ChronoUnit | ||
|
||
class LRUCacheTest { | ||
@Test | ||
fun `returns null if not value`() { | ||
val cache = createCache() | ||
assertNull(cache["key"], "unexpected value") | ||
} | ||
|
||
@Test | ||
fun `returns value if not expired yet`() { | ||
val cache = createCache() | ||
cache["key"] = 42 | ||
assertEquals(42, cache["key"], "unexpected value") | ||
} | ||
|
||
@Test | ||
fun `invalidates all keys after specified time`() { | ||
val invalidateAt = LocalTime.of(12, 0,0) | ||
val time = invalidateAt.atDate(LocalDate.now()).toInstant(ZoneOffset.UTC).plusSeconds(42) | ||
val timeSource = mock<Clock>() | ||
whenever(timeSource.instant()) doReturn time | ||
val cache = LRUCache<String, Int>(size = 10, expireAfterMillis = 1000, timeSource, invalidateAt) | ||
cache["key"] = 42 | ||
assertEquals(42, cache["key"], "unexpected value") | ||
// next day | ||
whenever(timeSource.instant()) doReturnConsecutively listOf(time.plus(1, ChronoUnit.DAYS)) | ||
|
||
assertNull(cache["key"], "unexpected value") | ||
} | ||
|
||
@Test | ||
fun `does not return value if it is expired`() { | ||
val expireAfterMillis: Long = 1000 | ||
val time = Instant.now() | ||
val timeSource = mock<Clock> { | ||
on { instant() } doReturn time doReturn time.plusMillis(expireAfterMillis + 1) | ||
} | ||
val cache = LRUCache<String, Int>(size = 10, expireAfterMillis = expireAfterMillis, timeSource) | ||
cache["key"] = 42 | ||
assertNull(cache["key"], "value should be expired") | ||
} | ||
|
||
private fun createCache(size: Int = 10, expireAfterMillis: Long = 1000): LRUCache<String, Int> { | ||
val cache = LRUCache<String, Int>(size = size, expireAfterMillis = expireAfterMillis) | ||
return cache | ||
} | ||
} |