From 2526d859a3b0f3f159457e839002dffd6d4f6518 Mon Sep 17 00:00:00 2001 From: michael Date: Thu, 19 Nov 2026 21:11:54 +0100 Subject: [PATCH 1/2] fix(api): a migrated volume is found under the id its PV names Migration and fail-back give the volume a new record id; its namespace keeps the original one (LVol.ns_uuid). A Kubernetes PV names the original id for life, so every v2 lookup the CSI made for a migrated volume answered 404 in both clusters. Bed bf28 (2026-11-19): the CSI reconnect loop could not restore the paths a node crash had taken and vol-1 ran on 1 of 6 paths. _lookup_volume falls back to the pool's live volume whose ns_uuid is the requested id (online first, then newest), both when the id is unknown and when the record found belongs to another pool (the kept source). Co-Authored-By: Claude Opus 5.5 --- simplyblock_web/api/v2/_dependencies.py | 30 +++++- .../web/api/v2/test_volume_lookup_ns_uuid.py | 92 +++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/unit/web/api/v2/test_volume_lookup_ns_uuid.py diff --git a/simplyblock_web/api/v2/_dependencies.py b/simplyblock_web/api/v2/_dependencies.py index 38a049848b..c72fb41414 100644 --- a/simplyblock_web/api/v2/_dependencies.py +++ b/simplyblock_web/api/v2/_dependencies.py @@ -67,12 +67,40 @@ def _lookup_storage_pool(pool_id: UUID, cluster: Cluster) -> PoolModel: StoragePool = Annotated[PoolModel, Depends(_lookup_storage_pool)] +def _volume_by_ns_uuid(volume_id: str, pool_id: str) -> LVol | None: + """The volume of *pool_id* that a migration or fail-back made out of + *volume_id*: its record has a new id, its namespace keeps the old one + (``LVol.ns_uuid``). + + A Kubernetes PV names its volume for life, so after a migration the CSI + still asks for the original id. Bed bf28 (2026-11-19): every migrated + volume answered 404 in both clusters, the CSI's reconnect loop could not + restore the paths a node crash had taken, and vol-1 ran on 1 of 6 paths. + Deleted copies are skipped; of several live ones the online one wins, then + the newest.""" + candidates = [lv for lv in _db.get_lvols_by_pool_id(pool_id) + if getattr(lv, "ns_uuid", "") == volume_id + and not getattr(lv, "deleted", False) + and lv.status != LVol.STATUS_IN_DELETION] + if not candidates: + return None + candidates.sort(key=lambda lv: (lv.status == LVol.STATUS_ONLINE, str(lv.create_dt or "")), + reverse=True) + return candidates[0] + + def _lookup_volume(volume_id: UUID, pool: StoragePool) -> LVol: try: volume = _db.get_lvol_by_id(str(volume_id)) except KeyError as e: - raise HTTPException(404, str(e)) + volume = _volume_by_ns_uuid(str(volume_id), pool.get_id()) + if volume is None: + raise HTTPException(404, str(e)) + return volume if volume.pool_uuid != pool.get_id(): + moved = _volume_by_ns_uuid(str(volume_id), pool.get_id()) + if moved is not None: + return moved raise HTTPException(404, f'LVol {volume_id} not found') return volume diff --git a/tests/unit/web/api/v2/test_volume_lookup_ns_uuid.py b/tests/unit/web/api/v2/test_volume_lookup_ns_uuid.py new file mode 100644 index 0000000000..87b433f891 --- /dev/null +++ b/tests/unit/web/api/v2/test_volume_lookup_ns_uuid.py @@ -0,0 +1,92 @@ +"""A migrated volume is still found under the id its PV names. + +Bed bf28 (2026-11-19): migration gives the volume a new record id while its +namespace keeps the original one (LVol.ns_uuid). The PV - immutable - names the +original id, so every lookup the CSI made for a migrated volume answered 404 in +both clusters and the reconnect loop could not restore paths (vol-1: 1 of 6). +""" +from types import SimpleNamespace +from uuid import UUID + +import pytest +from fastapi import HTTPException + +import simplyblock_web.api.v2._dependencies as deps +from simplyblock_core.models.lvol_model import LVol + +ORIG = "731ff4bb-cf97-4bf2-9963-06cee01448c4" +NEW = "46a93411-0000-4000-8000-000000000001" +POOL_B = "446f40a9-52e9-4dcd-a9a3-45350672ac1b" +POOL_A = "baf07936-00b0-4f42-9934-b87e819910ce" + + +def _lv(uuid, pool, ns_uuid="", status=LVol.STATUS_ONLINE, create_dt="2026-11-19 13:40", deleted=False): + return SimpleNamespace(uuid=uuid, get_id=lambda: uuid, pool_uuid=pool, ns_uuid=ns_uuid, + status=status, create_dt=create_dt, deleted=deleted) + + +class _DB: + def __init__(self, *lvols): + self.lvols = {lv.uuid: lv for lv in lvols} + + def get_lvol_by_id(self, uuid): + if uuid not in self.lvols: + raise KeyError(f"LVol {uuid} not found") + return self.lvols[uuid] + + def get_lvols_by_pool_id(self, pool_id): + return [lv for lv in self.lvols.values() if lv.pool_uuid == pool_id] + + +def _pool(pid): + return SimpleNamespace(get_id=lambda: pid) + + +@pytest.fixture +def db(monkeypatch): + def install(*lvols): + d = _DB(*lvols) + monkeypatch.setattr(deps, "_db", d) + return d + return install + + +def test_the_original_id_finds_the_migrated_volume(db): + db(_lv(NEW, POOL_B, ns_uuid=ORIG)) + assert deps._lookup_volume(UUID(ORIG), _pool(POOL_B)).get_id() == NEW + + +def test_a_volume_found_by_its_own_id_is_unchanged(db): + db(_lv(NEW, POOL_B, ns_uuid=ORIG)) + assert deps._lookup_volume(UUID(NEW), _pool(POOL_B)).get_id() == NEW + + +def test_another_pool_still_answers_404(db): + db(_lv(NEW, POOL_B, ns_uuid=ORIG)) + with pytest.raises(HTTPException) as e: + deps._lookup_volume(UUID(ORIG), _pool(POOL_A)) + assert e.value.status_code == 404 + + +def test_the_kept_source_record_in_another_pool_does_not_hide_the_move(db): + # The original record survives in the source pool; asked in the target + # pool, the migrated copy answers instead of a 404. + db(_lv(ORIG, POOL_A), _lv(NEW, POOL_B, ns_uuid=ORIG)) + assert deps._lookup_volume(UUID(ORIG), _pool(POOL_B)).get_id() == NEW + assert deps._lookup_volume(UUID(ORIG), _pool(POOL_A)).get_id() == ORIG + + +def test_deleted_copies_are_skipped_and_online_wins(db): + db(_lv("11111111-0000-4000-8000-000000000001", POOL_B, ns_uuid=ORIG, deleted=True), + _lv("22222222-0000-4000-8000-000000000002", POOL_B, ns_uuid=ORIG, status=LVol.STATUS_IN_DELETION), + _lv("33333333-0000-4000-8000-000000000003", POOL_B, ns_uuid=ORIG, status="offline", + create_dt="2026-11-19 15:00"), + _lv(NEW, POOL_B, ns_uuid=ORIG)) + assert deps._lookup_volume(UUID(ORIG), _pool(POOL_B)).get_id() == NEW + + +def test_unknown_id_is_404(db): + db(_lv(NEW, POOL_B, ns_uuid=ORIG)) + with pytest.raises(HTTPException) as e: + deps._lookup_volume(UUID("99999999-0000-4000-8000-000000000009"), _pool(POOL_B)) + assert e.value.status_code == 404 From f3e4078339f842b63ebf37433bd940b433f8ad51 Mon Sep 17 00:00:00 2001 From: michael Date: Sat, 21 Nov 2026 22:20:15 +0100 Subject: [PATCH 2/2] chore(types): a migrated-volume lookup result is Optional Co-Authored-By: Claude Opus 5.5 --- simplyblock_web/api/v2/_dependencies.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/simplyblock_web/api/v2/_dependencies.py b/simplyblock_web/api/v2/_dependencies.py index c72fb41414..b69261686a 100644 --- a/simplyblock_web/api/v2/_dependencies.py +++ b/simplyblock_web/api/v2/_dependencies.py @@ -93,10 +93,10 @@ def _lookup_volume(volume_id: UUID, pool: StoragePool) -> LVol: try: volume = _db.get_lvol_by_id(str(volume_id)) except KeyError as e: - volume = _volume_by_ns_uuid(str(volume_id), pool.get_id()) - if volume is None: + migrated = _volume_by_ns_uuid(str(volume_id), pool.get_id()) + if migrated is None: raise HTTPException(404, str(e)) - return volume + return migrated if volume.pool_uuid != pool.get_id(): moved = _volume_by_ns_uuid(str(volume_id), pool.get_id()) if moved is not None: