fix: flush graph LSM-trees on close to prevent data loss across restarts

GraphAdjacencyIndex.flush() was a no-op — LSM MemTables were never
written to SSTables for datasets under the 100K auto-flush threshold.
This caused readdir, getRelations, and getDescendants to return empty
results after close + reopen.

Three fixes:
- LSMTree.get(): merge MemTable + SSTable results (data spans both
  after flush, old early-return missed SSTable data)
- GraphAdjacencyIndex.flush(): actually flush all 4 LSM-trees
- GraphAdjacencyIndex.close(): close all 4 trees (was only closing 2)

Also: brain.close() and shutdown hooks now call close() on graphIndex,
HNSW index, and metadataIndex to release timers and file handles.
This commit is contained in:
David Snelling 2026-02-01 17:55:40 -08:00
parent 0add0af45a
commit ab2493af02
4 changed files with 213 additions and 22 deletions

View file

@ -296,18 +296,21 @@ export class LSMTree {
async get(sourceId: string): Promise<string[] | null> {
const startTime = performance.now()
// Check MemTable first (hot data)
// Merge results from MemTable AND SSTables
// Data can span both after a flush (old data in SSTables, new in MemTable)
const allTargets = new Set<string>()
// Check MemTable (hot data)
const memResult = this.memTable.get(sourceId)
if (memResult !== null) {
return memResult
for (const target of memResult) {
allTargets.add(target)
}
}
// Check SSTables from newest to oldest
// Newer levels (L0, L1, L2) checked first for better cache locality
const maxLevel = Math.max(...Array.from(this.sstablesByLevel.keys()), 0)
const allTargets = new Set<string>()
for (let level = 0; level <= maxLevel; level++) {
const sstables = this.sstablesByLevel.get(level) || []
@ -608,16 +611,20 @@ export class LSMTree {
}
/**
* Flush MemTable and stop compaction
* Called during shutdown
* Flush MemTable to SSTables without closing
* Called by GraphAdjacencyIndex.flush() and brain.close()
*/
async flush(): Promise<void> {
if (!this.memTable.isEmpty()) {
await this.flushMemTable()
}
}
async close(): Promise<void> {
this.stopCompactionTimer()
// Final MemTable flush
if (!this.memTable.isEmpty()) {
await this.flushMemTable()
}
await this.flush()
prodLog.info('LSMTree: Closed successfully')
}