|
|
@@ -249,6 +249,11 @@ bool DatabaseService::initialize() {
|
|
|
// Must be after applyRelationDeclarations() AND
|
|
|
// applyIndexDeclarations(); backfillIntoDocStore() below already sits
|
|
|
// at this point for the same reason. Never throws.
|
|
|
+#ifdef HAVE_SYSTEMD
|
|
|
+ // v2.21.4 - after a long replay this pass covers every id it touched.
|
|
|
+ sd_notify(0, "EXTEND_TIMEOUT_USEC=600000000");
|
|
|
+ sd_notify(0, "STATUS=Re-mirroring replayed documents to LMDB");
|
|
|
+#endif
|
|
|
persistence_->runPendingRemirror(*store_, recovery_outcome_);
|
|
|
|
|
|
// Run migrations if enabled
|
|
|
@@ -284,6 +289,8 @@ bool DatabaseService::initialize() {
|
|
|
// before 2.20.2 not writable), and the reverse. Read-only, advisory.
|
|
|
spdlog::info("Boot: mirror-pending replay");
|
|
|
replayMirrorPending();
|
|
|
+ spdlog::info("Boot: memory/disk version audit");
|
|
|
+ repairStaleResidentRows();
|
|
|
spdlog::info("Boot: memory/disk key audit");
|
|
|
auditMemoryDiskKeys();
|
|
|
|
|
|
@@ -1087,6 +1094,103 @@ void DatabaseService::replayMirrorPending() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// v2.21.4 - a production boot replayed 193,776 of the 354,089 WAL entries
|
|
|
+// above its snapshot (cause under investigation; the per-file replay lines
|
|
|
+// now show where entries go). LMDB had every write, because each one was
|
|
|
+// mirrored at write time, so reads were right; MemoryStore kept the snapshot's
|
|
|
+// older version of every row written in the gap, and MemoryStore is the base
|
|
|
+// of a patch (old fields + the patch, mirrored over LMDB's newer row), of
|
|
|
+// updateIfVersion's check, of history and of the next snapshot - which then
|
|
|
+// made the old version permanent. The key audit compares ids only, so it saw
|
|
|
+// nothing.
|
|
|
+//
|
|
|
+// LMDB is authoritative here: the re-mirror pass and the mirror-pending replay
|
|
|
+// have already written to LMDB every row for which memory is authoritative
|
|
|
+// (rows the replay applied, rows whose LMDB copy failed), so a difference that
|
|
|
+// remains means memory is behind. Runs before the listener starts (no writer
|
|
|
+// races it) and only while the mirror is healthy with zero drift. Reads every
|
|
|
+// resident row of every mirrored collection from LMDB once.
|
|
|
+uint64_t DatabaseService::repairStaleResidentRows() {
|
|
|
+ if (!projects_ || !store_) return 0;
|
|
|
+ if (!mirror_healthy_.load(std::memory_order_relaxed) ||
|
|
|
+ mirror_drift_count_.load(std::memory_order_relaxed) != 0) {
|
|
|
+ spdlog::warn("version audit: skipped (the LMDB mirror is unhealthy or has drift, so "
|
|
|
+ "LMDB may be behind memory)");
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ namespace sd = smartbotic::database;
|
|
|
+ const auto started = std::chrono::steady_clock::now();
|
|
|
+ uint64_t checked = 0, reloaded = 0, failed = 0;
|
|
|
+ constexpr size_t kBatch = 256;
|
|
|
+
|
|
|
+ for (const auto& name : store_->listCollections()) {
|
|
|
+ sd::ResolvedCollection rc;
|
|
|
+ try {
|
|
|
+ rc = sd::resolveCollection(name);
|
|
|
+ } catch (const std::exception&) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (rc.collection.empty() || rc.collection.front() == '_') continue;
|
|
|
+ auto* ds = docStore(rc.project);
|
|
|
+ if (!ds) continue;
|
|
|
+
|
|
|
+ const auto ids = store_->getDocumentIds(name);
|
|
|
+ std::vector<std::string> stale;
|
|
|
+ for (size_t from = 0; from < ids.size(); from += kBatch) {
|
|
|
+ const auto mem = store_->copyDocuments(name, ids, from, kBatch);
|
|
|
+ for (size_t k = 0; k < mem.size(); ++k) {
|
|
|
+ if (!mem[k]) continue; // deleted meanwhile
|
|
|
+ ++checked;
|
|
|
+ std::optional<Document> disk;
|
|
|
+ try {
|
|
|
+ disk = ds->get(rc.collection, ids[from + k]);
|
|
|
+ } catch (const std::exception& e) {
|
|
|
+ ++failed;
|
|
|
+ spdlog::warn("version audit: {}/{}: LMDB read failed: {}",
|
|
|
+ name, ids[from + k], e.what());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!disk) continue; // memory only: the key audit reports it
|
|
|
+ if (disk->version != mem[k]->version || disk->updatedAt != mem[k]->updatedAt) {
|
|
|
+ stale.push_back(ids[from + k]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (stale.empty()) continue;
|
|
|
+
|
|
|
+ uint64_t done = 0;
|
|
|
+ for (const auto& id : stale) {
|
|
|
+ try {
|
|
|
+ // A reload, not a write: no history entry, no WAL, no event.
|
|
|
+ MemoryStore::HistorySuppression noHistory(*store_);
|
|
|
+ if (store_->unloadDocument(name, id) && store_->adoptFromDocStore(name, id, true)) {
|
|
|
+ ++done;
|
|
|
+ } else {
|
|
|
+ ++failed;
|
|
|
+ }
|
|
|
+ } catch (const std::exception& e) {
|
|
|
+ ++failed;
|
|
|
+ spdlog::warn("version audit: {}/{}: reload failed: {}", name, id, e.what());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ reloaded += done;
|
|
|
+ std::string sample;
|
|
|
+ for (size_t i = 0; i < stale.size() && i < 10; ++i) sample += (i ? ", " : "") + stale[i];
|
|
|
+ spdlog::warn("version audit: {}: {} resident row(s) were older than LMDB's copy, "
|
|
|
+ "{} reloaded from LMDB (e.g. {})", name, stale.size(), done, sample);
|
|
|
+ }
|
|
|
+
|
|
|
+ const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
+ std::chrono::steady_clock::now() - started).count();
|
|
|
+ if (reloaded > 0 || failed > 0) {
|
|
|
+ spdlog::warn("version audit: {} resident row(s) checked, {} reloaded from LMDB, "
|
|
|
+ "{} failed ({} ms)", checked, reloaded, failed, ms);
|
|
|
+ } else {
|
|
|
+ spdlog::info("version audit: {} resident row(s) agree with LMDB ({} ms)", checked, ms);
|
|
|
+ }
|
|
|
+ return reloaded;
|
|
|
+}
|
|
|
+
|
|
|
void DatabaseService::auditMemoryDiskKeys() {
|
|
|
if (!projects_ || !store_) return;
|
|
|
namespace sd = smartbotic::database;
|