|
@@ -0,0 +1,153 @@
|
|
|
|
|
+// v2.12.2 client/server boundary test: dropping a collection (or a project)
|
|
|
|
|
+// forgets everything declared on it.
|
|
|
|
|
+//
|
|
|
|
|
+// Reported by smartbotic-vectorapi against 2.12.1: dropCollection removed the
|
|
|
|
|
+// documents but left the collection's CONFIG record - declared indexes, unique
|
|
|
|
|
+// and composite constraints, relations_enforced, versioning - plus the index
|
|
|
|
|
+// postings. A later collection of the same name silently inherited a unique
|
|
|
|
|
+// constraint nobody declared (a legitimate write refused as a duplicate), and
|
|
|
|
|
+// re-declaring it answered already_existed instead of validating the new data.
|
|
|
|
|
+// Worse, once the collection (or its project) was gone the record could not be
|
|
|
|
|
+// removed through the API at all: dropIndex refused without project storage,
|
|
|
|
|
+// dropCollection said false.
|
|
|
|
|
+//
|
|
|
|
|
+// Run twice by the .sh, with a restart between: removing the in-memory cache
|
|
|
|
|
+// but not the persisted record would pass every check before a restart and
|
|
|
|
|
+// resurrect the poisoned declarations after it.
|
|
|
|
|
+
|
|
|
|
|
+#include <smartbotic/database/client.hpp>
|
|
|
|
|
+#include <iostream>
|
|
|
|
|
+#include <string>
|
|
|
|
|
+
|
|
|
|
|
+using namespace smartbotic::database;
|
|
|
|
|
+static int pass = 0, fail = 0;
|
|
|
|
|
+static void ck(bool c, const std::string& m) {
|
|
|
|
|
+ if (c) { ++pass; } else { ++fail; std::cerr << "FAIL: " << m << "\n"; }
|
|
|
|
|
+}
|
|
|
|
|
+static const char* ADDR = "127.0.0.1:9020";
|
|
|
|
|
+
|
|
|
|
|
+static bool hasIndex(Client& c, const std::string& coll, const std::string& field) {
|
|
|
|
|
+ for (const auto& i : c.listIndexes(coll)) if (i.field == field) return true;
|
|
|
|
|
+ return false;
|
|
|
|
|
+}
|
|
|
|
|
+static bool throwsDuplicate(Client& c, const std::string& coll, const nlohmann::json& doc, const std::string& id) {
|
|
|
|
|
+ try { c.insert(coll, doc, id); return false; } catch (const Client::AlreadyExists&) { return true; }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void beforeRestart() {
|
|
|
|
|
+ Client c({.address = ADDR, .project = "acme"});
|
|
|
|
|
+ c.connect();
|
|
|
|
|
+
|
|
|
|
|
+ // ---- the reported sequence
|
|
|
|
|
+ c.upsert("leak", nlohmann::json{{"sku", "Q1"}, {"cat", "a"}}, "a1");
|
|
|
|
|
+ ck(c.createUniqueIndex("leak", "sku").success, "declare unique(sku)");
|
|
|
|
|
+ ck(c.createIndex("leak", "cat"), "declare index(cat)");
|
|
|
|
|
+ ck(c.createCompositeUniqueIndex("leak", {"sku", "cat"}).success, "declare composite(sku, cat)");
|
|
|
|
|
+ ck(c.setRelationsEnforced("leak", false), "set relations_enforced=false");
|
|
|
|
|
+ ck(c.dropCollection("leak"), "dropCollection");
|
|
|
|
|
+ ck(c.listIndexes("leak").empty(), "after drop: no single-field index declared");
|
|
|
|
|
+ ck(c.listCompositeUniqueIndexes("leak").empty(), "after drop: no composite constraint declared");
|
|
|
|
|
+ ck(!c.hasCollectionConfig("leak"), "after drop: no config record at all");
|
|
|
|
|
+ ck(c.tryGetRelationsEnforced("leak") == std::optional<bool>(true),
|
|
|
|
|
+ "after drop: relations_enforced back to its default");
|
|
|
|
|
+
|
|
|
|
|
+ // a new collection of the same name starts clean
|
|
|
|
|
+ c.insert("leak", nlohmann::json{{"sku", "Q9"}, {"cat", "a"}}, "b1");
|
|
|
|
|
+ ck(!throwsDuplicate(c, "leak", nlohmann::json{{"sku", "Q9"}, {"cat", "a"}}, "b2"),
|
|
|
|
|
+ "recreated collection: the old unique(sku) and composite are NOT inherited");
|
|
|
|
|
+ c.remove("leak", "b2");
|
|
|
|
|
+ auto r = c.createUniqueIndex("leak", "sku");
|
|
|
|
|
+ ck(r.success && !r.alreadyExisted, "recreated collection: declaring unique(sku) is a NEW declaration");
|
|
|
|
|
+ ck(r.rowsIndexed == 1, "recreated collection: the index holds only the new rows (old postings gone), got " +
|
|
|
|
|
+ std::to_string(r.rowsIndexed));
|
|
|
|
|
+
|
|
|
|
|
+ // ---- repairing a name whose collection does not exist
|
|
|
|
|
+ ck(c.createIndex("ghost", "f"), "declare an index on a collection that does not exist");
|
|
|
|
|
+ ck(hasIndex(c, "ghost", "f"), "...it is listed");
|
|
|
|
|
+ ck(c.dropIndex("ghost", "f"), "dropIndex on an absent collection succeeds");
|
|
|
|
|
+ ck(!hasIndex(c, "ghost", "f"), "...and the declaration is gone");
|
|
|
|
|
+
|
|
|
|
|
+ ck(c.createUniqueIndex("ghost2", "f").success, "declare a unique index on another absent collection");
|
|
|
|
|
+ c.dropCollection("ghost2"); // false: there is no collection - but it must still clean up
|
|
|
|
|
+ ck(c.listIndexes("ghost2").empty(), "dropCollection on an absent collection still clears its declarations");
|
|
|
|
|
+
|
|
|
|
|
+ // ---- dropping a project forgets its collections' configs
|
|
|
|
|
+ Client admin({.address = ADDR, .project = "default"});
|
|
|
|
|
+ admin.connect();
|
|
|
|
|
+ admin.createProject("tmpproj");
|
|
|
|
|
+ Client t({.address = ADDR, .project = "tmpproj"});
|
|
|
|
|
+ t.connect();
|
|
|
|
|
+ t.upsert("c", nlohmann::json{{"sku", "Z"}}, "z1");
|
|
|
|
|
+ ck(t.createUniqueIndex("c", "sku").success, "tmpproj: declare unique(sku)");
|
|
|
|
|
+ // a declaration on a collection that has NO documents: only the
|
|
|
|
|
+ // project-wide config purge can find it, the per-collection one cannot
|
|
|
|
|
+ ck(t.createIndex("never", "f"), "tmpproj: declare an index on an empty collection");
|
|
|
|
|
+ admin.dropProject("tmpproj");
|
|
|
|
|
+ admin.createProject("tmpproj");
|
|
|
|
|
+ ck(t.listIndexes("c").empty(), "recreated project: no inherited index declaration");
|
|
|
|
|
+ ck(t.listIndexes("never").empty(), "recreated project: no inherited declaration on an empty collection");
|
|
|
|
|
+ bool z1Free = true;
|
|
|
|
|
+ try { t.insert("c", nlohmann::json{{"sku", "Z"}}, "z1"); } catch (const std::exception&) { z1Free = false; }
|
|
|
|
|
+ ck(z1Free, "recreated project: the dropped project's document id is free");
|
|
|
|
|
+ ck(!throwsDuplicate(t, "c", nlohmann::json{{"sku", "Z"}}, "z2"), "recreated project: no inherited unique constraint");
|
|
|
|
|
+
|
|
|
|
|
+ // the POISONED state older versions left behind: a config record for a
|
|
|
|
|
+ // project whose storage no longer exists. Written straight into
|
|
|
|
|
+ // `_collection_meta` (global, so not project-qualified); the config cache
|
|
|
|
|
+ // loads it at the next boot, and after the restart it must be removable.
|
|
|
|
|
+ admin.upsert("_collection_meta", nlohmann::json{{"timestamp_precision", "ns"},
|
|
|
|
|
+ {"indexed_fields", {"f"}}, {"unique_fields", {"f"}}}, "ghostproj:c");
|
|
|
|
|
+
|
|
|
|
|
+ // a dropped project's DOCUMENTS must be gone too - from memory now, and
|
|
|
|
|
+ // from the WAL, or a restart replays them back (checked after the restart)
|
|
|
|
|
+ admin.createProject("tmpproj2");
|
|
|
|
|
+ Client t2({.address = ADDR, .project = "tmpproj2"});
|
|
|
|
|
+ t2.connect();
|
|
|
|
|
+ t2.insert("c", nlohmann::json{{"v", 1}}, "old1");
|
|
|
|
|
+ admin.dropProject("tmpproj2");
|
|
|
|
|
+ admin.createProject("tmpproj2");
|
|
|
|
|
+ ck(!t2.get("c", "old1").has_value(), "recreated project: the dropped project's document is gone");
|
|
|
|
|
+ bool reinsertOk = true;
|
|
|
|
|
+ try { t2.insert("c", nlohmann::json{{"v", 2}}, "old1b"); } catch (const std::exception&) { reinsertOk = false; }
|
|
|
|
|
+ ck(reinsertOk, "recreated project accepts writes");
|
|
|
|
|
+ t2.remove("c", "old1b");
|
|
|
|
|
+ admin.dropProject("tmpproj2");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void afterRestart() {
|
|
|
|
|
+ Client c({.address = ADDR, .project = "acme"});
|
|
|
|
|
+ c.connect();
|
|
|
|
|
+ ck(c.listCompositeUniqueIndexes("leak").empty(), "after restart: the dropped composite did not come back");
|
|
|
|
|
+ ck(hasIndex(c, "leak", "sku"), "after restart: the NEW unique(sku) is still declared");
|
|
|
|
|
+ ck(!hasIndex(c, "leak", "cat"), "after restart: the dropped index(cat) did not come back");
|
|
|
|
|
+ ck(c.tryGetRelationsEnforced("leak") == std::optional<bool>(true), "after restart: relations_enforced still default");
|
|
|
|
|
+ ck(c.listIndexes("ghost").empty() && c.listIndexes("ghost2").empty(), "after restart: repaired names stay clean");
|
|
|
|
|
+ Client t({.address = ADDR, .project = "tmpproj"});
|
|
|
|
|
+ t.connect();
|
|
|
|
|
+ ck(t.listIndexes("c").empty(), "after restart: the recreated project still has no inherited declaration");
|
|
|
|
|
+
|
|
|
|
|
+ Client admin({.address = ADDR, .project = "default"});
|
|
|
|
|
+ admin.connect();
|
|
|
|
|
+
|
|
|
|
|
+ // repair of the poisoned record: a project with no storage
|
|
|
|
|
+ Client gp({.address = ADDR, .project = "ghostproj"});
|
|
|
|
|
+ gp.connect();
|
|
|
|
|
+ ck(hasIndex(gp, "c", "f"), "poisoned name: the leftover declaration is visible (setup worked)");
|
|
|
|
|
+ ck(gp.dropIndex("c", "f"), "poisoned name: dropIndex succeeds without project storage");
|
|
|
|
|
+ ck(!hasIndex(gp, "c", "f"), "poisoned name: the declaration is gone");
|
|
|
|
|
+
|
|
|
|
|
+ admin.createProject("tmpproj2");
|
|
|
|
|
+ Client t2({.address = ADDR, .project = "tmpproj2"});
|
|
|
|
|
+ t2.connect();
|
|
|
|
|
+ ck(!t2.get("c", "old1").has_value(), "after restart: a dropped project's documents were not replayed back");
|
|
|
|
|
+ bool threw = false;
|
|
|
|
|
+ try { t2.insert("c", nlohmann::json{{"v", 3}}, "old1"); } catch (const std::exception&) { threw = true; }
|
|
|
|
|
+ ck(!threw, "after restart: the dropped document's id is free");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int main(int argc, char** argv) {
|
|
|
|
|
+ const std::string phase = argc > 1 ? argv[1] : "before-restart";
|
|
|
|
|
+ if (phase == "before-restart") beforeRestart(); else afterRestart();
|
|
|
|
|
+ std::cout << "\n[" << phase << "] passed=" << pass << " failed=" << fail << "\n";
|
|
|
|
|
+ return fail == 0 ? 0 : 1;
|
|
|
|
|
+}
|