Skip to content

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ class KeyValueLikeModelServer(val repositoriesManager: RepositoriesManager) {
7575
val storeClient: IStoreClient get() = repositoriesManager.client.store
7676

7777
fun init(application: Application) {
78+
// Functionally, it does not matter if the server ID
79+
// is created eagerly on startup or lazily on the first request,
80+
// as long as the same server ID is returned from the same server.
81+
//
82+
// The server ID is initialized eagerly because adding special conditions in the affected
83+
// request to initialize it lazily, would make the code less robust.
84+
// Each change in the logic of RepositoriesManager#maybeInitAndGetSeverId would need
85+
// the special conditions in the affected requests to be updated.
86+
repositoriesManager.maybeInitAndGetSeverId()
7887
application.apply {
7988
modelServerModule()
8089
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ class ModelReplicationServer(val repositoriesManager: RepositoriesManager) {
8585
call.respondText(storeClient.generateId("clientId").toString())
8686
}
8787
get("server-id") {
88-
call.respondText(repositoriesManager.getServerId())
88+
// Currently, the server ID is initialized in KeyValueLikeModelServer eagerly on startup.
89+
// Should KeyValueLikeModelServer be removed or change,
90+
// RepositoriesManager#maybeInitAndGetSeverId will initialize the server ID lazily on the first request.
91+
//
92+
// Functionally, it does not matter if the server ID is created eagerly or lazily,
93+
// as long as the same server ID is returned from the same server.
94+
val serverId = repositoriesManager.maybeInitAndGetSeverId()
95+
call.respondText(serverId)
8996
}
9097
get("user-id") {
9198
call.respondText(call.getUserName() ?: call.request.origin.remoteHost)

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@ class RepositoriesManager(val client: LocalModelClient) {
4444
return client.store.generateId("$KEY_PREFIX:${repositoryId.id}:clientId")
4545
}
4646

47-
fun getServerId(): String {
47+
/**
48+
* Used to retrieve the server ID. If needed, the server ID is created and stored.
49+
*
50+
* If a server ID was not created yet, it is generated and saved in the database.
51+
* It gets stored under the current and all legacy database keys.
52+
*
53+
* If the server ID was created previously but is only stored under a legacy database key,
54+
* it also gets stored under the current and all legacy database keys.
55+
*/
56+
fun maybeInitAndGetSeverId(): String {
4857
return store.runTransaction {
4958
var serverId = store[SERVER_ID_KEY]
5059
if (serverId == null) {

model-server/src/test/kotlin/org/modelix/model/server/ModelClientTest.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.modelix.model.server.store.LocalModelClient
2929
import java.util.Random
3030
import kotlin.test.Test
3131
import kotlin.test.assertEquals
32+
import kotlin.test.assertNotNull
3233
import kotlin.test.fail
3334
import kotlin.time.Duration.Companion.seconds
3435

@@ -49,8 +50,7 @@ class ModelClientTest {
4950
val numKeys = numListenersPerClient * 2
5051

5152
val rand = Random(67845)
52-
val url = "http://localhost/"
53-
val clients = (0 until numClients).map { RestWebModelClient(baseUrl = url, providedClient = client) }
53+
val clients = (0 until numClients).map { createModelClient() }
5454
val listeners: MutableList<Listener> = ArrayList()
5555
val expected: MutableMap<String, String> = HashMap()
5656
for (client in clients.withIndex()) {
@@ -104,6 +104,20 @@ class ModelClientTest {
104104
}
105105
}
106106

107+
@Test
108+
fun `can retrieve server id initially`() = runTest {
109+
val modelClient = createModelClient()
110+
111+
val serverId = modelClient.get("server-id")
112+
113+
assertNotNull(serverId)
114+
}
115+
116+
private fun ApplicationTestBuilder.createModelClient(): RestWebModelClient {
117+
val url = "http://localhost/"
118+
return RestWebModelClient(baseUrl = url, providedClient = client)
119+
}
120+
107121
inner class Listener(var key: String, private val client: RestWebModelClient, val clientIndex: Int, val listenerIndex: Int) : IKeyListener {
108122
var lastValue: String? = null
109123
override fun changed(key: String, value: String?) {

0 commit comments

Comments
 (0)