grip-rs updates

Updates in GRIP

How the index takes deletes and inserts while it serves searches. We follow the code: the data, the write paths, and the cleanup that folds everything back into a clean index.

Tombstoneshide a deleted document
Deltaa small index for new documents
Cleanuprewrite the files in the background
Arrow keys or click to move
The constraint

A built index does not like change

The graph, the sketches and the vectors are flat files. The server reads them through a memory map. So we never edit them.

fixed rows
Every graph row has 64 slots, and row p belongs to document p.
id = row
A document id is its position. Removing one document would move every document after it.
read-only
Many threads read the files with no lock. An edit in place would race with them.
So we hide deleted documents, keep new documents somewhere else, and rewrite the files later, in the background.
The plan

Three parts, one background job

Deletes and inserts are cheap and happen at once. The cleanup pays the bill later.

While the server answers
DeleteSet one bit in the tombstones. Append 4 bytes to delete.log.Tombstones
InsertAppend a record to delta.log. Add the document to the delta.Delta
SearchGraph search plus delta search, without the tombstones, merged by score.Tombstones Delta
In the background
HealGive live nodes new neighbors where a neighbor was deleted.Cleanup
→
CompactWrite a new version without the dead. Merge the delta into the graph.Cleanup
→
SwapCopy the writes that arrived meanwhile. Replace the index in memory.Cleanup
The state · one value grip-rs

What the server holds

One MutableIndex is one version of the index. The server keeps it behind a lock.

cleanup.rs · MutableIndex

Read-only inside

The graph, the sketches, the vectors and ids never change in a version.

Two things move

The tombstones are atomic bits. The delta has its own lock. So a delete or an insert only needs the shared lock on the index.

Replaced whole

A cleanup builds a new value and swaps it in.

The state · on disk grip-rs

One folder for each version

A version is a folder named vN. A cleanup writes vN+1.tmp and renames it when it is complete.

index.bingraph, seed lists, term_max
sketches.binrouting sketches, in memory at start
vectors.binfull vectors, memory-mapped
ids.binext_base, then the external id of each position (from v1)
delta.log and delete.logonly exist after a write. A missing log means empty.

v0

The original build has no ids.bin. The id is the position, and ext_base is the number of documents.

Start-up

from_env loads the highest vN and deletes every .tmp folder.

Writes

Logs go in the folder of the current version.

Deletes · the data Tombstones

One bit for each document

A delete is a single atomic OR. No lock, and searches read the same words at the same time.

tombstone.rs · Tombstones

Who deleted first

fetch_or returns the old word, so only one caller sees newly = true. The count stays right.

Room for the delta

The set has n_main + MAX_DELTA bits, so a delta document can be deleted too.

The trigger

count / documents tells the cleanup thread when to start.

Deletes · the search Tombstones

A dead node is a router

The beam search still walks through deleted nodes. It never returns one.

search.rs · run_beam_search

A test before the walk

A dead node is only worth a visit if its score could enter the results. Otherwise the walk stops there.

Nothing to rescore

Dead ids never reach the rescoring, so no vector is read for them.

A dead node costs a sketch score and a heap push. That is the price of a connected graph until the cleanup.
Deletes · the write path Tombstones

Bit first, log second

The log is how a restart gets the bits back.

tombstone.rs · delete

Internal ids

The log stores internal ids. It only makes sense next to its own version folder.

Answered means saved

The server answers after the append. A crash before it means the client never got a 202.

Ids · the translation grip-rs

Translate at the edge of the API

The graph knows internal ids. Clients know external ids. The server converts at the door.

cleanup.rs · MutableIndex

Sorted ids

Compaction keeps the order, so ids stays sorted and a lookup is a binary search.

Never reused

A deleted id answers 404 for ever. The next insert gets ext_base, never an old id.

Example: delete 2 from 0 to 6 (5 and 6 are delta). After: ids = [0, 1, 3, 4, 5, 6], ext_base = 7.
Inserts · the data Delta

An inverted index for new documents

The delta is small and grows by one document at a time. It needs no graph.

delta.rs · Delta

Append only

A document never moves inside the delta. Its local id is its place.

Arc

The cleanup copies the document list by cloning pointers, not vectors.

Bounded

The 50,001st insert gets a 507 until a cleanup empties the delta.

Inserts · the search Delta

Exact scores from the posting lists

A query only touches the lists of its own terms. The score is the full dot product.

delta.rs · delta_search

One scratch array

acc has a slot for each possible delta document. Each worker owns one, so no search allocates.

One id space

Delta ids continue after the graph: n_main + local. The tombstones need no translation.

search_all joins the two hit lists, sorts by score and keeps the top k. Both scores are exact, so they compare.
Inserts · the write path Delta

Disk first, memory second

An insert is in the log before it can be found. A crash cannot lose an insert that was answered.

delta.rs · persist_delta_insert

Ids follow the log

The id is chosen while we hold the write lock. So the ids and the order of the log always agree.

The price

The file write happens under the lock. Searches on the delta wait for it.

Inserts · recovery Delta

Replay the log at start

One record is nnz | indices | values | ext_id, 4 bytes each: 8 + 8 × nnz bytes.

delta.rs · load_delta

Torn record

A crash in the middle of a write leaves half a record. We drop it and truncate the file, so the next append starts clean.

A check on the id

Every record carries its id. A log that does not fit its ext_base is refused, not trusted.

Concurrency · locks grip-rs

Who takes which lock

Only the swap needs the write lock on the index.

OperationIndex lockDelta lockOther
Searchread, for the whole searchreadscratch mutex, to take one
Insertreadwrite, while it logs–
Deletereadread, to countone atomic bit
Cleanup: heal, compactreadread, to copy the list–
Cleanup: swapwriteread–
The std RwLock prefers writers. Once the cleanup waits for the write lock, new readers queue behind it.
So the time under the write lock is the time the server is frozen. We keep it as short as we can.
Concurrency · the search handler grip-rs

The scratch follows the version

A search holds the read guard from start to end. So it always sees one consistent version.

grip-server · post_search

Sized by version

The visited array has one slot for each document. After a swap the count changes, so an old scratch is replaced.

The swap waits

It cannot take the write lock while a search holds a read guard. Searches in flight finish on the old index.

Cleanup · the shape Cleanup

Slow work shared, short work alone

A thread wakes every 10 seconds. It starts a cleanup when half of the documents are deleted, or when the delta is full.

One cleanup, not to scale
snapshot
heal
compact
load new
swap
read lock: searches, inserts and deletes go on
write lock
The cleanup runs in its own rayon pool, with half of the cores by default, so it does not take every core from the searches.
The last step is the only one that blocks everybody. Pull request: github.com/AstraBert/grip-rs/pull/4
Cleanup · 1 of 5 Cleanup

Take a snapshot

Writes keep coming while the cleanup runs. The snapshot says what this cleanup covers.

cleanup.rs · cleanup

Late writes

Anything that arrives after this point is a late write. The swap copies it in.

Only pointers

We clone the list of Arc, then drop the delta guard at once. Holding it through the heal blocked every insert, and then every search.

Cleanup · 2 of 5 · heal Cleanup

Which rows change

Most rows do not. We only touch a live row that points at a deleted node.

healer.rs · heal

Dead rows

A deleted node gets an empty row. Nobody will reach it after the compaction anyway.

Isolated nodes

If a row loses every neighbor and finds no border, it becomes empty.

Cleanup · 2 of 5 · heal Cleanup

Walk through the dead

A dead node still has edges. We follow them until we reach live nodes.

healer.rs · the walk

A counter, not a clear

We do not clear an array of n entries for every row. A node is visited if its slot equals the counter.

A budget

max_dfs steps and max_border_length nodes for each row. Too small a budget leaves weak neighbors.

Row p reads other rows from the old graph and writes only its own row in the copy. That is why rows can run in parallel.
Cleanup · 2 of 5 · heal Cleanup

Choose the new neighbors

Sketches are for routing. Choosing neighbors uses the full vectors, like the build.

healer.rs · score and prune

Sequential decode

We are already inside a parallel loop, so the decode does not start another one.

Scratch for each split

try_for_each_init gives each rayon split its own visited array and dense buffer.

Local ids

The prune works on positions in ids. We map them back to node ids at the end.

Cleanup · 3 of 5 · compact Cleanup

Close the holes

Compaction writes a fresh index into vN+1.tmp. First, every live document gets a new position.

compact.rs · compact

Order kept

Live documents keep their relative order. That keeps ids.bin sorted.

Copy what you can

A sketch depends only on its own vector, so it is copied. Only delta documents need a new one.

Cleanup · 3 of 5 · compact Cleanup

Put the delta in the graph

Each live delta document is inserted one by one, like an online build.

compact.rs · step 4

Each sees the ones before

The search only reaches nodes that are already in the graph.

Single thread

The graph changes at every step, and the order matters.

Seeds translated

Old seeds point to old ids. We map them with new_pos and drop the dead ones.

Cleanup · 3 of 5 · compact Cleanup

Seeds, ids, files

The last steps of the compaction describe the new version, then write it.

compact.rs · steps 5 to 7

Ids stay unique

A deleted delta document still uses up an id, because ext_base moves by delta.len().

Careful

It refuses to write into a folder that has an index.bin. If nothing is alive, it returns an error.

Cleanup · 4 of 5 · swap Cleanup

Load the new index before the lock

Reading the files back is slow. It must not happen while everybody waits.

cleanup.rs · cleanup

What the load does

It parses the graph and the sketches, replays the logs, builds the delta postings and maps vectors.bin.

One lock, not two

An earlier version dropped the lock to reload, then locked again. A write that landed in between was lost. Now we load first, then one write lock covers the copy and the swap.

Cleanup · 4 of 5 · swap Cleanup

Copy the late writes

Under the write lock, nothing can change. We copy what arrived after the snapshot into the new version.

cleanup.rs · cleanup

One batch

The log is opened once and written once, not once for each insert.

Words, not ids

One AND checks 64 documents at a time.

Cleanup · 4 of 5 · swap Cleanup

Late deletes need new ids

The bits we found use old internal ids. The new version has other positions.

cleanup.rs · cleanup

Snapshot documents

They moved in the compaction. old_to_new knows where.

Late inserts

They went to the new delta. Their place there is the old delta place, minus the snapshot size.

Without this step, a late delete hits another document. A test should check which document is gone, not only how many.
Cleanup · 5 of 5 · swap Cleanup

Publish and swap

Three short steps, then the lock is released.

cleanup.rs · cleanup

WillNeed

A new memory map starts cold. We ask the kernel to read it in now, so the first searches do not wait for the disk.

The map survives

The rename changes the folder name, not the file. The map stays valid.

Free later

Dropping a big index takes time. We do it after the lock.

Safety · failures Cleanup

A cleanup that stops half way

A folder is complete or absent. The old version serves until the swap.

It stops duringWhat servesWhat happens next
heal, compact, loadthe old versionOn an error, a guard removes vN+1.tmp. After a crash, the next start removes it.
the copy of late writesthe old versionSame. The late writes are still in the old logs.
after the rename, before the swapafter a restart: vN+1The highest folder wins. Its logs already hold the late writes.
after the swapvN+1New writes go to the logs of vN+1.
If the cleanup returns an error, the cleanup thread logs it and tries again at the next check.
Safety · tests grip-rs

What the tests pin down

Fourteen tests in correctness_test.rs, grouped by the part they protect.

Deletes and delta
Deleted ids never appear. Delta scores match an exact oracle. Delta documents can be deleted. The delta survives a restart, a torn log, a missing log. A log with the wrong base is refused.
Heal
After the heal, no row points to a deleted node. Recall stays within a small margin of the recall before the heal.
Compact
The delta is merged and the dead are dropped. With nothing to change, the result is the same index. Two compactions in a row work. Every main document deleted works.
Two scripts drive a real server end to end: scripts/check_updates.py and scripts/check_cleanup.py.
What is left

Known limits

Things we know and have not fixed yet.