Files
document-haness/docs/clean-architecture-backend-template/tech-log-studio/state-machines-and-ownership/concept/concept-adapter-outbound-persistence-mongo-c06.md
T
DongHyeonkaandClaude Fable 5.1 b25357c48a docs(clean-architecture-backend-template): fold analysis into final and re-select one topic
- analysis/·source-index·state.json 을 final/document.md 제2부·제3부로 접었다. SSOT 는 하나다
- 파일럿 — commit-ambiguity-as-a-result 를 새 기준으로 재선별. 후보 14 → 글감 5
  (PROMOTE 5 · MERGE_INTO 3 · KEEP_IN_SSOT 4 · 보류 2). 기록 5건을 다시 썼고 그림 1개를
  techviz 로 만들었다
- 재선별이 잡은 것: 제1부 §6.2·§11.1 이 자기 §13.2 와 어긋나 있었다(레인을 안 돌렸다 vs
  돌렸다) — 정정. 이미 답이 나와 있던 Question 을 HEAD 재실행 질문으로 다시 세웠다.
  Concept 이 인용한 코드가 SSOT 에 없어 뺐다
- candidateScope·sourceRepository 기록. 나머지 43개 주제는 재선별 대기(PENDING 905)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-07 12:39:20 +09:00

53 lines
3.7 KiB
Markdown

---
kind: CONCEPT
slug: adapter-outbound-persistence-mongo-c06
title: 더 자주 갱신해도 고쳐지지 않는 문제라서 fencing을 얹는다
topic: state-machines-and-ownership
project: clean-architecture-backend-template
status: 게시 전
sourceRevision: 21234e38cdb9a926cbc92bb97a2aee2e4a7d2916
rootTreeNode: concept:adapter-outbound-persistence-mongo-c06
evidenceCapturedOn: 2026-09-01
assets:
- key: adapter-outbound-persistence-mongo-c06
file: ../../../final/evidence/rendered/adapter-outbound-persistence-mongo-c06.svg
evidence:
- ../../../final/evidence/raw/adapter-outbound-persistence-mongo-c06.txt
source:
- 원본 분석 절은 final/document.md#a06#L868 이다.
module: adapter-outbound-persistence-mongo
---
# 더 자주 갱신해도 고쳐지지 않는 문제라서 fencing을 얹는다
`MongoMigrationLock.fence()`의 javadoc이 lease 만료와 보유자 정지가 다르다는 것을 적는다 — 첫 runner는 갱신했어야 할 그 순간에 돌고 있지 않다.
## 본문
<!-- body:start -->
`MongoMigrationLock.fence()`의 javadoc이 이 sub-scope에서 가장 정확한 문장을 담고 있다.
> A lease expiring is not the same as its holder stopping. A runner paused inside a long `execute` — a stop-the-world pause, a stalled network write — loses the lease on the server while its thread is still alive and still writing… **Refreshing more often does not fix that: the first runner is not running at the moment it would refresh.**
그래서 lease 위에 monotonic fencing token을 얹고, `MongoCollectionMigrationLock.tryAcquire`가 그 token을 **lease를 부여하는 같은 조건부 update 안에서 서버가 증가**시킨다("A token handed out anywhere else could be handed out twice"). `held()`는 owner 이름이 같아도 fence가 다르면 false를 반환한다 — 프로세스가 재시작했거나 운영자가 owner 문자열을 재사용한 경우다.
## MongoCollectionMigrationLock 참조 위치
:::evidence key="adapter-outbound-persistence-mongo-c06" alt="코드베이스에서 MongoCollectionMigrationLock 를 검색한 출력 15줄. 이 기록이 세는 참조가 그 출력에 그대로 보인다." caption="MongoCollectionMigrationLock 코드베이스 검색 — 15줄 · exit 0" zoom="true"
:::
## heartbeat이 migration에게 넘겨진 이유
`matchedCount`를 쓰는 이유(같은 값을 다시 쓰면 `modifiedCount`가 0이라 소유권 판정이 뒤집힌다)도 두 곳에 적혀 있다. `MongoMigrationHeartbeat`은 이미 고쳐진 결함의 산물이다 — runner가 `execute`**반환된 뒤에** 한 번만 refresh했으므로, 40분짜리 `execute`는 35분 동안 만료된 lease를 들고 있었고 그 사이 두 번째 runner가 정당하게 획득해 같은 migration을 동시에 돌렸다. 이제 heartbeat이 migration에게 넘겨진다 — batch 경계를 아는 것은 migration뿐이기 때문이다.
## 첫 checkpoint가 전부 거부되던 두 결함
`MongoCollectionMigrationLedger.saveCheckpoint`에는 **두 개의** 결함 이력이 주석으로 남아 있다. upsert 하나로는 "매치할 게 없었다"와 "fence filter가 배제했다"를 구분할 수 없어 *모든 migration의 첫 checkpoint*가 "a newer migration runner owns the lease"로 거부됐고, 동시에 진짜 배제 경로는 unique index의 duplicate-key로 죽어 그 문장을 만드는 분기가 **도달 불가**였다. 지금은 replace-then-insert로 두 경우를 분리한다.
## rollback이 없는 것도 명시적 결정이다
"A rollback method implies the reverse operation is always safe and always possible, and for a backfill that dropped a column's old values it is neither." 실패한 production 변경은 forward-fix migration으로 고친다.
<!-- body:end -->