2020-07-31 13:21:15
1. mongos节点 收到用户 insert 请求
mongos 收到一个 insert 请求后,会经过如下流程处理将请求发送给 shard 节点:
ServiceEntryPointMongos::handleRequest() --> Strategy::clientCommand() --> runCommand() --> execCommandClient() --> ClusterWriteCmd::InvocationBase::run() --> ClusterWriteCmd::InvocationBase::runImpl() --> ClusterWriter::write() --> BatchWriteExec::executeBatch() --> MultiStatementTransactionRequestsSender()
在发送给 shard 节点的请求中,会通过 allowImplicitCollectionCreation
字段来控制是否允许 shard 节点隐式创建collection。sharding状态下,该字段基本为 false,即不允许 shard 节点隐式创建collection
- 对于
insert
/update
/delete
这三类请求,会在ClusterWriteCmd::InvocationBase::runImpl()
中进行batchedRequest.setAllowImplicitCreate(false);
src/mongo/s/cluster_commands_helpers.cpp
中也提供了scatterGatherVersionedTargetByRoutingTable
/scatterGatherUnversionedTargetAllShards
/scatterGatherOnlyVersionIfUnsharded
/executeCommandAgainstDatabasePrimary
函数。如果请求包中不显式声明allowImplicitCollectionCreation
,则默认为false
- 但是 renameCollection 及 covertToCapped 会将
appendAllowImplicitCreate
声明为true
(可以隐式创建collection)
- 但是 renameCollection 及 covertToCapped 会将
shard节点可以直接创建collection,即为隐式创建collection(rs实例均采用这种方式)。否则需要和 config 交互来完成 collection 的创建,即为显式创建collection
2. shard节点 收到 mongos节点 转发过来携带 allowImplicitCollectionCreation=false
的 insert 请求
正常情况下, shard节点 按照如下流程处理 insert 请求:
ServiceEntryPointMongod::handleRequest() --> ServiceEntryPointCommon::handleRequest() --> receivedCommands() --> execCommandDatabase() --> runCommandImpl() --> WriteCommand::InvocationBase::run() --> CmdInsert::Invocation::runImpl() --> performInserts() --> insertBatchAndHandleErrors() --> acquireCollecti