Skip to content

Commit 886221a

Browse files
committed
refactor(maven): Migrate from DiskLruCache to Kottage
The DiskLruCache has not seen any updates in at least 9 years, and has several shortcoming that ORT's `DiskCache` class wrapped away. Reduce the amount of custom code by migrating to a modern multi-platform solution called Kottage [2] that uses coroutines and does not have these known limitations. Its SQLite backend allows to inspect the cache easily with tools like DB4S [3]. [1]: https://github.com/JakeWharton/DiskLruCache [2]: https://github.com/irgaly/kottage [3]: https://sqlitebrowser.org/ Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
1 parent f7aa322 commit 886221a

File tree

5 files changed

+28
-167
lines changed

5 files changed

+28
-167
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ clikt = "5.0.3"
2121
commonsCompress = "1.28.0"
2222
cyclonedx = "11.0.1"
2323
diffUtils = "4.16"
24-
diskLruCache = "2.0.2"
2524
exposed = "0.61.0"
2625
flexmark = "0.64.8"
2726
freemarker = "2.3.34"
@@ -43,6 +42,7 @@ kotlinPoet = "2.2.0"
4342
kotlinxCoroutines = "1.10.2"
4443
kotlinxHtml = "0.12.0"
4544
kotlinxSerialization = "1.9.0"
45+
kottage = "1.9.0"
4646
ks3 = "1.0.0"
4747
tomlkt = "0.5.0"
4848
log4jApi = "2.25.2"
@@ -101,7 +101,6 @@ cyclonedx = { module = "org.cyclonedx:cyclonedx-core-java", version.ref = "cyclo
101101
detekt-api = { module = "io.gitlab.arturbosch.detekt:detekt-api", version.ref = "detektPlugin" }
102102
detekt-test = { module = "io.gitlab.arturbosch.detekt:detekt-test", version.ref = "detektPlugin" }
103103
diffUtils = { module = "io.github.java-diff-utils:java-diff-utils", version.ref = "diffUtils" }
104-
diskLruCache = { module = "com.jakewharton:disklrucache", version.ref = "diskLruCache" }
105104
exposed-core = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" }
106105
exposed-dao = { module = "org.jetbrains.exposed:exposed-dao", version.ref = "exposed" }
107106
exposed-javaTime = { module = "org.jetbrains.exposed:exposed-java-time", version.ref = "exposed" }
@@ -143,6 +142,7 @@ kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serializa
143142
kotlinx-serialization-toml = { module = "net.peanuuutz.tomlkt:tomlkt", version.ref = "tomlkt" }
144143
kotlinx-serialization-xml = { module = "io.github.pdvrieze.xmlutil:serialization", version.ref = "xmlutil" }
145144
kotlinx-serialization-yaml = { module = "com.charleskorn.kaml:kaml", version.ref = "kaml" }
145+
kottage = { module = "io.github.irgaly.kottage:kottage", version.ref = "kottage" }
146146
ks3-jdk = { module = "io.ks3:ks3-jdk", version.ref = "ks3" }
147147
ks3-standard = { module = "io.ks3:ks3-standard", version.ref = "ks3" }
148148
ksp = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp"}

plugins/package-managers/maven/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies {
3939
implementation(libs.maven.embedder)
4040
implementation(libs.kotlinx.serialization.core)
4141
implementation(libs.kotlinx.serialization.json)
42+
implementation(libs.kottage)
4243

4344
ksp(projects.analyzer)
4445

plugins/package-managers/maven/src/main/kotlin/utils/MavenSupport.kt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@
1919

2020
package org.ossreviewtoolkit.plugins.packagemanagers.maven.utils
2121

22+
import io.github.irgaly.kottage.Kottage
23+
import io.github.irgaly.kottage.KottageEnvironment
24+
import io.github.irgaly.kottage.getOrNull
25+
import io.github.irgaly.kottage.platform.KottageContext
26+
import io.github.irgaly.kottage.put
27+
import io.github.irgaly.kottage.strategy.KottageLruStrategy
28+
2229
import java.io.Closeable
2330
import java.io.File
2431
import java.net.URI
2532

2633
import kotlin.time.Duration.Companion.hours
2734

35+
import kotlinx.coroutines.MainScope
36+
2837
import org.apache.logging.log4j.kotlin.logger
2938
import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager
3039
import org.apache.maven.bridge.MavenRepositorySystem
@@ -79,16 +88,15 @@ import org.ossreviewtoolkit.model.PackageProvider
7988
import org.ossreviewtoolkit.model.RemoteArtifact
8089
import org.ossreviewtoolkit.model.fromYaml
8190
import org.ossreviewtoolkit.model.toYaml
82-
import org.ossreviewtoolkit.utils.common.DiskCache
8391
import org.ossreviewtoolkit.utils.common.collectMessages
8492
import org.ossreviewtoolkit.utils.common.div
85-
import org.ossreviewtoolkit.utils.common.gibibytes
8693
import org.ossreviewtoolkit.utils.common.searchUpwardFor
8794
import org.ossreviewtoolkit.utils.ort.OrtAuthenticator
8895
import org.ossreviewtoolkit.utils.ort.OrtProxySelector
8996
import org.ossreviewtoolkit.utils.ort.downloadText
9097
import org.ossreviewtoolkit.utils.ort.okHttpClient
9198
import org.ossreviewtoolkit.utils.ort.ortDataDirectory
99+
import org.ossreviewtoolkit.utils.ort.runBlocking
92100
import org.ossreviewtoolkit.utils.ort.showStackTrace
93101

94102
/**
@@ -101,12 +109,18 @@ class MavenSupport(private val workspaceReader: WorkspaceReader) : Closeable {
101109
private val container = createContainer()
102110
private val repositorySystemSession = createRepositorySystemSession(workspaceReader)
103111

104-
private val remoteArtifactCache = DiskCache(
105-
directory = ortDataDirectory / "cache" / "analyzer" / workspaceReader.repository.contentType,
106-
maxCacheSizeInBytes = 1.gibibytes,
107-
maxCacheEntryAgeInSeconds = 6.hours.inWholeSeconds
112+
val kottage = Kottage(
113+
name = "maven-support-store",
114+
directoryPath = (ortDataDirectory / "cache" / "analyzer" / workspaceReader.repository.contentType).absolutePath,
115+
environment = KottageEnvironment(KottageContext()),
116+
scope = MainScope()
108117
)
109118

119+
val remoteArtifactCache = kottage.cache("remote-artifact-cache") {
120+
strategy = KottageLruStrategy(maxEntryCount = 5000)
121+
defaultExpireTime = 6.hours
122+
}
123+
110124
// The MavenSettingsBuilder class is deprecated, but internally it uses its successor SettingsBuilder. Calling
111125
// MavenSettingsBuilder requires less code than calling SettingsBuilder, so use it until it is removed.
112126
@Suppress("DEPRECATION")
@@ -294,7 +308,8 @@ class MavenSupport(private val workspaceReader: WorkspaceReader) : Closeable {
294308

295309
val cacheKey = "$artifact@$allRepositories"
296310

297-
remoteArtifactCache.read(cacheKey)?.let {
311+
// TODO: Straight use `RemoteArtifact` instead of `String` here once the model classes use KxS.
312+
runBlocking { remoteArtifactCache.getOrNull<String>(cacheKey) }?.let {
298313
logger.debug { "Reading remote artifact for '$artifact' from disk cache." }
299314
return it.fromYaml()
300315
}
@@ -413,7 +428,7 @@ class MavenSupport(private val workspaceReader: WorkspaceReader) : Closeable {
413428

414429
return RemoteArtifact(info.downloadUrl, hash).also { remoteArtifact ->
415430
logger.debug { "Writing remote artifact for '$artifact' to disk cache." }
416-
remoteArtifactCache.write(cacheKey, remoteArtifact.toYaml())
431+
runBlocking { remoteArtifactCache.put(cacheKey, remoteArtifact.toYaml()) }
417432
}
418433
} else {
419434
logger.debug { artifactDownload.exception.collectMessages() }
@@ -430,7 +445,7 @@ class MavenSupport(private val workspaceReader: WorkspaceReader) : Closeable {
430445
if (downloadUrls.any { url -> PACKAGING_TYPES.any { url.endsWith(".$it") } }) {
431446
logger.debug { "Writing empty remote artifact for '$artifact' to disk cache." }
432447

433-
remoteArtifactCache.write(cacheKey, remoteArtifact.toYaml())
448+
runBlocking { remoteArtifactCache.put(cacheKey, remoteArtifact.toYaml()) }
434449
} else {
435450
logger.warn { "Could not find artifact $artifact in any of $downloadUrls." }
436451
}
@@ -596,7 +611,7 @@ class MavenSupport(private val workspaceReader: WorkspaceReader) : Closeable {
596611
}
597612

598613
override fun close() {
599-
remoteArtifactCache.close()
614+
runBlocking { kottage.close() }
600615
}
601616
}
602617

utils/common/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ dependencies {
2626
api(libs.commonsCompress)
2727
api(libs.semver4j)
2828

29-
implementation(libs.diskLruCache)
3029
implementation(libs.log4j.api)
3130
implementation(libs.springCore)
3231

utils/common/src/main/kotlin/DiskCache.kt

Lines changed: 0 additions & 154 deletions
This file was deleted.

0 commit comments

Comments
 (0)