feat: replicate thread membership and record merges (#41)
Threads replicated without their members: the replica could list a thread but not summarise it. Membership was stored two node-local ways — thread_observations by local observation id, provenance_links by ThreadFP(int id) — so neither could cross a node boundary. The replicated fact is the entry-level edge (thread uid, entry fingerprint), because the fingerprint is already stable across nodes; both local forms are materialised from it on arrival. Unlinks replicate as tombstones, or the next reconcile would resurrect the edge. Merges now record superseded_by = successor uid and replicate both threads, so a peer resolves the merge instead of restoring the absorbed thread. One deadlock fixed along the way: MergeThreads resolved the successor uid inside its transaction, and the pool is pinned to a single connection, so the query waited on itself.
This commit is contained in:
+39
-5
@@ -1844,7 +1844,13 @@ func (k *KnoxDB) LinkObservationToThread(threadID, observationID int64, relevanc
|
||||
if _, err := tx.Exec(`UPDATE threads SET updated_at=datetime('now') WHERE id=?`, threadID); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
if e := k.EmitLinkForObservation(threadID, observationID, relevance); e != nil {
|
||||
log.Printf("[knox] emit link obs %d: %v", observationID, e)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AutoLinkThreadObservations resolves each entry fingerprint to its most recent
|
||||
@@ -2056,11 +2062,20 @@ func (k *KnoxDB) LinkEntryToThread(threadID int64, entryFP, relation string) err
|
||||
return fmt.Errorf("entry fingerprint must not be empty")
|
||||
}
|
||||
tfp := ThreadFP(threadID)
|
||||
_, err := k.db.Exec(
|
||||
if _, err := k.db.Exec(
|
||||
`INSERT OR IGNORE INTO provenance_links (descendant_fp, ancestor_fp, hop_distance, relation) VALUES (?, ?, 1, ?)`,
|
||||
entryFP, tfp, relation,
|
||||
)
|
||||
return err
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
// Membership is a replicated fact: without it a peer receives the thread
|
||||
// without its members, which is worse than not receiving it at all.
|
||||
if uid, err := k.EnsureThreadUID(threadID); err == nil {
|
||||
if e := k.EmitLink(uid, LinkDoc{EntryFP: entryFP, Relation: relation}); e != nil {
|
||||
log.Printf("[knox] emit link %s: %v", entryFP, e)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// profileText is the text a thread's relevance keywords are mined from: the
|
||||
@@ -2693,6 +2708,12 @@ func (k *KnoxDB) MergeThreads(into, from int64) error {
|
||||
if into == from || into <= 0 || from <= 0 {
|
||||
return fmt.Errorf("invalid merge: into=%d from=%d", into, from)
|
||||
}
|
||||
// Resolve the successor identity BEFORE opening the transaction: the pool is
|
||||
// pinned to one connection, so a query inside the tx would wait on itself.
|
||||
intoUID, err := k.EnsureThreadUID(into)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx, err := k.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -2716,7 +2737,20 @@ func (k *KnoxDB) MergeThreads(into, from int64) error {
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
// Point the absorbed thread at its successor so a peer can resolve the
|
||||
// merge rather than resurrecting it on the next reconcile.
|
||||
if _, err := tx.Exec(`UPDATE threads SET superseded_by=? WHERE id=?`, intoUID, from); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range []int64{into, from} {
|
||||
if e := k.EmitThread(id); e != nil {
|
||||
log.Printf("[knox] emit thread #%d: %v", id, e)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DenyThreadNoise refocuses existing threads against the shared denylist:
|
||||
|
||||
Reference in New Issue
Block a user