fix(messaging): inbox 트랜잭션 runner가 쓰기와 같은 30초 상한으로 트랜잭션을 연다
문제: inbound messaging의 IdempotentConsumer.TransactionRunner는
new TransactionTemplate(transactionManager)를 상한 없이 썼다. handler의
유스케이스가 부르는 TransactionPort.inWrite는 이 트랜잭션에 합류하고, 합류한
쓰기는 자기 상한(app.persistence-jpa.transaction.write-timeout 30s)을 걸지
않는다. 그래서 막힌 문장이나 잠금 대기가 연결과 partition을 그 시간 내내
쥐었다. docs/work/reference/decisions.md 2.5 JPA 기본값
「트랜잭션 제한 30초」와 다르다.
고른 방법: TransactionTemplate에 쓰기와 같은 30초를 건다. 값은 상수 하나
(TRANSACTION_TIMEOUT_SECONDS)이고, 설정 키는 더하지 않았다 — 쓰기 상한과
따로 움직일 이유가 아직 없고, 키를 더하면 env 계약도 늘어난다.
버린 대안: 쓰기 상한 설정(write-timeout)을 읽어 쓰기 — 이 leaf가
persistence-jpa 설정에 기대게 되어 registry edge가 생긴다.
위험·한계: 30초를 넘기던 handler는 이제 롤백되고 재전달된다. 그런
handler는 원래 쓰기 상한도 넘긴 것이다.
검증(desktop, --no-build-cache):
- RED: 기준 a7e38c10 + 시험만으로
MessagingInboxAutoConfigurationTest 8건 중 새 시험 1건 실패(상한이 걸리지 않음).
- GREEN: :adapter:inbound:messaging:check rc=0.
This commit is contained in:
@@ -27,6 +27,8 @@ Kafka ─▶ platform consumer (starter-kafka)
|
||||
- `IdempotentConsumer.TransactionRunner`(애플리케이션 `PlatformTransactionManager` 위의
|
||||
`TransactionTemplate`). inbox 예약과 use case의 JPA 쓰기가 한 트랜잭션에서 함께 커밋되거나 함께
|
||||
롤백된다. handler가 실패하면 예약도 롤백되어 재전달이 효과를 다시 적용한다.
|
||||
이 트랜잭션은 30초 상한(`TransactionPort` 쓰기와 같은 값)으로 열린다. handler의 유스케이스 `inWrite`는 이
|
||||
트랜잭션에 합류해 자기 상한을 쓰지 않으므로, 막힌 문장이나 잠금 대기는 이 상한에서 끊긴다.
|
||||
- platform starter(`MessagingReliabilityAutoConfiguration`)가 이 두 bean으로 `IdempotentConsumer`를
|
||||
만든다. platform은 애플리케이션의 datasource·트랜잭션을 스스로 고르지 않는다는 기존 설계를 그대로 둔다.
|
||||
- 애플리케이션의 handler(`MessageHandler<T>` 구현)는 이 leaf에 둔다. 컨트롤러가 HTTP 요청을 use case
|
||||
|
||||
+10
@@ -68,12 +68,22 @@ public class MessagingInboxAutoConfiguration {
|
||||
return new MessagingInboxSchemaStream(dataSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* The upper bound on one delivery's transaction, in whole seconds: the same 30 s as one {@code
|
||||
* TransactionPort} write ({@code app.persistence-jpa.transaction.write-timeout}). The handler's
|
||||
* use case joins this transaction, and a joined {@code inWrite} leaves the deadline to the call
|
||||
* that began it, so without one here a blocked statement or lock wait held the connection and the
|
||||
* partition for as long as it lasted (review 2026-11-12).
|
||||
*/
|
||||
static final int TRANSACTION_TIMEOUT_SECONDS = 30;
|
||||
|
||||
/** Runs the inbox reservation and the handler in one application transaction. */
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(IdempotentConsumer.TransactionRunner.class)
|
||||
public IdempotentConsumer.TransactionRunner messagingInboxTransactions(
|
||||
PlatformTransactionManager transactionManager) {
|
||||
TransactionTemplate template = new TransactionTemplate(transactionManager);
|
||||
template.setTimeout(TRANSACTION_TIMEOUT_SECONDS);
|
||||
return new IdempotentConsumer.TransactionRunner() {
|
||||
@Override
|
||||
public <T> T inTransaction(java.util.function.Supplier<T> work) {
|
||||
|
||||
+23
-1
@@ -164,6 +164,25 @@ class MessagingInboxAutoConfigurationTest {
|
||||
assertThat(transactions.outcomes).containsExactly("commit", "rollback");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("트랜잭션 runner는 TransactionPort 쓰기와 같은 30초 상한으로 트랜잭션을 연다")
|
||||
void theRunnerOpensItsTransactionWithTheWriteTimeout() {
|
||||
// given
|
||||
RecordingTransactions transactions = new RecordingTransactions();
|
||||
IdempotentConsumer.TransactionRunner runner =
|
||||
new MessagingInboxAutoConfiguration().messagingInboxTransactions(transactions);
|
||||
|
||||
// when
|
||||
runner.inTransaction(() -> "applied");
|
||||
|
||||
// then
|
||||
assertThat(transactions.timeouts)
|
||||
.as(
|
||||
"the handler's use case joins this transaction, and a joined inWrite leaves the deadline"
|
||||
+ " to the call that began it")
|
||||
.containsExactly(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("같은 메시지가 두 번 전달돼도 handler의 부작용은 한 번만 일어난다")
|
||||
void theSameMessageDeliveredTwiceRunsTheSideEffectOnce() {
|
||||
@@ -232,6 +251,7 @@ class MessagingInboxAutoConfigurationTest {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final transient List<String> outcomes = new ArrayList<>();
|
||||
private final transient List<Integer> timeouts = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
protected Object doGetTransaction() {
|
||||
@@ -239,7 +259,9 @@ class MessagingInboxAutoConfigurationTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doBegin(Object transaction, TransactionDefinition definition) {}
|
||||
protected void doBegin(Object transaction, TransactionDefinition definition) {
|
||||
timeouts.add(definition.getTimeout());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doCommit(DefaultTransactionStatus status) {
|
||||
|
||||
Reference in New Issue
Block a user