|
|
@@ -2387,6 +2387,119 @@ TEST_F(ApiFixture, ARecycledCollectionNameInheritsNoConstraint) {
|
|
|
// the vector so a read-modify-write cannot put it back. Measured against the
|
|
|
// database before fixing: a merge PATCH preserves the vector, an update drops
|
|
|
// it. On data that cost money to produce.
|
|
|
+TEST_F(ApiFixture, ExportCarriesTheSystemFieldsAndASnapshotTimestamp) {
|
|
|
+ // Asked by the consumer who will build a backup on this: does `document`
|
|
|
+ // include the server-owned metadata? If it did not, the export would be a
|
|
|
+ // WORSE backup than the plain document dump they already take, and they
|
|
|
+ // would keep both. "When did this row first appear" was a question they
|
|
|
+ // needed answered this week.
|
|
|
+ //
|
|
|
+ // They also asked for a snapshot timestamp, because "as of when" is the
|
|
|
+ // first question anyone asks of a backup and a filesystem mtime does not
|
|
|
+ // survive a copy.
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ const std::string ct = "application/json";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","expmeta"},{"kind","json"}}.dump(),
|
|
|
+ ct)->status, 201);
|
|
|
+ ASSERT_EQ(c.Post((base + "/expmeta/documents").c_str(),
|
|
|
+ nlohmann::json{{"id","m1"},{"a",1}}.dump(), ct)->status, 201);
|
|
|
+
|
|
|
+ auto r = c.Get((base + "/expmeta/export").c_str());
|
|
|
+ ASSERT_TRUE(r); ASSERT_EQ(r->status, 200) << r->body;
|
|
|
+ std::istringstream is(r->body);
|
|
|
+ std::string line;
|
|
|
+ nlohmann::json header, row, trailer;
|
|
|
+ while (std::getline(is, line)) {
|
|
|
+ if (line.empty()) continue;
|
|
|
+ const auto j = nlohmann::json::parse(line);
|
|
|
+ const std::string t = j.value("type", "");
|
|
|
+ if (t == "header") header = j;
|
|
|
+ else if (t == "row") row = j;
|
|
|
+ else if (t == "trailer") trailer = j;
|
|
|
+ }
|
|
|
+ ASSERT_FALSE(header.is_null());
|
|
|
+ ASSERT_FALSE(row.is_null());
|
|
|
+ ASSERT_FALSE(trailer.is_null());
|
|
|
+
|
|
|
+ // The timestamp, and it must be plausible rather than merely present - a
|
|
|
+ // field that is always 0 would pass a contains() check and be useless.
|
|
|
+ ASSERT_TRUE(header.contains("snapshot_at"));
|
|
|
+ const uint64_t at = header["snapshot_at"].get<uint64_t>();
|
|
|
+ EXPECT_GT(at, 1700000000u) << "snapshot_at must be a real epoch, not a placeholder";
|
|
|
+
|
|
|
+ // The system fields a forensic question needs. Asserted by NAME so the
|
|
|
+ // answer to the consumer is a fact rather than a reading of the database's
|
|
|
+ // documentation.
|
|
|
+ const auto& doc = row["document"];
|
|
|
+ for (const char* f : {"_id", "_version", "_created_at", "_updated_at"})
|
|
|
+ EXPECT_TRUE(doc.contains(f)) << "export omits " << f
|
|
|
+ << ", which makes it a worse backup than a document dump";
|
|
|
+ EXPECT_EQ(doc.value("_id", ""), "m1");
|
|
|
+ EXPECT_EQ(doc.value("a", 0), 1) << "and the user's own fields are still there";
|
|
|
+
|
|
|
+ // The digest: present, and the right shape.
|
|
|
+ ASSERT_TRUE(trailer.contains("content_sha256"));
|
|
|
+ EXPECT_EQ(trailer["content_sha256"].get<std::string>().size(), 64u);
|
|
|
+
|
|
|
+ (void)c.Delete((base + "/expmeta").c_str());
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, TheExportChecksumCannotFailBecauseOfAConcurrentDelete) {
|
|
|
+ // The consumer was told to treat `rows_sent == rows_at_snapshot` as their
|
|
|
+ // checksum, and asked - rightly - whether it can go unequal for a
|
|
|
+ // LEGITIMATE reason. "A checksum that can fail when nothing is wrong gets
|
|
|
+ // ignored within a month, and then it is not a checksum."
|
|
|
+ //
|
|
|
+ // The database documents one stream as one read transaction, so a
|
|
|
+ // concurrent write should be invisible to it. Documented is not measured,
|
|
|
+ // so: delete rows WHILE the export is open and assert the snapshot held.
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ const std::string ct = "application/json";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","expiso"},{"kind","json"}}.dump(),
|
|
|
+ ct)->status, 201);
|
|
|
+ for (int i = 0; i < 20; ++i)
|
|
|
+ ASSERT_EQ(c.Post((base + "/expiso/documents").c_str(),
|
|
|
+ nlohmann::json{{"id","iso-" + std::to_string(i)},{"n",i}}.dump(),
|
|
|
+ ct)->status, 201);
|
|
|
+
|
|
|
+ // Delete from another connection while the export runs. The export is a
|
|
|
+ // single blocking GET here, so the deletes land during it only if they are
|
|
|
+ // issued from a separate thread - which is the point.
|
|
|
+ std::atomic<bool> go{false};
|
|
|
+ std::thread deleter([&] {
|
|
|
+ httplib::Client dc("127.0.0.1", port_);
|
|
|
+ dc.set_default_headers({{"Authorization", "Bearer " + adminKey_}});
|
|
|
+ while (!go.load()) std::this_thread::yield();
|
|
|
+ for (int i = 0; i < 10; ++i)
|
|
|
+ (void)dc.Delete((base + "/expiso/documents/iso-" + std::to_string(i)).c_str());
|
|
|
+ });
|
|
|
+ go.store(true);
|
|
|
+ auto r = c.Get((base + "/expiso/export").c_str());
|
|
|
+ deleter.join();
|
|
|
+
|
|
|
+ ASSERT_TRUE(r); ASSERT_EQ(r->status, 200) << r->body;
|
|
|
+ std::istringstream is(r->body);
|
|
|
+ std::string line;
|
|
|
+ uint64_t atSnapshot = 0, sent = 0, rows = 0;
|
|
|
+ bool complete = false;
|
|
|
+ while (std::getline(is, line)) {
|
|
|
+ if (line.empty()) continue;
|
|
|
+ const auto j = nlohmann::json::parse(line);
|
|
|
+ const std::string t = j.value("type", "");
|
|
|
+ if (t == "header") atSnapshot = j.value("rows_at_snapshot", 0ull);
|
|
|
+ else if (t == "row") ++rows;
|
|
|
+ else if (t == "trailer") { sent = j.value("rows_sent", 0ull); complete = j.value("complete", false); }
|
|
|
+ }
|
|
|
+ EXPECT_TRUE(complete) << "a concurrent delete must not end the stream early";
|
|
|
+ EXPECT_EQ(sent, atSnapshot)
|
|
|
+ << "the checksum went unequal with nothing actually wrong, which would make it worthless";
|
|
|
+ EXPECT_EQ(rows, sent) << "and the handler saw exactly what the trailer claims";
|
|
|
+
|
|
|
+ (void)c.Delete((base + "/expiso").c_str());
|
|
|
+}
|
|
|
+
|
|
|
TEST_F(ApiFixture, ExportingAVectorCollectionRoundTripsThroughOurOwnApi) {
|
|
|
// The property that matters, and the only one worth asserting: a collection
|
|
|
// exported and replayed through our OWN write route comes back searchable.
|