fix(idempotency)!: 두 멱등 저장소가 operation id·release·inspect에 같게 답한다

문제: 같은 호출에 Redis와 PostgreSQL 저장소(와 in-memory fake)가 다르게
답한다.
- owner token이 같아도 호출이 claim과 다른 operation id를 대면 Redis는
  다섯 전이(start·renew·complete·fail·release)를 OPERATION_CONFLICT로
  거절하고 PostgreSQL·fake는 적용한다. PostgreSQL은 CAS에 핸들의 claim
  operation만 쓰고 호출의 id는 last_transition_operation_id에 적기만 한다.
- PostgreSQL complete 재생은 operation id만 보고 owner를 보지 않아, 같은
  id를 쓴 다른 attempt의 늦은 complete를 이미 적용된 완료로 확인한다.
  Redis는 NOT_OWNER.
- fake의 claim·inspect는 같은 token·다른 operation에 OwnerOperationConflict
  ·OPERATION_CONFLICT를 내지 않는다. PostgreSQL·fake의 inspect는 완료된
  기록이면 그 응답을 재생하고 Redis는 OPERATION_CONFLICT.
- 기록이 없는 release에 Redis는 ALREADY_RELEASED_SAME_OPERATION, PostgreSQL
  ·fake는 ABSENT. Redis release는 revision을 보지 않아, 같은 attempt가 claim을
  넘겨받기 전 핸들로 지금의 claim을 지운다.

방법: 소유자 결정 (a)로 PostgreSQL·fake를 Redis에 맞춘다. owner token이
같고 호출 id가 기록의 claim id와 다르면 다섯 전이 모두 OPERATION_CONFLICT,
기록은 그대로. 나머지 셋은 "거절이 안전한 쪽, 운영 호출자 경로가 안 바뀌는
쪽"으로 정했다.
- complete 재생은 같은 owner token에만 확인한다(Redis에 맞춤).
- fake claim은 진행 중 기록에 OwnerOperationConflict, inspect는 같은 token·
  다른 operation이면 상태와 상관없이 OPERATION_CONFLICT. PostgreSQL inspect도
  완료 재생보다 이 검사를 먼저 한다.
- 기록 없는 release는 셋 다 ABSENT. Redis adapter가 ABSENT를 그대로 넘기고
  RELEASE 스크립트와 command fake가 핸들 revision을 본다. release한 기록을
  지울지 남길지는 저장소 선택이라 release 재시도는 ABSENT(Redis·fake)와
  ALREADY_RELEASED_SAME_OPERATION(PostgreSQL)으로 갈리는 것을 계약이 허용한다.
버린 대안: Redis를 PostgreSQL에 맞추기(Redis에는 전이 id를 둘 필드가 없어
인자가 죽는다), PostgreSQL release를 삭제로 바꾸기(release 뒤 기록 모양은
계약이 저장소 선택으로 정해 두었다), 기록 없는 release를 셋 다
ALREADY_RELEASED로(찾지 못한 것을 성공으로 확인한다 — JPA 설계 22.2와 반대).
공유 계약 IdempotencyStoreContract에 12케이스를 더했다. 포트·결과 enum
Javadoc, persistence-jpa·cache-redis README, TESTING_STRATEGY, application-core
CLAUDE.md, 결정 색인, 업그레이드 안내 5절을 같이 고쳤다.

위험·한계: 템플릿의 호출자(IdempotencyExecutor)는 늘 claim의 id를 넘기고
release를 부르지 않아 HTTP 동작은 같다. store 포트를 직접 부르며 전이마다
다른 id를 넘기던 코드는 OPERATION_CONFLICT를 받는다. PostgreSQL 통합
테스트가 그렇게 쓰고 있어 claim의 id로 바꿨다. last_transition_operation_id
열은 이제 늘 claim_operation_id와 같다(마이그레이션은 건드리지 않음).
같은 token의 넘겨받기 전 핸들로 이미 끝난 complete·markFailed를 다시 보내는
경우는 저장소마다 답이 다르다(범위 밖, 보고에 적음).

검증: desktop, 공유 잠금, 기준 06f520e6.
- RED(기준 커밋 + 계약만): in-memory fake 계약 49 중 8 실패, Redis
  command fake 2, 실 Redis(redisContainerContractTest) 2, 실 PostgreSQL
  (jpaPlatformContractTest) 7 — 실패 메시지가 위 차이 그대로("expected:
  OPERATION_CONFLICT but was: STARTED", "expected: ABSENT but was:
  ALREADY_RELEASED_SAME_OPERATION" 등).
- GREEN(이 커밋과 같은 트리): 네 leaf check(application-core 412·skip 19
  시간 케이스, persistence-jpa 664, cache-redis 215, redis-runtime 65),
  네 leaf testQualityCheck, jpaPlatformContractTest 419,
  postgresqlIdempotencyIntegrationTest 10, redisContainerContractTest 90,
  app-bootstrap integrationTest 10, sample integrationTest 63,
  documentContractTest 65, architectureTest 228, composition:idempotency
  check 32 — 모두 rc=0, 실패·skip 0(fake 시간 케이스 19 제외).
- scripts/check-work-docs.py, --links docs, git diff --check 통과.

Refs: docs/reviews/2026-11-03-rereview/round6/02-idempotency-outbound.md Declined to judge
BREAKING CHANGE: 멱등 store 포트를 직접 부르는 코드는 전이마다 claim의
operation id(IdempotencyOwner.claimOperationId())를 넘겨야 한다. 다른 id는
OPERATION_CONFLICT다. Redis release는 기록이 없으면 ABSENT를 돌려준다.
직접 구현한 store는 IdempotencyStoreContract의 새 케이스를 통과해야 한다.
This commit is contained in:
DongHyeonka
2026-11-06 22:25:45 +09:00
parent 06f520e667
commit 200019aa5c
20 changed files with 437 additions and 47 deletions
+6 -2
View File
@@ -252,11 +252,15 @@ source set에서 태그 `jpa-contract`로 `jpaPlatformContractTest` lane이 돌
갖게 고쳤다. `committed()`는 이제 실제로 커밋된 경계만 담는다(바깥이 롤백되면 안쪽도 빠진다).
- PostgreSQL 멱등성 store는 (1) 이전 revision의 핸들을 `OPERATION_CONFLICT`로, Redis·fake는
`NOT_OWNER`로 답했다. 핸들의 owner 튜플에는 revision이 들어가므로 `NOT_OWNER`로 맞췄다.
`OPERATION_CONFLICT`는 "같은 operation id가 다른 인자로 이미 적용됨" 한 뜻만 남는다. (2) 실행
그때 `OPERATION_CONFLICT`는 "같은 operation id가 다른 인자로 이미 적용됨" 한 뜻만 남겼다. 2026-11-06 소유자
결정으로 뜻이 하나 늘었다: owner token이 같아도 호출이 claim과 다른 operation id를 대면 다섯 전이 모두
`OPERATION_CONFLICT`다(Redis만 거절하고 PostgreSQL·fake는 적용했다). (2) 실행
전(CLAIMED) claim의 renew를 받아 줬고 Redis·fake는 거부했다. renew는 실행 중인 작업의 lease만
늘리도록 PostgreSQL을 맞췄다(호출자 없음 — 템플릿 안 동작 변화 없음). (3) release 뒤의 다음
claim을 `TakenOverClaimed`(attempt 2)로, Redis는 `Acquired`로 답한다. executor는 둘을 같게
처리하므로 계약은 "다음 claim이 scope를 가진다"만 요구한다.
처리하므로 계약은 "다음 claim이 scope를 가진다"만 요구한다. 같은 이유로 release 재시도는 PostgreSQL이
`ALREADY_RELEASED_SAME_OPERATION`, Redis·fake가 `ABSENT`다. 기록이 없으면 셋 다 `ABSENT`다(Redis는 전에
`ALREADY_RELEASED_SAME_OPERATION`으로 답해 확인할 수 없는 해제를 확인했다, 2026-11-06).
- `OutboxAppendContract`는 static helper 두 개였고 JPA readiness 테스트만 불렀다. 추상 계약으로 바꿔
readiness 테스트가 상속한다(같은 두 케이스 + 트랜잭션 밖·읽기 전용·중복 eventId). 실 PostgreSQL에서
`inRead` 안의 append가 예외 없이 사라지는 것을 찾았다(실측: 예외 없음, 행 없음) — JPA 트랜잭션
+1
View File
@@ -238,6 +238,7 @@ V21 실패의 분류는 `docs/runbooks/migration-failed.md` "Classify the failur
| 멱등성(lease가 지난 실행) | lease가 지난 `EXECUTING` 기록을 claim이 만나면 기록을 바꾸지 않고 500 `IDEMPOTENCY_RECOVERY_REQUIRED`로 답한다. 그 실행의 주인이 늦게 부른 `complete`·`markFailed`는 기록된다. 전에는 두 저장소 모두 기록을 `ABANDONED`로 바꿔(PostgreSQL은 `last_transition_kind` `EXPIRED_EXECUTION`) 늦은 결과를 거절했고 첫 요청도 500이었다. 이미 끝난 lease의 `renew`는 두 저장소 모두 거절한다(Redis는 전에 받아들였다). 남의 lease 지난 실행을 `inspect`하면 `ABANDONED`(전에는 `IN_PROGRESS_OTHER`) | 30s를 넘는 정상 요청이 이제 결과를 남기고 정상 응답을 받는다. 같은 키 재전송은 500을 받은 뒤 재생을 받을 수 있다. 직접 구현한 store는 `IdempotencyStoreContract`의 바뀐 시간 케이스로 확인한다. 운영자는 `EXECUTING` 기록을 지우지 않는다 — 살아 있는 실행의 늦은 결과가 기록을 못 찾아(`ABSENT`) 사라진다(`docs/runbooks/idempotency-unavailable.md`) | `70fb80bf`(2026-11-04 소유자 결정) · `src/application-core/src/testFixtures/.../IdempotencyStoreContract.java`, `src/adapter/outbound/cache-redis/.../idempotency/IdempotencyScripts.java`, `src/adapter/outbound/persistence-jpa/.../postgresql/PostgreSqlIdempotencyStore.java` |
| 멱등성(lease가 끝난 시작, r9) | lease가 지난 `CLAIMED`는 주인도 시작할 수 없다(`LEASE_EXPIRED`). executor는 작업을 돌리지 않고 503 `IDEMPOTENCY_UNAVAILABLE`(실행 전 거부, 메시지 `the claim's processing lease ran out before the action started`)로 답하고, 다음 같은 키 요청이 그 claim을 넘겨받아 실행한다. 전에는 Redis가 시작해 작업을 돌렸고 그동안 다른 요청은 lease가 지난 실행으로 500을 받았다. PostgreSQL은 `NOT_OWNER`로 거절해 500 `IDEMPOTENCY_RECOVERY_REQUIRED`였다. | claim과 시작 사이가 lease(기본 30s)보다 길었던 드문 요청이 500 대신 503(같은 키 재전송)을 받는다. 클라이언트는 `error.code`로 이미 가르므로 바꿀 것이 없다. 운영자는 이 503에서 저장소 장애를 찾지 않는다(runbook) | r9(재리뷰 README 15 남음, RR5-01-02·03) · `IdempotencyExecutor.java`, `src/adapter/outbound/cache-redis/.../idempotency/IdempotencyScripts.java`, `src/adapter/outbound/persistence-jpa/.../postgresql/PostgreSqlIdempotencyStore.java` |
| 멱등성(같은 attempt의 재시도, r10) | lease가 지나 시작을 거절당한 claim과 재시도 가능 실패 기록은 같은 attempt로 다시 claim해도 넘겨받는다(`TakenOverClaimed`, attempt+1). 전에는 두 저장소가 그 claim을 재생해(`ReplayedAcquire`) 시작이 다시 거절되거나(503, 기록이 사라질 때까지) 시작할 수 없는 기록이라 500 `IDEMPOTENCY_RECOVERY_REQUIRED`였다. 효과 불명으로 포기한 기록은 같은 attempt에도 `RecoveryRequired`(PostgreSQL은 전에 `ReplayedAcquire`). Redis는 이미 실행 중인 기록에 같은 attempt의 다른 핸들(넘겨받기 전 핸들, 재생받은 현재 핸들)로 온 시작을 `ALREADY_STARTED_SAME_OPERATION`으로 확인하지 않는다 — 전에는 executor가 작업을 한 번 더 돌렸다(PostgreSQL은 전부터 거절). in-memory fake는 재시도 가능 실패 기록에 다른 요청 본문이 오면 넘겨주지 않고 `FingerprintMismatch` | attempt를 재시도 사이에 들고 있다가 503 뒤 같은 attempt로 다시 부르는 호출자(재시도 래퍼, 메시지 소비자)가 이제 실행된다. 요청마다 새 attempt를 만드는 호출자(템플릿의 HTTP 경로)는 바뀌는 것이 없다. 같은 attempt를 두 호출이 동시에 쓰는 것은 여전히 가려내지 못한다 — 재시도는 앞 호출이 돌아온 뒤에. 직접 구현한 store는 `IdempotencyStoreContract`의 새 케이스로 확인한다 | r10(라운드6 RR6-02-01) · `src/adapter/outbound/cache-redis/.../idempotency/IdempotencyScripts.java`, `src/adapter/outbound/persistence-jpa/.../postgresql/PostgreSqlIdempotencyStore.java`, `src/application-core/src/testFixtures/.../IdempotencyStoreContract.java`, `InMemoryIdempotencyStore.java` |
| 멱등성(전이의 operation id·release·inspect, 2026-11-06) | owner token이 같아도 호출이 claim과 다른 operation id를 대면 PostgreSQL·in-memory fake도 다섯 전이(start·renew·complete·fail·release)를 `OPERATION_CONFLICT`로 거절하고 기록을 바꾸지 않는다(전에는 적용했다. Redis는 전부터 거절). in-memory fake의 claim은 같은 token·다른 operation에 `OwnerOperationConflict`(전에는 `InProgress`), inspect는 `OPERATION_CONFLICT`(전에는 `IN_PROGRESS_OTHER`). PostgreSQL·fake의 inspect는 완료된 기록에도 `OPERATION_CONFLICT`(전에는 응답을 재생). PostgreSQL complete는 같은 operation id를 쓴 다른 attempt에 `NOT_OWNER`(전에는 `ALREADY_COMPLETED_SAME_RESULT`). Redis release는 기록이 없으면 `ABSENT`(전에는 `ALREADY_RELEASED_SAME_OPERATION`)이고 핸들 revision을 본다: 같은 attempt가 넘겨받기 전 핸들로 release하면 `NOT_OWNER`(전에는 지금의 claim을 지웠다) | 템플릿의 호출자(`IdempotencyExecutor`)는 늘 claim의 operation id를 넘기고 release를 부르지 않아 HTTP 동작은 같다. store 포트를 직접 부르며 전이마다 다른 operation id를 넘기던 코드는 이제 `OPERATION_CONFLICT`를 받는다 — `IdempotencyOwner.claimOperationId()`를 넘긴다. Redis release의 `ALREADY_RELEASED_SAME_OPERATION`을 기다리던 코드는 `ABSENT`도 "해제됨"으로 읽는다. 직접 구현한 store는 `IdempotencyStoreContract`의 새 케이스로 확인한다 | 2026-11-06(소유자 결정 "(a)로 진행", 라운드6 02 Declined) · `src/adapter/outbound/persistence-jpa/.../postgresql/PostgreSqlIdempotencyStore.java`, `src/adapter/outbound/cache-redis/.../idempotency/IdempotencyScripts.java`, `RedisIdempotencyStoreAdapter.java`, `src/application-core/src/testFixtures/.../IdempotencyStoreContract.java`, `InMemoryIdempotencyStore.java` |
| 빌드 단계(HTTP client) | `httpClientFailureInjectionTest`(Toxiproxy 장애 주입)가 `mainCheck`에서 빠졌다. `integrationCheck`에만 있고, main push·nightly의 `integration-main.yml` `integration-check` job이 돌린다 | main push마다 같은 lane이 `main-check`·`integration-check` 두 job에서 두 번 돌던 것이 한 번이 된다. 로컬에서 `mainCheck`만 돌리면 이 lane은 돌지 않는다(`integrationCheck`로 돌린다) | r9 · `src/adapter/outbound/httpclient/build.gradle` |
| 서버 로그(GraphQL·gRPC·STOMP) | carrier로 답한 실패를 web과 같은 줄(shared-contract `ServerFailureLog`)로 ERROR에 남긴다: persistence 실패는 분류만, 5xx carrier는 예외와 함께, 4xx carrier는 남기지 않는다. gRPC는 ` (method=…)`, GraphQL은 ` (executionId=…)`를 붙인다. gRPC는 인식 못 한 실패(`INTERNAL_ERROR`)도 `unhandled failure on <method> …`로 남긴다. 클라이언트가 취소한 호출은 남기지 않고, 서비스가 `CANCELLED` status로 끝낸 살아 있는 호출은 남긴다. raw status는 넘겼든 던졌든 설명을 남기지 않는다(원인 예외가 있으면 그것만 붙인다). `IllegalArgumentException`(`BAD_PARAMETER`)은 web과 같은 WARN 한 줄 `request refused as BAD_PARAMETER by <예외 타입>`(메시지 없음)으로 남긴다. STOMP `@MessageMapping` 실패는 Spring의 `Unhandled exception from message handler method`(예외 전체) 대신 같은 줄로 남는다 | ERROR 로그가 늘어난다(전에는 gRPC가 아무것도 남기지 않았다). GraphQL·gRPC·STOMP는 잘못된 입력마다 WARN 한 줄이 생긴다. STOMP에서 Spring 문구로 경보를 걸었다면 고친다. `IDEMPOTENCY_RECOVERY_REQUIRED`의 scope digest를 모든 전송에서 찾을 수 있다(`docs/runbooks/idempotency-unavailable.md`) | r9 · `src/shared-contract/.../error/ServerFailureLog.java`, `GrpcExceptionHandlingInterceptor.java`, `GraphQlWireErrorMapper.java`, `StompHandlerFailureLog.java` |
| sample-portfolio | 익명 쓰기 401, 포스터 제목 대소문자 무시 유일(409 `POSTER_ALREADY_EXISTS`), 관리자도 남의 work log 수정 불가, 샘플 이벤트 tenant `global` | sample을 남긴 서비스만 | `34565130`, `1400dc15` |
+4
View File
@@ -94,6 +94,10 @@
| 보존 기간 관계 | `failure-retention < replay-ttl`이면 기동 실패 | 소유자 | `1103` 15절 ③ |
| 같은 attempt 재claim | lease 지난 `CLAIMED`·`FAILED_RETRYABLE`을 넘겨받는다. 같은 attempt를 두 호출이 동시에 쓰는 것은 호출자 계약 | 기본값 | `1103` 17절 r10-02 |
| 다른 핸들의 시작 답 | PostgreSQL도 Redis와 같게 `NOT_OWNER`/`NOT_CLAIMED`로 답한다 | 기본값 | `1103` 17절 r10-02 |
| 전이의 operation id | owner token이 같아도 호출의 operation id가 기록된 claim의 operation id와 다르면 다섯 전이(start·renew·complete·fail·release) 모두 `OPERATION_CONFLICT`로 거절하고 기록은 그대로 둔다. PostgreSQL·in-memory fake를 Redis에 맞췄다. 그래서 `OPERATION_CONFLICT`는 "claim과 다른 id"와 "같은 id·다른 인자" 두 뜻이다 | 소유자(2026-11-06) | 2026-11-06 총괄 세션 전달(소유자 원문 "(a)로 진행"), `IdempotencyStoreContract` |
| 같은 token·다른 operation의 claim·inspect | claim은 진행 중인 기록에 `OwnerOperationConflict`, inspect는 상태와 상관없이(완료된 기록도) `OPERATION_CONFLICT`. 거절이 안전한 쪽을 골랐다 | 기본값(2026-11-06) | 위 결정과 같은 전달의 판단 원칙, `IdempotencyStoreContract` |
| complete 재생 판정 | 완료 확인은 같은 owner token에만 준다. 같은 operation id를 쓴 다른 attempt의 늦은 complete는 `NOT_OWNER` | 기본값(2026-11-06) | Redis TRANSITION에 맞춤, `IdempotencyStoreContract` |
| 기록 없는 release | 저장소 셋 다 `ABSENT`로 답한다(확인할 수 없는 해제를 확인하지 않는다). Redis release도 핸들 revision을 본다. release한 기록을 지울지 남길지는 저장소가 정하므로 release 재시도 답은 `ABSENT`(Redis·fake)와 `ALREADY_RELEASED_SAME_OPERATION`(PostgreSQL)으로 갈린다 | 기본값(2026-11-06) | JPA 설계 22.2 "affected row가 0이면 성공으로 간주하지 않는다", `IdempotencyStoreContract` |
| Redis 연결 예산 | 캐시 조건부 쓰기는 멱등성·lease와 다른 연결 예산(SCRIPT lane) | 위임 | `1103` 4절(RR-04-02) |
### 2.5 영속성
@@ -287,6 +287,15 @@ A lost renewal reply is recognised by what the renewal recorded — the revision
the lease it asked for: the same lease is `ALREADY_RENEWED_SAME_OPERATION` with the current handle,
a different lease is `OPERATION_CONFLICT`.
Every program compares the operation a call names with the one the record was claimed under, right
after the owner token: another operation is `OPERATION_CONFLICT` and changes nothing. The PostgreSQL
store and the in-memory fake used to apply it; they now answer the same (owner decision 2026-11-06,
`IdempotencyStoreContract`). The release program also checks the handle's revision, as the
transition does: owner token and operation alone let the handle from before the same attempt took
its claim over release the claim it holds now. A release deletes the record, so a release that finds
no record — its own retry, an expired record, one never claimed — is `ABSENT`; it used to be
confirmed as `ALREADY_RELEASED_SAME_OPERATION`, which the store cannot know.
A lease that runs out is taken over only from a claim that never started or an attempt marked
retryable — by whoever claims next, the holder's own attempt included: the claim program checks for
a takeover before it replays a same-owner claim, so an attempt whose start was refused takes its
@@ -267,7 +267,15 @@ public final class IdempotencyScripts {
return {'APPLIED', attempt, rev + 1, owner, '', ''}
""";
/** Release before execution: only from CLAIMED, and only by the owner that holds it. */
/**
* Release before execution: only from CLAIMED, and only by the owner that holds it at the
* revision its handle carries.
*
* <p>The release deletes the record, so a retry finds nothing and is {@code ABSENT}, as is a
* release whose record expired. The revision is checked as in {@link #TRANSITION}: the owner
* token and operation alone let the handle from before the same attempt took its claim over
* release the claim it holds now ({@code IdempotencyStoreContract}).
*/
private static final String RELEASE =
"""
local state = redis.call('HGET', KEYS[1], 'state')
@@ -284,6 +292,9 @@ public final class IdempotencyScripts {
if op ~= ARGV[3] then
return {'OPERATION_CONFLICT', attempt, rev, owner, '', ''}
end
if rev ~= tonumber(ARGV[2]) then
return {'NOT_OWNER', attempt, rev, owner, '', ''}
end
if state ~= 'CLAIMED' then
return {'WRONG_STATE', attempt, rev, owner, state, ''}
end
@@ -320,7 +320,10 @@ public final class RedisIdempotencyStoreAdapter implements IdempotencyStorePort
operationId.value())));
return switch (reply.status()) {
case APPLIED -> IdempotencyReleaseOutcome.RELEASED_BEFORE_EXECUTION;
case ABSENT -> IdempotencyReleaseOutcome.ALREADY_RELEASED_SAME_OPERATION;
// The release deletes the record, so nothing tells this release's retry from a record
// that expired or was never there. It was confirmed as the release already applied;
// the PostgreSQL store answers a missing record ABSENT (IdempotencyStoreContract).
case ABSENT -> IdempotencyReleaseOutcome.ABSENT;
case NOT_OWNER -> IdempotencyReleaseOutcome.NOT_OWNER;
case OPERATION_CONFLICT -> IdempotencyReleaseOutcome.OPERATION_CONFLICT;
case WRONG_STATE -> IdempotencyReleaseOutcome.EXECUTION_ALREADY_STARTED;
@@ -437,8 +437,13 @@ RR6-02-01). raw client key는 저장하지 않고 versioned HMAC scope digest만
Redis store·in-memory fake와 같은 계약(`IdempotencyStoreContract`)을 실 PostgreSQL에서 통과한다
(`PostgreSqlIdempotencyStoreContractTest`, `jpaPlatformContractTest`). 그 계약에 맞춰 두 가지를
정했다(review 2026-11-01 01-05). 이전 revision의 핸들은 owner 튜플이 다르므로 `NOT_OWNER`다
(`OPERATION_CONFLICT`는 같은 operation id가 다른 인자로 이미 적용된 경우만). 시작은 핸들 말고 인자가 없어서,
정했다(review 2026-11-01 01-05). 이전 revision의 핸들은 owner 튜플이 다르므로 `NOT_OWNER`다.
`OPERATION_CONFLICT`는 둘이다. 하나는 owner token이 같은데 호출이 claim과 다른 operation id를 댄 경우로, 다섯
전이(start·renew·complete·fail·release) 모두 거절하고 행을 바꾸지 않는다. 같은 token·다른 operation의 inspect도
상태와 상관없이(완료된 행도) `OPERATION_CONFLICT`다(2026-11-06 소유자 결정 — 전에는 CAS가 핸들의 claim
operation만 보고 호출의 id는 `last_transition_operation_id`에 적기만 해서 적용했다). 다른 하나는 같은 operation
id가 다른 인자로 이미 적용된 경우다. complete 재생은 같은 owner token에만 확인한다 — 같은 operation id를 쓴 다른
attempt의 늦은 complete는 `NOT_OWNER`다(전에는 operation id만 보고 `ALREADY_COMPLETED_SAME_RESULT`). 시작은 핸들 말고 인자가 없어서,
이미 기록된 시작과 digest가 다르면 다른 핸들이다 — 넘겨받기 전 핸들은 `NOT_OWNER`, 재생받은 현재 핸들은
`NOT_CLAIMED`(라운드6 RR6-02-01, 전에는 `OPERATION_CONFLICT`). renew는 `EXECUTING`의
lease만 늘리고, 시작 전 `CLAIMED`는 `NOT_IN_PROGRESS`다.
@@ -183,6 +183,9 @@ public final class PostgreSqlIdempotencyStore implements IdempotencyStorePort {
if (row == null) {
return startResult(IdempotencyStartOutcome.ABSENT, null);
}
if (namesAnotherOperation(row, owner, operationId)) {
return startResult(IdempotencyStartOutcome.OPERATION_CONFLICT, null);
}
String startDigest = transitionDigest("START", operationId, owner);
// A start carries no argument but the handle, so a recorded start with another digest was
// applied from another handle of the same operation — one from before a takeover by the same
@@ -220,6 +223,9 @@ public final class PostgreSqlIdempotencyStore implements IdempotencyStorePort {
if (row == null) {
return renewResult(IdempotencyRenewOutcome.ABSENT, null);
}
if (namesAnotherOperation(row, owner, operationId)) {
return renewResult(IdempotencyRenewOutcome.OPERATION_CONFLICT, null);
}
// The lease TTL is part of what a renewal decided, so it is part of the digest. Without it, a
// retry asking for a different lease was confirmed as the renewal already applied, and the
// caller went on believing it held the record for longer than the row says it does.
@@ -267,9 +273,16 @@ public final class PostgreSqlIdempotencyStore implements IdempotencyStorePort {
if (row == null) {
return IdempotencyCompleteOutcome.ABSENT;
}
if (namesAnotherOperation(row, owner, operationId)) {
return IdempotencyCompleteOutcome.OPERATION_CONFLICT;
}
String responseDigest = sha256(response.payload());
// A completion is reported back to its own owner only. The operation id alone confirmed the
// late completion of another attempt that carried the same id, which the Redis store refuses
// as NOT_OWNER (IdempotencyStoreContract).
if (row.state() == IdempotencyState.COMPLETED
&& "COMPLETE".equals(row.lastTransitionKind())
&& row.ownerToken().equals(owner.ownerToken())
&& operationId.value().equals(row.lastTransitionOperationId())) {
return responseDigest.equals(row.responseDigest())
? IdempotencyCompleteOutcome.ALREADY_COMPLETED_SAME_RESULT
@@ -315,6 +328,9 @@ public final class PostgreSqlIdempotencyStore implements IdempotencyStorePort {
if (row == null) {
return IdempotencyFailOutcome.ABSENT;
}
if (namesAnotherOperation(row, owner, operationId)) {
return IdempotencyFailOutcome.OPERATION_CONFLICT;
}
String transitionKind =
disposition == IdempotencyFailureDisposition.NO_EFFECT_RETRYABLE
? "FAIL_RETRYABLE"
@@ -372,6 +388,9 @@ public final class PostgreSqlIdempotencyStore implements IdempotencyStorePort {
if (row == null) {
return IdempotencyReleaseOutcome.ABSENT;
}
if (namesAnotherOperation(row, owner, operationId)) {
return IdempotencyReleaseOutcome.OPERATION_CONFLICT;
}
String releaseDigest = transitionDigest("RELEASE", operationId, owner);
switch (replayVerdict(row, "RELEASE", operationId, releaseDigest)) {
case SAME_ARGUMENTS -> {
@@ -411,6 +430,11 @@ public final class PostgreSqlIdempotencyStore implements IdempotencyStorePort {
return IdempotencyInspection.outcome(IdempotencyInspectionOutcome.FINGERPRINT_MISMATCH);
}
boolean sameAttempt = sameClaimAttempt(row, request.claimAttempt());
// The owner token under another operation is refused in every state, a completed record
// included, as the Redis store refuses it; the response used to be replayed to it here.
if (row.ownerToken().equals(request.claimAttempt().ownerToken()) && !sameAttempt) {
return IdempotencyInspection.outcome(IdempotencyInspectionOutcome.OPERATION_CONFLICT);
}
if (row.state() == IdempotencyState.COMPLETED && row.responsePayload() != null) {
return new IdempotencyInspection(
IdempotencyInspectionOutcome.COMPLETED_REPLAY,
@@ -425,9 +449,6 @@ public final class PostgreSqlIdempotencyStore implements IdempotencyStorePort {
if (sameAttempt && row.state() == IdempotencyState.EXECUTING) {
return inspectionWithOwner(IdempotencyInspectionOutcome.EXECUTING_SAME_OPERATION, row);
}
if (row.ownerToken().equals(request.claimAttempt().ownerToken()) && !sameAttempt) {
return IdempotencyInspection.outcome(IdempotencyInspectionOutcome.OPERATION_CONFLICT);
}
// Somebody else's execution whose lease ran out is reported as a claim would answer it. The
// database's clock decides, as it does for the claim; the read takes no lock, so a lease that
// ends between the row and the clock is reported as ended, which is what a claim would see.
@@ -452,9 +473,25 @@ public final class PostgreSqlIdempotencyStore implements IdempotencyStorePort {
return new IdempotencyClaimOutcome.TakenOverClaimed(owner(reset), reset.processingLeaseUntil());
}
/**
* Whether the holder of the record names an operation other than the one it was claimed under.
*
* <p>Every transition names the claim's operation, and a holder naming another one is refused
* before anything else is read: nothing changes, as in the Redis store and the in-memory fake. It
* used to be applied — the owner tuple took the claim's operation from the handle, and the named
* one was only written to {@code last_transition_operation_id} (owner decision 2026-11-06, {@code
* IdempotencyStoreContract}). Another owner token is still {@code NOT_OWNER}.
*/
private static boolean namesAnotherOperation(
IdempotencyRecordRow row, IdempotencyOwner owner, OperationId operationId) {
return row.ownerToken().equals(owner.ownerToken())
&& !row.claimOperationId().equals(operationId.value());
}
// A handle at an older revision is a stale owner tuple, so it is NOT_OWNER — what the Redis store
// and the in-memory fake answer (IdempotencyStoreContract). OPERATION_CONFLICT keeps its one
// meaning: the same operation id already applied this transition with different arguments.
// and the in-memory fake answer (IdempotencyStoreContract). OPERATION_CONFLICT means the holder
// named another operation than the claim's (namesAnotherOperation), or the same operation id
// already applied this transition with different arguments.
private IdempotencyStartOutcome classifyStartMismatch(
IdempotencyRecordRow row, IdempotencyOwner owner) {
if (!sameOwnerIdentity(row, owner)) {
@@ -91,8 +91,7 @@ class PostgreSqlIdempotencyIntegrationTest {
ignored -> {
IdempotencyOwner owner =
((IdempotencyClaimOutcome.Acquired) store.claim(request)).owner();
owner =
store.markExecutionStarted(owner, new OperationId("start-1")).owner().orElseThrow();
owner = store.markExecutionStarted(owner, owner.claimOperationId()).owner().orElseThrow();
jdbc.update(
"insert into idempotency_business_probe(scope_hash, mutation_count) values (?, 1)",
SCOPE.digest());
@@ -102,7 +101,7 @@ class PostgreSqlIdempotencyIntegrationTest {
owner,
new StoredResponse("{\"workLogId\":\"42\"}"),
Duration.ofHours(24),
new OperationId("complete-1")))
owner.claimOperationId()))
.isEqualTo(IdempotencyCompleteOutcome.COMPLETED);
});
@@ -157,7 +156,7 @@ class PostgreSqlIdempotencyIntegrationTest {
transactions
.execute(
ignored ->
store.markExecutionStarted(staleOwner, new OperationId("stale-start")))
store.markExecutionStarted(staleOwner, staleOwner.claimOperationId()))
.outcome())
.isEqualTo(IdempotencyStartOutcome.NOT_OWNER);
}
@@ -173,7 +172,7 @@ class PostgreSqlIdempotencyIntegrationTest {
store.claim(request(original, Duration.ofMillis(25))))
.owner();
return store
.markExecutionStarted(owner, new OperationId("start-executing"))
.markExecutionStarted(owner, owner.claimOperationId())
.owner()
.orElseThrow();
});
@@ -217,7 +216,7 @@ class PostgreSqlIdempotencyIntegrationTest {
((IdempotencyClaimOutcome.Acquired) store.claim(firstRequest)).owner();
owner =
store
.markExecutionStarted(owner, new OperationId("start-first"))
.markExecutionStarted(owner, owner.claimOperationId())
.owner()
.orElseThrow();
jdbc.update(
@@ -229,7 +228,7 @@ class PostgreSqlIdempotencyIntegrationTest {
owner,
new StoredResponse("done"),
Duration.ofHours(1),
new OperationId("complete-first")))
owner.claimOperationId()))
.isEqualTo(IdempotencyCompleteOutcome.COMPLETED);
firstHasCompletedInsideTransaction.countDown();
await(allowFirstCommit);
@@ -284,7 +283,7 @@ class PostgreSqlIdempotencyIntegrationTest {
((IdempotencyClaimOutcome.Acquired) store.claim(firstRequest)).owner();
IdempotencyOwner executing =
store
.markExecutionStarted(owner, new OperationId("start-slow"))
.markExecutionStarted(owner, owner.claimOperationId())
.owner()
.orElseThrow();
firstStarted.countDown();
@@ -293,7 +292,7 @@ class PostgreSqlIdempotencyIntegrationTest {
executing,
new StoredResponse("slow-but-done"),
Duration.ofHours(1),
new OperationId("complete-slow"));
executing.claimOperationId());
});
return null;
});
@@ -346,7 +345,7 @@ class PostgreSqlIdempotencyIntegrationTest {
((IdempotencyClaimOutcome.Acquired) store.claim(firstRequest)).owner();
IdempotencyOwner executing =
store
.markExecutionStarted(owner, new OperationId("start-fails"))
.markExecutionStarted(owner, owner.claimOperationId())
.owner()
.orElseThrow();
firstStarted.countDown();
@@ -357,7 +356,7 @@ class PostgreSqlIdempotencyIntegrationTest {
executing,
IdempotencyFailureDisposition.EFFECT_UNKNOWN_ABANDONED,
Duration.ofHours(1),
new OperationId("fail-unknown"));
executing.claimOperationId());
throw new IllegalStateException("the action failed");
});
return null;
@@ -383,7 +382,7 @@ class PostgreSqlIdempotencyIntegrationTest {
@Test
void aRetriedFailureWithTheSameArgumentsIsConfirmedRatherThanAppliedTwice() {
IdempotencyOwner executing = claimAndStart("claim-fail-replay", "start-fail-replay");
IdempotencyOwner executing = claimAndStart("claim-fail-replay");
IdempotencyFailOutcome first =
transactions.execute(
@@ -392,7 +391,7 @@ class PostgreSqlIdempotencyIntegrationTest {
executing,
IdempotencyFailureDisposition.NO_EFFECT_RETRYABLE,
Duration.ofHours(1),
new OperationId("fail-1")));
executing.claimOperationId()));
IdempotencyFailOutcome retry =
transactions.execute(
ignored ->
@@ -400,7 +399,7 @@ class PostgreSqlIdempotencyIntegrationTest {
executing,
IdempotencyFailureDisposition.NO_EFFECT_RETRYABLE,
Duration.ofHours(1),
new OperationId("fail-1")));
executing.claimOperationId()));
assertThat(first).isEqualTo(IdempotencyFailOutcome.MARKED_RETRYABLE);
assertThat(retry).isEqualTo(IdempotencyFailOutcome.ALREADY_MARKED_SAME_OPERATION);
@@ -408,7 +407,7 @@ class PostgreSqlIdempotencyIntegrationTest {
@Test
void aFailureRepeatedUnderOneOperationIdWithDifferentRetentionIsAConflict() {
IdempotencyOwner executing = claimAndStart("claim-fail-conflict", "start-fail-conflict");
IdempotencyOwner executing = claimAndStart("claim-fail-conflict");
transactions.execute(
ignored ->
@@ -416,7 +415,7 @@ class PostgreSqlIdempotencyIntegrationTest {
executing,
IdempotencyFailureDisposition.NO_EFFECT_RETRYABLE,
Duration.ofHours(1),
new OperationId("fail-2")));
executing.claimOperationId()));
IdempotencyFailOutcome different =
transactions.execute(
ignored ->
@@ -424,7 +423,7 @@ class PostgreSqlIdempotencyIntegrationTest {
executing,
IdempotencyFailureDisposition.NO_EFFECT_RETRYABLE,
Duration.ofHours(9),
new OperationId("fail-2")));
executing.claimOperationId()));
assertThat(different)
.as(
@@ -443,25 +442,25 @@ class PostgreSqlIdempotencyIntegrationTest {
@Test
void aRenewalRepeatedUnderOneOperationIdWithADifferentLeaseIsAConflict() {
// Started first: only an executing operation's lease is renewed (IdempotencyStoreContract).
IdempotencyOwner executing = claimAndStart("claim-renew-conflict", "start-renew-conflict");
IdempotencyOwner executing = claimAndStart("claim-renew-conflict");
IdempotencyRenewOutcome first =
transactions
.execute(
ignored ->
store.renew(executing, Duration.ofSeconds(30), new OperationId("renew-1")))
store.renew(executing, Duration.ofSeconds(30), executing.claimOperationId()))
.outcome();
IdempotencyRenewOutcome same =
transactions
.execute(
ignored ->
store.renew(executing, Duration.ofSeconds(30), new OperationId("renew-1")))
store.renew(executing, Duration.ofSeconds(30), executing.claimOperationId()))
.outcome();
IdempotencyRenewOutcome different =
transactions
.execute(
ignored ->
store.renew(executing, Duration.ofMinutes(45), new OperationId("renew-1")))
store.renew(executing, Duration.ofMinutes(45), executing.claimOperationId()))
.outcome();
assertThat(first).isEqualTo(IdempotencyRenewOutcome.RENEWED);
@@ -488,10 +487,12 @@ class PostgreSqlIdempotencyIntegrationTest {
.owner());
}
private static IdempotencyOwner claimAndStart(String claimOperation, String startOperation) {
// Every transition names the operation its record was claimed under; these tests gave each
// transition an id of its own, which the store now refuses (owner decision 2026-11-06).
private static IdempotencyOwner claimAndStart(String claimOperation) {
IdempotencyOwner claimed = claim(claimOperation);
return transactions
.execute(ignored -> store.markExecutionStarted(claimed, new OperationId(startOperation)))
.execute(ignored -> store.markExecutionStarted(claimed, claimed.claimOperationId()))
.owner()
.orElseThrow();
}
@@ -366,6 +366,7 @@ public final class InMemorySemanticRedisCommands implements SemanticRedisCommand
private List<Object> idempotencyRelease(String rendered, List<byte[]> arguments) {
String ownerToken = text(arguments.get(0));
long expectedRev = Long.parseLong(text(arguments.get(1)));
String operationId = text(arguments.get(2));
String state = field(rendered, "state");
if (state == null) {
@@ -380,6 +381,9 @@ public final class InMemorySemanticRedisCommands implements SemanticRedisCommand
if (!operationId.equals(field(rendered, "op"))) {
return reply("OPERATION_CONFLICT", attempt, rev, owner, "", "");
}
if (rev != expectedRev) {
return reply("NOT_OWNER", attempt, rev, owner, "", "");
}
if (!"CLAIMED".equals(state)) {
return reply("WRONG_STATE", attempt, rev, owner, state, "");
}
+3
View File
@@ -288,6 +288,9 @@ generation; the V1 `IdempotencyScope`/`tryBegin` API is gone.
(owner decision 2026-11-04, re-review RR5-01-02·03). A record lives for the lease plus the replay TTL. `IdempotencyStoreContract` holds Redis and PostgreSQL to both on
real servers; the in-memory fake models no time, so it runs only the contract's time-free cases
(re-review 2026-11-04 RR-01-11, RR-03-11, RR3-03-04).
- Every transition names the operation its record was claimed under (`IdempotencyOwner.claimOperationId()`).
Another one is `OPERATION_CONFLICT` in every store and changes nothing; so is an inspection that
brings the owner token under another operation (owner decision 2026-11-06).
The former ArchUnit freeze rule `inbound_port_implementations_do_not_declare_keyed_idempotency`
and its fixture were removed when this branch merged.
@@ -8,6 +8,10 @@ public enum IdempotencyCompleteOutcome {
ABSENT,
NOT_OWNER,
NOT_IN_PROGRESS,
/**
* The call names an operation other than the one the record was claimed under; nothing was
* recorded (owner decision 2026-11-06).
*/
OPERATION_CONFLICT,
INDETERMINATE,
UNAVAILABLE
@@ -8,6 +8,11 @@ public enum IdempotencyFailOutcome {
ABSENT,
NOT_OWNER,
NOT_IN_PROGRESS,
/**
* The call names an operation other than the one the record was claimed under (owner decision
* 2026-11-06), or this failure was already recorded with another disposition or retention.
* Nothing changed.
*/
OPERATION_CONFLICT,
INDETERMINATE,
UNAVAILABLE
@@ -16,6 +16,10 @@ public enum IdempotencyInspectionOutcome {
*/
ABANDONED,
FINGERPRINT_MISMATCH,
/**
* The attempt's owner token holds the record under another operation. It is the answer in every
* state, a completed record included: its response is not replayed to it.
*/
OPERATION_CONFLICT,
UNAVAILABLE
}
@@ -3,10 +3,19 @@ package dev.caskeleton.application.idempotency;
/** Outcomes of releasing a claim only while business execution has not started. */
public enum IdempotencyReleaseOutcome {
RELEASED_BEFORE_EXECUTION,
/** This release was already applied: a store that keeps released records recognises it. */
ALREADY_RELEASED_SAME_OPERATION,
/**
* No record exists: it expired, was never there, or was released by a store that deletes what it
* releases — that store cannot tell its own release's retry apart, so it confirms nothing.
*/
ABSENT,
NOT_OWNER,
EXECUTION_ALREADY_STARTED,
/**
* The call names an operation other than the one the record was claimed under; nothing was
* released (owner decision 2026-11-06).
*/
OPERATION_CONFLICT,
INDETERMINATE,
UNAVAILABLE
@@ -29,7 +29,10 @@ public enum IdempotencyRenewOutcome {
* with the same handle are still recorded (owner decision 2026-11-04).
*/
LEASE_EXPIRED(false),
/** The same operation id already renewed with different arguments. */
/**
* The call names an operation other than the one the record was claimed under (owner decision
* 2026-11-06), or this renewal was already applied asking for a different lease. Nothing changed.
*/
OPERATION_CONFLICT(false),
/** The store may have applied the renewal; inspect before relying on either answer. */
INDETERMINATE(false),
@@ -33,7 +33,11 @@ public enum IdempotencyStartOutcome {
* store started it).
*/
LEASE_EXPIRED(false),
/** The same operation id already started this record with different arguments. */
/**
* The handle's owner token holds the record, but the call names an operation other than the one
* the record was claimed under; nothing changed. Every store refuses it (owner decision
* 2026-11-06 — the PostgreSQL store started it).
*/
OPERATION_CONFLICT(false),
/** The store may have applied the start; inspect before relying on either answer. */
INDETERMINATE(false),
@@ -21,6 +21,14 @@ import java.time.Duration;
* record from its holder. Only the record's own lifetime ends it: once it is gone, every call
* answers {@code ABSENT} (owner decision 2026-11-04; re-review 2026-11-04 RR5-01-02·03; {@code
* IdempotencyStoreContract}).
*
* <p>Every transition names the operation its record was claimed under — the handle's {@link
* IdempotencyOwner#claimOperationId()}. A holder naming another one is answered {@code
* OPERATION_CONFLICT} and nothing changes; a claim that brings the owner token under another
* operation to a record still in progress is {@link
* IdempotencyClaimOutcome.OwnerOperationConflict}, and an inspection is {@code OPERATION_CONFLICT}
* in every state (owner decision 2026-11-06). For renew and failure {@code OPERATION_CONFLICT} also
* still means the same transition was already applied with other arguments.
*/
public interface IdempotencyStorePort {
@@ -514,6 +514,244 @@ public abstract class IdempotencyStoreContract {
.isGreaterThan(claimed.stateRevision());
}
// Every transition names the operation its record was claimed under. A holder naming another one
// is refused as OPERATION_CONFLICT and the record is left as it was: the Redis store refused it,
// the PostgreSQL store and the in-memory fake applied it — the PostgreSQL store wrote the named
// operation beside the claim's and read the claim's from the handle (owner decision 2026-11-06).
@Test
void startingUnderAnotherOperationThanTheClaimIsAConflictThatStartsNothing() {
// given
IdempotencyStorePort store = subject();
IdempotencyOwner claimed = acquire(store);
// when
IdempotencyMutationResult<IdempotencyStartOutcome> conflict =
store.markExecutionStarted(claimed, OTHER_OPERATION);
// then
assertThat(conflict.outcome()).isEqualTo(IdempotencyStartOutcome.OPERATION_CONFLICT);
assertThat(store.markExecutionStarted(claimed, OPERATION).outcome())
.isEqualTo(IdempotencyStartOutcome.STARTED);
}
@Test
void renewingUnderAnotherOperationThanTheClaimIsAConflictThatRenewsNothing() {
// given
IdempotencyStorePort store = subject();
IdempotencyOwner executing = executing(store);
// when
IdempotencyMutationResult<IdempotencyRenewOutcome> conflict =
store.renew(executing, Duration.ofSeconds(30), OTHER_OPERATION);
// then
assertThat(conflict.outcome()).isEqualTo(IdempotencyRenewOutcome.OPERATION_CONFLICT);
// The revision did not move, so the handle the start returned still completes the record.
assertThat(store.complete(executing, new StoredResponse("{}"), Duration.ofHours(1), OPERATION))
.isEqualTo(IdempotencyCompleteOutcome.COMPLETED);
}
@Test
void completingUnderAnotherOperationThanTheClaimIsAConflictThatRecordsNothing() {
// given
IdempotencyStorePort store = subject();
IdempotencyOwner executing = executing(store);
// when
IdempotencyCompleteOutcome conflict =
store.complete(
executing, new StoredResponse("{\"v\":1}"), Duration.ofHours(1), OTHER_OPERATION);
// then
assertThat(conflict).isEqualTo(IdempotencyCompleteOutcome.OPERATION_CONFLICT);
// No response was recorded: another one completes the record rather than conflicting with it.
assertThat(
store.complete(
executing, new StoredResponse("{\"v\":2}"), Duration.ofHours(1), OPERATION))
.isEqualTo(IdempotencyCompleteOutcome.COMPLETED);
}
@Test
void markingFailedUnderAnotherOperationThanTheClaimIsAConflictThatMarksNothing() {
// given
IdempotencyStorePort store = subject();
IdempotencyOwner executing = executing(store);
// when
IdempotencyFailOutcome conflict =
store.markFailed(
executing,
IdempotencyFailureDisposition.NO_EFFECT_RETRYABLE,
Duration.ofHours(1),
OTHER_OPERATION);
// then
assertThat(conflict).isEqualTo(IdempotencyFailOutcome.OPERATION_CONFLICT);
assertThat(store.complete(executing, new StoredResponse("{}"), Duration.ofHours(1), OPERATION))
.isEqualTo(IdempotencyCompleteOutcome.COMPLETED);
}
@Test
void releasingUnderAnotherOperationThanTheClaimIsAConflictThatReleasesNothing() {
// given
IdempotencyStorePort store = subject();
IdempotencyOwner claimed = acquire(store);
// when
IdempotencyReleaseOutcome conflict = store.releaseBeforeExecution(claimed, OTHER_OPERATION);
// then
assertThat(conflict).isEqualTo(IdempotencyReleaseOutcome.OPERATION_CONFLICT);
assertThat(store.markExecutionStarted(claimed, OPERATION).outcome())
.isEqualTo(IdempotencyStartOutcome.STARTED);
}
@Test
void anotherAttemptCompletingARecordCompletedUnderTheSameOperationIsNotItsOwner() {
// given
// One operation id on two attempts — a caller that keeps its operation id across resends. The
// second attempt took the retryable failure over and completed it; the first attempt's late
// completion is not that completion. The PostgreSQL store confirmed it, because its replay
// check read the operation id and not the owner.
IdempotencyStorePort store = subject();
IdempotencyOwner first = executing(store);
store.markFailed(
first, IdempotencyFailureDisposition.NO_EFFECT_RETRYABLE, Duration.ofHours(1), OPERATION);
IdempotencyOwner second =
started(store, ownerOf(store.claim(claim(store.newClaimAttempt(OPERATION), FINGERPRINT))));
store.complete(second, new StoredResponse("{\"v\":1}"), Duration.ofHours(1), OPERATION);
// when
IdempotencyCompleteOutcome late =
store.complete(first, new StoredResponse("{\"v\":1}"), Duration.ofHours(1), OPERATION);
// then
assertThat(late).isEqualTo(IdempotencyCompleteOutcome.NOT_OWNER);
}
@Test
void aClaimWithTheOwnerTokenUnderAnotherOperationIsAnOwnerOperationConflict() {
// given
IdempotencyStorePort store = subject();
IdempotencyClaimAttempt attempt = store.newClaimAttempt(OPERATION);
IdempotencyOwner claimed = ownerOf(store.claim(claim(attempt, FINGERPRINT)));
// when
IdempotencyClaimOutcome conflict =
store.claim(
claim(new IdempotencyClaimAttempt(attempt.ownerToken(), OTHER_OPERATION), FINGERPRINT));
// then
assertThat(conflict).isInstanceOf(IdempotencyClaimOutcome.OwnerOperationConflict.class);
assertThat(store.markExecutionStarted(claimed, OPERATION).outcome())
.isEqualTo(IdempotencyStartOutcome.STARTED);
}
@Test
void inspectingAClaimWithTheOwnerTokenUnderAnotherOperationIsAnOperationConflict() {
// given
IdempotencyStorePort store = subject();
IdempotencyClaimAttempt attempt = store.newClaimAttempt(OPERATION);
store.claim(claim(attempt, FINGERPRINT));
// when
IdempotencyInspection inspection =
store.inspect(
new IdempotencyInspectionRequest(
SCOPE,
FINGERPRINT,
new IdempotencyClaimAttempt(attempt.ownerToken(), OTHER_OPERATION)));
// then
assertThat(inspection.outcome()).isEqualTo(IdempotencyInspectionOutcome.OPERATION_CONFLICT);
}
@Test
void inspectingACompletedRecordWithTheOwnerTokenUnderAnotherOperationIsAnOperationConflict() {
// given
// In every state, a completed record included: the Redis store refused it, the PostgreSQL
// store and the in-memory fake replayed the response to it.
IdempotencyStorePort store = subject();
IdempotencyClaimAttempt attempt = store.newClaimAttempt(OPERATION);
store.complete(
started(store, ownerOf(store.claim(claim(attempt, FINGERPRINT)))),
new StoredResponse("{\"ok\":true}"),
Duration.ofHours(1),
OPERATION);
// when
IdempotencyInspection inspection =
store.inspect(
new IdempotencyInspectionRequest(
SCOPE,
FINGERPRINT,
new IdempotencyClaimAttempt(attempt.ownerToken(), OTHER_OPERATION)));
// then
assertThat(inspection.outcome()).isEqualTo(IdempotencyInspectionOutcome.OPERATION_CONFLICT);
}
@Test
void releasingAScopeWithNoRecordIsAbsent() {
// given
// A handle whose record is gone — expired, reaped — stands in here as one for a scope nobody
// claimed. Nothing was found, so nothing is confirmed: the Redis store answered that it had
// already released it.
IdempotencyStorePort store = subject();
IdempotencyOwner gone =
new IdempotencyOwner(SCOPE, store.newClaimAttempt(OPERATION).ownerToken(), 1, 1, OPERATION);
// when
IdempotencyReleaseOutcome released = store.releaseBeforeExecution(gone, OPERATION);
// then
assertThat(released).isEqualTo(IdempotencyReleaseOutcome.ABSENT);
}
@Test
void aReleaseRetriedAfterItWasAppliedReleasesNothingMore() {
// given
IdempotencyStorePort store = subject();
IdempotencyOwner claimed = acquire(store);
store.releaseBeforeExecution(claimed, OPERATION);
// when
IdempotencyReleaseOutcome again = store.releaseBeforeExecution(claimed, OPERATION);
// then
// A store that keeps the released record recognises the release (PostgreSQL); one that forgets
// it finds no record (Redis, the in-memory fake). Neither releases a second time.
assertThat(again)
.isIn(
IdempotencyReleaseOutcome.ALREADY_RELEASED_SAME_OPERATION,
IdempotencyReleaseOutcome.ABSENT);
}
@Test
void releasingThroughTheHandleFromBeforeTheAttemptTookItsClaimOverIsRefusedAsAStaleOwner() {
// given
// The same attempt took its retryable failure over, so the record is a claim again under the
// same owner token and operation. The Redis program read no revision and released it.
IdempotencyStorePort store = subject();
IdempotencyClaimAttempt attempt = store.newClaimAttempt(OPERATION);
IdempotencyOwner before = ownerOf(store.claim(claim(attempt, FINGERPRINT)));
store.markFailed(
started(store, before),
IdempotencyFailureDisposition.NO_EFFECT_RETRYABLE,
Duration.ofHours(1),
OPERATION);
IdempotencyOwner current = ownerOf(store.claim(claim(attempt, FINGERPRINT)));
// when
IdempotencyReleaseOutcome released = store.releaseBeforeExecution(before, OPERATION);
// then
assertThat(released).isEqualTo(IdempotencyReleaseOutcome.NOT_OWNER);
assertThat(store.markExecutionStarted(current, OPERATION).outcome())
.isEqualTo(IdempotencyStartOutcome.STARTED);
}
@Test
@DisplayName("실행 중인 자기 기록을 inspect하면 기록 수명이 아니라 claim이 받은 처리 lease 만료 시각이 나온다")
void inspectingAnOwnExecutionReportsItsProcessingLeaseNotTheRecordLifetime() {
@@ -34,7 +34,8 @@ import java.util.Random;
* attempt against an in-flight record is always {@code InProgress}. Leases and replay windows are
* reported as {@link Instant#MAX}. What it keeps exact is what a test of a caller relies on: one
* claim wins, the owner handle's revision advances on every transition, a completed response
* replays, a different fingerprint is refused, and a retryable failure can be claimed again.
* replays, a different fingerprint is refused, a retryable failure can be claimed again, and an
* owner naming an operation other than its claim's is refused.
*/
public final class InMemoryIdempotencyStore implements IdempotencyStorePort {
@@ -82,6 +83,10 @@ public final class InMemoryIdempotencyStore implements IdempotencyStorePort {
&& row.owner.claimOperationId().equals(request.claimAttempt().operationId())) {
return new IdempotencyClaimOutcome.ReplayedAcquire(row.owner, Instant.MAX);
}
// The owner token under another operation is refused, not reported in progress.
if (row.owner.ownerToken().equals(request.claimAttempt().ownerToken())) {
return new IdempotencyClaimOutcome.OwnerOperationConflict();
}
return new IdempotencyClaimOutcome.InProgress(Duration.ZERO, row.owner.attempt());
}
@@ -92,11 +97,13 @@ public final class InMemoryIdempotencyStore implements IdempotencyStorePort {
IdempotencyStartOutcome outcome =
row == null
? IdempotencyStartOutcome.ABSENT
: !row.owner.equals(owner)
? IdempotencyStartOutcome.NOT_OWNER
: row.state != IdempotencyState.CLAIMED
? IdempotencyStartOutcome.NOT_CLAIMED
: IdempotencyStartOutcome.STARTED;
: row.namesAnotherOperation(owner, operationId)
? IdempotencyStartOutcome.OPERATION_CONFLICT
: !row.owner.equals(owner)
? IdempotencyStartOutcome.NOT_OWNER
: row.state != IdempotencyState.CLAIMED
? IdempotencyStartOutcome.NOT_CLAIMED
: IdempotencyStartOutcome.STARTED;
if (outcome != IdempotencyStartOutcome.STARTED) {
return new IdempotencyMutationResult<>(outcome, null, IdempotencyStartOutcome::carriesOwner);
}
@@ -114,11 +121,13 @@ public final class InMemoryIdempotencyStore implements IdempotencyStorePort {
IdempotencyRenewOutcome refused =
row == null
? IdempotencyRenewOutcome.ABSENT
: !row.owner.equals(owner)
? IdempotencyRenewOutcome.NOT_OWNER
: row.state != IdempotencyState.EXECUTING
? IdempotencyRenewOutcome.NOT_IN_PROGRESS
: null;
: row.namesAnotherOperation(owner, operationId)
? IdempotencyRenewOutcome.OPERATION_CONFLICT
: !row.owner.equals(owner)
? IdempotencyRenewOutcome.NOT_OWNER
: row.state != IdempotencyState.EXECUTING
? IdempotencyRenewOutcome.NOT_IN_PROGRESS
: null;
if (refused != null) {
return new IdempotencyMutationResult<>(refused, null, IdempotencyRenewOutcome::carriesOwner);
}
@@ -137,6 +146,9 @@ public final class InMemoryIdempotencyStore implements IdempotencyStorePort {
if (row == null) {
return IdempotencyCompleteOutcome.ABSENT;
}
if (row.namesAnotherOperation(owner, operationId)) {
return IdempotencyCompleteOutcome.OPERATION_CONFLICT;
}
if (row.state == IdempotencyState.COMPLETED && owner.equals(row.completedBy)) {
// The completing handle retried, as after a lost reply: the same result is a no-op, another
// result is a conflict, exactly as the stores answer.
@@ -166,6 +178,9 @@ public final class InMemoryIdempotencyStore implements IdempotencyStorePort {
if (row == null) {
return IdempotencyFailOutcome.ABSENT;
}
if (row.namesAnotherOperation(owner, operationId)) {
return IdempotencyFailOutcome.OPERATION_CONFLICT;
}
if (!row.owner.equals(owner)) {
return IdempotencyFailOutcome.NOT_OWNER;
}
@@ -186,6 +201,9 @@ public final class InMemoryIdempotencyStore implements IdempotencyStorePort {
if (row == null) {
return IdempotencyReleaseOutcome.ABSENT;
}
if (row.namesAnotherOperation(owner, operationId)) {
return IdempotencyReleaseOutcome.OPERATION_CONFLICT;
}
if (!row.owner.equals(owner)) {
return IdempotencyReleaseOutcome.NOT_OWNER;
}
@@ -205,6 +223,11 @@ public final class InMemoryIdempotencyStore implements IdempotencyStorePort {
if (!row.fingerprint.equals(request.requestFingerprint())) {
return IdempotencyInspection.outcome(IdempotencyInspectionOutcome.FINGERPRINT_MISMATCH);
}
// The stores' order: the owner token under another operation is refused in every state.
if (row.owner.ownerToken().equals(request.claimAttempt().ownerToken())
&& !row.owner.claimOperationId().equals(request.claimAttempt().operationId())) {
return IdempotencyInspection.outcome(IdempotencyInspectionOutcome.OPERATION_CONFLICT);
}
if (row.state == IdempotencyState.COMPLETED) {
return new IdempotencyInspection(
IdempotencyInspectionOutcome.COMPLETED_REPLAY,
@@ -274,5 +297,15 @@ public final class InMemoryIdempotencyStore implements IdempotencyStorePort {
owner = owner.withStateRevision(owner.stateRevision() + 1);
state = next;
}
/**
* Whether the record's owner names an operation other than its claim's. The stores refuse that
* before anything else is read, whatever the handle's revision ({@code
* IdempotencyStoreContract}).
*/
boolean namesAnotherOperation(IdempotencyOwner caller, OperationId operationId) {
return owner.ownerToken().equals(caller.ownerToken())
&& !owner.claimOperationId().equals(operationId);
}
}
}