Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add metrics for big meta storing #2476

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import io.tolgee.util.logger
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime

class BigMetaControllerTest : ProjectAuthControllerTest("/v2/projects/"), Logging {
Expand Down Expand Up @@ -57,11 +56,11 @@ class BigMetaControllerTest : ProjectAuthControllerTest("/v2/projects/"), Loggin
bigMetaService.findExistingKeysDistancesDtosByIds(listOf(testData.yepKey.id)).assert.hasSize(1)
}

@OptIn(ExperimentalTime::class)
@Test
@ProjectJWTAuthTestMethod
fun `it performs well`() {
val keys = testData.addLotOfData()
testData.addLotOfReferences(keys)
saveTestDataAndPrepare()

logger.infoMeasureTime("it performs well time 1") {
Expand All @@ -87,6 +86,16 @@ class BigMetaControllerTest : ProjectAuthControllerTest("/v2/projects/"), Loggin
bigMetaService.findExistingKeysDistancesDtosByIds(keys.map { it.id }).assert.hasSize(104790)
}

@Test
@ProjectJWTAuthTestMethod
fun `it performs well (large)`() {
val keys = testData.addLotOfData()
testData.addLotOfReferences(keys)
saveTestDataAndPrepare()

storeLogOfBigMeta(keys, 0, 200)
}

private fun storeLogOfBigMeta(
keys: List<Key>,
drop: Int,
Expand All @@ -96,7 +105,7 @@ class BigMetaControllerTest : ProjectAuthControllerTest("/v2/projects/"), Loggin
"big-meta",
mapOf(
"relatedKeysInOrder" to
keys.drop(drop).take(take).map {
keys.drop(drop).take(take).reversed().map {
mapOf(
"namespace" to it.namespace,
"keyName" to it.name,
Expand All @@ -110,6 +119,7 @@ class BigMetaControllerTest : ProjectAuthControllerTest("/v2/projects/"), Loggin
@ProjectJWTAuthTestMethod
fun `it returns correct data`() {
val keys = testData.addLotOfData()
testData.addLotOfReferences(keys)
saveTestDataAndPrepare()

performProjectAuthGet("keys/${keys.first().id}/big-meta").andIsOk.andAssertThatJson {
Expand Down
13 changes: 13 additions & 0 deletions backend/data/src/main/kotlin/io/tolgee/Metrics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.tolgee
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Timer
import org.springframework.stereotype.Component
import java.util.concurrent.ConcurrentLinkedQueue

Expand All @@ -27,4 +28,16 @@ class Metrics(
.description("Total number of failures when trying to store data about batch job execution (execution failed)")
.register(meterRegistry)
}

val bigMetaStoringTimer: Timer by lazy {
Timer.builder("tolgee.big_meta.storing.timer")
.description("Time spent storing big meta data")
.register(meterRegistry)
}

val bigMetaNewDistancesComputeTimer: Timer by lazy {
Timer.builder("tolgee.big_meta.new_distances.compute.timer")
.description("Time spent computing new distances for big meta data")
.register(meterRegistry)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class BigMetaTestData {
(0..5000).map {
projectBuilder.addKey(null, "key$it").self
}
return keys
}

fun addLotOfReferences(keys: List<Key>) {
keys.forEachIndexed forEach1@{ idx1, key1 ->
keys.forEachIndexed forEach2@{ idx2, key2 ->
if (idx1 >= idx2 || abs(idx1 - idx2) > (BigMetaService.MAX_ORDER_DISTANCE + 1)) return@forEach2
Expand All @@ -46,7 +49,5 @@ class BigMetaTestData {
}
}
}

return keys
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.tolgee.service.bigMeta

import io.tolgee.Metrics
import io.tolgee.component.CurrentDateProvider
import io.tolgee.dtos.BigMetaDto
import io.tolgee.dtos.RelatedKeyDto
Expand Down Expand Up @@ -36,6 +37,7 @@ class BigMetaService(
private val transactionManager: PlatformTransactionManager,
private val jdbcTemplate: JdbcTemplate,
private val currentDateProvider: CurrentDateProvider,
private val metrics: Metrics,
) : Logging {
companion object {
const val MAX_DISTANCE_SCORE = 10000L
Expand Down Expand Up @@ -64,9 +66,14 @@ class BigMetaService(
return
}

val util = KeysDistanceUtil(relatedKeysInOrder, project, this)
val util =
metrics.bigMetaNewDistancesComputeTimer.recordCallable {
KeysDistanceUtil(relatedKeysInOrder, project, this)
}!!

insertNewDistances(util.newDistances)
metrics.bigMetaStoringTimer.recordCallable {
insertNewDistances(util.newDistances)
}
}

private fun insertNewDistances(toInsert: List<KeysDistanceDto>) {
Expand All @@ -78,7 +85,7 @@ class BigMetaService(
on conflict (key1id, key2id) do update set score = excluded.score, hits = excluded.hits, updated_at = ?
""",
toInsert,
100,
1000,
) { ps, dto ->
ps.setLong(1, dto.key1Id)
ps.setLong(2, dto.key2Id)
Expand Down
Loading