Expand description
Loading a stored graph index without reading it whole (#382).
At full scale a stored index is tens of GiB, and reading it into memory took 15 s on one core, which every query in a new process paid, even one that reads the few thousand edges around one node. So an index file is memory-mapped where it can be, and read in parallel where it cannot.
The format (index_file, versions 3 and 4) is the version 2 layout: raw
little-endian arrays at 64-byte-aligned offsets, each a ready &[u64] or
&[u32] in place, with no decode step. What changes is the checksum. A
version 2 file had one CRC over the whole main part, so nothing in it could
be trusted before every byte was read. Version 3 gives each section its
own CRC, in the section table, and the trailer’s CRC covers the header,
key and table (so, through the section CRCs, still every byte: it remains
the main part’s identity, which chunks are checked against). Padding is
zero, and checked.
What is resident and what is mapped. Loading checks the header, key
and table, then the O(nodes) sections in full: the out-adjacency’s
offsets (CRC, and that they rise from 0 to the edge count, so every
neighbour slice is in bounds), the node ids (CRC, and that the strings are
UTF-8) and, in version 4, the ids’ lookup table (CRC). The id map is those
two arrays as they lie in the map (IdMap::from_stored_ids): no id is
hashed or copied when the file loads (#421). A version 3 file has no
table, so one is built in memory, the first time a query looks an id up.
The O(edges) sections, the targets and edge
ids, are borrowed from the map as Arrow buffers (ursa_core::Adjacency
holds ScalarBuffers) and not read: their CRCs are deferred
(Unverified). The in-adjacency chunk, when a query first asks for it,
is borrowed from the same map the same way, its data CRC deferred too.
Who checks the deferred CRCs. A whole-graph algorithm reads every byte
of the arrays anyway, so before one runs (GraphAlgorithmExec) the engine
checks every deferred CRC, in parallel, once per loaded index; a CRC that
does not match drops the stored index and builds it again from its
sources, with a notice, never a result from damaged arrays. A point
traversal (hop, bfs, a path search) reads only the adjacency lists it
visits and does not check the arrays’ CRCs: that is the point of mapping.
Its safety does not rest on them: the offsets are checked, so every slice
is in bounds, and a damaged target or edge id can only fail a bounds check
in a kernel (no kernel indexes with one unchecked), which is a query
error, never undefined behaviour.
Atomic replacement, and files that change under a map. Ursa never
changes a byte of a stored index in place: a new index is written to a
temporary file and renamed over the old one, an eviction unlinks the file,
and a chunk is only appended after the last chunk that checks out. On
Unix a rename or unlink leaves a mapped file’s pages in place until the
last map of them goes, so a process keeps reading the index it loaded
while another replaces it. A process maps only the main part and the
chunks that check out when it loads, never the bytes past them, which are
the only bytes an append truncates (a chunk cut short by a crash), so
nothing it maps ever shrinks under it (which would be SIGBUS). Editing a
file in indexes/ by other means while Ursa runs is not supported, as for
any memory-mapped database.
Windows keeps an open mapping from being renamed over or deleted, which
the store relies on, so there (and on any target without mmap, and for a
version 2 file, whose single CRC needs every byte read anyway) the file is
read whole, in parallel, into memory (read_whole), and every CRC is
checked as it loads.
Memory accounting. Mapped pages are the operating system’s page
cache: they count in a process’s RSS once touched, but they are shared
between processes reading the same index, and the kernel drops clean pages
under memory pressure and reads them again on demand, instead of the
process being killed. A fresh process that answers a point query touches
only the pages it reads. ursa.index_storage_limit counts the files on
disk, as before.
Compatibility. A version 2 file (written by 0.10) is read as before, whole and checked, and a version 3 file (0.11) is mapped; a writable database then rewrites either as version 4 (a notice says so), so the next process maps it and finds its ids’ lookup table in it. Version 1 files are not read, as before: they are built again.
Structs§
- Region
- One deferred checksum: the bytes
rangeof a mapped file, and their CRC. - Unverified
- The checksums of a mapped index that have not been checked yet (see the module docs): its edge arrays’, and its in-adjacency’s once read.
Constants§
- MAPS
- Whether this build maps index files: Unix, little-endian (the files are little-endian, so a mapped array is usable in place).
Functions§
- are_
strings - Whether
valuesandoffsetsare the bytes and offsets of valid strings: stringiisvalues[offsets[i]..offsets[i + 1]], and each is UTF-8. This is what makes them aStringArray(StringArray::new_unchecked), checked in parallel pieces: the ids of a stored index are read once here, on all cores, when it loads. - crc
- The CRC-32 of
bytes, computed in parallel pieces that are then combined (the same value as one pass). - map
- The first
lenbytes offile, memory-mapped read-only, as an Arrow buffer that unmaps them when its last clone is dropped. - read_
whole - The first
lenbytes offile, read into memory, in parallel, as an Arrow buffer aligned for 64-bit values.