|
@@ -0,0 +1,112 @@
|
|
|
|
|
+// v2.19.0 - a document's expiry can be READ: get/find/export attach
|
|
|
|
|
+// `_expires_at` (absolute, ms since the epoch) when the document has one, it can
|
|
|
|
|
+// be filtered on, and a write body never stores it. Before, a TTL could be
|
|
|
|
|
+// written (insert/upsert ttl, patchWithTtl) and never read back, so a restore
|
|
|
|
|
+// from a backup or export could not carry it and made expired rows permanent
|
|
|
|
|
+// (reported by smartbotic-vectorapi after an incident).
|
|
|
|
|
+
|
|
|
|
|
+#include <smartbotic/database/client.hpp>
|
|
|
|
|
+
|
|
|
|
|
+#include <chrono>
|
|
|
|
|
+#include <cstdlib>
|
|
|
|
|
+#include <iostream>
|
|
|
|
|
+#include <set>
|
|
|
|
|
+#include <string>
|
|
|
|
|
+#include <thread>
|
|
|
|
|
+
|
|
|
|
|
+using namespace smartbotic::database;
|
|
|
|
|
+using F = Client::Filter;
|
|
|
|
|
+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 int64_t nowMs() {
|
|
|
|
|
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
|
+ std::chrono::system_clock::now().time_since_epoch()).count();
|
|
|
|
|
+}
|
|
|
|
|
+static bool near(const nlohmann::json& d, int64_t expect, int64_t slackMs) {
|
|
|
|
|
+ if (!d.contains("_expires_at") || !d["_expires_at"].is_number()) return false;
|
|
|
|
|
+ return std::llabs(d["_expires_at"].get<int64_t>() - expect) <= slackMs;
|
|
|
|
|
+}
|
|
|
|
|
+static std::set<std::string> ids(Client& c, const std::string& coll, std::vector<F> f) {
|
|
|
|
|
+ Client::QueryOptions o;
|
|
|
|
|
+ o.filters = std::move(f);
|
|
|
|
|
+ std::set<std::string> out;
|
|
|
|
|
+ for (auto& d : c.find(coll, o)) out.insert(d["_id"].get<std::string>());
|
|
|
|
|
+ return out;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int main() {
|
|
|
|
|
+ Client::Config cfg;
|
|
|
|
|
+ cfg.address = "127.0.0.1:9035";
|
|
|
|
|
+ cfg.writeRetries = 0;
|
|
|
|
|
+ Client c(cfg);
|
|
|
|
|
+ c.connect();
|
|
|
|
|
+ ck(c.serverSupports(Client::Capability::kDocumentExpiresAt), "server reports document_expires_at");
|
|
|
|
|
+
|
|
|
|
|
+ const int64_t t0 = nowMs();
|
|
|
|
|
+ c.insert("exp", {{"name", "marker"}}, "t1", 3600);
|
|
|
|
|
+ c.insert("exp", {{"name", "forever"}}, "n1");
|
|
|
|
|
+
|
|
|
|
|
+ auto t1 = c.get("exp", "t1");
|
|
|
|
|
+ ck(t1 && near(*t1, t0 + 3600000, 60000), "get: _expires_at is now + 3600 s");
|
|
|
|
|
+ auto n1 = c.get("exp", "n1");
|
|
|
|
|
+ ck(n1 && !n1->contains("_expires_at"), "get: no _expires_at on a document without expiry");
|
|
|
|
|
+ {
|
|
|
|
|
+ Client::QueryOptions o;
|
|
|
|
|
+ bool t1Has = false, n1Has = true;
|
|
|
|
|
+ for (auto& d : c.find("exp", o)) {
|
|
|
|
|
+ if (d["_id"] == "t1") t1Has = near(d, t0 + 3600000, 60000);
|
|
|
|
|
+ if (d["_id"] == "n1") n1Has = d.contains("_expires_at");
|
|
|
|
|
+ }
|
|
|
|
|
+ ck(t1Has && !n1Has, "find: the expiry is on the TTL'd row only");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Filtering. A document without expiry is ABSENT for _expires_at, so an
|
|
|
|
|
+ // upper bound does not match it.
|
|
|
|
|
+ ck(ids(c, "exp", {F::exists("_expires_at", true)}) == std::set<std::string>{"t1"},
|
|
|
|
|
+ "filter: _expires_at exists selects the TTL'd row");
|
|
|
|
|
+ ck(ids(c, "exp", {F::exists("_expires_at", false)}) == std::set<std::string>{"n1"},
|
|
|
|
|
+ "filter: _expires_at exists false selects the permanent row");
|
|
|
|
|
+ ck(ids(c, "exp", {F::lt("_expires_at", t0 + 7200000)}) == std::set<std::string>{"t1"},
|
|
|
|
|
+ "filter: expires before now+2h matches the TTL'd row, not the permanent one");
|
|
|
|
|
+ ck(ids(c, "exp", {F::gt("_expires_at", t0 + 7200000)}).empty(), "filter: nothing expires after now+2h");
|
|
|
|
|
+ ck(c.count("exp", {F::lt("_expires_at", t0 + 7200000)}) == 1, "count agrees");
|
|
|
|
|
+
|
|
|
|
|
+ // Changing the TTL moves it.
|
|
|
|
|
+ ck(c.patchWithTtl("exp", "t1", {{"touched", true}}, 60) > 0, "patchWithTtl accepted");
|
|
|
|
|
+ t1 = c.get("exp", "t1");
|
|
|
|
|
+ ck(t1 && near(*t1, nowMs() + 60000, 30000), "patchWithTtl(60) moved _expires_at to now + 60 s");
|
|
|
|
|
+
|
|
|
|
|
+ // A get-then-write round trip does not store it as a field.
|
|
|
|
|
+ auto copy = *c.get("exp", "t1");
|
|
|
|
|
+ c.insert("exp2", copy, "c1");
|
|
|
|
|
+ auto c1 = c.get("exp2", "c1");
|
|
|
|
|
+ ck(c1 && !c1->contains("_expires_at"), "a read body re-inserted without a TTL has no _expires_at");
|
|
|
|
|
+ ck(c1 && c1->value("name", "") == "marker", "the round trip kept the user fields");
|
|
|
|
|
+ copy["note"] = "updated";
|
|
|
|
|
+ ck(c.update("exp2", "c1", copy), "update with a read body (carrying _expires_at) succeeds");
|
|
|
|
|
+ c1 = c.get("exp2", "c1");
|
|
|
|
|
+ ck(c1 && !c1->contains("_expires_at") && c1->value("note", "") == "updated",
|
|
|
|
|
+ "update did not store _expires_at either");
|
|
|
|
|
+
|
|
|
|
|
+ // An export carries it, so a restore can compute the remaining TTL.
|
|
|
|
|
+ {
|
|
|
|
|
+ bool sawT1 = false, sawN1Without = false;
|
|
|
|
|
+ c.exportCollection("exp", false, "", [](const Client::ExportHeader&) {}, [&](Client::ExportRow&& row) {
|
|
|
|
|
+ if (row.id == "t1") sawT1 = row.document.contains("_expires_at");
|
|
|
|
|
+ if (row.id == "n1") sawN1Without = !row.document.contains("_expires_at");
|
|
|
|
|
+ return true;
|
|
|
|
|
+ });
|
|
|
|
|
+ ck(sawT1 && sawN1Without, "export rows carry _expires_at only where set");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Expiry is real: a 1 s TTL row is reaped by the sweep.
|
|
|
|
|
+ c.insert("exp", {{"name", "short"}}, "s1", 1);
|
|
|
|
|
+ ck(c.get("exp", "s1").has_value(), "a 1 s row exists right after insert");
|
|
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(3000));
|
|
|
|
|
+ ck(!c.get("exp", "s1").has_value(), "and is gone after the sweep");
|
|
|
|
|
+
|
|
|
|
|
+ std::cout << "document expiry: " << pass << " passed, " << fail << " failed\n";
|
|
|
|
|
+ return fail == 0 ? 0 : 1;
|
|
|
|
|
+}
|