본문으로 건너뛰기

[Pending PR] RawBlockCore: 중복/도달 불가 조건 정리

작성: 2026-05-29
상태: 변경 미커밋 — PR 예정
대상 파일: lmcache/v1/storage_backend/raw_block/core.py
성격: Pure refactoring (동작 변경 없음)


1. 배경

리더로부터 "raw block backend 로직 개선부터 시작" 방향을 받음 (2026-05-26). 큰 성능 개선 작업(M3 io_uring setup flags)은 #3274 머지 대기 중이라, 그 사이 core.py 내부의 마이너 정리 항목 3건을 묶어 워밍업 PR로 진행.

세 항목 모두 동작이 동일한 순수 refactor이며, 새로운 테스트는 필요하지 않다.


2. 변경 항목 (3건)

delete_manyexisted 변수 제거 (line 672)

변경 전:

with self._lock:
for encoded_key in encoded_keys:
existed = encoded_key in self._index or encoded_key in self._inflight
entry = self._index.get(encoded_key)
...
removed_entry = self._index.pop(encoded_key, None)
inflight = self._inflight.get(encoded_key)
...
deleted.append(
existed and (removed_entry is not None or inflight is not None)
)

변경 후:

with self._lock:
for encoded_key in encoded_keys:
entry = self._index.get(encoded_key)
...
removed_entry = self._index.pop(encoded_key, None)
inflight = self._inflight.get(encoded_key)
...
deleted.append(removed_entry is not None or inflight is not None)

근거:

케이스_index_inflightexisted(removed_entry is not None or inflight is not None)
ATrueTrue
BTrueTrue
CTrueTrue
DFalseFalse

_lock 보호 하에서 평가 시점과 pop/get 시점 사이 상태 변경 불가능 → existed가 결과에 영향을 주는 케이스 없음. 논리적으로 redundant.


_is_valid_checkpoint_entryslot < 0 조건 제거 (line 1227)

변경 전:

if offset < self._data_base_offset:
return False
rel = offset - self._data_base_offset
if rel % self.slot_bytes != 0:
return False
slot = rel // self.slot_bytes
if slot < 0 or slot >= self._max_slots:
return False

변경 후:

if offset < self._data_base_offset:
return False
rel = offset - self._data_base_offset
if rel % self.slot_bytes != 0:
return False
slot = rel // self.slot_bytes
if slot >= self._max_slots:
return False

근거:

  • Line 1221에서 offset < self._data_base_offset 이면 이미 return False
  • 따라서 rel = offset - self._data_base_offset >= 0 보장
  • self.slot_bytes__init__에서 > 0 검증됨 (line 210-213)
  • 양수 // 양수 → slot >= 0 항상 성립
  • slot < 0은 도달 불가 (dead code).

Corruption 시나리오 검토:

시나리오slot < 0에 도달?
디스크에서 음수 offsetline 1221에서 차단
디스크에서 거대한 양수 offsetslot >= _max_slots에서 차단
_data_base_offset 동시성 raceprivate이고 init/_ensure_capacity_and_layout에서만 설정 → race 없음
slot_bytes 외부 monkey-patch그 수준 corruption이면 slot < 0 하나로 못 막음

→ 실질적으로 도달 가능한 corruption 경로 없음.


_apply_loaded_statedata.get("device_path") 중복 호출 (line 1237)

변경 전:

if data.get("device_path") and data.get("device_path") != self.device_path:
logger.warning(...)
return False

변경 후:

checkpoint_device_path = data.get("device_path")
if checkpoint_device_path and checkpoint_device_path != self.device_path:
logger.warning(...)
return False

근거:

  • 같은 dict의 같은 키를 두 번 lookup
  • data는 함수 안에서 수정되지 않음
  • 단순 가독성/스타일 개선, 동작 동일

3. 검증

테스트 결과

.venv/bin/python -m pytest -xvs tests/v1/storage_backend/test_raw_block_core.py
# 4 passed

.venv/bin/python -m pytest -xvs tests/v1/storage_backend/test_rust_raw_block_backend.py
# 29 passed

.venv/bin/python -m pytest -x tests/v1/distributed/test_raw_block_l2_adapter.py \
tests/v1/multiprocess/test_raw_block_l2_adapter.py
# 20 passed

총 53/53 통과.

새 테스트 추가 여부

  • 세 변경 모두 동작 동일 → 새 테스트 불필요
  • 기존 test_raw_block_core_delete_and_missing_loaddelete_many의 A, D 케이스 커버
  • B, C 케이스는 동일 로직 경로 → 별도 커버 불필요
  • _is_valid_checkpoint_entry, _apply_loaded_state는 checkpoint recovery 테스트로 간접 커버

4. 리뷰 리스크 예상

항목리스크대응
existed낮음 — 명확한 단순화표로 4 케이스 모두 동치 증명 제시
slot < 0중간 — defensive code 선호 의견 가능도달 불가 근거를 PR description에 명시
③ device_path 중복낮음 — 명백한 개선별도 설명 불필요

②에 대한 대안: 그대로 두고 주석으로 invariant 명시.
하지만 같은 함수 내 다른 조건들(rel % slot_bytes != 0 등)은 방어적 주석 없이 처리하고 있어 일관성 측면에서 제거가 자연스러움.


5. PR 제목 후보

  • [Cleanup] Remove redundant conditions in RawBlockCore
  • [Refactor] Simplify dead/redundant checks in RawBlockCore

6. 다음 단계

  1. ☐ git diff 최종 검토
  2. ☐ branch 생성 (dev base)
  3. ☐ commit (3 항목 묶음 또는 분리)
  4. ☐ PR 생성 (/create-pr 활용)
  5. ☐ pre-commit 통과 확인