gabriel / musehub public
test_wire_fetch_mpack_ranged_e2e.py python
240 lines 11.2 KB
Raw
sha256:cff6528dd110d59db8e460aaf29c3677f27dce5fedc4ccfde4f2bbeea37efa25 test(wire-fetch): RED — fail closed when a required blob is… Sonnet 5.5 4 days ago
1 """End-to-end (isolated test DB): ``wire_fetch_mpack`` reads indexed blobs by range and builds the
2 same mpack bytes as the whole-mpack path.
3
4 The strongest equivalence available: the built mpack is content-addressed, so equal ``mpack_id`` means
5 byte-identical object sets and contents. Requires ``TEST_DATABASE_URL`` (never the shared hub).
6 """
7 from __future__ import annotations
8
9 import pytest
10 from sqlalchemy import delete, update
11 from sqlalchemy.dialects.postgresql import insert as pg_insert
12 from sqlalchemy.ext.asyncio import AsyncSession
13
14 from muse.core.mpack import build_wire_mpack
15 from muse.core.types import blob_id
16 from musehub.db import musehub_repo_models as db
17 from musehub.services.musehub_wire_push import compute_object_byte_offsets
18 from tests.factories import create_repo
19 from tests.test_mpack_blob_extraction import _SpyBackend, _sizeable
20 from tests.test_wire_fetch_mpack import _make_commit
21
22
23 def _patch_backend(monkeypatch: pytest.MonkeyPatch, backend: _SpyBackend) -> None:
24 for target in (
25 "musehub.services.musehub_wire.get_backend",
26 "musehub.services.musehub_wire_fetch.get_backend",
27 "musehub.services.musehub_wire_shared.get_backend",
28 ):
29 monkeypatch.setattr(target, lambda: backend)
30
31
32 async def _seed(
33 session: AsyncSession, repo_id: str, backend: _SpyBackend, groups: list[list[tuple[str, bytes]]],
34 ) -> list[str]:
35 """Store each group as one wire mpack and index its objects *with* byte ranges."""
36 mpack_ids: list[str] = []
37 for group in groups:
38 wire = build_wire_mpack({
39 "commits": [], "snapshots": [], "tags": [],
40 "blobs": [{"object_id": oid, "content": data} for oid, data in group],
41 })
42 mpack_id = blob_id(wire)
43 await backend.put_mpack(mpack_id, wire)
44 mpack_ids.append(mpack_id)
45 offsets = compute_object_byte_offsets(wire)
46 for oid, data in group:
47 await session.execute(
48 pg_insert(db.MusehubObject)
49 .values(object_id=oid, path="", size_bytes=len(data), storage_uri=f"mpack://{mpack_id}")
50 .on_conflict_do_nothing(index_elements=["object_id"])
51 )
52 await session.execute(
53 pg_insert(db.MusehubObjectRef).values(repo_id=repo_id, object_id=oid).on_conflict_do_nothing()
54 )
55 await session.execute(
56 pg_insert(db.MusehubMPackIndex).values(
57 entity_id=oid, mpack_id=mpack_id, entity_type="object",
58 byte_offset=offsets[oid][0], byte_length=offsets[oid][1],
59 )
60 )
61 await session.commit()
62 return mpack_ids
63
64
65 @pytest.mark.asyncio
66 async def test_ranged_build_is_byte_identical_to_whole_mpack_build_and_transfers_little(
67 db_session: AsyncSession, monkeypatch: pytest.MonkeyPatch,
68 ) -> None:
69 from musehub.services.musehub_wire import wire_fetch_mpack
70
71 backend = _SpyBackend()
72 _patch_backend(monkeypatch, backend)
73 repo = await create_repo(db_session, owner="gabriel", visibility="public")
74
75 # Two stored mpacks; the tip needs 2 of the 40 blobs in the first and 1 of the 40 in the second.
76 ballast_a = [(blob_id(c), c) for c in (_sizeable(300, f"a{i}") for i in range(38))]
77 ballast_b = [(blob_id(c), c) for c in (_sizeable(300, f"b{i}") for i in range(39))]
78 tip_a = [(blob_id(c), c) for c in (_sizeable(2, "tip-a1"), _sizeable(3, "tip-a2"))]
79 tip_b = [(blob_id(c), c) for c in (_sizeable(4, "tip-b1"),)]
80 mpack_a, mpack_b = await _seed(db_session, repo.repo_id, backend, [ballast_a + tip_a, ballast_b + tip_b])
81 tips = tip_a + tip_b
82 commit, _ = await _make_commit(
83 db_session, repo.repo_id, manifest={f"f{i}": oid for i, (oid, _) in enumerate(tips)}, seed="rr-e2e",
84 )
85
86 ranged = await wire_fetch_mpack(db_session, repo.repo_id, want=[commit.commit_id], have=[], force_build=True)
87 ranged_whole_calls, ranged_bytes = list(backend.whole_calls), backend.range_bytes
88 ranged_mpack_bytes = backend._mpacks[ranged["mpack_id"]]
89
90 assert ranged["blob_count"] == len(tips)
91 assert not {mpack_a, mpack_b} & set(ranged_whole_calls), "an indexed blob must not download its stored mpack"
92 assert ranged_bytes == sum(len(c) for _, c in tips)
93 assert ranged_bytes < len(backend._mpacks[mpack_a]) // 20
94
95 # The same fetch with the byte ranges NULL (pre-0067 rows) takes the whole-mpack path.
96 await db_session.execute(delete(db.MusehubFetchMPackCache)) # the first build cached its mpack
97 await db_session.execute(
98 update(db.MusehubMPackIndex).values(byte_offset=None, byte_length=None)
99 )
100 await db_session.commit()
101 backend.whole_calls.clear()
102 legacy = await wire_fetch_mpack(db_session, repo.repo_id, want=[commit.commit_id], have=[], force_build=True)
103
104 assert {mpack_a, mpack_b} <= set(backend.whole_calls)
105 assert legacy["mpack_id"] == ranged["mpack_id"], "identical object/content sets ⇒ identical content-addressed mpack"
106 assert backend._mpacks[legacy["mpack_id"]] == ranged_mpack_bytes
107
108
109 @pytest.mark.asyncio
110 async def test_bad_index_ranges_still_yield_the_same_mpack(
111 db_session: AsyncSession, monkeypatch: pytest.MonkeyPatch,
112 ) -> None:
113 from musehub.services.musehub_wire import wire_fetch_mpack
114
115 backend = _SpyBackend()
116 _patch_backend(monkeypatch, backend)
117 repo = await create_repo(db_session, owner="gabriel", visibility="public")
118 blobs = [(blob_id(c), c) for c in (_sizeable(5, f"z{i}") for i in range(4))]
119 (mpack_id,) = await _seed(db_session, repo.repo_id, backend, [blobs])
120 commit, _ = await _make_commit(
121 db_session, repo.repo_id, manifest={f"f{i}": oid for i, (oid, _) in enumerate(blobs)}, seed="rr-bad",
122 )
123
124 good = await wire_fetch_mpack(db_session, repo.repo_id, want=[commit.commit_id], have=[], force_build=True)
125 # Damage every range: offsets now point at the wrong bytes.
126 await db_session.execute(delete(db.MusehubFetchMPackCache))
127 await db_session.execute(update(db.MusehubMPackIndex).values(byte_offset=5))
128 await db_session.commit()
129 backend.whole_calls.clear()
130 healed = await wire_fetch_mpack(db_session, repo.repo_id, want=[commit.commit_id], have=[], force_build=True)
131
132 assert healed["mpack_id"] == good["mpack_id"]
133 assert backend.whole_calls == [mpack_id]
134
135
136 # ── fail closed: never build or cache an mpack that lacks a required object ─────────────────────────────
137
138
139 async def _tip_with(
140 session: AsyncSession, repo_id: str, backend: _SpyBackend, groups: list[list[tuple[str, bytes]]], seed: str,
141 ) -> tuple[db.MusehubCommit, list[str], list[tuple[str, bytes]]]:
142 mpack_ids = await _seed(session, repo_id, backend, groups)
143 blobs = [pair for group in groups for pair in group]
144 commit, _ = await _make_commit(
145 session, repo_id, manifest={f"f{i}": oid for i, (oid, _) in enumerate(blobs)}, seed=seed,
146 )
147 return commit, mpack_ids, blobs
148
149
150 async def _cache_rows(session: AsyncSession) -> int:
151 from sqlalchemy import func, select
152 return int((await session.execute(select(func.count()).select_from(db.MusehubFetchMPackCache))).scalar_one())
153
154
155 @pytest.mark.asyncio
156 async def test_unrecoverable_blob_fails_before_build_upload_or_cache(
157 db_session: AsyncSession, monkeypatch: pytest.MonkeyPatch,
158 ) -> None:
159 from musehub.services.musehub_wire import wire_fetch_mpack
160 from musehub.services.musehub_wire_shared import FetchBlobsUnavailableError
161
162 backend = _SpyBackend()
163 _patch_backend(monkeypatch, backend)
164 repo = await create_repo(db_session, owner="gabriel", visibility="public")
165 keep = [(blob_id(c), c) for c in (_sizeable(2, "keep1"), _sizeable(2, "keep2"))]
166 lost = [(blob_id(c), c) for c in (_sizeable(2, "lost1"),)]
167 commit, (_, lost_mpack), _ = await _tip_with(db_session, repo.repo_id, backend, [keep, lost], "fc-lost")
168 # The stored mpack holding one required blob is gone, and the per-object store never had it.
169 del backend._mpacks[lost_mpack]
170 stored_before = set(backend._mpacks)
171
172 with pytest.raises(FetchBlobsUnavailableError) as excinfo:
173 await wire_fetch_mpack(db_session, repo.repo_id, want=[commit.commit_id], have=[], force_build=True)
174
175 assert excinfo.value.missing_count == 1
176 assert set(backend._mpacks) == stored_before, "an incomplete mpack must not be uploaded"
177 assert await _cache_rows(db_session) == 0, "an incomplete mpack must not be cached"
178
179
180 @pytest.mark.asyncio
181 async def test_every_unrecoverable_blob_is_counted(
182 db_session: AsyncSession, monkeypatch: pytest.MonkeyPatch,
183 ) -> None:
184 from musehub.services.musehub_wire import wire_fetch_mpack
185 from musehub.services.musehub_wire_shared import FetchBlobsUnavailableError
186
187 backend = _SpyBackend()
188 _patch_backend(monkeypatch, backend)
189 repo = await create_repo(db_session, owner="gabriel", visibility="public")
190 blobs = [(blob_id(c), c) for c in (_sizeable(2, f"all{i}") for i in range(3))]
191 commit, (mpack_id,), _ = await _tip_with(db_session, repo.repo_id, backend, [blobs], "fc-all")
192 del backend._mpacks[mpack_id]
193
194 with pytest.raises(FetchBlobsUnavailableError) as excinfo:
195 await wire_fetch_mpack(db_session, repo.repo_id, want=[commit.commit_id], have=[], force_build=True)
196 assert excinfo.value.missing_count == 3
197
198
199 @pytest.mark.asyncio
200 async def test_valid_empty_blob_is_kept_in_the_built_mpack(
201 db_session: AsyncSession, monkeypatch: pytest.MonkeyPatch,
202 ) -> None:
203 from muse.core.mpack import parse_wire_mpack
204 from musehub.services.musehub_wire import wire_fetch_mpack
205
206 backend = _SpyBackend()
207 _patch_backend(monkeypatch, backend)
208 repo = await create_repo(db_session, owner="gabriel", visibility="public")
209 blobs = [(blob_id(b""), b""), (blob_id(_sizeable(2, "full")), _sizeable(2, "full"))]
210 commit, _, _ = await _tip_with(db_session, repo.repo_id, backend, [blobs], "fc-empty")
211
212 result = await wire_fetch_mpack(db_session, repo.repo_id, want=[commit.commit_id], have=[], force_build=True)
213
214 built = {b["object_id"]: bytes(b["content"]) for b in parse_wire_mpack(backend._mpacks[result["mpack_id"]])["blobs"]}
215 assert built == dict(blobs), "the empty blob is a required object, not an absent one"
216 assert result["blob_count"] == 2
217
218
219 @pytest.mark.asyncio
220 async def test_empty_blob_resolved_from_per_object_store_is_kept(
221 db_session: AsyncSession, monkeypatch: pytest.MonkeyPatch,
222 ) -> None:
223 """Fallback order ends at the per-object store; its empty bytes are content, not a miss."""
224 from muse.core.mpack import parse_wire_mpack
225 from musehub.services.musehub_wire import wire_fetch_mpack
226
227 backend = _SpyBackend()
228 _patch_backend(monkeypatch, backend)
229 repo = await create_repo(db_session, owner="gabriel", visibility="public")
230 full = _sizeable(2, "full2")
231 blobs = [(blob_id(b""), b""), (blob_id(full), full)]
232 commit, (mpack_id,), _ = await _tip_with(db_session, repo.repo_id, backend, [blobs], "fc-legacy-empty")
233 del backend._mpacks[mpack_id]
234 backend._store[blob_id(b"")] = b""
235 backend._store[blob_id(full)] = full
236
237 result = await wire_fetch_mpack(db_session, repo.repo_id, want=[commit.commit_id], have=[], force_build=True)
238
239 built = {b["object_id"]: bytes(b["content"]) for b in parse_wire_mpack(backend._mpacks[result["mpack_id"]])["blobs"]}
240 assert built == dict(blobs)
File History 2 commits
sha256:cff6528dd110d59db8e460aaf29c3677f27dce5fedc4ccfde4f2bbeea37efa25 test(wire-fetch): RED — fail closed when a required blob is… Sonnet 5.5 4 days ago
sha256:7adb1a8e34c5ac5b1a40c5dedf7c80dea5d64acd7a1ae2bcc5787fe6a953f449 perf(prebuild): read indexed blobs by bounded range with ve… Sonnet 5.5 patch 4 days ago