From 3feeaf99a606bf4ce882d8fafbba1c63dd092dd5 Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Thu, 18 Apr 2024 15:04:54 +0200 Subject: [PATCH 1/8] chore(model-server): drop unneeded code in LightModelServer --- .../org/modelix/model/server/handlers/LightModelServer.kt | 8 -------- 1 file changed, 8 deletions(-) diff --git a/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt b/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt index 624e8301c0..24b6ad8a5e 100644 --- a/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt +++ b/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt @@ -358,10 +358,6 @@ class LightModelServer(val client: LocalModelClient) { node.allChildren.forEach { node2json(it, true, outputList) } } } - - companion object { - private val LOG = mu.KotlinLogging.logger { } - } } private suspend fun Channel.receiveLast(): T { @@ -369,7 +365,3 @@ private suspend fun Channel.receiveLast(): T { while (!isEmpty) latest = receive() return latest } - -private fun Channel<*>.clear() { - while (!isEmpty) tryReceive() -} From 0b3fc19c6e19cdcb941c440cfe1f07cbc35e6ad8 Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Thu, 18 Apr 2024 15:05:23 +0200 Subject: [PATCH 2/8] chore(model-server): explicitly opt into experimental API usage --- .../org/modelix/model/server/handlers/LightModelServer.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt b/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt index 24b6ad8a5e..cc7a2c465a 100644 --- a/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt +++ b/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt @@ -23,6 +23,7 @@ import io.ktor.websocket.Frame import io.ktor.websocket.readText import io.ktor.websocket.send import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex @@ -360,6 +361,7 @@ class LightModelServer(val client: LocalModelClient) { } } +@OptIn(ExperimentalCoroutinesApi::class) private suspend fun Channel.receiveLast(): T { var latest = receive() while (!isEmpty) latest = receive() From 3bb6c33d5b2c0118f3796249f906af4e1deaff80 Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Thu, 18 Apr 2024 15:06:09 +0200 Subject: [PATCH 3/8] refactor(model-server): remove some warnings in LightModelServer Removes some unneeded not-null assertions and reduces the visibility of a function. --- .../org/modelix/model/server/handlers/LightModelServer.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt b/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt index cc7a2c465a..300c65aa3c 100644 --- a/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt +++ b/model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt @@ -65,7 +65,7 @@ import kotlin.collections.set class LightModelServer(val client: LocalModelClient) { - fun getStore() = client.storeCache!! + private fun getStore() = client.storeCache fun init(application: Application) { application.apply { @@ -80,7 +80,7 @@ class LightModelServer(val client: LocalModelClient) { } private fun getCurrentVersion(repositoryId: RepositoryId): CLVersion { - val versionHash = client.asyncStore?.get(repositoryId.getBranchKey())!! + val versionHash = client.asyncStore.get(repositoryId.getBranchKey())!! return CLVersion.loadFromHash(versionHash, getStore()) } From 19a23d5515306652955fdf2860662ad7fe90b6b7 Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Thu, 18 Apr 2024 15:18:50 +0200 Subject: [PATCH 4/8] refactor(model-server): reduce Visibilities of consts --- .../model/server/handlers/KeyValueLikeModelServer.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt b/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt index 99a0a07843..089aef688c 100644 --- a/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt +++ b/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt @@ -79,9 +79,9 @@ class KeyValueLikeModelServer( companion object { private val LOG = LoggerFactory.getLogger(KeyValueLikeModelServer::class.java) - val HASH_PATTERN = Pattern.compile("[a-zA-Z0-9\\-_]{5}\\*[a-zA-Z0-9\\-_]{38}") - const val PROTECTED_PREFIX = "$$$" - val HEALTH_KEY = PROTECTED_PREFIX + "health2" + private val HASH_PATTERN: Pattern = Pattern.compile("[a-zA-Z0-9\\-_]{5}\\*[a-zA-Z0-9\\-_]{38}") + private const val PROTECTED_PREFIX = "$$$" + private const val HEALTH_KEY = PROTECTED_PREFIX + "health2" } fun init(application: Application) { From 0ebf41fdca13a0ff43587c573ae9d4988e18c7fc Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Thu, 18 Apr 2024 15:19:22 +0200 Subject: [PATCH 5/8] refactor(model-server): remove unneeded logger --- .../modelix/model/server/handlers/KeyValueLikeModelServer.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt b/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt index 089aef688c..fd4358f959 100644 --- a/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt +++ b/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt @@ -52,7 +52,6 @@ import org.modelix.model.server.store.IStoreClient import org.modelix.model.server.store.pollEntry import org.modelix.model.server.store.runTransactionSuspendable import org.modelix.model.server.templates.PageWithMenuBar -import org.slf4j.LoggerFactory import java.io.IOException import java.util.* import java.util.regex.Pattern @@ -78,7 +77,6 @@ class KeyValueLikeModelServer( this(repositoriesManager, repositoriesManager.client.store, InMemoryModels()) companion object { - private val LOG = LoggerFactory.getLogger(KeyValueLikeModelServer::class.java) private val HASH_PATTERN: Pattern = Pattern.compile("[a-zA-Z0-9\\-_]{5}\\*[a-zA-Z0-9\\-_]{38}") private const val PROTECTED_PREFIX = "$$$" private const val HEALTH_KEY = PROTECTED_PREFIX + "health2" From 5abdee8c8b4b72934c0b7ab5f438e6f21c8b1d91 Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Thu, 18 Apr 2024 15:20:21 +0200 Subject: [PATCH 6/8] refactor(model-server): improve boolean condition readability Use simpler versions avoiding negations or null checks. --- .../modelix/model/server/handlers/KeyValueLikeModelServer.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt b/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt index fd4358f959..333c19b3bb 100644 --- a/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt +++ b/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt @@ -60,7 +60,7 @@ val PERMISSION_MODEL_SERVER = "model-server".asResource() val MODEL_SERVER_ENTRY = KeycloakResourceType("model-server-entry", KeycloakScope.READ_WRITE_DELETE) private fun toLong(value: String?): Long { - return if (value == null || value.isEmpty()) 0 else value.toLong() + return if (value.isNullOrEmpty()) 0 else value.toLong() } private class NotFoundException(description: String?) : RuntimeException(description) @@ -260,7 +260,7 @@ class KeyValueLikeModelServer( val processed: MutableSet = HashSet() val pending: MutableSet = HashSet() pending.add(rootKey) - while (!pending.isEmpty()) { + while (pending.isNotEmpty()) { val keys: List = ArrayList(pending) pending.clear() val values = storeClient.getAll(keys) From f9ea5159b294a8230d4e67c76d98371aa01f6826 Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Thu, 18 Apr 2024 15:22:52 +0200 Subject: [PATCH 7/8] refactor(model-server): reduce visibilities in KeyValueLikeModelServer --- .../modelix/model/server/handlers/KeyValueLikeModelServer.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt b/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt index 333c19b3bb..ccd212e763 100644 --- a/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt +++ b/model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt @@ -293,7 +293,7 @@ class KeyValueLikeModelServer( return result } - protected suspend fun CallContext.putEntries(newEntries: Map) { + private suspend fun CallContext.putEntries(newEntries: Map) { val referencedKeys: MutableSet = HashSet() for ((key, value) in newEntries) { checkKeyPermission(key, EPermissionType.WRITE) @@ -395,7 +395,7 @@ class KeyValueLikeModelServer( call.checkPermission(MODEL_SERVER_ENTRY.createInstance(key), type.toKeycloakScope()) } - fun isHealthy(): Boolean { + private fun isHealthy(): Boolean { val value = toLong(storeClient[HEALTH_KEY]) + 1 storeClient.put(HEALTH_KEY, java.lang.Long.toString(value)) return toLong(storeClient[HEALTH_KEY]) >= value From bad37ad41d2d359118b133cc259350a5f7f7b576 Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Thu, 18 Apr 2024 15:29:10 +0200 Subject: [PATCH 8/8] chore(model-server): remove deprecated ktor/html DSL usages --- .../kotlin/org/modelix/model/server/Main.kt | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/model-server/src/main/kotlin/org/modelix/model/server/Main.kt b/model-server/src/main/kotlin/org/modelix/model/server/Main.kt index e053919ef1..42a31e2d2c 100644 --- a/model-server/src/main/kotlin/org/modelix/model/server/Main.kt +++ b/model-server/src/main/kotlin/org/modelix/model/server/Main.kt @@ -22,8 +22,7 @@ import io.ktor.server.application.call import io.ktor.server.application.install import io.ktor.server.engine.embeddedServer import io.ktor.server.html.respondHtmlTemplate -import io.ktor.server.http.content.resources -import io.ktor.server.http.content.static +import io.ktor.server.http.content.staticResources import io.ktor.server.netty.Netty import io.ktor.server.netty.NettyApplicationEngine import io.ktor.server.plugins.contentnegotiation.ContentNegotiation @@ -44,6 +43,7 @@ import kotlinx.html.h1 import kotlinx.html.li import kotlinx.html.style import kotlinx.html.ul +import kotlinx.html.unsafe import org.apache.commons.io.FileUtils import org.apache.ignite.Ignition import org.modelix.authorization.KeycloakUtils @@ -204,24 +204,26 @@ object Main { modelReplicationServer.init(this) metricsHandler.init(this) routing { - static("/public") { - resources("public") - } + staticResources("/public", "public") get("/") { call.respondHtmlTemplate(PageWithMenuBar("root", ".")) { headContent { style { - +""" - body { - font-family: sans-serif; - table { - border-collapse: collapse; - } - td, th { - border: 1px solid #888; - padding: 3px 12px; + unsafe { + raw( + """ + body { + font-family: sans-serif; + table { + border-collapse: collapse; + } + td, th { + border: 1px solid #888; + padding: 3px 12px; + } + """.trimIndent(), + ) } - """.trimIndent() } } bodyContent {