Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -26,6 +26,7 @@ class RetrospectiveCompletionCoordinator(
private val aiClient: AIClient,
private val transactionTemplate: TransactionTemplate,
private val metrics: RetrospectiveAiMetrics,
private val summarySaveConcurrencyLimiter: SummarySaveConcurrencyLimiter,
) {
companion object {
private val logger = LoggerFactory.getLogger(RetrospectiveCompletionCoordinator::class.java)
Expand All @@ -43,7 +44,11 @@ class RetrospectiveCompletionCoordinator(
metrics.recordStage("summary", "openai") {
aiClient.generateSummaryWithTitle(snapshot.job, snapshot.answers, snapshot.deepQuestion)
}
metrics.recordStage("summary", "save") { save(retrospectiveId, userId, summary) }
metrics.recordStage("summary", "save") {
summarySaveConcurrencyLimiter.execute {
save(retrospectiveId, userId, summary)
}
}
summary
} catch (exception: Exception) {
reset(retrospectiveId, userId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.didit.application.retrospect

import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import java.util.concurrent.Semaphore

@Component
class SummarySaveConcurrencyLimiter(
@Value("\${retrospective.summary-save-max-concurrency:4}") maxConcurrency: Int,
) {
private val semaphore =
Semaphore(
requireNotNull(maxConcurrency.takeIf { it > 0 }) {
"retrospective.summary-save-max-concurrency must be greater than 0"
},
true,
)

fun <T> execute(action: () -> T): T {
semaphore.acquire()
try {
return action()
} finally {
semaphore.release()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class RetrospectiveCompletionCoordinatorTest {
aiClient,
TransactionTemplate(transactionManager),
metrics,
SummarySaveConcurrencyLimiter(4),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.didit.application.retrospect

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

class SummarySaveConcurrencyLimiterTest {
@Test
fun `execute - simultaneously runs no more than configured number of actions`() {
val limiter = SummarySaveConcurrencyLimiter(4)
val executor = Executors.newFixedThreadPool(10)
val ready = CountDownLatch(10)
val start = CountDownLatch(1)
val firstWaveEntered = CountDownLatch(4)
val release = CountDownLatch(1)
val active = AtomicInteger()
val maxActive = AtomicInteger()
val completed = AtomicInteger()

try {
val futures =
(1..10).map {
executor.submit {
ready.countDown()
start.await()
limiter.execute {
val currentActive = active.incrementAndGet()
maxActive.accumulateAndGet(currentActive, ::maxOf)
firstWaveEntered.countDown()
try {
release.await()
completed.incrementAndGet()
} finally {
active.decrementAndGet()
}
}
}
}

assertThat(ready.await(1, TimeUnit.SECONDS)).isTrue()
start.countDown()
assertThat(firstWaveEntered.await(1, TimeUnit.SECONDS)).isTrue()
assertThat(active.get()).isEqualTo(4)

release.countDown()
futures.forEach { it.get(1, TimeUnit.SECONDS) }

assertThat(maxActive.get()).isEqualTo(4)
assertThat(completed.get()).isEqualTo(10)
} finally {
release.countDown()
executor.shutdownNow()
}
}

@Test
fun `execute - releases permit when action fails`() {
val limiter = SummarySaveConcurrencyLimiter(1)

assertThrows<IllegalStateException> {
limiter.execute { throw IllegalStateException("failed") }
}

assertThat(limiter.execute { "completed" }).isEqualTo("completed")
}

@Test
fun `constructor - rejects non-positive max concurrency`() {
assertThrows<IllegalArgumentException> { SummarySaveConcurrencyLimiter(0) }
}
}
Loading