|
|
@@ -498,6 +498,110 @@ void test_patch_stamps_unmarked_rows() {
|
|
|
check(healthy.load() && drift.load() == 0, "patch stamp: clean");
|
|
|
}
|
|
|
|
|
|
+// v2.20.2 - a row in LMDB that MemoryStore does not hold (not resident, no
|
|
|
+// eviction stub). Found on a production instance: reads (served from LMDB)
|
|
|
+// returned it, every write said not-found, and an insert or upsert of its id
|
|
|
+// was ACCEPTED and overwrote it at version 1. MemoryStore is rebuilt from
|
|
|
+// snapshot + WAL, never from LMDB, so a restart could not repair it. Every
|
|
|
+// write path must now find the row; the planted rows are written to LMDB
|
|
|
+// only, exactly the production state.
|
|
|
+void test_lmdb_only_rows_are_writable() {
|
|
|
+ using smartbotic::database::DuplicateDocumentId;
|
|
|
+ TmpEnv tmp("lmdb-only");
|
|
|
+ LmdbDocumentStore doc_store(tmp.env);
|
|
|
+ std::atomic<bool> healthy{true};
|
|
|
+ std::atomic<uint64_t> drift{0};
|
|
|
+ MemoryStore::Config cfg;
|
|
|
+ cfg.nodeId = "test-node";
|
|
|
+ cfg.maxMemoryBytes = 64ULL * 1024 * 1024;
|
|
|
+ MemoryStore mem(cfg);
|
|
|
+ mem.setDocumentStoreMirror([&doc_store](std::string_view) { return &doc_store; },
|
|
|
+ &healthy, &drift);
|
|
|
+ const std::string C = "proj:things";
|
|
|
+ mem.createCollection(C, {});
|
|
|
+ mem.insert(C, make_doc("resident", C, {{"n", 0}})); // the collection exists in memory
|
|
|
+
|
|
|
+ auto plant = [&](const std::string& id, uint64_t version) {
|
|
|
+ Document d = make_doc(id, "things", {{"n", 1}, {"keep", "me"}});
|
|
|
+ d.version = version;
|
|
|
+ doc_store.put("things", id, d); // LMDB only
|
|
|
+ };
|
|
|
+ for (const char* id : {"patch", "update", "cas", "upsert", "insert", "remove", "upsert-gone"}) {
|
|
|
+ plant(id, 7);
|
|
|
+ }
|
|
|
+ check(!mem.get(C, "patch").has_value(), "lmdb-only: the planted row is not in memory");
|
|
|
+
|
|
|
+ check(mem.patchDocument(C, "patch", {{"n", 2}}) == 8, "lmdb-only: patch applies, version 7 -> 8");
|
|
|
+ auto p = doc_store.get("things", "patch");
|
|
|
+ check(p && p->data()["n"] == 2 && p->data()["keep"] == "me",
|
|
|
+ "lmdb-only: patch merged into the on-disk body (other fields kept)");
|
|
|
+
|
|
|
+ check(mem.update(C, "update", make_doc("update", C, {{"n", 3}})), "lmdb-only: update applies");
|
|
|
+ auto u = doc_store.get("things", "update");
|
|
|
+ check(u && u->data()["n"] == 3 && u->version == 8, "lmdb-only: update wrote version 8");
|
|
|
+
|
|
|
+ check(mem.updateIfVersion(C, "cas", make_doc("cas", C, {{"n", 4}}), 7),
|
|
|
+ "lmdb-only: updateIfVersion against the on-disk version applies");
|
|
|
+
|
|
|
+ Document up = make_doc("upsert", C, {{"n", 5}});
|
|
|
+ mem.upsert(C, up);
|
|
|
+ auto us = doc_store.get("things", "upsert");
|
|
|
+ check(us && us->version == 8 && us->createdAt == 1731628800000ULL,
|
|
|
+ "lmdb-only: upsert updates (version 8, createdAt kept), not re-created at version 1");
|
|
|
+
|
|
|
+ bool dup = false;
|
|
|
+ try {
|
|
|
+ mem.insert(C, make_doc("insert", C, {{"n", 99}}));
|
|
|
+ } catch (const DuplicateDocumentId&) {
|
|
|
+ dup = true;
|
|
|
+ }
|
|
|
+ check(dup, "lmdb-only: insert of the id is refused as a duplicate");
|
|
|
+ auto ins = doc_store.get("things", "insert");
|
|
|
+ check(ins && ins->data()["n"] == 1 && ins->version == 7, "lmdb-only: insert left the row untouched");
|
|
|
+
|
|
|
+ check(mem.remove(C, "remove"), "lmdb-only: remove returns true");
|
|
|
+ check(!doc_store.get("things", "remove").has_value(), "lmdb-only: remove deleted it on disk");
|
|
|
+
|
|
|
+ // A row deleted from LMDB between reads is simply absent.
|
|
|
+ check(mem.patchDocument(C, "never-existed", {{"n", 1}}) == 0, "lmdb-only: a truly absent row is still not found");
|
|
|
+ check(!mem.get(C, "never-existed").has_value(), "lmdb-only: and nothing was conjured");
|
|
|
+
|
|
|
+ // Mirror not healthy: MemoryStore may be ahead of LMDB (a delete here that
|
|
|
+ // never reached LMDB), so nothing is adopted - the old behaviour.
|
|
|
+ healthy.store(false);
|
|
|
+ check(mem.patchDocument(C, "upsert-gone", {{"n", 6}}) == 0,
|
|
|
+ "lmdb-only: with the mirror unhealthy, nothing is loaded from LMDB");
|
|
|
+ healthy.store(true);
|
|
|
+ drift.store(1);
|
|
|
+ check(mem.patchDocument(C, "upsert-gone", {{"n", 6}}) == 0,
|
|
|
+ "lmdb-only: with drift, nothing is loaded from LMDB");
|
|
|
+ drift.store(0);
|
|
|
+
|
|
|
+ // A legacy bare alias holding the id: no second copy is made.
|
|
|
+ mem.createCollection("things", {});
|
|
|
+ mem.loadDocument("things", make_doc("aliased", "things", {{"n", 1}}));
|
|
|
+ plant("aliased", 7);
|
|
|
+ check(mem.patchDocument("default:things", "aliased", {{"n", 2}}) == 0,
|
|
|
+ "lmdb-only: an id held under the bare alias is not copied into the qualified name");
|
|
|
+
|
|
|
+ // Vectors come along, so a MemoryStore-served search still sees the row.
|
|
|
+ smartbotic::database::CollectionOptions vopts;
|
|
|
+ vopts.vectorDimension = 4;
|
|
|
+ mem.createCollection("proj:emb", vopts);
|
|
|
+ mem.insert("proj:emb", make_doc("seed", "proj:emb", {{"n", 0}}));
|
|
|
+ {
|
|
|
+ Document d = make_doc("v", "emb", {{"n", 1}});
|
|
|
+ d.hasVector = true;
|
|
|
+ doc_store.put("emb", "v", d);
|
|
|
+ doc_store.put_vector("emb", "v", {1.f, 0.f, 0.f, 0.f});
|
|
|
+ }
|
|
|
+ check(mem.patchDocument("proj:emb", "v", {{"n", 2}}) == 2, "lmdb-only: patch of a vector row applies");
|
|
|
+ auto vecs = mem.getCollectionVectors("proj:emb");
|
|
|
+ check(vecs && vecs->count("v") == 1, "lmdb-only: its vector was loaded with it");
|
|
|
+ check(doc_store.get_vector("emb", "v").has_value(), "lmdb-only: and kept on disk");
|
|
|
+ check(healthy.load() && drift.load() == 0, "lmdb-only: clean");
|
|
|
+}
|
|
|
+
|
|
|
// Simulates DatabaseService::backfillIntoDocStore — populate MemoryStore
|
|
|
// without a persist callback (no live mirror), then walk listCollections +
|
|
|
// getAllDocuments and feed each doc into applyDualWriteMirror with
|
|
|
@@ -613,6 +717,7 @@ int main() {
|
|
|
test_vector_mirror_system_collection_skipped();
|
|
|
test_vector_mirror_via_memory_store();
|
|
|
test_patch_stamps_unmarked_rows();
|
|
|
+ test_lmdb_only_rows_are_writable();
|
|
|
test_backfill_iteration_pattern();
|
|
|
test_scan_vectors_round_trip();
|
|
|
|