9f378946b9
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.
114 lines
3.6 KiB
Go
114 lines
3.6 KiB
Go
package db_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/david/knox/internal/db"
|
|
)
|
|
|
|
// TestLinkReplicates: membership must travel with the thread. A peer that
|
|
// receives a thread without its members can list it but cannot summarise it.
|
|
func TestLinkReplicates(t *testing.T) {
|
|
kdb, raw := openBoth(t)
|
|
|
|
// An entry to link to, created the way the indexer does.
|
|
if _, _, err := kdb.RecordObservation(db.ObservationRecord{
|
|
Fingerprint: "entry-fp-1", SourceID: "test", Title: "grafana dashboard notes",
|
|
Content: "dashboards", IngesterVersion: "test/v1", Trigger: "test",
|
|
}); err != nil {
|
|
t.Fatalf("record entry: %v", err)
|
|
}
|
|
|
|
id, err := kdb.CreateThread("Dashboards", "why", "high", `[]`, `{}`)
|
|
if err != nil {
|
|
t.Fatalf("create thread: %v", err)
|
|
}
|
|
if err := kdb.LinkEntryToThread(id, "entry-fp-1", "references"); err != nil {
|
|
t.Fatalf("link: %v", err)
|
|
}
|
|
|
|
var linkObs int
|
|
if err := raw.QueryRow(`SELECT COUNT(*) FROM observations WHERE source_id='thread_link'`).Scan(&linkObs); err != nil {
|
|
t.Fatalf("count links: %v", err)
|
|
}
|
|
if linkObs != 1 {
|
|
t.Fatalf("link observations = %d, want 1", linkObs)
|
|
}
|
|
|
|
// Wipe local membership, then rebuild it from the log alone.
|
|
if _, err := raw.Exec(`DELETE FROM thread_observations; DELETE FROM provenance_links;`); err != nil {
|
|
t.Fatalf("wipe: %v", err)
|
|
}
|
|
if _, err := kdb.ProjectLinks(); err != nil {
|
|
t.Fatalf("project links: %v", err)
|
|
}
|
|
|
|
var members int
|
|
if err := raw.QueryRow(
|
|
`SELECT COUNT(*) FROM thread_observations WHERE thread_id=?`, id).Scan(&members); err != nil {
|
|
t.Fatalf("count members: %v", err)
|
|
}
|
|
if members != 1 {
|
|
t.Fatalf("thread members = %d, want 1 after projection", members)
|
|
}
|
|
var prov int
|
|
if err := raw.QueryRow(
|
|
`SELECT COUNT(*) FROM provenance_links WHERE descendant_fp='entry-fp-1'`).Scan(&prov); err != nil {
|
|
t.Fatalf("count provenance: %v", err)
|
|
}
|
|
if prov != 1 {
|
|
t.Fatalf("provenance links = %d, want 1", prov)
|
|
}
|
|
}
|
|
|
|
// TestMergeRecordsSupersede: a merged thread must point at its successor, so a
|
|
// peer resolves the merge instead of resurrecting the absorbed thread.
|
|
func TestMergeRecordsSupersede(t *testing.T) {
|
|
kdb, raw := openBoth(t)
|
|
|
|
into, err := kdb.CreateThread("Successor", "m", "high", `[]`, `{}`)
|
|
if err != nil {
|
|
t.Fatalf("create into: %v", err)
|
|
}
|
|
from, err := kdb.CreateThread("Absorbed", "m", "low", `[]`, `{}`)
|
|
if err != nil {
|
|
t.Fatalf("create from: %v", err)
|
|
}
|
|
if err := kdb.MergeThreads(into, from); err != nil {
|
|
t.Fatalf("merge: %v", err)
|
|
}
|
|
|
|
var intoUID, superseded, status string
|
|
if err := raw.QueryRow(`SELECT COALESCE(uid,'') FROM threads WHERE id=?`, into).Scan(&intoUID); err != nil {
|
|
t.Fatalf("read successor: %v", err)
|
|
}
|
|
if err := raw.QueryRow(
|
|
`SELECT COALESCE(superseded_by,''), COALESCE(status,'') FROM threads WHERE id=?`, from,
|
|
).Scan(&superseded, &status); err != nil {
|
|
t.Fatalf("read absorbed: %v", err)
|
|
}
|
|
if superseded != intoUID {
|
|
t.Fatalf("superseded_by = %q, want the successor uid %q", superseded, intoUID)
|
|
}
|
|
if status != "resolved" {
|
|
t.Fatalf("absorbed status = %q, want resolved", status)
|
|
}
|
|
|
|
// The pointer must also survive a rebuild from the log.
|
|
if _, err := raw.Exec(`DELETE FROM thread_observations; DELETE FROM threads;`); err != nil {
|
|
t.Fatalf("wipe: %v", err)
|
|
}
|
|
if _, err := kdb.ProjectThreads(); err != nil {
|
|
t.Fatalf("project: %v", err)
|
|
}
|
|
var restored string
|
|
if err := raw.QueryRow(
|
|
`SELECT COALESCE(superseded_by,'') FROM threads WHERE uid=(SELECT uid FROM observations WHERE source_id='thread' AND content LIKE '%Absorbed%' LIMIT 1)`,
|
|
).Scan(&restored); err != nil {
|
|
t.Fatalf("read restored: %v", err)
|
|
}
|
|
if restored != intoUID {
|
|
t.Fatalf("restored superseded_by = %q, want %q", restored, intoUID)
|
|
}
|
|
}
|