Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
rocketchat: Index attachment chunks by offset for O(1) lookup.
Alex Vandiverdf60923c5e2e
diff --git a/zerver/data_import/rocketchat.py b/zerver/data_import/rocketchat.pyindex 3e2d1eadd06..876802ae847 100644--- a/zerver/data_import/rocketchat.py+++ b/zerver/data_import/rocketchat.py@@ -1,11 +1,13 @@import loggingimport os+import structimport uuidfrom collections import defaultdictfrom collections.abc import Callable, Iterator-from typing import Any+from typing import IO, Anyimport bson+from bson.errors import InvalidBSONfrom bsonstream import KeyValueBSONInputfrom django.conf import settingsfrom django.forms.models import model_to_dict@@ -43,6 +45,116 @@ bson_codec_options = bson.DEFAULT_CODEC_OPTIONS.with_options(tz_aware=True)MESSAGE_BATCH_SIZE = 1000+# We parse GridFS chunk document headers directly rather than using+# bson.decode or KeyValueBSONInput, because chunk documents contain+# large binary data payloads (up to 16 MB each). Using bson.decode+# would require reading the entire document into memory; using+# KeyValueBSONInput with fast_string_prematch still reads every byte+# (it just skips the BSON decode). By parsing only the small header+# portion, the index pass reads ~200 bytes per chunk regardless of+# payload size.+#+# GridFS chunk documents have the schema {_id: ObjectId, files_id,+# n: int32, data: BinData}. Per the GridFS spec, _id, files_id, and+# n appear before the large data field and together occupy ~60 bytes+# for typical Rocket.Chat document IDs. We read 200 bytes to allow+# for IDs up to ~140 characters; if the fields aren't found within+# that window, the import aborts with a clear error rather than+# silently producing corrupt output.+_CHUNK_HEADER_READ_SIZE = 200+++def _parse_chunk_header(raw: bytes) -> tuple[str, int]:+ """Extract files_id and n from the beginning of a raw BSON chunk+ document header, without reading the large binary data payload.++ Walks BSON fields sequentially, handling only the types expected in+ GridFS chunk headers: ObjectId (0x07), UTF-8 string (0x02), and+ int32 (0x10). Raises InvalidBSON on any unexpected structure —+ this is a data import from an untrusted source, so we reject+ anything outside the expected GridFS schema rather than trying to+ handle it.+ """+ pos = 4 # skip document size prefix+ files_id = None+ n_val = None++ while pos < len(raw) - 1:+ type_byte = raw[pos]+ if type_byte == 0x00: # nocoverage # end of document+ break+ pos += 1+ null_pos = raw.find(0, pos)+ if null_pos == -1 or null_pos - pos > 100: # nocoverage+ raise InvalidBSON(f"Invalid or overlong field name at offset {pos}")+ key = raw[pos:null_pos].decode("ascii")+ pos = null_pos + 1++ if type_byte == 0x07: # ObjectId — fixed 12 bytes+ if pos + 12 > len(raw): # nocoverage+ break+ pos += 12+ elif type_byte == 0x02: # UTF-8 string+ if pos + 4 > len(raw): # nocoverage+ break+ (str_len,) = struct.unpack("<i", raw[pos : pos + 4])+ if str_len < 1 or pos + 4 + str_len > len(raw): # nocoverage+ break+ if key == "files_id":+ files_id = raw[pos + 4 : pos + 4 + str_len - 1].decode("ascii")+ pos += 4 + str_len+ elif type_byte == 0x10: # int32+ if pos + 4 > len(raw): # nocoverage+ break+ if key == "n":+ (n_val,) = struct.unpack("<i", raw[pos : pos + 4])+ pos += 4+ else: # nocoverage+ # Binary data or unrecognized type — stop scanning.+ break++ if files_id is not None and n_val is not None:+ break++ if files_id is None or n_val is None: # nocoverage+ raise InvalidBSON(+ "Could not extract files_id and n from chunk header; "+ f"parsed {pos} of {len(raw)} header bytes"+ )++ return files_id, n_val+++def _build_chunk_index(chunks_fh: IO[bytes]) -> dict[str, list[tuple[int, int]]]:+ """Build an index from files_id to sorted [(n, file_offset)] by scanning+ chunk document headers without reading the large binary data payloads.++ Each BSON document starts with a 4-byte little-endian size. We read+ only the first few hundred bytes of each document to extract files_id+ and n, then seek past the remainder.+ """+ chunk_index: dict[str, list[tuple[int, int]]] = {}++ while True:+ doc_offset = chunks_fh.tell()+ size_bits = chunks_fh.read(4)+ if len(size_bits) < 4:+ break+ (doc_size,) = struct.unpack("<i", size_bits)+ if doc_size < 5: # nocoverage — minimum BSON document is 5 bytes+ raise InvalidBSON(f"Invalid BSON document size {doc_size} at offset {doc_offset}")+ header_bytes = min(doc_size, _CHUNK_HEADER_READ_SIZE)+ header = size_bits + chunks_fh.read(header_bytes - 4)+ chunks_fh.seek(doc_offset + doc_size)++ files_id, n = _parse_chunk_header(header)+ chunk_index.setdefault(files_id, []).append((n, doc_offset))++ for entries in chunk_index.values():+ entries.sort()++ return chunk_index+def make_realm(realm_id: int, realm_subdomain: str, domain_name: str, rc_instance: dict[str, Any]@@ -1142,35 +1254,29 @@ def do_convert_data(rocketchat_data_dir: str, output_dir: str) -> None:os.path.join(rocketchat_data_dir, "rocketchat_uploads.chunks.bson"), "rb") as chunks_fh,):+ # Build indexes in a single pass over each file. Upload+ # metadata is small (no binary blobs), so we store it fully.+ # For chunks, we store only (n, file_offset) per chunk —+ # reading just the first few hundred bytes of each document+ # header and seeking past the large binary data payloads.+ upload_index: dict[str, dict[str, Any]] = {}+ for doc in KeyValueBSONInput(fh=uploads_fh):+ upload_index[doc["_id"]] = doc++ chunk_index = _build_chunk_index(chunks_fh)def attachment_lookup(file_id: str) -> None | tuple[dict[str, Any], Iterator[bytes]]:- uploads_fh.seek(0)- uploads = [- doc- for doc in KeyValueBSONInput(fh=uploads_fh, fast_string_prematch=file_id.encode())- if doc.get("_id") == file_id- ]- if len(uploads) == 0: # nocoverage+ if file_id not in upload_index: # nocoveragereturn None- if len(uploads) > 1: # nocoverage- logging.info(- "Found %d metadata entries for upload %s, using first", len(uploads), file_id- )def iterate_chunks() -> Iterator[bytes]:- chunks_fh.seek(0)- matching = [- chunk- for chunk in KeyValueBSONInput(- fh=chunks_fh, fast_string_prematch=file_id.encode()- )- if chunk.get("files_id") == file_id- ]- matching.sort(key=lambda c: c.get("n", 0))- for chunk in matching:- yield chunk.get("data", b"")+ for _n, offset in chunk_index.get(file_id, []):+ chunks_fh.seek(offset)+ doc = KeyValueBSONInput(fh=chunks_fh).read()+ if doc is not None:+ yield doc.get("data", b"")- return uploads[0], iterate_chunks()+ return upload_index[file_id], iterate_chunks()process_messages(realm_id=realm_id,diff --git a/zerver/tests/test_rocketchat_importer.py b/zerver/tests/test_rocketchat_importer.pyindex 5484c3d47c5..258426e79f7 100644--- a/zerver/tests/test_rocketchat_importer.py+++ b/zerver/tests/test_rocketchat_importer.py@@ -932,14 +932,14 @@ class RocketChatImporter(ZulipTestCase):for um in dm_ums:self.assertTrue(um["flags_mask"] & 2048)- def test_attachment_lookup_found(self) -> None:- """Test that attachment_lookup correctly finds uploads and streams chunks- from the BSON fixture files, matching the behavior of the streaming- attachment_lookup closure in do_convert_data."""- from collections.abc import Iterator-+ def test_attachment_index_and_lookup(self) -> None:+ """Test the index-based attachment lookup: _build_chunk_index builds+ a correct offset index, and attachment_lookup uses it to find uploads+ and yield chunk data in correct n-order."""from bsonstream import KeyValueBSONInput+ from zerver.data_import.rocketchat import _build_chunk_index+fixture_dir_name = self.fixture_file_name("", "rocketchat_fixtures")uploads_path = os.path.join(fixture_dir_name, "rocketchat_uploads.bson")chunks_path = os.path.join(fixture_dir_name, "rocketchat_uploads.chunks.bson")@@ -948,65 +948,35 @@ class RocketChatImporter(ZulipTestCase):open(uploads_path, "rb") as uploads_fh,open(chunks_path, "rb") as chunks_fh,):-- def attachment_lookup(- file_id: str,- ) -> None | tuple[dict[str, Any], Iterator[bytes]]:- uploads_fh.seek(0)- uploads = [- doc- for doc in KeyValueBSONInput(- fh=uploads_fh, fast_string_prematch=file_id.encode()- )- if doc.get("_id") == file_id- ]- if len(uploads) == 0:- return None- self.assert_length(uploads, 1)-- chunks_fh.seek(0)- matching = [- chunk- for chunk in KeyValueBSONInput(- fh=chunks_fh, fast_string_prematch=file_id.encode()- )- if chunk.get("files_id") == file_id- ]- matching.sort(key=lambda c: c.get("n", 0))-- def iterate_chunks() -> Iterator[bytes]:- for chunk in matching:- yield chunk.get("data", b"")-- return uploads[0], iterate_chunks()-- # Known upload: harry-ron.jpg has one chunk- result = attachment_lookup("MmgXWQbD3hXYyGSai")- assert result is not None- upload_meta, chunk_iter = result- self.assertEqual(upload_meta["name"], "harry-ron.jpg")- self.assertEqual(upload_meta["size"], 149467)- chunks = list(chunk_iter)- self.assert_length(chunks, 1)- self.assertEqual(len(chunks[0]), 149467)-- # Known upload: Hogwarts Curriculum.pdf has multiple chunks.- # The fixture stores these as n=0, n=2, n=1 in the BSON file;- # they must be yielded in n=0, n=1, n=2 order.- result = attachment_lookup("4BoikTP2ZZkE5RsGh")- assert result is not None- upload_meta, chunk_iter = result- self.assertEqual(upload_meta["name"], "Hogwarts Curriculum.pdf")- chunks = list(chunk_iter)- self.assert_length(chunks, 3)- self.assertEqual(len(chunks[0]), 261120)- self.assertEqual(len(chunks[1]), 261120)- self.assertEqual(len(chunks[2]), 47300)- self.assertEqual(sum(len(c) for c in chunks), upload_meta["size"])-- # Unknown upload: returns None- result = attachment_lookup("nonexistent_id")- self.assertIsNone(result)+ upload_index: dict[str, dict[str, Any]] = {}+ for doc in KeyValueBSONInput(fh=uploads_fh):+ upload_index[doc["_id"]] = doc++ chunk_index = _build_chunk_index(chunks_fh)++ self.assert_length(upload_index, 4)+ self.assert_length(chunk_index, 4)++ # harry-ron.jpg: single chunk+ self.assertIn("MmgXWQbD3hXYyGSai", chunk_index)+ self.assert_length(chunk_index["MmgXWQbD3hXYyGSai"], 1)++ # Hogwarts Curriculum.pdf: 3 chunks, stored as n=0,2,1 in file.+ # Index must be sorted by n.+ pdf_entries = chunk_index["4BoikTP2ZZkE5RsGh"]+ self.assert_length(pdf_entries, 3)+ self.assertEqual([n for n, _offset in pdf_entries], [0, 1, 2])++ # Verify seeking to indexed offsets yields correct data+ for n, offset in pdf_entries:+ chunks_fh.seek(offset)+ doc = KeyValueBSONInput(fh=chunks_fh).read()+ assert doc is not None+ self.assertEqual(doc["files_id"], "4BoikTP2ZZkE5RsGh")+ self.assertEqual(doc["n"], n)++ # Unknown upload+ self.assertNotIn("nonexistent_id", upload_index)def test_process_message_attachment_unknown(self) -> None:"""Test that process_message_attachment handles an unknown attachment