Skip to content

Commit

Permalink
Global Event Listener (#259)
Browse files Browse the repository at this point in the history
* Revert Kotlin version to 2.0.10 due to publishing issues

New Features:
* Expose JsonPath API
* Add hasContext method to Task.kt
* eventListener are now global, with auto-refresh

Breaking Changes:
* Remove default values for storage. Previously default values were set for local developement. we think now it can be misleading when going to production.
* Normalize setters on all config builders
* Refactor field names for consistency in PoliciesConfig
* Updated field names to use consistent terminology by removing "In" and improving readability. (timeoutSeconds, shutdownGracePeriodSeconds, retentionTimeMinutes, retentionSizeMB, messageTTLSeconds, etc..)
* Change 'user' to 'username' in MySQLConfig, PostgresConfig and RedisConfig
* Transport definition in config
* Update InfiniticClient and InfiniticWorker constructor, to only take a config object
* Update the 'from***' static method for Clients and Workers

Improvements:
* More reliable client deletion when topic is closing
  • Loading branch information
geomagilles authored Sep 11, 2024
1 parent 9990a61 commit bc3945d
Show file tree
Hide file tree
Showing 218 changed files with 8,182 additions and 5,799 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "kotlin",
"request": "launch",
"name": "Kotlin Launch",
"projectRoot": "${workspaceFolder}",
"mainClass": "path.to.your.MainClassKt"
}
]
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Ci.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object Ci {
private const val SNAPSHOT = "-SNAPSHOT"

// base version number
private const val BASE = "0.15.1"
private const val BASE = "0.16.0"

// GitHub run number
private val githubRunNumber = System.getenv("GITHUB_RUN_NUMBER")
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Plugins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* Licensor: infinitic.io
*/
const val kotlinVersion = "2.0.20"
const val kotlinVersion = "2.0.0"

@Suppress("ConstPropertyName")
object Plugins {
Expand Down
2 changes: 2 additions & 0 deletions infinitic-cache/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* Licensor: infinitic.io
*/
dependencies {
implementation(project(":infinitic-utils"))

implementation(Libs.Caffeine.caffeine)

testImplementation(Libs.Hoplite.yaml)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ package io.infinitic.cache.caches.caffeine
import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import io.infinitic.cache.Flushable
import io.infinitic.cache.config.CaffeineConfig
import io.infinitic.cache.config.CaffeineCacheConfig
import io.infinitic.cache.data.Bytes
import io.infinitic.cache.keySet.CachedKeySet

internal class CaffeineCachedKeySet(config: CaffeineConfig) : CachedKeySet<ByteArray>, Flushable {
internal class CaffeineCachedKeySet(config: CaffeineCacheConfig) : CachedKeySet<ByteArray>,
Flushable {

private val caffeine: Cache<String, Set<Bytes>> = Caffeine.newBuilder().setup(config).build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ package io.infinitic.cache.caches.caffeine
import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import io.infinitic.cache.Flushable
import io.infinitic.cache.config.CaffeineConfig
import io.infinitic.cache.config.CaffeineCacheConfig
import io.infinitic.cache.keyValue.CachedKeyValue

internal class CaffeineCachedKeyValue<S : Any>(config: CaffeineConfig) : CachedKeyValue<S>,
internal class CaffeineCachedKeyValue<S : Any>(config: CaffeineCacheConfig) : CachedKeyValue<S>,
Flushable {
private var caffeine: Cache<String, S> = Caffeine.newBuilder().setup(config).build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ package io.infinitic.cache.caches.caffeine
import com.github.benmanes.caffeine.cache.RemovalCause
import io.github.oshai.kotlinlogging.KotlinLogging
import io.infinitic.cache.config.CacheConfig
import io.infinitic.cache.config.CaffeineConfig
import io.infinitic.cache.config.CaffeineCacheConfig
import java.util.concurrent.TimeUnit
import com.github.benmanes.caffeine.cache.Caffeine as CaffeineCache

internal val logger = KotlinLogging.logger(CacheConfig::class.java.name)

internal fun <S, T> CaffeineCache<S, T>.setup(config: CaffeineConfig): CaffeineCache<S, T> {
internal fun <S, T> CaffeineCache<S, T>.setup(config: CaffeineCacheConfig): CaffeineCache<S, T> {

config.maximumSize?.let { maximumSize(it) }
config.expireAfterAccess?.let { expireAfterAccess(it, TimeUnit.SECONDS) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,36 @@
*/
package io.infinitic.cache.config

import io.infinitic.cache.caches.caffeine.CaffeineCachedKeySet
import io.infinitic.cache.caches.caffeine.CaffeineCachedKeyValue
import io.infinitic.cache.config.CaffeineCacheConfig.CaffeineConfigBuilder
import io.infinitic.cache.keySet.CachedKeySet
import io.infinitic.cache.keyValue.CachedKeyValue
import io.infinitic.config.loadFromYamlFile
import io.infinitic.config.loadFromYamlResource
import io.infinitic.config.loadFromYamlString

data class CacheConfig(
internal val caffeine: CaffeineConfig? = null
) {
sealed interface CacheConfig {
val keySet: CachedKeySet<ByteArray>?
val keyValue: CachedKeyValue<ByteArray>?
val type: String

companion object {
@JvmStatic
fun from(caffeineConfig: CaffeineConfig) = CacheConfig(caffeine = caffeineConfig)
}

val type: CacheType by lazy {
when {
caffeine != null -> CacheType.CAFFEINE
else -> CacheType.NONE
}
}
fun builder() = CaffeineConfigBuilder()

val keySet: CachedKeySet<ByteArray>? by lazy {
when {
caffeine != null -> CaffeineCachedKeySet(caffeine)
else -> null
}
}
/** Create CacheConfig from files in file system */
@JvmStatic
fun fromYamlFile(vararg files: String): CacheConfig =
loadFromYamlFile(*files)

val keyValue: CachedKeyValue<ByteArray>? by lazy {
when {
caffeine != null -> CaffeineCachedKeyValue(caffeine)
else -> null
}
}
/** Create CacheConfig from files in resources directory */
@JvmStatic
fun fromYamlResource(vararg resources: String): CacheConfig =
loadFromYamlResource(*resources)

enum class CacheType {
NONE,
CAFFEINE
/** Create CacheConfig from yaml strings */
@JvmStatic
fun fromYamlString(vararg yamls: String): CacheConfig =
loadFromYamlString(*yamls)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@
*/
package io.infinitic.cache.config

import io.infinitic.cache.caches.caffeine.CaffeineCachedKeySet
import io.infinitic.cache.caches.caffeine.CaffeineCachedKeyValue
import io.infinitic.cache.keySet.CachedKeySet
import io.infinitic.cache.keyValue.CachedKeyValue

@Suppress("unused")
data class CaffeineConfig(
data class CaffeineCacheConfig(
val maximumSize: Long? = 10000, // 10k units
val expireAfterAccess: Long? = 3600, // 1 hour
val expireAfterWrite: Long? = 3600 // 1 hour
) {
) : CacheConfig {

override val type = "caffeine"

override val keySet: CachedKeySet<ByteArray> by lazy { CaffeineCachedKeySet(this) }

override val keyValue: CachedKeyValue<ByteArray> by lazy { CaffeineCachedKeyValue(this) }


init {
maximumSize?.let { require(it > 0) { "maximumSize MUST be > 0" } }
expireAfterAccess?.let { require(it >= 0) { "expireAfterAccess MUST be >= 0" } }
Expand All @@ -43,7 +56,7 @@ data class CaffeineConfig(
* Caffeine builder (Useful for Java user)
*/
class CaffeineConfigBuilder {
private val default = CaffeineConfig()
private val default = CaffeineCacheConfig()
private var maximumSize = default.maximumSize
private var expireAfterAccess = default.expireAfterAccess
private var expireAfterWrite = default.expireAfterWrite
Expand All @@ -52,7 +65,7 @@ data class CaffeineConfig(
fun setExpireAfterAccess(expAftAccess: Long) = apply { this.expireAfterAccess = expAftAccess }
fun setExpireAfterWrite(expAftWrite: Long) = apply { this.expireAfterWrite = expAftWrite }

fun build() = CaffeineConfig(
fun build() = CaffeineCacheConfig(
maximumSize = maximumSize,
expireAfterAccess = expireAfterAccess,
expireAfterWrite = expireAfterWrite,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
*/
package io.infinitic.cache.caches.caffeine

import io.infinitic.cache.config.CaffeineConfig
import io.infinitic.cache.config.CaffeineCacheConfig
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class CaffeineCachedKeySetTests :
StringSpec(
{
val storage = CaffeineCachedKeySet(CaffeineConfig())
val storage = CaffeineCachedKeySet(CaffeineCacheConfig())

beforeTest { storage.set("key", setOf("foo".toByteArray(), "bar".toByteArray())) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
*/
package io.infinitic.cache.caches.caffeine

import io.infinitic.cache.config.CaffeineConfig
import io.infinitic.cache.config.CaffeineCacheConfig
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class CaffeineCachedKeyValueTests :
StringSpec(
{
val storage = CaffeineCachedKeyValue<ByteArray>(CaffeineConfig())
val storage = CaffeineCachedKeyValue<ByteArray>(CaffeineCacheConfig())

val known = "foo"
val unknown = "bar"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
package io.infinitic.cache.config

internal data class CacheConfigImpl(
override val cache: CacheConfig = CacheConfig()
override val cache: CacheConfig? = null
) : CacheConfigInterface
Original file line number Diff line number Diff line change
Expand Up @@ -35,57 +35,40 @@ class CacheConfigTests :
"default cache should be None" {
val config = loadConfigFromYaml<CacheConfigImpl>("nothing:")

config shouldBe CacheConfigImpl(cache = CacheConfig())
config.cache.type shouldBe CacheConfig.CacheType.NONE
config.cache.keyValue shouldBe null
config.cache.keySet shouldBe null
config shouldBe CacheConfigImpl(cache = null)
}

"cache without type should be None" {
val config = loadConfigFromYaml<CacheConfigImpl>("cache:")
"can set null cache " {
val config = loadConfigFromYaml<CacheConfigImpl>("cache: null")

config shouldBe CacheConfigImpl(cache = CacheConfig())
config.cache.type shouldBe CacheConfig.CacheType.NONE
config.cache.keyValue shouldBe null
config.cache.keySet shouldBe null
config shouldBe CacheConfigImpl(cache = null)
}

"can choose Caffeine cache" {
val config = loadConfigFromYaml<CacheConfigImpl>(
"""
cache:
caffeine:
""",
)
config shouldBe CacheConfigImpl(cache = CacheConfig(caffeine = CaffeineConfig()))
config.cache.type shouldBe CacheConfig.CacheType.CAFFEINE
config.cache.keyValue!!::class shouldBe CaffeineCachedKeyValue::class
"default cache should be Caffeine" {
val config = loadConfigFromYaml<CacheConfigImpl>("cache:")

config shouldBe CacheConfigImpl(cache = CaffeineCacheConfig())
config.cache?.keyValue!!::class shouldBe CaffeineCachedKeyValue::class
config.cache.keySet!!::class shouldBe CaffeineCachedKeySet::class
}

"can choose Caffeine cache with correct values" {
val config = loadConfigFromYaml<CacheConfigImpl>(
"""
cache:
caffeine:
maximumSize: 100
expireAfterAccess: 42
expireAfterWrite: 64
maximumSize: 100
expireAfterAccess: 42
expireAfterWrite: 64
""",
)

config shouldBe CacheConfigImpl(
cache = CacheConfig(
caffeine = CaffeineConfig(
100,
42,
64,
),
cache = CaffeineCacheConfig(
100,
42,
64,
),
)
config.cache.type shouldBe CacheConfig.CacheType.CAFFEINE
config.cache.keyValue!!::class shouldBe CaffeineCachedKeyValue::class
config.cache.keySet!!::class shouldBe CaffeineCachedKeySet::class
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import io.kotest.matchers.shouldBe
class CaffeineConfigTests : StringSpec(
{
"Can create CaffeineConfig through builder" {
val caffeineConfig = CaffeineConfig
val caffeineCacheConfig = CaffeineCacheConfig
.builder()
.build()

caffeineConfig shouldBe CaffeineConfig()
caffeineCacheConfig shouldBe CaffeineCacheConfig()
}
},
)
Loading

0 comments on commit bc3945d

Please sign in to comment.