fix: speed up fluree-memory adds and stop them hanging under concurrency#1384
Open
bplatz wants to merge 2 commits into
Open
fix: speed up fluree-memory adds and stop them hanging under concurrency#1384bplatz wants to merge 2 commits into
bplatz wants to merge 2 commits into
Conversation
memory_add re-read every memory from the ledger (all_memories_for_scope, a SPARQL round-trip) and rewrote the whole file on each call. Under concurrent MCP requests that O(N) work serialized behind the mutation lock and made adds take seconds (13.5s at N=100), which read as hangs. A wedged op could also hold a lock forever (an observed CPU-spun server froze every later call). - add() now splices the new block into its sorted (branch, id) position by reading only the .ttl text (turtle_io::insert_memory_into_file) — never the ledger. Untouched blocks stay byte-identical, so the file is identical to a full rewrite (asserted) and concurrent adds on different branches still merge cleanly under a *default* git merge, while edits to the same memory correctly conflict. No merge=union, so update/forget can't silently merge-corrupt. - both locks are now bounded: the cross-process flock polls try_lock to a deadline, and the in-process mutation lock uses a timeout (lock_mutation); shared file_sync::lock_timeout, 30s default, FLUREE_MEMORY_LOCK_TIMEOUT_SECS. - tests: splice == full-rewrite byte-for-byte; distinct-region adds merge clean; same-memory edits conflict; flock times out when held.
The add path now splices a memory block into its sorted (branch, id) position by reading the .ttl text rather than re-deriving the file from the ledger; update/forget still rewrite. Note the bounded lock wait, and make explicit that a same-memory edit on two branches conflicts by design (default git merge) rather than silently merging.
5d0a7da to
e2a579e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
memory_add(CLI and MCP) re-read every memory from the__memoryledger via a SPARQL query (all_memories_for_scope) and rewrote the wholerepo.ttlon each call. Under concurrent MCP requests — e.g. an agent saving 2–3 memories at once, which the client dispatches in parallel — that O(N) work serialized behind the per-process mutation lock and made adds take seconds (~13.5s at 100 memories), which users experienced as hangs. A separately-observed failure mode: a wedged operation could hold a lock forever (a CPU-spun MCP server froze every later call in that process).Changes
addnow inserts the new block at its sorted(branch, id)position by reading only the.ttltext (turtle_io::insert_memory_into_file), never the ledger. Untouched blocks stay byte-identical (asserted equal to a full rewrite), so the on-disk result and merge behavior are unchanged. The add itself drops to ~2ms; the per-add ledger round-trip is gone.(branch, id)sort under a default git merge: distinct-branch adds auto-merge, while two branches changing the same memory correctly conflict — no silent corruption.try_lockto a deadline, and the in-process mutation lock uses a timeout, so a wedged operation surfaces a recoverable error instead of freezing the session. Sharedfile_sync::lock_timeout(30s default, override withFLUREE_MEMORY_LOCK_TIMEOUT_SECS).Tests
it_merge: distinct-region adds merge cleanly; same-memory edits conflict.turtle_io: incremental splice equals a full sorted rewrite byte-for-byte.file_sync: the rebuild lock times out when already held.