Skip to content

Commit

Permalink
Merge pull request #704 from modelix/refactor/cleanup
Browse files Browse the repository at this point in the history
Model server code cleanup
  • Loading branch information
languitar authored Apr 18, 2024
2 parents f03e5f5 + bad37ad commit 1fcee2a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
32 changes: 17 additions & 15 deletions model-server/src/main/kotlin/org/modelix/model/server/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,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)
Expand All @@ -78,10 +77,9 @@ class KeyValueLikeModelServer(
this(repositoriesManager, repositoriesManager.client.store, InMemoryModels())

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) {
Expand Down Expand Up @@ -262,7 +260,7 @@ class KeyValueLikeModelServer(
val processed: MutableSet<String> = HashSet()
val pending: MutableSet<String> = HashSet()
pending.add(rootKey)
while (!pending.isEmpty()) {
while (pending.isNotEmpty()) {
val keys: List<String> = ArrayList(pending)
pending.clear()
val values = storeClient.getAll(keys)
Expand Down Expand Up @@ -295,7 +293,7 @@ class KeyValueLikeModelServer(
return result
}

protected suspend fun CallContext.putEntries(newEntries: Map<String, String?>) {
private suspend fun CallContext.putEntries(newEntries: Map<String, String?>) {
val referencedKeys: MutableSet<String> = HashSet()
for ((key, value) in newEntries) {
checkKeyPermission(key, EPermissionType.WRITE)
Expand Down Expand Up @@ -397,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,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 {
Expand All @@ -79,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())
}

Expand Down Expand Up @@ -362,18 +363,11 @@ class LightModelServer(val client: LocalModelClient) {
node.allChildren.forEach { node2json(it, true, outputList) }
}
}

companion object {
private val LOG = mu.KotlinLogging.logger { }
}
}

@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun <T> Channel<T>.receiveLast(): T {
var latest = receive()
while (!isEmpty) latest = receive()
return latest
}

private fun Channel<*>.clear() {
while (!isEmpty) tryReceive()
}

0 comments on commit 1fcee2a

Please sign in to comment.