|
@@ -207,6 +207,7 @@ bool MemoryStore::dropCollection(const std::string& name) {
|
|
|
|
|
|
|
|
uint64_t docCount = 0;
|
|
uint64_t docCount = 0;
|
|
|
uint64_t totalSize = 0;
|
|
uint64_t totalSize = 0;
|
|
|
|
|
+ uint64_t vectorSize = 0;
|
|
|
{
|
|
{
|
|
|
std::shared_lock<std::shared_mutex> collLock(it->second->mutex);
|
|
std::shared_lock<std::shared_mutex> collLock(it->second->mutex);
|
|
|
docCount = it->second->documents.size();
|
|
docCount = it->second->documents.size();
|
|
@@ -217,8 +218,9 @@ bool MemoryStore::dropCollection(const std::string& name) {
|
|
|
totalSize += estimateDocumentSize(doc);
|
|
totalSize += estimateDocumentSize(doc);
|
|
|
}
|
|
}
|
|
|
for (const auto& [id, vec] : it->second->vectors) {
|
|
for (const auto& [id, vec] : it->second->vectors) {
|
|
|
- totalSize += estimateVectorSize(vec);
|
|
|
|
|
|
|
+ vectorSize += estimateVectorSize(vec);
|
|
|
}
|
|
}
|
|
|
|
|
+ totalSize += vectorSize;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
collections_.erase(it);
|
|
collections_.erase(it);
|
|
@@ -243,6 +245,7 @@ bool MemoryStore::dropCollection(const std::string& name) {
|
|
|
|
|
|
|
|
// Update memory tracking atomically
|
|
// Update memory tracking atomically
|
|
|
estimatedMemoryBytes_.fetch_sub(totalSize, std::memory_order_relaxed);
|
|
estimatedMemoryBytes_.fetch_sub(totalSize, std::memory_order_relaxed);
|
|
|
|
|
+ vectorMemoryBytes_.fetch_sub(vectorSize, std::memory_order_relaxed);
|
|
|
} // globalMutex_ released here — see below.
|
|
} // globalMutex_ released here — see below.
|
|
|
|
|
|
|
|
// v2.11.0 close-out (round-3 review, item 2) — release any TTL blocked-set
|
|
// v2.11.0 close-out (round-3 review, item 2) — release any TTL blocked-set
|
|
@@ -2418,9 +2421,9 @@ void MemoryStore::storeVector(CollectionData& coll, const std::string& docId,
|
|
|
|
|
|
|
|
// Update memory counter by the delta
|
|
// Update memory counter by the delta
|
|
|
if (newSize > oldSize) {
|
|
if (newSize > oldSize) {
|
|
|
- estimatedMemoryBytes_.fetch_add(newSize - oldSize, std::memory_order_relaxed);
|
|
|
|
|
|
|
+ addVectorBytes(newSize - oldSize);
|
|
|
} else if (oldSize > newSize) {
|
|
} else if (oldSize > newSize) {
|
|
|
- estimatedMemoryBytes_.fetch_sub(oldSize - newSize, std::memory_order_relaxed);
|
|
|
|
|
|
|
+ subVectorBytes(oldSize - newSize);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -2429,7 +2432,7 @@ void MemoryStore::removeVector(CollectionData& coll, const std::string& docId) {
|
|
|
if (it != coll.vectors.end()) {
|
|
if (it != coll.vectors.end()) {
|
|
|
uint64_t freed = estimateVectorSize(it->second);
|
|
uint64_t freed = estimateVectorSize(it->second);
|
|
|
coll.vectors.erase(it);
|
|
coll.vectors.erase(it);
|
|
|
- estimatedMemoryBytes_.fetch_sub(freed, std::memory_order_relaxed);
|
|
|
|
|
|
|
+ subVectorBytes(freed);
|
|
|
}
|
|
}
|
|
|
coll.vectorNorms.erase(docId);
|
|
coll.vectorNorms.erase(docId);
|
|
|
}
|
|
}
|
|
@@ -2532,11 +2535,26 @@ void MemoryStore::loadVector(const std::string& collection, const std::string& d
|
|
|
std::vector<float> vec) {
|
|
std::vector<float> vec) {
|
|
|
CollectionData* coll = getOrCreateCollection(collection);
|
|
CollectionData* coll = getOrCreateCollection(collection);
|
|
|
|
|
|
|
|
|
|
+ // v2.21.2 - a zero-length entry is a snapshot placeholder for a vector
|
|
|
|
|
+ // deleted while the snapshot was written (see copyVectors), not a vector.
|
|
|
|
|
+ if (vec.empty()) return;
|
|
|
|
|
+
|
|
|
std::unique_lock<std::shared_mutex> lock(coll->mutex);
|
|
std::unique_lock<std::shared_mutex> lock(coll->mutex);
|
|
|
|
|
|
|
|
|
|
+ // v2.21.2 - counted, through storeVector's accounting. Boot-loaded
|
|
|
|
|
+ // vectors (snapshot, WAL replay) used to bypass the estimate entirely, so
|
|
|
|
|
+ // a store of mostly vectors reported a fraction of what it held and the
|
|
|
|
|
+ // memory budget never saw them.
|
|
|
|
|
+ uint64_t oldSize = 0;
|
|
|
|
|
+ if (auto existing = coll->vectors.find(docId); existing != coll->vectors.end()) {
|
|
|
|
|
+ oldSize = estimateVectorSize(existing->second);
|
|
|
|
|
+ }
|
|
|
coll->vectors[docId] = std::move(vec);
|
|
coll->vectors[docId] = std::move(vec);
|
|
|
coll->vectorNorms[docId] = computeNorm(coll->vectors[docId].data(),
|
|
coll->vectorNorms[docId] = computeNorm(coll->vectors[docId].data(),
|
|
|
coll->vectors[docId].size());
|
|
coll->vectors[docId].size());
|
|
|
|
|
+ const uint64_t newSize = estimateVectorSize(coll->vectors[docId]);
|
|
|
|
|
+ if (newSize > oldSize) addVectorBytes(newSize - oldSize);
|
|
|
|
|
+ else if (oldSize > newSize) subVectorBytes(oldSize - newSize);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ===== TTL Management =====
|
|
// ===== TTL Management =====
|
|
@@ -2917,6 +2935,7 @@ MemoryStore::Stats MemoryStore::getStats() const {
|
|
|
|
|
|
|
|
// Use atomic counter for document memory (O(1) instead of O(n) iteration)
|
|
// Use atomic counter for document memory (O(1) instead of O(n) iteration)
|
|
|
stats.estimatedMemoryBytes = estimatedMemoryBytes_.load(std::memory_order_relaxed);
|
|
stats.estimatedMemoryBytes = estimatedMemoryBytes_.load(std::memory_order_relaxed);
|
|
|
|
|
+ stats.vectorMemoryBytes = vectorMemoryBytes_.load(std::memory_order_relaxed);
|
|
|
|
|
|
|
|
return stats;
|
|
return stats;
|
|
|
}
|
|
}
|
|
@@ -3371,6 +3390,101 @@ std::vector<std::optional<Document>> MemoryStore::copyDocuments(
|
|
|
return out;
|
|
return out;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+bool MemoryStore::canReleaseVector(const std::string& collection) const {
|
|
|
|
|
+ if (!docStoreResolver_ || !mirrorHealthy_ || !mirrorDriftCount_) return false;
|
|
|
|
|
+ if (!mirrorHealthy() || mirrorDriftCount() != 0) return false;
|
|
|
|
|
+ try {
|
|
|
|
|
+ const auto pc = smartbotic::database::parseProjectCollection(collection);
|
|
|
|
|
+ if (pc.collection.empty() || pc.collection.front() == '_') return false; // never mirrored
|
|
|
|
|
+ return docStoreResolver_(pc.project) != nullptr;
|
|
|
|
|
+ } catch (const std::exception&) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::optional<std::vector<float>> MemoryStore::readStoredVector(
|
|
|
|
|
+ const std::string& collection, const std::string& id) const {
|
|
|
|
|
+ if (!docStoreResolver_) return std::nullopt;
|
|
|
|
|
+ const auto pc = smartbotic::database::parseProjectCollection(collection);
|
|
|
|
|
+ auto* ds = docStoreResolver_(pc.project);
|
|
|
|
|
+ if (ds == nullptr) return std::nullopt;
|
|
|
|
|
+ return ds->get_vector(pc.collection, id);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::optional<std::vector<float>> MemoryStore::residentVector(
|
|
|
|
|
+ const std::string& collection, const std::string& id) const {
|
|
|
|
|
+ const CollectionData* coll = getCollection(collection);
|
|
|
|
|
+ if (coll == nullptr) return std::nullopt;
|
|
|
|
|
+ std::shared_lock<std::shared_mutex> lock(coll->mutex);
|
|
|
|
|
+ auto it = coll->vectors.find(id);
|
|
|
|
|
+ if (it == coll->vectors.end()) return std::nullopt;
|
|
|
|
|
+ return it->second;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool MemoryStore::hasVector(const std::string& collection, const std::string& id) const {
|
|
|
|
|
+ if (const CollectionData* coll = getCollection(collection)) {
|
|
|
|
|
+ std::shared_lock<std::shared_mutex> lock(coll->mutex);
|
|
|
|
|
+ if (coll->vectors.count(id) != 0) return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ auto stub = getEvictedStub(collection, id);
|
|
|
|
|
+ return stub && stub->vectorEvicted;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::vector<std::string> MemoryStore::getVectorIds(const std::string& collection) const {
|
|
|
|
|
+ std::vector<std::string> ids;
|
|
|
|
|
+ if (const CollectionData* coll = getCollection(collection)) {
|
|
|
|
|
+ std::shared_lock<std::shared_mutex> lock(coll->mutex);
|
|
|
|
|
+ ids.reserve(coll->vectors.size());
|
|
|
|
|
+ for (const auto& [id, vec] : coll->vectors) ids.push_back(id);
|
|
|
|
|
+ }
|
|
|
|
|
+ // Evicted after the resident ids were listed: listed twice, written
|
|
|
|
|
+ // twice, and the loader keeps one. Never missed.
|
|
|
|
|
+ std::shared_lock<std::shared_mutex> lock(evictionMutex_);
|
|
|
|
|
+ if (auto it = evictedDocs_.find(collection); it != evictedDocs_.end()) {
|
|
|
|
|
+ for (const auto& [id, stub] : it->second) {
|
|
|
|
|
+ if (stub.vectorEvicted) ids.push_back(id);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return ids;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::vector<std::optional<std::vector<float>>> MemoryStore::copyVectors(
|
|
|
|
|
+ const std::string& collection, const std::vector<std::string>& ids,
|
|
|
|
|
+ size_t from, size_t count) const {
|
|
|
|
|
+ std::vector<std::optional<std::vector<float>>> out;
|
|
|
|
|
+ const size_t end = std::min(ids.size(), from + count);
|
|
|
|
|
+ if (from >= end) return out;
|
|
|
|
|
+ out.resize(end - from);
|
|
|
|
|
+ std::vector<size_t> missing;
|
|
|
|
|
+ if (const CollectionData* coll = getCollection(collection)) {
|
|
|
|
|
+ std::shared_lock<std::shared_mutex> lock(coll->mutex);
|
|
|
|
|
+ for (size_t i = from; i < end; ++i) {
|
|
|
|
|
+ auto it = coll->vectors.find(ids[i]);
|
|
|
|
|
+ if (it != coll->vectors.end()) out[i - from] = it->second;
|
|
|
|
|
+ else missing.push_back(i);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ for (size_t i = from; i < end; ++i) missing.push_back(i);
|
|
|
|
|
+ }
|
|
|
|
|
+ // Released by eviction (before or after the listing): read from LMDB,
|
|
|
|
|
+ // outside the collection lock. A read ERROR throws (the snapshot is
|
|
|
|
|
+ // abandoned and the WAL kept). A vector LMDB does not have is written as
|
|
|
|
|
+ // absent: it was released only because LMDB held it, so absence means it
|
|
|
|
|
+ // was deleted meanwhile, and failing every snapshot over it would grow
|
|
|
|
|
+ // the WAL without bound (the 2.20.1 outage).
|
|
|
|
|
+ for (size_t i : missing) {
|
|
|
|
|
+ auto stub = getEvictedStub(collection, ids[i]);
|
|
|
|
|
+ if (stub && stub->vectorEvicted) {
|
|
|
|
|
+ out[i - from] = readStoredVector(collection, ids[i]);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ // No released copy: reloaded since the memory pass (the stub goes
|
|
|
|
|
+ // after the vector is back in memory), or deleted. Look once more.
|
|
|
|
|
+ out[i - from] = residentVector(collection, ids[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ return out;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
std::vector<std::pair<std::string, CollectionOptions>> MemoryStore::getAllCollectionsWithOptions() const {
|
|
std::vector<std::pair<std::string, CollectionOptions>> MemoryStore::getAllCollectionsWithOptions() const {
|
|
|
std::shared_lock<std::shared_mutex> lock(globalMutex_);
|
|
std::shared_lock<std::shared_mutex> lock(globalMutex_);
|
|
|
|
|
|
|
@@ -3523,6 +3637,7 @@ void MemoryStore::clear() {
|
|
|
// Reset the memory accounting counter — every document, version entry,
|
|
// Reset the memory accounting counter — every document, version entry,
|
|
|
// and vector just went away, so any pending increments are stale.
|
|
// and vector just went away, so any pending increments are stale.
|
|
|
estimatedMemoryBytes_.store(0, std::memory_order_relaxed);
|
|
estimatedMemoryBytes_.store(0, std::memory_order_relaxed);
|
|
|
|
|
+ vectorMemoryBytes_.store(0, std::memory_order_relaxed);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ===== Private Methods =====
|
|
// ===== Private Methods =====
|
|
@@ -4075,10 +4190,14 @@ uint64_t MemoryStore::getWalSequence(const std::string& collection,
|
|
|
|
|
|
|
|
void MemoryStore::logMemoryCheck() {
|
|
void MemoryStore::logMemoryCheck() {
|
|
|
auto stats = getStats();
|
|
auto stats = getStats();
|
|
|
- spdlog::info("Memory check: {} MB estimated ({}%, pressure={}), {} docs, {} evicted",
|
|
|
|
|
|
|
+ // v2.21.2 - the vector share is reported on its own: it is usually the
|
|
|
|
|
+ // largest single thing in a vector store and was invisible before.
|
|
|
|
|
+ spdlog::info("Memory check: {} MB estimated ({}%, pressure={}), of which vectors {} MB, "
|
|
|
|
|
+ "{} docs, {} evicted",
|
|
|
stats.estimatedMemoryBytes / (1024 * 1024),
|
|
stats.estimatedMemoryBytes / (1024 * 1024),
|
|
|
pressurePercent(),
|
|
pressurePercent(),
|
|
|
memoryPressureToString(pressure()),
|
|
memoryPressureToString(pressure()),
|
|
|
|
|
+ stats.vectorMemoryBytes / (1024 * 1024),
|
|
|
stats.totalDocuments,
|
|
stats.totalDocuments,
|
|
|
stats.evictedCount);
|
|
stats.evictedCount);
|
|
|
|
|
|
|
@@ -4359,6 +4478,10 @@ uint64_t MemoryStore::evictOneChunk() {
|
|
|
c.lastAccessedAt = doc.lastAccessedAt > 0 ? doc.lastAccessedAt
|
|
c.lastAccessedAt = doc.lastAccessedAt > 0 ? doc.lastAccessedAt
|
|
|
: (doc.updatedAt > 0 ? doc.updatedAt : doc.createdAt);
|
|
: (doc.updatedAt > 0 ? doc.updatedAt : doc.createdAt);
|
|
|
c.estimatedSize = estimateDocumentSize(doc);
|
|
c.estimatedSize = estimateDocumentSize(doc);
|
|
|
|
|
+ // v2.21.2 - eviction releases the vector too.
|
|
|
|
|
+ if (auto vit = collPtr->vectors.find(docId); vit != collPtr->vectors.end()) {
|
|
|
|
|
+ c.estimatedSize += estimateVectorSize(vit->second);
|
|
|
|
|
+ }
|
|
|
c.priority = collPtr->options.memoryPriority;
|
|
c.priority = collPtr->options.memoryPriority;
|
|
|
candidates.push_back(std::move(c));
|
|
candidates.push_back(std::move(c));
|
|
|
}
|
|
}
|
|
@@ -4507,6 +4630,10 @@ std::vector<std::pair<std::string, std::string>> MemoryStore::selectDocumentsFor
|
|
|
uint64_t accessTime = doc.lastAccessedAt > 0 ? doc.lastAccessedAt :
|
|
uint64_t accessTime = doc.lastAccessedAt > 0 ? doc.lastAccessedAt :
|
|
|
(doc.updatedAt > 0 ? doc.updatedAt : doc.createdAt);
|
|
(doc.updatedAt > 0 ? doc.updatedAt : doc.createdAt);
|
|
|
uint64_t size = estimateDocumentSize(doc);
|
|
uint64_t size = estimateDocumentSize(doc);
|
|
|
|
|
+ // v2.21.2 - eviction releases the vector too.
|
|
|
|
|
+ if (auto vit = coll->vectors.find(id); vit != coll->vectors.end()) {
|
|
|
|
|
+ size += estimateVectorSize(vit->second);
|
|
|
|
|
+ }
|
|
|
docs.emplace_back(accessTime, size, collName, id);
|
|
docs.emplace_back(accessTime, size, collName, id);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -4547,6 +4674,21 @@ bool MemoryStore::evictDocument(const std::string& collection, const std::string
|
|
|
stub.estimatedSize = estimateDocumentSize(it->second);
|
|
stub.estimatedSize = estimateDocumentSize(it->second);
|
|
|
stub.evictedAt = currentTimeMs();
|
|
stub.evictedAt = currentTimeMs();
|
|
|
|
|
|
|
|
|
|
+ // v2.21.2 - release the vector too. Eviction used to leave coll.vectors
|
|
|
|
|
+ // alone, so a vector outlived its body for the life of the process (and,
|
|
|
|
|
+ // through the snapshot's vector section, across restarts), and the one
|
|
|
|
|
+ // mechanism that frees memory under pressure could not touch the largest
|
|
|
|
|
+ // thing in a vector store. Only while LMDB holds the current copy; a
|
|
|
|
|
+ // reload, the snapshot and the degraded search read it from there.
|
|
|
|
|
+ uint64_t vectorFreed = 0;
|
|
|
|
|
+ if (auto vit = coll->vectors.find(id);
|
|
|
|
|
+ vit != coll->vectors.end() && canReleaseVector(collection)) {
|
|
|
|
|
+ vectorFreed = estimateVectorSize(vit->second);
|
|
|
|
|
+ coll->vectors.erase(vit);
|
|
|
|
|
+ coll->vectorNorms.erase(id);
|
|
|
|
|
+ stub.vectorEvicted = true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Remove from expiration index
|
|
// Remove from expiration index
|
|
|
if (it->second.expiresAt > 0) {
|
|
if (it->second.expiresAt > 0) {
|
|
|
removeFromExpirationIndex(*coll, id, it->second.expiresAt);
|
|
removeFromExpirationIndex(*coll, id, it->second.expiresAt);
|
|
@@ -4584,6 +4726,7 @@ bool MemoryStore::evictDocument(const std::string& collection, const std::string
|
|
|
|
|
|
|
|
// Update memory tracking atomically (subtract evicted document size).
|
|
// Update memory tracking atomically (subtract evicted document size).
|
|
|
estimatedMemoryBytes_.fetch_sub(stub.estimatedSize, std::memory_order_relaxed);
|
|
estimatedMemoryBytes_.fetch_sub(stub.estimatedSize, std::memory_order_relaxed);
|
|
|
|
|
+ if (vectorFreed > 0) subVectorBytes(vectorFreed);
|
|
|
|
|
|
|
|
SPDLOG_DEBUG("Evicted document {}/{} (size: {} bytes)", collection, id, stub.estimatedSize);
|
|
SPDLOG_DEBUG("Evicted document {}/{} (size: {} bytes)", collection, id, stub.estimatedSize);
|
|
|
|
|
|
|
@@ -4627,6 +4770,26 @@ std::optional<Document> MemoryStore::loadEvictedDocument(const std::string& coll
|
|
|
return std::nullopt;
|
|
return std::nullopt;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // v2.21.2 - bring back the vector eviction released. Read before taking
|
|
|
|
|
+ // the collection lock and stored only if this call is the one that
|
|
|
|
|
+ // inserts the document. A read error fails the reload (the stub stays, so
|
|
|
|
|
+ // a write refuses rather than proceed without the vector, and an undo
|
|
|
|
|
+ // copy taken by that write would otherwise lose it).
|
|
|
|
|
+ std::optional<std::vector<float>> vector;
|
|
|
|
|
+ if (stub.vectorEvicted) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ vector = readStoredVector(collection, id);
|
|
|
|
|
+ } catch (const std::exception& e) {
|
|
|
|
|
+ spdlog::warn("Failed to recover the vector of evicted document {}/{}: {}",
|
|
|
|
|
+ collection, id, e.what());
|
|
|
|
|
+ return std::nullopt;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!vector) {
|
|
|
|
|
+ spdlog::warn("Evicted document {}/{} had a vector, and the on-disk store has "
|
|
|
|
|
+ "none for it; reloaded without one", collection, id);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
doc->collection = collection;
|
|
doc->collection = collection;
|
|
|
|
|
|
|
|
// Re-insert into memory (hot-load). Another reader may have raced us; if so,
|
|
// Re-insert into memory (hot-load). Another reader may have raced us; if so,
|
|
@@ -4658,6 +4821,9 @@ std::optional<Document> MemoryStore::loadEvictedDocument(const std::string& coll
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
coll->documents.emplace(id, *doc);
|
|
coll->documents.emplace(id, *doc);
|
|
|
|
|
+ if (vector && coll->vectors.find(id) == coll->vectors.end()) {
|
|
|
|
|
+ storeVector(*coll, id, *vector);
|
|
|
|
|
+ }
|
|
|
inserted = true;
|
|
inserted = true;
|
|
|
returned = *doc;
|
|
returned = *doc;
|
|
|
}
|
|
}
|