Repository navigation
fix(core)!: emit OutboxProcessorListener events after the transaction commits (#137) #147
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,8 @@ import java.util.concurrent.TimeUnit | |
| import java.util.concurrent.atomic.AtomicBoolean | ||
|
|
||
| /** | ||
| * Standalone scheduler that periodically calls [OutboxProcessor.processNext]. | ||
| * Standalone scheduler that periodically calls [OutboxProcessor.processBatch] in a transaction and, | ||
| * once it has committed, [OutboxProcessor.publishEvents]. | ||
| * | ||
| * Each worker's call runs inside [transactionRunner]. The runner is required: without a | ||
| * surrounding transaction, `FOR UPDATE SKIP LOCKED` releases its row lock at the end of the | ||
|
|
@@ -179,7 +180,9 @@ class OutboxScheduler @JvmOverloads constructor( | |
|
|
||
| private fun processBatch() { | ||
| try { | ||
| transactionRunner.runInTransaction { outboxProcessor.processNext(config.batchSize) } | ||
| val batch = transactionRunner.runInTransaction { outboxProcessor.processBatch(config.batchSize) } | ||
| // After the commit (issue #137): a batch whose commit failed is rolled back and is not reported. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should-fix (contract docs): the after-commit guarantee now rests on an unstated |
||
| outboxProcessor.publishEvents(batch) | ||
| logger.debug("Outbox processor tick completed [batchSize={}]", config.batchSize) | ||
| } catch (e: Exception) { | ||
| logger.error("Outbox processor tick failed, will retry at next scheduled interval", e) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.softwaremill.okapi.core | ||
|
|
||
| import java.time.Duration | ||
| import java.time.Instant | ||
| import java.util.Collections | ||
| import java.util.concurrent.atomic.AtomicBoolean | ||
|
|
||
| /** | ||
| * The outcome of [OutboxProcessor.processBatch]: [entries] in the state just persisted, whose | ||
| * listener events are held back until [OutboxProcessor.publishEvents]. Only [OutboxProcessor] | ||
| * creates one; its constructor and timing fields are internal to okapi-core. | ||
| */ | ||
| class ProcessedBatch internal constructor( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit (Java interop / API surface): |
||
| entries: List<OutboxEntry>, | ||
| internal val startedAt: Instant, | ||
| internal val deliveryDuration: Duration, | ||
| ) { | ||
| /** The processed entries, in the state persisted for them. Read-only. */ | ||
| val entries: List<OutboxEntry> = Collections.unmodifiableList(entries.toList()) | ||
|
|
||
| /** Number of entries processed (0 if the store had nothing to claim). */ | ||
| val count: Int get() = entries.size | ||
|
|
||
| private val published = AtomicBoolean(false) | ||
|
|
||
| /** Returns `true` the first time only. */ | ||
| internal fun markPublished(): Boolean = published.compareAndSet(false, true) | ||
|
|
||
| override fun toString(): String = "ProcessedBatch(count=$count, startedAt=$startedAt, deliveryDuration=$deliveryDuration)" | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "Call it at most once per batch" is only documented. Calling it twice double-counts every Micrometer counter, which is exactly the class of bug this PR fixes. A one-shot guard is cheap: an
AtomicBooleaninProcessedBatch, with a second call either logged and ignored or failing viacheck. It would make the new public API harder to misuse.