2525
2626using namespace FDB ;
2727
28+ std::string fullCollNameToString (Namespace const & ns) {
29+ return ns.first + " ." + (ns.second .empty () ? " $cmd" : ns.second );
30+ }
31+
2832Future<uint64_t > getMetadataVersion (Reference<DocTransaction> tr, Reference<DirectorySubspace> metadataDirectory) {
2933 std::string versionKey = metadataDirectory->key ().toString () +
3034 DataValue (DocLayerConstants::VERSION_KEY, DVTypeCode::STRING).encode_key_part ();
@@ -82,12 +86,8 @@ IndexInfo MetadataManager::indexInfoFromObj(const bson::BSONObj& indexObj, Refer
8286 }
8387}
8488
85- ACTOR static Future<std::pair<Reference<UnboundCollectionContext>, uint64_t >> constructContext (
86- Namespace ns,
87- Reference<DocTransaction> tr,
88- DocumentLayer* docLayer,
89- bool includeIndex,
90- bool createCollectionIfAbsent) {
89+ ACTOR static Future<std::pair<Reference<UnboundCollectionContext>, uint64_t >>
90+ constructContext (Namespace ns, Reference<DocTransaction> tr, DocumentLayer* docLayer, bool createCollectionIfAbsent) {
9191 try {
9292 // The initial set of directory reads take place in a separate transaction with the same read version as `tr'.
9393 // This hopefully prevents us from accidentally RYWing a directory that `tr' itself created, and then adding it
@@ -108,20 +108,15 @@ ACTOR static Future<std::pair<Reference<UnboundCollectionContext>, uint64_t>> co
108108 state Reference<UnboundCollectionContext> cx =
109109 Reference<UnboundCollectionContext>(new UnboundCollectionContext (collectionDirectory, metadataDirectory));
110110
111- // Only include existing indexes into the context when it's NOT building a new index.
112- // When it's building a new index, it's unnecessary and inefficient to pass each recorded returned by a
113- // TableScan through the existing indexes.
114- if (includeIndex) {
115- state Reference<UnboundCollectionContext> indexCx = Reference<UnboundCollectionContext>(
116- new UnboundCollectionContext (indexDirectory, Reference<DirectorySubspace>()));
117- state Reference<Plan> indexesPlan = getIndexesForCollectionPlan (indexCx, ns);
118- std::vector<bson::BSONObj> allIndexes = wait (getIndexesTransactionally (indexesPlan, tr));
111+ state Reference<UnboundCollectionContext> indexCx = Reference<UnboundCollectionContext>(
112+ new UnboundCollectionContext (indexDirectory, Reference<DirectorySubspace>()));
113+ state Reference<Plan> indexesPlan = getIndexesForCollectionPlan (indexCx, ns);
114+ std::vector<bson::BSONObj> allIndexes = wait (getIndexesTransactionally (indexesPlan, tr));
119115
120- for (const auto & indexObj : allIndexes) {
121- IndexInfo index = MetadataManager::indexInfoFromObj (indexObj, cx);
122- if (index.status != IndexInfo::IndexStatus::INVALID) {
123- cx->addIndex (index);
124- }
116+ for (const auto & indexObj : allIndexes) {
117+ IndexInfo index = MetadataManager::indexInfoFromObj (indexObj, cx);
118+ if (index.status != IndexInfo::IndexStatus::INVALID) {
119+ cx->addIndex (index);
125120 }
126121 }
127122
@@ -157,6 +152,9 @@ ACTOR static Future<std::pair<Reference<UnboundCollectionContext>, uint64_t>> co
157152 // collectionName.c_str(), printable(tcollectionDirectory->key()).c_str(),
158153 // printable(tmetadataDirectory->key()).c_str(), "");
159154 tcx->bindCollectionContext (tr)->bumpMetadataVersion (); // We start at version 1.
155+ TraceEvent (SevInfo, " BumpMetadataVersion" )
156+ .detail (" reason" , " createCollection" )
157+ .detail (" ns" , fullCollNameToString (ns));
160158
161159 return std::make_pair (tcx, -1 ); // So we don't pollute the cache in case this transaction never commits
162160 }
@@ -165,20 +163,22 @@ ACTOR static Future<std::pair<Reference<UnboundCollectionContext>, uint64_t>> co
165163ACTOR static Future<Reference<UnboundCollectionContext>> assembleCollectionContext (Reference<DocTransaction> tr,
166164 Namespace ns,
167165 Reference<MetadataManager> self,
168- bool includeIndex,
169166 bool createCollectionIfAbsent) {
170- if (self->contexts .size () > 100 )
167+ if (self->contexts .size () > DocLayerConstants::METADATA_CACHE_SIZE )
171168 self->contexts .clear ();
172169
173170 auto match = self->contexts .find (ns);
174171
175172 if (match == self->contexts .end ()) {
176173 std::pair<Reference<UnboundCollectionContext>, uint64_t > unboundPair =
177- wait (constructContext (ns, tr, self->docLayer , includeIndex, createCollectionIfAbsent));
174+ wait (constructContext (ns, tr, self->docLayer , createCollectionIfAbsent));
178175
179176 // Here and below don't pollute the cache if we just created the directory, since this transaction might
180177 // not commit.
181178 if (unboundPair.second != -1 ) {
179+ TraceEvent (SevInfo, " MetadataCacheAdd" )
180+ .detail (" ns" , fullCollNameToString (ns))
181+ .detail (" version" , unboundPair.second );
182182 auto insert_result = self->contexts .insert (std::make_pair (ns, unboundPair));
183183 // Somebody else may have done the lookup and finished ahead of us. Either way, replace it with ours (can no
184184 // longer optimize this by only replacing if ours is newer, because the directory may have moved or
@@ -194,19 +194,21 @@ ACTOR static Future<Reference<UnboundCollectionContext>> assembleCollectionConte
194194 uint64_t version = wait (getMetadataVersion (tr, oldUnbound->metadataDirectory ));
195195 if (version != oldVersion) {
196196 std::pair<Reference<UnboundCollectionContext>, uint64_t > unboundPair =
197- wait (constructContext (ns, tr, self->docLayer , includeIndex, createCollectionIfAbsent));
197+ wait (constructContext (ns, tr, self->docLayer , createCollectionIfAbsent));
198198 if (unboundPair.second != -1 ) {
199199 // Create the iterator again instead of making the previous value state, because the map could have
200200 // changed during the previous wait. Either way, replace it with ours (can no longer optimize this by
201201 // only replacing if ours is newer, because the directory may have moved or vanished.
202- // std::map<std::pair<std::string, std::string>, std::pair<Reference<UnboundCollectionContext>,
203- // uint64_t>>::iterator match = self->contexts.find(ns);
204202 auto match = self->contexts .find (ns);
205-
206203 if (match != self->contexts .end ())
207204 match->second = unboundPair;
208205 else
209206 self->contexts .insert (std::make_pair (ns, unboundPair));
207+
208+ TraceEvent (SevInfo, " MetadataCacheUpdate" )
209+ .detail (" ns" , fullCollNameToString (ns))
210+ .detail (" oldVersion" , oldVersion)
211+ .detail (" newVersion" , unboundPair.second );
210212 }
211213 return unboundPair.first ;
212214 } else {
@@ -219,19 +221,17 @@ Future<Reference<UnboundCollectionContext>> MetadataManager::getUnboundCollectio
219221 Reference<DocTransaction> tr,
220222 Namespace const & ns,
221223 bool allowSystemNamespace,
222- bool includeIndex,
223224 bool createCollectionIfAbsent) {
224225 if (!allowSystemNamespace && startsWith (ns.second .c_str (), " system." ))
225226 throw write_system_namespace ();
226- return assembleCollectionContext (tr, ns, Reference<MetadataManager>::addRef (this ), includeIndex,
227- createCollectionIfAbsent);
227+ return assembleCollectionContext (tr, ns, Reference<MetadataManager>::addRef (this ), createCollectionIfAbsent);
228228}
229229
230230Future<Reference<UnboundCollectionContext>> MetadataManager::refreshUnboundCollectionContext (
231231 Reference<UnboundCollectionContext> cx,
232232 Reference<DocTransaction> tr) {
233233 return assembleCollectionContext (tr, std::make_pair (cx->databaseName (), cx->collectionName ()),
234- Reference<MetadataManager>::addRef (this ), false , false );
234+ Reference<MetadataManager>::addRef (this ), false );
235235}
236236
237237ACTOR static Future<Void> buildIndex_impl (bson::BSONObj indexObj,
@@ -242,7 +242,7 @@ ACTOR static Future<Void> buildIndex_impl(bson::BSONObj indexObj,
242242 state IndexInfo info;
243243 try {
244244 state Reference<DocTransaction> tr = ec->getOperationTransaction ();
245- state Reference<UnboundCollectionContext> mcx = wait (ec->mm ->getUnboundCollectionContext (tr, ns, false , false ));
245+ state Reference<UnboundCollectionContext> mcx = wait (ec->mm ->getUnboundCollectionContext (tr, ns, false ));
246246 info = MetadataManager::indexInfoFromObj (indexObj, mcx);
247247 info.status = IndexInfo::IndexStatus::BUILDING;
248248 info.buildId = build_id;
0 commit comments