|
@@ -8,9 +8,14 @@
|
|
|
//
|
|
//
|
|
|
// The test is deterministic: from INSIDE the scan callback it starts an
|
|
// The test is deterministic: from INSIDE the scan callback it starts an
|
|
|
// append on another thread and waits up to 2 s for it. Under forEach the
|
|
// append on another thread and waits up to 2 s for it. Under forEach the
|
|
|
-// append completes at once; under rewrite() it cannot complete until the scan
|
|
|
|
|
-// ends, so the wait times out (asserted for rewrite() too, so a forEach that
|
|
|
|
|
-// quietly took the lock would fail, and the harness is shown to detect it).
|
|
|
|
|
|
|
+// append completes at once; under rewrite() an append to the SAME collection
|
|
|
|
|
+// cannot complete until the scan ends, so the wait times out (asserted, so a
|
|
|
|
|
+// forEach that quietly took the lock would fail, and the harness is shown to
|
|
|
|
|
+// detect it).
|
|
|
|
|
+//
|
|
|
|
|
+// v2.17.2 - history locks are per file: a rewrite or prune of one collection
|
|
|
|
|
+// must not block an append to ANOTHER (2.17.1 had one global lock, so pruning
|
|
|
|
|
+// a large file stalled every collection's history-recording writes).
|
|
|
|
|
|
|
|
#include "persistence/history_store.hpp"
|
|
#include "persistence/history_store.hpp"
|
|
|
|
|
|
|
@@ -21,6 +26,8 @@
|
|
|
#include <iostream>
|
|
#include <iostream>
|
|
|
#include <string>
|
|
#include <string>
|
|
|
#include <thread>
|
|
#include <thread>
|
|
|
|
|
+#include <unordered_map>
|
|
|
|
|
+#include <vector>
|
|
|
|
|
|
|
|
using namespace smartbotic::database;
|
|
using namespace smartbotic::database;
|
|
|
|
|
|
|
@@ -43,11 +50,12 @@ int main() {
|
|
|
std::filesystem::remove_all(dir);
|
|
std::filesystem::remove_all(dir);
|
|
|
HistoryStore hs(HistoryStore::Config{dir});
|
|
HistoryStore hs(HistoryStore::Config{dir});
|
|
|
for (uint64_t i = 1; i <= 200; ++i) hs.append("audited", "d" + std::to_string(i % 7), version(i));
|
|
for (uint64_t i = 1; i <= 200; ++i) hs.append("audited", "d" + std::to_string(i % 7), version(i));
|
|
|
|
|
+ // The lock tests append into "busy" while scanning it, so they get their own file.
|
|
|
|
|
+ for (uint64_t i = 1; i <= 200; ++i) hs.append("busy", "b" + std::to_string(i % 7), version(i));
|
|
|
|
|
|
|
|
- // An append to ANOTHER collection, started from inside the scan: it takes
|
|
|
|
|
- // the same global lock, which is exactly what stalled a live server.
|
|
|
|
|
- const auto appendDuringScan = [&](bool& sawCompletion, uint64_t n) {
|
|
|
|
|
- auto fut = std::async(std::launch::async, [&] { hs.append("other", "o", version(n)); });
|
|
|
|
|
|
|
+ // An append started from inside the scan, to `target`.
|
|
|
|
|
+ const auto appendDuringScan = [&](bool& sawCompletion, uint64_t n, const std::string& target) {
|
|
|
|
|
+ auto fut = std::async(std::launch::async, [&hs, n, target] { hs.append(target, "o", version(n)); });
|
|
|
sawCompletion = fut.wait_for(std::chrono::seconds(2)) == std::future_status::ready;
|
|
sawCompletion = fut.wait_for(std::chrono::seconds(2)) == std::future_status::ready;
|
|
|
return fut; // joined by the caller after the scan, so rewrite() can finish
|
|
return fut; // joined by the caller after the scan, so rewrite() can finish
|
|
|
};
|
|
};
|
|
@@ -55,24 +63,59 @@ int main() {
|
|
|
{
|
|
{
|
|
|
bool first = true, completed = false;
|
|
bool first = true, completed = false;
|
|
|
std::future<void> pending;
|
|
std::future<void> pending;
|
|
|
- const uint64_t visited = hs.forEach("audited", [&](const std::string&, const DocumentVersion&) {
|
|
|
|
|
- if (first) { first = false; pending = appendDuringScan(completed, 1); }
|
|
|
|
|
|
|
+ const uint64_t visited = hs.forEach("busy", [&](const std::string&, const DocumentVersion&) {
|
|
|
|
|
+ if (first) { first = false; pending = appendDuringScan(completed, 1, "busy"); }
|
|
|
});
|
|
});
|
|
|
if (pending.valid()) pending.wait();
|
|
if (pending.valid()) pending.wait();
|
|
|
- check(visited == 200, "forEach visits every stored version (" + std::to_string(visited) + ")");
|
|
|
|
|
- check(completed, "an append completes while forEach is reading (no append lock held)");
|
|
|
|
|
|
|
+ // 200, or 201 if the scan also reached the record appended meanwhile.
|
|
|
|
|
+ check(visited == 200 || visited == 201, "forEach visits every stored version (" + std::to_string(visited) + ")");
|
|
|
|
|
+ check(hs.forEach("audited", [](const std::string&, const DocumentVersion&) {}) == 200,
|
|
|
|
|
+ "forEach on a quiet file visits exactly its 200 versions");
|
|
|
|
|
+ check(completed, "an append to the SAME collection completes while forEach is reading (no lock held)");
|
|
|
}
|
|
}
|
|
|
{
|
|
{
|
|
|
bool first = true, completed = true;
|
|
bool first = true, completed = true;
|
|
|
std::future<void> pending;
|
|
std::future<void> pending;
|
|
|
- hs.rewrite("audited", [&](DocumentVersion&) {
|
|
|
|
|
- if (first) { first = false; pending = appendDuringScan(completed, 2); }
|
|
|
|
|
|
|
+ hs.rewrite("busy", [&](DocumentVersion&) {
|
|
|
|
|
+ if (first) { first = false; pending = appendDuringScan(completed, 2, "busy"); }
|
|
|
return false;
|
|
return false;
|
|
|
});
|
|
});
|
|
|
if (pending.valid()) pending.wait();
|
|
if (pending.valid()) pending.wait();
|
|
|
- check(!completed, "control: under rewrite() the same append waits (the harness detects the lock)");
|
|
|
|
|
|
|
+ check(!completed, "control: under rewrite() an append to the same collection waits (the harness detects the lock)");
|
|
|
|
|
+ }
|
|
|
|
|
+ {
|
|
|
|
|
+ bool first = true, completed = false;
|
|
|
|
|
+ std::future<void> pending;
|
|
|
|
|
+ hs.rewrite("busy", [&](DocumentVersion&) {
|
|
|
|
|
+ if (first) { first = false; pending = appendDuringScan(completed, 3, "other"); }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ });
|
|
|
|
|
+ if (pending.valid()) pending.wait();
|
|
|
|
|
+ check(completed, "v2.17.2: a rewrite of one collection does NOT block an append to another");
|
|
|
|
|
+ }
|
|
|
|
|
+ {
|
|
|
|
|
+ // prune: keeps the newest N per doc, in file order.
|
|
|
|
|
+ for (uint64_t i = 1; i <= 50; ++i) hs.append("pruned", "p" + std::to_string(i % 5), version(i));
|
|
|
|
|
+ // (Its lock is the same per-file mutex the rewrite case above proves.)
|
|
|
|
|
+ hs.prune("pruned", 3);
|
|
|
|
|
+ std::unordered_map<std::string, std::vector<uint64_t>> kept;
|
|
|
|
|
+ uint64_t total = 0;
|
|
|
|
|
+ hs.forEach("pruned", [&](const std::string& id, const DocumentVersion& v) {
|
|
|
|
|
+ kept[id].push_back(v.version);
|
|
|
|
|
+ ++total;
|
|
|
|
|
+ });
|
|
|
|
|
+ bool newest = total == 15;
|
|
|
|
|
+ for (const auto& [id, vs] : kept) {
|
|
|
|
|
+ newest = newest && vs.size() == 3 && vs[0] < vs[1] && vs[1] < vs[2] && vs[2] > 40;
|
|
|
|
|
+ }
|
|
|
|
|
+ check(newest, "prune keeps exactly the newest 3 versions per doc, oldest first (" + std::to_string(total) + " kept)");
|
|
|
|
|
+ hs.prune("pruned", 3);
|
|
|
|
|
+ uint64_t again = 0;
|
|
|
|
|
+ hs.forEach("pruned", [&](const std::string&, const DocumentVersion&) { ++again; });
|
|
|
|
|
+ check(again == 15, "pruning again to the same cap changes nothing");
|
|
|
}
|
|
}
|
|
|
- check(hs.countVersions("other", "o") == 2, "both appends landed");
|
|
|
|
|
|
|
+ check(hs.countVersions("other", "o") == 1 && hs.countVersions("busy", "o") == 2,
|
|
|
|
|
+ "every append landed (1 in the other collection, 2 in the scanned one)");
|
|
|
|
|
|
|
|
// v2.17.1 - rewrite() streams record by record. A pass that changes nothing
|
|
// v2.17.1 - rewrite() streams record by record. A pass that changes nothing
|
|
|
// leaves the file byte-identical and no tmp behind; a pass that changes
|
|
// leaves the file byte-identical and no tmp behind; a pass that changes
|