|
|
@@ -3000,6 +3000,7 @@ void MemoryStore::saveToHistory(CollectionData& coll, const Document& currentDoc
|
|
|
// `estimatedMemoryBytes_` with the version's overhead. That was
|
|
|
// the dominant source of the Zoe RSS gap; see docs/incidents/2026-04-22-zoe-rss-exceeds-budget.md.
|
|
|
if (!historyStore_) return;
|
|
|
+ if (historySuppressed_.load(std::memory_order_relaxed) > 0) return; // v2.21.1: WAL replay
|
|
|
|
|
|
// v2.4.5 — per-collection versioning switch. Checked here rather than at
|
|
|
// each of the ~10 write sites that call saveToHistory, so there is exactly
|
|
|
@@ -3023,13 +3024,34 @@ void MemoryStore::saveToHistory(CollectionData& coll, const Document& currentDoc
|
|
|
ver.createdBy = currentDoc.createdBy;
|
|
|
|
|
|
historyStore_->append(currentDoc.collection, currentDoc.id, ver);
|
|
|
- // Per-collection `max_versions` pruning runs in the snapshot loop
|
|
|
- // (PersistenceManager::createSnapshot calls historyStore_->prune for
|
|
|
- // every collection that has a cap set). Doing it inline on every
|
|
|
+ // Per-collection `max_versions` is enforced after each snapshot by
|
|
|
+ // enforceVersionCaps() (v2.21.1; before, nothing enforced it although
|
|
|
+ // this comment said the snapshot loop did). Doing it inline on every
|
|
|
// write would either rewrite the file each time (expensive) or
|
|
|
// require an in-memory counter we no longer keep.
|
|
|
}
|
|
|
|
|
|
+size_t MemoryStore::enforceVersionCaps() const {
|
|
|
+ if (!historyStore_) return 0;
|
|
|
+ std::vector<std::pair<std::string, uint32_t>> capped;
|
|
|
+ {
|
|
|
+ std::shared_lock<std::shared_mutex> lock(globalMutex_);
|
|
|
+ for (const auto& [name, coll] : collections_) {
|
|
|
+ std::shared_lock<std::shared_mutex> collLock(coll->mutex);
|
|
|
+ if (coll->options.maxVersions > 0) capped.emplace_back(name, coll->options.maxVersions);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ size_t pruned = 0;
|
|
|
+ for (const auto& [name, maxVersions] : capped) {
|
|
|
+ try {
|
|
|
+ if (historyStore_->pruneIfGrown(name, maxVersions)) ++pruned;
|
|
|
+ } catch (const std::exception& e) {
|
|
|
+ spdlog::warn("history cap: '{}' could not be pruned: {}", name, e.what());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return pruned;
|
|
|
+}
|
|
|
+
|
|
|
MemoryStore::VersionHistoryResult MemoryStore::getVersionHistory(
|
|
|
const std::string& collection, const std::string& id,
|
|
|
uint32_t limit, uint32_t offset) const {
|
|
|
@@ -3667,7 +3689,8 @@ std::vector<std::string> memoryAliasesOf(const smartbotic::database::ProjectColl
|
|
|
}
|
|
|
} // namespace
|
|
|
|
|
|
-bool MemoryStore::adoptFromDocStore(const std::string& collection, const std::string& id) {
|
|
|
+bool MemoryStore::adoptFromDocStore(const std::string& collection, const std::string& id,
|
|
|
+ bool quiet) {
|
|
|
// Only while LMDB is what reads are served from: with the mirror unhealthy
|
|
|
// or drifted, MemoryStore may be AHEAD of LMDB (a row deleted here whose
|
|
|
// delete never reached LMDB), and adopting would resurrect it.
|
|
|
@@ -3734,9 +3757,11 @@ bool MemoryStore::adoptFromDocStore(const std::string& collection, const std::st
|
|
|
stats_.totalDocuments++;
|
|
|
}
|
|
|
estimatedMemoryBytes_.fetch_add(size, std::memory_order_relaxed);
|
|
|
- spdlog::warn("write to {}/{}: the row was on disk but not in memory (readable, not "
|
|
|
- "writable before 2.20.2); loaded it so the write applies to it. The boot "
|
|
|
- "audit lists every such row.", collection, id);
|
|
|
+ if (!quiet) {
|
|
|
+ spdlog::warn("write to {}/{}: the row was on disk but not in memory (readable, not "
|
|
|
+ "writable before 2.20.2); loaded it unchanged so the write applies to it. "
|
|
|
+ "The boot key audit lists every such row.", collection, id);
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -4527,6 +4552,18 @@ bool MemoryStore::evictDocument(const std::string& collection, const std::string
|
|
|
removeFromExpirationIndex(*coll, id, it->second.expiresAt);
|
|
|
}
|
|
|
|
|
|
+ // v2.21.1 - the stub goes in BEFORE the document leaves, still under the
|
|
|
+ // collection lock (lock order collection -> eviction, as everywhere). It
|
|
|
+ // used to be added after the collection lock was released, leaving a
|
|
|
+ // moment in which the document was in neither map: a snapshot that had
|
|
|
+ // listed it as resident then found neither a document nor a stub and
|
|
|
+ // wrote it as deleted, and a write saw "absent". Briefly in both maps is
|
|
|
+ // harmless (a snapshot writes it twice; the loader keeps one).
|
|
|
+ {
|
|
|
+ std::unique_lock<std::shared_mutex> evictLock(evictionMutex_);
|
|
|
+ evictedDocs_[collection][id] = stub;
|
|
|
+ }
|
|
|
+
|
|
|
// Remove document from memory
|
|
|
coll->documents.erase(it);
|
|
|
|
|
|
@@ -4537,12 +4574,6 @@ bool MemoryStore::evictDocument(const std::string& collection, const std::string
|
|
|
|
|
|
collLock.unlock();
|
|
|
|
|
|
- // Store stub in evicted map
|
|
|
- {
|
|
|
- std::unique_lock<std::shared_mutex> evictLock(evictionMutex_);
|
|
|
- evictedDocs_[collection][id] = stub;
|
|
|
- }
|
|
|
-
|
|
|
// Update stats
|
|
|
{
|
|
|
std::lock_guard<std::mutex> statsLock(statsMutex_);
|