diff --git a/src/Constants.cpp b/src/Constants.cpp index 27149c1..fcdebdc 100644 --- a/src/Constants.cpp +++ b/src/Constants.cpp @@ -27,6 +27,8 @@ const uint64_t INDEX_KEY_LENGTH_LIMIT = (uint64_t)1e4; const uint64_t FDB_KEY_LENGTH_LIMIT = (uint64_t)1e4; const uint64_t FDB_VALUE_LENGTH_LIMIT = (uint64_t)1e5; +const uint64_t METADATA_CACHE_SIZE = (uint64_t)100; + const std::string METADATA = "metadata"; const std::string VERSION_KEY = "version"; const std::string INDICES_KEY = "indices"; diff --git a/src/Constants.h b/src/Constants.h index f5e95cd..c2d99a1 100644 --- a/src/Constants.h +++ b/src/Constants.h @@ -34,6 +34,9 @@ extern const uint64_t INDEX_KEY_LENGTH_LIMIT; // This is set to 10K bytes, inste extern const uint64_t FDB_KEY_LENGTH_LIMIT; extern const uint64_t FDB_VALUE_LENGTH_LIMIT; +// Size of metadata cache in entries, number of collections +extern const uint64_t METADATA_CACHE_SIZE; + // KVS DocLayer internal keys extern const std::string METADATA; extern const std::string VERSION_KEY; diff --git a/src/ExtMsg.actor.cpp b/src/ExtMsg.actor.cpp index ae5c0fc..fc81a61 100644 --- a/src/ExtMsg.actor.cpp +++ b/src/ExtMsg.actor.cpp @@ -1205,7 +1205,7 @@ ACTOR Future doDeleteCmd(Namespace ns, // If collection not found then just return success from here. try { Reference _cx = - wait(ec->mm->getUnboundCollectionContext(dtr, ns, false, true, false)); + wait(ec->mm->getUnboundCollectionContext(dtr, ns, false, false)); cx = _cx; } catch (Error& e) { if (e.code() == error_code_collection_not_found) diff --git a/src/MetadataManager.actor.cpp b/src/MetadataManager.actor.cpp index 0827ac5..425d90b 100644 --- a/src/MetadataManager.actor.cpp +++ b/src/MetadataManager.actor.cpp @@ -90,7 +90,6 @@ ACTOR static Future, uint64_t>> co Namespace ns, Reference tr, DocumentLayer* docLayer, - bool includeIndex, bool createCollectionIfAbsent) { try { // The initial set of directory reads take place in a separate transaction with the same read version as `tr'. @@ -112,20 +111,15 @@ ACTOR static Future, uint64_t>> co state Reference cx = Reference(new UnboundCollectionContext(collectionDirectory, metadataDirectory)); - // Only include existing indexes into the context when it's NOT building a new index. - // When it's building a new index, it's unnecessary and inefficient to pass each recorded returned by a - // TableScan through the existing indexes. - if (includeIndex) { - state Reference indexCx = Reference( - new UnboundCollectionContext(indexDirectory, Reference())); - state Reference indexesPlan = getIndexesForCollectionPlan(indexCx, ns); - std::vector allIndexes = wait(getIndexesTransactionally(indexesPlan, tr)); + state Reference indexCx = Reference( + new UnboundCollectionContext(indexDirectory, Reference())); + state Reference indexesPlan = getIndexesForCollectionPlan(indexCx, ns); + std::vector allIndexes = wait(getIndexesTransactionally(indexesPlan, tr)); - for (const auto& indexObj : allIndexes) { - IndexInfo index = MetadataManager::indexInfoFromObj(indexObj, cx); - if (index.status != IndexInfo::IndexStatus::INVALID) { - cx->addIndex(index); - } + for (const auto& indexObj : allIndexes) { + IndexInfo index = MetadataManager::indexInfoFromObj(indexObj, cx); + if (index.status != IndexInfo::IndexStatus::INVALID) { + cx->addIndex(index); } } @@ -172,16 +166,15 @@ ACTOR static Future, uint64_t>> co ACTOR static Future> assembleCollectionContext(Reference tr, Namespace ns, Reference self, - bool includeIndex, bool createCollectionIfAbsent) { - if (self->contexts.size() > 100) + if (self->contexts.size() > DocLayerConstants::METADATA_CACHE_SIZE) self->contexts.clear(); auto match = self->contexts.find(ns); if (match == self->contexts.end()) { std::pair, uint64_t> unboundPair = - wait(constructContext(ns, tr, self->docLayer, includeIndex, createCollectionIfAbsent)); + wait(constructContext(ns, tr, self->docLayer, createCollectionIfAbsent)); // Here and below don't pollute the cache if we just created the directory, since this transaction might // not commit. @@ -204,7 +197,7 @@ ACTOR static Future> assembleCollectionConte uint64_t version = wait(getMetadataVersion(tr, oldUnbound->metadataDirectory)); if (version != oldVersion) { std::pair, uint64_t> unboundPair = - wait(constructContext(ns, tr, self->docLayer, includeIndex, createCollectionIfAbsent)); + wait(constructContext(ns, tr, self->docLayer, createCollectionIfAbsent)); if (unboundPair.second != -1) { // Create the iterator again instead of making the previous value state, because the map could have // changed during the previous wait. Either way, replace it with ours (can no longer optimize this by @@ -231,19 +224,17 @@ Future> MetadataManager::getUnboundCollectio Reference tr, Namespace const& ns, bool allowSystemNamespace, - bool includeIndex, bool createCollectionIfAbsent) { if (!allowSystemNamespace && startsWith(ns.second.c_str(), "system.")) throw write_system_namespace(); - return assembleCollectionContext(tr, ns, Reference::addRef(this), includeIndex, - createCollectionIfAbsent); + return assembleCollectionContext(tr, ns, Reference::addRef(this), createCollectionIfAbsent); } Future> MetadataManager::refreshUnboundCollectionContext( Reference cx, Reference tr) { return assembleCollectionContext(tr, std::make_pair(cx->databaseName(), cx->collectionName()), - Reference::addRef(this), false, false); + Reference::addRef(this), false); } ACTOR static Future buildIndex_impl(bson::BSONObj indexObj, @@ -254,7 +245,7 @@ ACTOR static Future buildIndex_impl(bson::BSONObj indexObj, state IndexInfo info; try { state Reference tr = ec->getOperationTransaction(); - state Reference mcx = wait(ec->mm->getUnboundCollectionContext(tr, ns, false, false)); + state Reference mcx = wait(ec->mm->getUnboundCollectionContext(tr, ns, false)); info = MetadataManager::indexInfoFromObj(indexObj, mcx); info.status = IndexInfo::IndexStatus::BUILDING; info.buildId = build_id; diff --git a/src/MetadataManager.h b/src/MetadataManager.h index 42c86ed..e55642b 100644 --- a/src/MetadataManager.h +++ b/src/MetadataManager.h @@ -40,7 +40,6 @@ struct MetadataManager : ReferenceCounted, NonCopyable { Future> getUnboundCollectionContext(Reference tr, Namespace const& ns, bool allowSystemNamespace = false, - bool includeIndex = true, bool createCollectionIfAbsent = true); Future> refreshUnboundCollectionContext(Reference cx, Reference tr);