Skip to content

Commit f050a35

Browse files
committed
storage analyze
1 parent f421548 commit f050a35

21 files changed

+134
-21
lines changed

image/单机模块化架构图.png

-127 Bytes
Loading

mongo/src/mongo/db/background.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ typedef BgInfoMap::const_iterator BgInfoMapIterator;
7474

7575
// Static data for this file is never destroyed.
7676
stdx::mutex& m = *(new stdx::mutex());
77+
//当前真在后台加索引的库
7778
BgInfoMap& dbsInProg = *(new BgInfoMap());
79+
//当前真在后台加索引的表
7880
BgInfoMap& nsInProg = *(new BgInfoMap());
7981

8082
void BgInfo::recordBegin() {

mongo/src/mongo/db/catalog/capped_utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "mongo/db/views/view.h"
5555
#include "mongo/util/scopeguard.h"
5656

57+
//EmptyCapped::run->mongo::emptyCapped
5758
mongo::Status mongo::emptyCapped(OperationContext* opCtx, const NamespaceString& collectionName) {
5859
AutoGetDb autoDb(opCtx, collectionName.db(), MODE_X);
5960

mongo/src/mongo/db/catalog/collection_impl.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,13 @@ CollectionImpl::CollectionImpl(Collection* _this_init,
178178

179179
void CollectionImpl::init(OperationContext* opCtx) {
180180
_magic = kMagicNumber;
181-
//IndexCatalogImpl::init
181+
//IndexCatalogImpl::init 获取该表下面的所有索引信息
182182
_indexCatalog.init(opCtx).transitional_ignore();
183183
if (isCapped())
184184
_recordStore->setCappedCallback(this);
185185

186186
//CollectionInfoCacheImpl::init
187+
//初始化该集合的planCache IndexEntry
187188
_infoCache.init(opCtx);
188189
}
189190

@@ -207,6 +208,7 @@ CollectionImpl::~CollectionImpl() {
207208
_magic = 0;
208209
}
209210

211+
//除了system.indexes system.namespaces system.profile外的其他表都需要id索引
210212
bool CollectionImpl::requiresIdIndex() const {
211213
if (_ns.isVirtualized() || _ns.isOplog()) {
212214
// No indexes on virtual collections or the oplog.
@@ -223,6 +225,7 @@ bool CollectionImpl::requiresIdIndex() const {
223225
return true;
224226
}
225227

228+
//获取SeekableRecordCursor游标
226229
std::unique_ptr<SeekableRecordCursor> CollectionImpl::getCursor(OperationContext* opCtx,
227230
bool forward) const {
228231
dassert(opCtx->lockState()->isCollectionLockedForMode(ns().toString(), MODE_IS));
@@ -726,6 +729,7 @@ RecordId CollectionImpl::updateDocument(OperationContext* opCtx,
726729
Status updateStatus = _recordStore->updateRecord(
727730
opCtx, oldLocation, newDoc.objdata(), newDoc.objsize(), _enforceQuota(enforceQuota), this);
728731

732+
//mmap才会有该错误状态码
729733
if (updateStatus == ErrorCodes::NeedsDocumentMove) {
730734
return uassertStatusOK(_updateDocumentWithMove(
731735
opCtx, oldLocation, oldDoc, newDoc, enforceQuota, opDebug, args, sid));
@@ -758,6 +762,7 @@ RecordId CollectionImpl::updateDocument(OperationContext* opCtx,
758762
return {oldLocation};
759763
}
760764

765+
//mmap才会用到
761766
StatusWith<RecordId> CollectionImpl::_updateDocumentWithMove(OperationContext* opCtx,
762767
const RecordId& oldLocation,
763768
const Snapshotted<BSONObj>& oldDoc,
@@ -853,6 +858,7 @@ StatusWith<RecordData> CollectionImpl::updateDocumentWithDamages(
853858
return newRecStatus;
854859
}
855860

861+
//针对mmapv1引擎,忽略
856862
bool CollectionImpl::_enforceQuota(bool userEnforeQuota) const {
857863
if (!userEnforeQuota)
858864
return false;
@@ -869,6 +875,7 @@ bool CollectionImpl::_enforceQuota(bool userEnforeQuota) const {
869875
return true;
870876
}
871877

878+
//是否固定集合
872879
bool CollectionImpl::isCapped() const {
873880
return _cappedNotifier.get();
874881
}
@@ -918,7 +925,9 @@ uint64_t CollectionImpl::getIndexSize(OperationContext* opCtx, BSONObjBuilder* d
918925
* 2) drop indexes
919926
* 3) truncate record store
920927
* 4) re-write indexes
921-
*/ //drop表数据,但是不drop索引
928+
*/
929+
//EmptyCapped::run->mongo::emptyCapped调用
930+
//drop表数据,但是不drop索引
922931
Status CollectionImpl::truncate(OperationContext* opCtx) {
923932
dassert(opCtx->lockState()->isCollectionLockedForMode(ns().toString(), MODE_X));
924933
BackgroundOperation::assertNoBgOpInProgForNs(ns());
@@ -953,6 +962,7 @@ Status CollectionImpl::truncate(OperationContext* opCtx) {
953962
return Status::OK();
954963
}
955964

965+
//captrunc::run命令调用,测试使用
956966
void CollectionImpl::cappedTruncateAfter(OperationContext* opCtx, RecordId end, bool inclusive) {
957967
dassert(opCtx->lockState()->isCollectionLockedForMode(ns().toString(), MODE_X));
958968
invariant(isCapped());
@@ -1135,6 +1145,7 @@ namespace {
11351145

11361146
using ValidateResultsMap = std::map<std::string, ValidateResults>;
11371147

1148+
//CollectionImpl::validate调用
11381149
void _validateRecordStore(OperationContext* opCtx,
11391150
RecordStore* recordStore,
11401151
ValidateCmdLevel level,
@@ -1155,6 +1166,7 @@ void _validateRecordStore(OperationContext* opCtx,
11551166
}
11561167
}
11571168

1169+
//CollectionImpl::validate调用
11581170
void _validateIndexes(OperationContext* opCtx,
11591171
IndexCatalog* indexCatalog,
11601172
BSONObjBuilder* keysPerIndex,
@@ -1206,6 +1218,7 @@ void _validateIndexes(OperationContext* opCtx,
12061218
}
12071219
}
12081220

1221+
//CollectionImpl::validate调用
12091222
void _markIndexEntriesInvalid(ValidateResultsMap* indexNsResultsMap, ValidateResults* results) {
12101223

12111224
// The error message can't be more specific because even though the index is
@@ -1221,6 +1234,7 @@ void _markIndexEntriesInvalid(ValidateResultsMap* indexNsResultsMap, ValidateRes
12211234
results->valid = false;
12221235
}
12231236

1237+
//CollectionImpl::validate调用
12241238
void _validateIndexKeyCount(OperationContext* opCtx,
12251239
IndexCatalog* indexCatalog,
12261240
RecordStore* recordStore,
@@ -1239,6 +1253,7 @@ void _validateIndexKeyCount(OperationContext* opCtx,
12391253
}
12401254
}
12411255

1256+
//CollectionImpl::validate调用
12421257
void _reportValidationResults(OperationContext* opCtx,
12431258
IndexCatalog* indexCatalog,
12441259
ValidateResultsMap* indexNsResultsMap,
@@ -1286,6 +1301,9 @@ void _reportValidationResults(OperationContext* opCtx,
12861301
}
12871302
} // namespace
12881303

1304+
//db.xx.validate()
1305+
//https://docs.mongodb.com/v3.0/reference/command/validate/index.html
1306+
//该命令验证表中数据结构是否符合要求,因为建表的时候可能指定了validator
12891307
Status CollectionImpl::validate(OperationContext* opCtx,
12901308
ValidateCmdLevel level,
12911309
bool background,
@@ -1351,6 +1369,10 @@ Status CollectionImpl::validate(OperationContext* opCtx,
13511369
return Status::OK();
13521370
}
13531371

1372+
//touch::run touch命令调用
1373+
//db.runCommand({ touch: "records", data: true, index: true })
1374+
//加载表的数据和索引到内存
1375+
//参考https://docs.mongodb.com/v3.0/reference/command/touch/index.html
13541376
Status CollectionImpl::touch(OperationContext* opCtx,
13551377
bool touchData,
13561378
bool touchIndexes,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ void CollectionInfoCacheImpl::updatePlanCacheIndexEntries(OperationContext* opCt
218218
//获取索引信息
219219
const IndexDescriptor* desc = ii.next();
220220
const IndexCatalogEntry* ice = ii.catalogEntry(desc);
221+
//完成IndexEntry和IndexDescriptor的转换
221222
indexEntries.emplace_back(desc->keyPattern(),
222223
desc->getAccessMethodName(),
223224
desc->isMultikey(opCtx),
@@ -235,23 +236,27 @@ void CollectionInfoCacheImpl::updatePlanCacheIndexEntries(OperationContext* opCt
235236
}
236237

237238
//CollectionImpl::init中调用
239+
//初始化该集合的planCache IndexEntry
238240
void CollectionInfoCacheImpl::init(OperationContext* opCtx) {
239241
// Requires exclusive collection lock.
240242
invariant(opCtx->lockState()->isCollectionLockedForMode(_collection->ns().ns(), MODE_X));
241243

242244
const bool includeUnfinishedIndexes = false;
243245
IndexCatalog::IndexIterator ii =
244246
_collection->getIndexCatalog()->getIndexIterator(opCtx, includeUnfinishedIndexes);
245-
while (ii.more()) {
247+
//遍历该表的所有索引信息注册到_indexUsageTracker
248+
while (ii.more()) {
246249
const IndexDescriptor* desc = ii.next();
247250
_indexUsageTracker.registerIndex(desc->indexName(), desc->keyPattern());
248251
}
249252

253+
//重新生产索引data 重新生成planCache的IndexEntry
250254
rebuildIndexData(opCtx);
251255
}
252256

253257
//通过类似_collection->infoCache()->addedIndex(_opCtx, descriptorPtr);方式调用,参考IndexCatalogImpl::IndexBuildBlock::init()
254258
//IndexCatalogImpl::IndexBuildBlock::init()调用
259+
//新索引注册到_indexUsageTracker
255260
void CollectionInfoCacheImpl::addedIndex(OperationContext* opCtx, const IndexDescriptor* desc) {
256261
// Requires exclusive collection lock.
257262
invariant(opCtx->lockState()->isCollectionLockedForMode(_collection->ns().ns(), MODE_X));
@@ -263,6 +268,7 @@ void CollectionInfoCacheImpl::addedIndex(OperationContext* opCtx, const IndexDes
263268
}
264269

265270
//IndexCatalogImpl::_dropIndex调用
271+
//从_indexUsageTracker中注销索引
266272
void CollectionInfoCacheImpl::droppedIndex(OperationContext* opCtx, StringData indexName) {
267273
// Requires exclusive collection lock.
268274
invariant(opCtx->lockState()->isCollectionLockedForMode(_collection->ns().ns(), MODE_X));
@@ -273,6 +279,7 @@ void CollectionInfoCacheImpl::droppedIndex(OperationContext* opCtx, StringData i
273279

274280
//CollectionInfoCacheImpl::init CollectionInfoCacheImpl::addedIndex
275281
//CollectionInfoCacheImpl::droppedIndex调用
282+
//重新生成planCache的IndexEntry
276283
void CollectionInfoCacheImpl::rebuildIndexData(OperationContext* opCtx) {
277284
//清除缓存的querycache
278285
clearQueryCache();

mongo/src/mongo/db/catalog/collection_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ struct CollectionOptions {
111111
// Collection UUID. Will exist if featureCompatibilityVersion >= 3.6.
112112
OptionalCollectionUUID uuid; //每个collection会对应一个uuid,见DatabaseImpl::createCollection,只有3.6以上版本有该uuid
113113
//说明是MongoDB固定集合(capped collection)
114+
//可以通过下面的方式创建固定集合
115+
//db.createCollection("xx",{capped:true, size: 1000000})
114116
bool capped = false;
115117
long long cappedSize = 0;
116118
long long cappedMaxDocs = 0;

0 commit comments

Comments
 (0)