Skip to content

Commit f421548

Browse files
committed
storage analyze
1 parent f966d78 commit f421548

11 files changed

+55
-4
lines changed

mongo/src/mongo/db/catalog/collection_info_cache_impl.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ void CollectionInfoCacheImpl::computeIndexKeys(OperationContext* opCtx) {
159159
_keysComputed = true;
160160
}
161161

162+
/*
163+
调用的地方比较多:
164+
Count_cmd.cpp (mongo\src\mongo\db\commands): collection->infoCache()->notifyOfQuery(opCtx, summaryStats.indexesUsed);
165+
Distinct.cpp (mongo\src\mongo\db\commands): collection->infoCache()->notifyOfQuery(opCtx, stats.indexesUsed);
166+
Document_source_cursor.cpp (mongo\src\mongo\db\pipeline): collection->infoCache()->notifyOfQuery(pExpCtx->opCtx, _planSummaryStats.indexesUsed);
167+
Find.cpp (mongo\src\mongo\db\query): collection->infoCache()->notifyOfQuery(opCtx, summaryStats.indexesUsed);
168+
Find_and_modify.cpp (mongo\src\mongo\db\commands): collection->infoCache()->notifyOfQuery(opCtx, summaryStats.indexesUsed);
169+
Find_and_modify.cpp (mongo\src\mongo\db\commands): collection->infoCache()->notifyOfQuery(opCtx, summaryStats.indexesUsed);
170+
Geo_near_cmd.cpp (mongo\src\mongo\db\commands): collection->infoCache()->notifyOfQuery(opCtx, summary.indexesUsed);
171+
Group_cmd.cpp (mongo\src\mongo\db\commands): coll->infoCache()->notifyOfQuery(opCtx, summaryStats.indexesUsed);
172+
Mr.cpp (mongo\src\mongo\db\commands): coll->infoCache()->notifyOfQuery(opCtx, stats.indexesUsed);
173+
Write_ops_exec.cpp (mongo\src\mongo\db\ops): collection->getCollection()->infoCache()->notifyOfQuery(opCtx, summary.indexesUsed);
174+
Write_ops_exec.cpp (mongo\src\mongo\db\ops): collection.getCollection()->infoCache()->notifyOfQuery(opCtx, summary.indexesUsed);
175+
*/
176+
//db.collection.aggregate({"$indexStats":{}})里面有对索引的统计
177+
//当前真在使用的索引统计
162178
void CollectionInfoCacheImpl::notifyOfQuery(OperationContext* opCtx,
163179
const std::set<std::string>& indexesUsed) {
164180
// Record indexes used to fulfill query.
@@ -167,11 +183,12 @@ void CollectionInfoCacheImpl::notifyOfQuery(OperationContext* opCtx,
167183
// index was dropped (and we would not get here).
168184
dassert(NULL != _collection->getIndexCatalog()->findIndexByName(opCtx, *it));
169185

186+
//CollectionIndexUsageTracker::recordIndexAccess
170187
_indexUsageTracker.recordIndexAccess(*it);
171188
}
172189
}
173190

174-
//清除缓存的_planCache
191+
//清除缓存的_planCache CollectionInfoCacheImpl::rebuildIndexData调用
175192
void CollectionInfoCacheImpl::clearQueryCache() {
176193
LOG(1) << _collection->ns().ns() << ": clearing plan cache - collection info cache reset";
177194
if (NULL != _planCache.get()) {
@@ -187,6 +204,8 @@ QuerySettings* CollectionInfoCacheImpl::getQuerySettings() const {
187204
return _querySettings.get();
188205
}
189206

207+
//CollectionInfoCacheImpl::rebuildIndexData中调用
208+
//CollectionInfoCacheImpl::updatePlanCacheIndexEntries中完成IndexEntry和IndexDescriptor的转换
190209
void CollectionInfoCacheImpl::updatePlanCacheIndexEntries(OperationContext* opCtx) {
191210
std::vector<IndexEntry> indexEntries;
192211

@@ -196,6 +215,7 @@ void CollectionInfoCacheImpl::updatePlanCacheIndexEntries(OperationContext* opCt
196215
IndexCatalog::IndexIterator ii =
197216
_collection->getIndexCatalog()->getIndexIterator(opCtx, includeUnfinishedIndexes);
198217
while (ii.more()) {
218+
//获取索引信息
199219
const IndexDescriptor* desc = ii.next();
200220
const IndexCatalogEntry* ice = ii.catalogEntry(desc);
201221
indexEntries.emplace_back(desc->keyPattern(),
@@ -210,6 +230,7 @@ void CollectionInfoCacheImpl::updatePlanCacheIndexEntries(OperationContext* opCt
210230
ice->getCollator());
211231
}
212232

233+
//PlanCache::notifyOfIndexEntries
213234
_planCache->notifyOfIndexEntries(indexEntries);
214235
}
215236

@@ -241,6 +262,7 @@ void CollectionInfoCacheImpl::addedIndex(OperationContext* opCtx, const IndexDes
241262
_indexUsageTracker.registerIndex(desc->indexName(), desc->keyPattern());
242263
}
243264

265+
//IndexCatalogImpl::_dropIndex调用
244266
void CollectionInfoCacheImpl::droppedIndex(OperationContext* opCtx, StringData indexName) {
245267
// Requires exclusive collection lock.
246268
invariant(opCtx->lockState()->isCollectionLockedForMode(_collection->ns().ns(), MODE_X));
@@ -249,6 +271,8 @@ void CollectionInfoCacheImpl::droppedIndex(OperationContext* opCtx, StringData i
249271
_indexUsageTracker.unregisterIndex(indexName);
250272
}
251273

274+
//CollectionInfoCacheImpl::init CollectionInfoCacheImpl::addedIndex
275+
//CollectionInfoCacheImpl::droppedIndex调用
252276
void CollectionInfoCacheImpl::rebuildIndexData(OperationContext* opCtx) {
253277
//清除缓存的querycache
254278
clearQueryCache();
@@ -258,7 +282,10 @@ void CollectionInfoCacheImpl::rebuildIndexData(OperationContext* opCtx) {
258282
updatePlanCacheIndexEntries(opCtx);
259283
}
260284

285+
//db.collection.aggregate({"$indexStats":{}})会用到
286+
//getIndexStats调用
261287
CollectionIndexUsageMap CollectionInfoCacheImpl::getIndexUsageStats() const {
288+
//CollectionIndexUsageTracker::getUsageStats()
262289
return _indexUsageTracker.getUsageStats();
263290
}
264291
} // namespace mongo

mongo/src/mongo/db/catalog/collection_info_cache_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class OperationContext;
4545
* this is for storing things that you want to cache about a single collection
4646
* life cycle is managed for you from inside Collection
4747
*/
48+
4849
class CollectionInfoCacheImpl : public CollectionInfoCache::Impl {
4950
public:
5051
explicit CollectionInfoCacheImpl(Collection* collection, const NamespaceString& ns);

mongo/src/mongo/db/catalog/index_catalog_entry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class IndexCatalogEntryContainer {
328328
}
329329

330330
private:
331-
331+
//对应IndexCatalogEntryImpl,也就是索引信息,一个索引对应一个IndexCatalogEntryImpl
332332
std::vector<std::unique_ptr<IndexCatalogEntry>> _entries;
333333
};
334334
} // namespace mongo

mongo/src/mongo/db/catalog/index_catalog_entry_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class IndexCatalogEntryImpl : public IndexCatalogEntry::Impl {
187187

188188
//对应KVCollectionCatalogEntry
189189
CollectionCatalogEntry* _collection; // not owned here
190-
//索引描述信息全部存到这里
190+
//索引描述信息全部存到这里,对应索引
191191
std::unique_ptr<IndexDescriptor> _descriptor; // owned here
192192

193193
//CollectionInfoCacheImpl类型

mongo/src/mongo/db/collection_index_usage_tracker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ CollectionIndexUsageTracker::CollectionIndexUsageTracker(ClockSource* clockSourc
4242
invariant(_clockSource);
4343
}
4444

45+
//CollectionInfoCacheImpl::notifyOfQueryµ÷ÓÃ
4546
void CollectionIndexUsageTracker::recordIndexAccess(StringData indexName) {
4647
invariant(!indexName.empty());
4748
dassert(_indexUsageMap.find(indexName) != _indexUsageMap.end());
@@ -64,6 +65,7 @@ void CollectionIndexUsageTracker::unregisterIndex(StringData indexName) {
6465
_indexUsageMap.erase(indexName);
6566
}
6667

68+
//CollectionInfoCacheImpl::getIndexUsageStats()µ÷ÓÃ
6769
CollectionIndexUsageMap CollectionIndexUsageTracker::getUsageStats() const {
6870
return _indexUsageMap;
6971
}

mongo/src/mongo/db/index/index_descriptor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class IndexCatalogEntryContainer;
5353
*
5454
* All synchronization is the responsibility of the caller.
5555
*/
56+
57+
//CollectionInfoCacheImpl::updatePlanCacheIndexEntries中完成IndexEntry和IndexDescriptor的转换
58+
5659
//IndexCatalogImpl::init IndexCatalogImpl::IndexBuildBlock::init()中构造使用
5760

5861
//索引信息,获取所有所有信息可以参考fillOutPlannerParams

mongo/src/mongo/db/pipeline/document_source_index_stats.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ const char* DocumentSourceIndexStats::getSourceName() const {
4747
return "$indexStats";
4848
}
4949

50+
/*
51+
mongos> db.news_doc_profile.aggregate({"$indexStats":{}})
52+
{ "name" : "outId_1", "key" : { "outId" : 1 }, "host" : "bj6535:10001", "accesses" : { "ops" : NumberLong(1292908), "since" : ISODate("2020-05-28T10:51:19.683Z") } }
53+
{ "name" : "_id_", "key" : { "_id" : 1 }, "host" : "bj6535:10001", "accesses" : { "ops" : NumberLong("9656445014"), "since" : ISODate("2020-05-28T10:51:19.683Z") } }
54+
{ "name" : "docId_1", "key" : { "docId" : 1 }, "host" : "bj6535:10001", "accesses" : { "ops" : NumberLong(132015475), "since" : ISODate("2020-05-28T10:51:19.683Z") } }
55+
{ "name" : "publishTime_1", "key" : { "publishTime" : 1 }, "host" : "bj6535:10001", "accesses" : { "ops" : NumberLong(25), "since" : ISODate("2020-05-28T10:51:19.683Z") } }
56+
{ "name" : "updateTime_1", "key" : { "updateTime" : 1 }, "host" : "bj6535:10001", "accesses" : { "ops" : NumberLong(82756), "since" : ISODate("2020-05-28T10:51:19.683Z") } }
57+
mongos>
58+
*/
5059
DocumentSource::GetNextResult DocumentSourceIndexStats::getNext() {
5160
pExpCtx->checkForInterrupt();
5261

mongo/src/mongo/db/pipeline/pipeline_d.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class MongodProcessInterface final
127127
return _client.getLastErrorDetailed();
128128
}
129129

130+
//DocumentSourceIndexStats::getNext()Öе÷ÓÃ
131+
//db.collection.aggregate({"$indexStats":{}})»áÓõ½
130132
CollectionIndexUsageMap getIndexStats(OperationContext* opCtx,
131133
const NamespaceString& ns) final {
132134
AutoGetCollectionForReadCommand autoColl(opCtx, ns);
@@ -137,6 +139,7 @@ class MongodProcessInterface final
137139
return CollectionIndexUsageMap();
138140
}
139141

142+
//CollectionInfoCacheImpl::getIndexUsageStats()
140143
return collection->infoCache()->getIndexUsageStats();
141144
}
142145

mongo/src/mongo/db/query/index_entry.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ class MatchExpression;
4242

4343
/**
4444
* This name sucks, but every name involving 'index' is used somewhere.
45-
*/ //QueryPlannerParams.indices(集合对应的所有IndexEntry都存入该数组)成员为该类型,
45+
*/
46+
//CollectionInfoCacheImpl::updatePlanCacheIndexEntries中完成IndexEntry和IndexDescriptor的转换
47+
48+
//QueryPlannerParams.indices(集合对应的所有IndexEntry都存入该数组)成员为该类型,
4649
//代表一个索引实例,一条索引的详细信息就记录在该结构中
4750
//赋值参考fillOutPlannerParams //PlanCacheIndexTree.entry成员为该类型
4851
struct IndexEntry { //IndexEntry中的取值实际上是从IndexDescriptor获取的,参考fillOutPlannerParams

mongo/src/mongo/db/query/plan_cache.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,8 @@ size_t PlanCache::size() const {
957957
return _cache.size();
958958
}
959959

960+
//CollectionInfoCacheImpl::updatePlanCacheIndexEntries中调用,
961+
//CollectionInfoCacheImpl::updatePlanCacheIndexEntries中完成IndexEntry和IndexDescriptor的转换
960962
void PlanCache::notifyOfIndexEntries(const std::vector<IndexEntry>& indexEntries) {
961963
_indexabilityState.updateDiscriminators(indexEntries);
962964
}

mongo/src/mongo/db/query/plan_cache_indexability.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ const IndexToDiscriminatorMap& PlanCacheIndexabilityState::getDiscriminators(
124124
return it->second;
125125
}
126126

127+
//PlanCache::notifyOfIndexEntriesÖе÷ÓÃ
127128
void PlanCacheIndexabilityState::updateDiscriminators(const std::vector<IndexEntry>& indexEntries) {
128129
_pathDiscriminatorsMap = PathDiscriminatorsMap();
129130

0 commit comments

Comments
 (0)