Skip to content

Commit 1fcee2a

Browse files
authored
Merge pull request #704 from modelix/refactor/cleanup
Model server code cleanup
2 parents f03e5f5 + bad37ad commit 1fcee2a

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

model-server/src/main/kotlin/org/modelix/model/server/Main.kt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import io.ktor.server.application.call
2222
import io.ktor.server.application.install
2323
import io.ktor.server.engine.embeddedServer
2424
import io.ktor.server.html.respondHtmlTemplate
25-
import io.ktor.server.http.content.resources
26-
import io.ktor.server.http.content.static
25+
import io.ktor.server.http.content.staticResources
2726
import io.ktor.server.netty.Netty
2827
import io.ktor.server.netty.NettyApplicationEngine
2928
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
@@ -44,6 +43,7 @@ import kotlinx.html.h1
4443
import kotlinx.html.li
4544
import kotlinx.html.style
4645
import kotlinx.html.ul
46+
import kotlinx.html.unsafe
4747
import org.apache.commons.io.FileUtils
4848
import org.apache.ignite.Ignition
4949
import org.modelix.authorization.KeycloakUtils
@@ -204,24 +204,26 @@ object Main {
204204
modelReplicationServer.init(this)
205205
metricsHandler.init(this)
206206
routing {
207-
static("/public") {
208-
resources("public")
209-
}
207+
staticResources("/public", "public")
210208
get("/") {
211209
call.respondHtmlTemplate(PageWithMenuBar("root", ".")) {
212210
headContent {
213211
style {
214-
+"""
215-
body {
216-
font-family: sans-serif;
217-
table {
218-
border-collapse: collapse;
219-
}
220-
td, th {
221-
border: 1px solid #888;
222-
padding: 3px 12px;
212+
unsafe {
213+
raw(
214+
"""
215+
body {
216+
font-family: sans-serif;
217+
table {
218+
border-collapse: collapse;
219+
}
220+
td, th {
221+
border: 1px solid #888;
222+
padding: 3px 12px;
223+
}
224+
""".trimIndent(),
225+
)
223226
}
224-
""".trimIndent()
225227
}
226228
}
227229
bodyContent {

model-server/src/main/kotlin/org/modelix/model/server/handlers/KeyValueLikeModelServer.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import org.modelix.model.server.store.IStoreClient
5252
import org.modelix.model.server.store.pollEntry
5353
import org.modelix.model.server.store.runTransactionSuspendable
5454
import org.modelix.model.server.templates.PageWithMenuBar
55-
import org.slf4j.LoggerFactory
5655
import java.io.IOException
5756
import java.util.*
5857
import java.util.regex.Pattern
@@ -61,7 +60,7 @@ val PERMISSION_MODEL_SERVER = "model-server".asResource()
6160
val MODEL_SERVER_ENTRY = KeycloakResourceType("model-server-entry", KeycloakScope.READ_WRITE_DELETE)
6261

6362
private fun toLong(value: String?): Long {
64-
return if (value == null || value.isEmpty()) 0 else value.toLong()
63+
return if (value.isNullOrEmpty()) 0 else value.toLong()
6564
}
6665

6766
private class NotFoundException(description: String?) : RuntimeException(description)
@@ -78,10 +77,9 @@ class KeyValueLikeModelServer(
7877
this(repositoriesManager, repositoriesManager.client.store, InMemoryModels())
7978

8079
companion object {
81-
private val LOG = LoggerFactory.getLogger(KeyValueLikeModelServer::class.java)
82-
val HASH_PATTERN = Pattern.compile("[a-zA-Z0-9\\-_]{5}\\*[a-zA-Z0-9\\-_]{38}")
83-
const val PROTECTED_PREFIX = "$$$"
84-
val HEALTH_KEY = PROTECTED_PREFIX + "health2"
80+
private val HASH_PATTERN: Pattern = Pattern.compile("[a-zA-Z0-9\\-_]{5}\\*[a-zA-Z0-9\\-_]{38}")
81+
private const val PROTECTED_PREFIX = "$$$"
82+
private const val HEALTH_KEY = PROTECTED_PREFIX + "health2"
8583
}
8684

8785
fun init(application: Application) {
@@ -262,7 +260,7 @@ class KeyValueLikeModelServer(
262260
val processed: MutableSet<String> = HashSet()
263261
val pending: MutableSet<String> = HashSet()
264262
pending.add(rootKey)
265-
while (!pending.isEmpty()) {
263+
while (pending.isNotEmpty()) {
266264
val keys: List<String> = ArrayList(pending)
267265
pending.clear()
268266
val values = storeClient.getAll(keys)
@@ -295,7 +293,7 @@ class KeyValueLikeModelServer(
295293
return result
296294
}
297295

298-
protected suspend fun CallContext.putEntries(newEntries: Map<String, String?>) {
296+
private suspend fun CallContext.putEntries(newEntries: Map<String, String?>) {
299297
val referencedKeys: MutableSet<String> = HashSet()
300298
for ((key, value) in newEntries) {
301299
checkKeyPermission(key, EPermissionType.WRITE)
@@ -397,7 +395,7 @@ class KeyValueLikeModelServer(
397395
call.checkPermission(MODEL_SERVER_ENTRY.createInstance(key), type.toKeycloakScope())
398396
}
399397

400-
fun isHealthy(): Boolean {
398+
private fun isHealthy(): Boolean {
401399
val value = toLong(storeClient[HEALTH_KEY]) + 1
402400
storeClient.put(HEALTH_KEY, java.lang.Long.toString(value))
403401
return toLong(storeClient[HEALTH_KEY]) >= value

model-server/src/main/kotlin/org/modelix/model/server/handlers/LightModelServer.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.ktor.websocket.Frame
2323
import io.ktor.websocket.readText
2424
import io.ktor.websocket.send
2525
import kotlinx.coroutines.CoroutineScope
26+
import kotlinx.coroutines.ExperimentalCoroutinesApi
2627
import kotlinx.coroutines.channels.Channel
2728
import kotlinx.coroutines.launch
2829
import kotlinx.coroutines.sync.Mutex
@@ -64,7 +65,7 @@ import kotlin.collections.set
6465

6566
class LightModelServer(val client: LocalModelClient) {
6667

67-
fun getStore() = client.storeCache!!
68+
private fun getStore() = client.storeCache
6869

6970
fun init(application: Application) {
7071
application.apply {
@@ -79,7 +80,7 @@ class LightModelServer(val client: LocalModelClient) {
7980
}
8081

8182
private fun getCurrentVersion(repositoryId: RepositoryId): CLVersion {
82-
val versionHash = client.asyncStore?.get(repositoryId.getBranchKey())!!
83+
val versionHash = client.asyncStore.get(repositoryId.getBranchKey())!!
8384
return CLVersion.loadFromHash(versionHash, getStore())
8485
}
8586

@@ -362,18 +363,11 @@ class LightModelServer(val client: LocalModelClient) {
362363
node.allChildren.forEach { node2json(it, true, outputList) }
363364
}
364365
}
365-
366-
companion object {
367-
private val LOG = mu.KotlinLogging.logger { }
368-
}
369366
}
370367

368+
@OptIn(ExperimentalCoroutinesApi::class)
371369
private suspend fun <T> Channel<T>.receiveLast(): T {
372370
var latest = receive()
373371
while (!isEmpty) latest = receive()
374372
return latest
375373
}
376-
377-
private fun Channel<*>.clear() {
378-
while (!isEmpty) tryReceive()
379-
}

0 commit comments

Comments
 (0)