Skip to content

Commit 2c595f0

Browse files
committed
kv analyze
1 parent 96f8f2f commit 2c595f0

9 files changed

+46
-6
lines changed

mongo/src/mongo/db/commands/dbcommands.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ class CmdRepairDatabase : public ErrmsgCommandDeprecated {
235235

236236
CmdRepairDatabase() : ErrmsgCommandDeprecated("repairDatabase") {}
237237

238+
//CmdRepairDatabase::errmsgRun
238239
bool errmsgRun(OperationContext* opCtx,
239240
const string& dbname,
240241
const BSONObj& cmdObj,

mongo/src/mongo/db/commands/list_databases.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class CmdListDatabases : public BasicCommand {
8080
out->push_back(Privilege(ResourcePattern::forClusterResource(), actions));
8181
}
8282

83+
//db.runCommand({ listDatabases : 1 })
8384
CmdListDatabases() : BasicCommand("listDatabases") {}
8485

8586
bool run(OperationContext* opCtx,

mongo/src/mongo/db/repair_database.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ Status rebuildIndexesOnCollection(OperationContext* opCtx,
230230
return Status::OK();
231231
}
232232

233+
////CmdRepairDatabase::errmsgRun
233234
Status repairDatabase(OperationContext* opCtx,
234235
StorageEngine* engine,
235236
const std::string& dbName,

mongo/src/mongo/db/storage/kv/kv_database_catalog_entry_base.cpp

+31-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ bool KVDatabaseCatalogEntryBase::hasUserData() const {
149149
return !isEmpty();
150150
}
151151

152+
//db.runCommand({ listDatabases : 1 })获取所有库的磁盘信息
153+
/*
154+
> db.runCommand({ listDatabases : 1 })
155+
{
156+
"databases" : [
157+
{
158+
"name" : "admin",
159+
"sizeOnDisk" : 32768,
160+
"empty" : false
161+
},
162+
{
163+
"name" : "config",
164+
"sizeOnDisk" : 73728,
165+
"empty" : false
166+
},
167+
{
168+
"name" : "local",
169+
"sizeOnDisk" : 77824,
170+
"empty" : false
171+
},
172+
{
173+
"name" : "test",
174+
"sizeOnDisk" : 90112,
175+
"empty" : false
176+
}
177+
],
178+
"totalSize" : 274432,
179+
"ok" : 1
180+
}
181+
*/
152182
//磁盘数据大小=所有表的数据+所有表的索引总和
153183
int64_t KVDatabaseCatalogEntryBase::sizeOnDisk(OperationContext* opCtx) const {
154184
int64_t size = 0;
@@ -252,7 +282,7 @@ Status KVDatabaseCatalogEntryBase::createCollection(OperationContext* opCtx,
252282

253283
//WiredTigerKVEngine::createGroupedRecordStore(数据文件相关)
254284
//WiredTigerKVEngine::createGroupedSortedDataInterface(索引文件相关)
255-
//调用WT存储引擎的create接口建表
285+
//调用WT存储引擎的create接口建表,底层建索引表
256286
status = _engine->getEngine()->createGroupedRecordStore(opCtx, ns, ident, options, prefix);
257287
if (!status.isOK())
258288
return status;

mongo/src/mongo/db/storage/kv/kv_prefix.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ std::string KVPrefix::toString() const {
6666
return generateNextPrefix();
6767
}
6868

69+
//groupCollection才会生效,参考KVPrefix KVPrefix::getNextPrefix
6970
/* static */ KVPrefix KVPrefix::generateNextPrefix() {
7071
stdx::lock_guard<stdx::mutex> lk(_nextValueMutex);
7172
return KVPrefix(_nextValue++);

mongo/src/mongo/db/storage/kv/kv_prefix.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace mongo {
4141
* such allows multiple MongoDB collections share an underlying table. This can be a beneficial
4242
* tradeoff for workloads that create many collections.
4343
*/
44-
//IndexCatalogEntryImpl._prefixΪ¸ÃÀàÐÍ
44+
//IndexCatalogEntryImpl._prefix为该类型 只有groupCollection才会生效,参考KVPrefix::getNextPrefix
4545
class KVPrefix {
4646
public:
4747
// Represents a table that is not grouped and should not have its keys prefixed.

mongo/src/mongo/db/storage/kv/kv_storage_engine.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ KVStorageEngine::KVStorageEngine(
194194
会有一份对应的WiredTiger需要的元数据维护在WiredTiger.wt表中。因此,事实上这里有两份数据表的列表,
195195
并且在某些情况下可能会存在不一致,比如,异常宕机的场景。因此MongoDB在启动过程中,会对这两份数据
196196
进行一致性检查,如果是异常宕机启动过程,会以WiredTiger.wt表中的数据为准,对_mdb_catalog表中的记录进行修正。这个过程会需要遍历WiredTiger.wt表得到所有数据表的列表。
197-
???? 这里说法有误,应该是以_mdb_catalog.wt为准--yangyazhou todo xxx
197+
???? 这里说法有误,应该是以_mdb_catalog.wt为准--yangyazhou todo xxx 感觉有点问题
198198
199199
综上,可以看到,在MongoDB启动过程中,有多处涉及到需要从WiredTiger.wt表中读取数据表的元数据。
200200
对这种需求,WiredTiger专门提供了一类特殊的『metadata』类型的cursor。
@@ -240,7 +240,7 @@ StatusWith<std::vector<StorageEngine::CollectionIndexNamePair>>
240240
// Drop all idents in the storage engine that are not known to the catalog. This can happen in
241241
// the case of a collection or index creation being rolled back.
242242

243-
//把WiredTiger.wt中有,但是_mdb_catalog.wt中没用的元数据信息清除
243+
//把WiredTiger.wt中有,但是_mdb_catalog.wt中没有的元数据信息清除
244244
for (const auto& it : engineIdents) {
245245
log() << "yang test ....reconcileCatalogAndIdents...... ident: " << it;
246246
//找到了相同的ident数据目录文件,继续下一个
@@ -256,6 +256,7 @@ StatusWith<std::vector<StorageEngine::CollectionIndexNamePair>>
256256
const auto& toRemove = it;
257257
log() << "Dropping unknown ident: " << toRemove;
258258
WriteUnitOfWork wuow(opCtx);
259+
//WiredTigerKVEngine::dropIdent 删除对应ident文件
259260
fassertStatusOK(40591, _engine->dropIdent(opCtx, toRemove));
260261
wuow.commit();
261262
}
@@ -302,6 +303,8 @@ StatusWith<std::vector<StorageEngine::CollectionIndexNamePair>>
302303
return ret;
303304
}
304305

306+
//ServiceContextMongoD::shutdownGlobalStorageEngineCleanly()调用
307+
//shutdown回收处理
305308
void KVStorageEngine::cleanShutdown() {
306309
for (DBMap::const_iterator it = _dbs.begin(); it != _dbs.end(); ++it) {
307310
delete it->second;
@@ -405,6 +408,7 @@ Status KVStorageEngine::dropDatabase(OperationContext* opCtx, StringData db) {
405408
return Status::OK();
406409
}
407410

411+
//FSyncLockThread::run()调用 db.adminCommand( { fsync: 1, lock: true } )
408412
int KVStorageEngine::flushAllFiles(OperationContext* opCtx, bool sync) {
409413
return _engine->flushAllFiles(opCtx, sync);
410414
}
@@ -444,6 +448,7 @@ SnapshotManager* KVStorageEngine::getSnapshotManager() const {
444448
return _engine->getSnapshotManager();
445449
}
446450

451+
////CmdRepairDatabase::errmsgRun
447452
Status KVStorageEngine::repairRecordStore(OperationContext* opCtx, const std::string& ns) {
448453
Status status = _engine->repairIdent(opCtx, _catalog->getCollectionIdent(ns));
449454
if (!status.isOK())
@@ -453,6 +458,7 @@ Status KVStorageEngine::repairRecordStore(OperationContext* opCtx, const std::st
453458
return Status::OK();
454459
}
455460

461+
//ReplicationCoordinatorExternalStateImpl::startThreads
456462
void KVStorageEngine::setJournalListener(JournalListener* jl) {
457463
_engine->setJournalListener(jl);
458464
}

mongo/src/mongo/db/storage/kv/kv_storage_engine.h

+1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ class KVStorageEngine final : public StorageEngine {
334334
//对应数据目录的"_mdb_catalog.wt"相关操作 _mdb_catalog.wt存储元数据信息
335335
std::unique_ptr<KVCatalog> _catalog;
336336

337+
//库名及其对应KVDatabaseCatalogEntryBase
337338
typedef std::map<std::string, KVDatabaseCatalogEntryBase*> DBMap;
338339
//DatabaseHolderImpl.dbs[]和KVStorageEngine._dbs[]的区别及联系:
339340
//1. DatabaseHolderImpl.dbs[]包含实例启动后使用过或者正在使用的DB信息

mongo/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ Breakpoint 2, mongo::WiredTigerKVEngine::createGroupedSortedDataInterface (this=
998998
//DatabaseImpl::createCollection->IndexCatalogImpl::createIndexOnEmptyCollection->IndexCatalogImpl::IndexBuildBlock::init
999999
//->KVCollectionCatalogEntry::prepareForIndexBuild中执行
10001000

1001-
//KVCollectionCatalogEntry::prepareForIndexBuild调用,创建索引对应wt session信息
1001+
//KVCollectionCatalogEntry::prepareForIndexBuild调用,创建索引对应wt session信息,底层建索引表
10021002
Status WiredTigerKVEngine::createGroupedSortedDataInterface(OperationContext* opCtx,
10031003
StringData ident,
10041004
const IndexDescriptor* desc,
@@ -1220,7 +1220,6 @@ bool WiredTigerKVEngine::_hasUri(WT_SESSION* session, const std::string& uri) co
12201220
并且在某些情况下可能会存在不一致,比如,异常宕机的场景。因此MongoDB在启动过程中,会对这两份数据
12211221
进行一致性检查,如果是异常宕机启动过程,会以WiredTiger.wt表中的数据为准,对_mdb_catalog表中的记录
12221222
进行修正。这个过程会需要遍历WiredTiger.wt表得到所有数据表的列表。
1223-
???? 这里说法有误,表应该是以_mdb_catalog.wt为准 yangyazhou todo xxx
12241223
12251224
综上,可以看到,在MongoDB启动过程中,有多处涉及到需要从WiredTiger.wt表中读取数据表的元数据。
12261225
对这种需求,WiredTiger专门提供了一类特殊的『metadata』类型的cursor。

0 commit comments

Comments
 (0)