|
|
@@ -0,0 +1,138 @@
|
|
|
+// v2.12.1 client/server boundary test: writes to a document MemoryStore has
|
|
|
+// EVICTED, and the duplicate-id refusal.
|
|
|
+//
|
|
|
+// Since v2.0 reads are served from LMDB, so a read no longer hot-loads an
|
|
|
+// evicted document back into MemoryStore - and every write path looked the
|
|
|
+// document up in MemoryStore only. Once evicted, a document was invisible to
|
|
|
+// writes for the life of the process (measured on 2.12.0, 200 of 200 each):
|
|
|
+// patch / update -> "not found"
|
|
|
+// remove -> false, and the document STAYED
|
|
|
+// insert same id -> ACCEPTED, and OVERWROTE the existing document
|
|
|
+// upsert -> re-created it at version 1
|
|
|
+// The .sh runs the server with a tiny memory budget and no hot-write floor so
|
|
|
+// eviction really happens, and each collection is only used for one kind of
|
|
|
+// write, so every operation meets documents that are genuinely evicted.
|
|
|
+//
|
|
|
+// Also: a duplicate primary id on insert used to throw a bare runtime_error
|
|
|
+// and reach the client as Internal; it is now AlreadyExists, reason
|
|
|
+// "duplicate_id", conflictingId = the id.
|
|
|
+
|
|
|
+#include <smartbotic/database/client.hpp>
|
|
|
+#include <chrono>
|
|
|
+#include <iostream>
|
|
|
+#include <map>
|
|
|
+#include <string>
|
|
|
+#include <thread>
|
|
|
+
|
|
|
+using namespace smartbotic::database;
|
|
|
+using Code = Client::ErrorCode;
|
|
|
+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 int N = 150;
|
|
|
+static std::string id(int i) { return "d" + std::to_string(i); }
|
|
|
+
|
|
|
+int main() {
|
|
|
+ Client c({.address = "127.0.0.1:9019", .project = "acme"});
|
|
|
+ c.connect();
|
|
|
+
|
|
|
+ // ---- duplicate primary id on a RESIDENT document
|
|
|
+ c.insert("dup", nlohmann::json{{"n", 1}}, "x1");
|
|
|
+ {
|
|
|
+ bool typed = false;
|
|
|
+ Client::FailureDetail d;
|
|
|
+ try { c.insert("dup", nlohmann::json{{"n", 2}}, "x1"); }
|
|
|
+ catch (const Client::AlreadyExists& e) {
|
|
|
+ typed = true;
|
|
|
+ if (const auto* p = Client::errorDetail(e)) d = *p;
|
|
|
+ } catch (const std::exception&) {}
|
|
|
+ ck(typed, "duplicate id: AlreadyExists (was Internal)");
|
|
|
+ ck(d.reason == "duplicate_id", "duplicate id: reason duplicate_id, got '" + d.reason + "'");
|
|
|
+ ck(d.conflictingId == "x1", "duplicate id: conflictingId is the id");
|
|
|
+ ck(d.fields.empty(), "duplicate id: no field collided, so fields is empty");
|
|
|
+ auto x = c.get("dup", "x1");
|
|
|
+ ck(x && (*x)["n"] == 1, "duplicate id: the existing document is untouched");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ---- force eviction
|
|
|
+ const std::string pad(4000, 'x');
|
|
|
+ const char* colls[] = {"pa", "up", "uv", "pv", "inc", "de", "in", "ups"};
|
|
|
+ for (const char* coll : colls)
|
|
|
+ for (int i = 0; i < N; ++i) c.insert(coll, nlohmann::json{{"n", i}, {"pad", pad}}, id(i));
|
|
|
+ std::map<std::string, uint64_t> stubs;
|
|
|
+ bool evicted = false;
|
|
|
+ for (int t = 0; t < 80 && !evicted; ++t) {
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
+ for (const auto& col : c.getMemoryStats().collections) stubs[col.collection] = col.evictedStubCount;
|
|
|
+ evicted = true;
|
|
|
+ for (const char* coll : colls) evicted = evicted && stubs["acme:" + std::string(coll)] >= N / 2;
|
|
|
+ }
|
|
|
+ ck(evicted, "eviction happened in every collection (else this test proves nothing)");
|
|
|
+ uint64_t minStubs = N;
|
|
|
+ for (const char* coll : colls) minStubs = std::min<uint64_t>(minStubs, stubs["acme:" + std::string(coll)]);
|
|
|
+ std::cout << "evicted at least " << minStubs << " of " << N << " in each collection\n";
|
|
|
+
|
|
|
+ int bad = 0;
|
|
|
+ for (int i = 0; i < N; ++i) { Client::FailureDetail f; if (c.patch("pa", id(i), {{"t", 1}}, f) == 0) ++bad; }
|
|
|
+ ck(bad == 0, "patch of evicted documents: " + std::to_string(bad) + " failed");
|
|
|
+
|
|
|
+ bad = 0;
|
|
|
+ for (int i = 0; i < N; ++i) { Client::FailureDetail f; if (!c.update("up", id(i), {{"n", i}, {"u", 1}}, f)) ++bad; }
|
|
|
+ ck(bad == 0, "update of evicted documents: " + std::to_string(bad) + " failed");
|
|
|
+
|
|
|
+ bad = 0;
|
|
|
+ for (int i = 0; i < N; ++i) {
|
|
|
+ auto d = c.get("uv", id(i));
|
|
|
+ Client::FailureDetail f;
|
|
|
+ if (!d || !c.updateIfVersion("uv", id(i), {{"n", i}, {"v", 1}}, (*d)["_version"].get<uint64_t>(), f)) ++bad;
|
|
|
+ }
|
|
|
+ ck(bad == 0, "updateIfVersion of evicted documents: " + std::to_string(bad) + " failed");
|
|
|
+
|
|
|
+ bad = 0;
|
|
|
+ for (int i = 0; i < N; ++i) {
|
|
|
+ auto d = c.get("pv", id(i));
|
|
|
+ Client::FailureDetail f;
|
|
|
+ if (!d || c.patchIfVersion("pv", id(i), {{"claimed", true}}, (*d)["_version"].get<uint64_t>(), f) == 0) ++bad;
|
|
|
+ }
|
|
|
+ ck(bad == 0, "patchIfVersion of evicted documents: " + std::to_string(bad) + " failed");
|
|
|
+
|
|
|
+ bad = 0;
|
|
|
+ for (int i = 0; i < N; ++i) {
|
|
|
+ Client::FailureDetail f;
|
|
|
+ auto r = c.increment("inc", id(i), {{"n", 1000}}, f);
|
|
|
+ if (!r || (*r)["n"] != i + 1000) ++bad;
|
|
|
+ }
|
|
|
+ ck(bad == 0, "increment of evicted documents (from their REAL value): " + std::to_string(bad) + " wrong");
|
|
|
+
|
|
|
+ int falseDel = 0, survived = 0;
|
|
|
+ for (int i = 0; i < N; ++i) if (!c.remove("de", id(i))) ++falseDel;
|
|
|
+ for (int i = 0; i < N; ++i) if (c.exists("de", id(i))) ++survived;
|
|
|
+ ck(falseDel == 0 && survived == 0, "remove of evicted documents: " + std::to_string(falseDel) +
|
|
|
+ " reported false, " + std::to_string(survived) + " survived");
|
|
|
+
|
|
|
+ int accepted = 0, overwritten = 0, notDupId = 0;
|
|
|
+ for (int i = 0; i < N; ++i) {
|
|
|
+ try { c.insert("in", nlohmann::json{{"marker", "OVERWRITE"}}, id(i)); ++accepted; }
|
|
|
+ catch (const Client::AlreadyExists& e) {
|
|
|
+ const auto* d = Client::errorDetail(e);
|
|
|
+ if (!d || d->reason != "duplicate_id") ++notDupId;
|
|
|
+ } catch (const std::exception&) { ++notDupId; }
|
|
|
+ auto d = c.get("in", id(i));
|
|
|
+ if (d && d->contains("marker")) ++overwritten;
|
|
|
+ }
|
|
|
+ ck(accepted == 0 && overwritten == 0, "insert over evicted documents: " + std::to_string(accepted) +
|
|
|
+ " accepted, " + std::to_string(overwritten) + " overwritten");
|
|
|
+ ck(notDupId == 0, "insert over evicted documents is refused as duplicate_id");
|
|
|
+
|
|
|
+ int reset = 0;
|
|
|
+ for (int i = 0; i < N; ++i) {
|
|
|
+ c.upsert("ups", nlohmann::json{{"n", i}, {"again", true}}, id(i));
|
|
|
+ auto d = c.get("ups", id(i));
|
|
|
+ if (!d || (*d)["_version"].get<uint64_t>() == 1) ++reset;
|
|
|
+ }
|
|
|
+ ck(reset == 0, "upsert of evicted documents keeps their history: " + std::to_string(reset) + " restarted at version 1");
|
|
|
+
|
|
|
+ std::cout << "\npassed=" << pass << " failed=" << fail << "\n";
|
|
|
+ return fail == 0 ? 0 : 1;
|
|
|
+}
|