SlideShare a Scribd company logo
⼀一个 MongoDB
Command 的前世今⽣生
庄晓丹丹@LeanCloud

2017.06.23
插⼊入⼀一个⽂文档,中间了了发⽣生了了什什么?
• 测试代码: MongodbTest.java
Java Driver
• MongoClient 建⽴立和管理理连接

• Cluster

• SingleServerCluster

• MultiServerCluster

• Server

• DefaultServer

• MongoDatabase 和 MongoCollection 指向 DB 和 collection

• 元信息和配置保存

• Document : Binary JSON 

• insertOne 发⽣生了了什什么?
insertOne(doc)
Request-Response Style
• Mongodb Wire Protocol

• sendMessage

• receiveMessage

• WriteCommandProtocol#execute
Driver ⼩小结
Server
• https://github.com/mongodb/mongo

• cloc 源码统计 src/mongo:
⽹网络和业务框架
• src/mongo/transport, src/mongo/util/net

• TransportLayer

• Listener: 处理理链接接⼊入, select 原语

• MessagingPort: ⽹网络链接,封装 socket

• ServiceEntryPoint :业务处理理

• Session:链接会话,封装 Connection -> MessagingPort

• 直接⼦子类 ServiceEntryPointImpl 和实现类:

• Mongod: Mongodb 数据库服务器器 

• src/mongo/db/service_entry_point_mongod.cpp

• Mongos: MongoDB 分⽚片路路由服务

• src/mongo/s/service_entry_point_mongos.cpp
一个 Mongodb command 的前世今生
链接接⼊入
• Listener::initAndListen

• Listener::accepted(AbstractMessagingPort)

• TransportLayerLegacy::_handleNewConnection(AbstractMessaging
Port)

• ServiceEntryPoint::startSession(session)

• ServiceEntryPoint::launchWrappedServiceEntryWorkerThread

• ServiceEntryPoint::_sessionLoop(session)

• ServiceEntryPoint::handleRequest(context, message,
remote)
请求处理理
• ServiceEntryPointImpl::_sessionLoop

• sesson->sourceMessage(inMsg)

• dbresponse = this->handleRequest(ctx, inMsg,
remote)

• session->sinkMessage(dbresponse.response)
可插拔的存储引擎设计
• src/mongo/db/storage

• src/mongo/db/wiredtiger

• src/mongo/db/mmap_v1

• StorageEngine: the StorageEngine class is the top level
interface for creating a new storage engine.

• RecordStore: a RecordStore provides an abstraction used
for storing documents in a collection,
数据库服务器器框架
• ⼊入⼝口:ServiceEntryPointMongod 

• 命令处理理: assembly_response.cpp 

• assembleResponse(ctx, msg, remote)

• 命令管理理:  commands.cpp,查表法

• static CommandMap* _commands;

• 操作: src/mongo/db/ops

• 数据库模型:src/mongo/db/catalog

• 数据库管理理:DatabaseHolder

• typedef StringMap<Database*> DBs;

• DBs _dbs;

• 数据库:Database

• typedef StringMap<Collection*> CollectionMap;

• CollectionMap _collections;

• Collection:Collection

• RecordStore* const _recordStore;
一个 Mongodb command 的前世今生
插⼊入命令处理理
• ServiceEntryPointMongod::handleRequest

• assembleResponse(ctx, request, client)

• runCommands(ctx, req, rb)

• WriteCommand:run(ctx, dbname, obj, err, result)

• CmdInsert::runImpl(ctx, dbname, obj, err, result)

• performInserts(ctx, InsertOp)

• insertBatchAndHandleErrors

• CollectionImpl::_insertDocuments

• _recordStore->insertRecords
MMapV1 存储引擎
• src/mongo/db/storage/mmap_v1

• 每个 db:

• ⼀一个namespace⽂文件 xxx.ns

• 多个数据⽂文件 xxx.0, xxx.1 …
数据组织
来源:http://www.mongoing.com/archives/1484
更更多
• 索引管理理

• WiredTiger 引擎

• 复制

• 分⽚片

• ……

More Related Content

PDF
Mesos intro
PPTX
QCon - 一次 Clojure Web 编程实战
PDF
Hacking Nginx at Taobao
PPTX
快速入坑 Node.js - 0613 SITCON 雲林定期聚
PPTX
RxJS 6 新手入門
PDF
基于OpenResty的百万级长连接推送
PDF
Bypat博客出品-服务器运维集群方法总结
PDF
Bypat博客出品-服务器运维集群方法总结2
Mesos intro
QCon - 一次 Clojure Web 编程实战
Hacking Nginx at Taobao
快速入坑 Node.js - 0613 SITCON 雲林定期聚
RxJS 6 新手入門
基于OpenResty的百万级长连接推送
Bypat博客出品-服务器运维集群方法总结
Bypat博客出品-服务器运维集群方法总结2

What's hot (20)

PDF
阿里CDN技术揭秘
PDF
移动搜索在慢网速下的优化
PDF
Bypat博客出品-服务器运维集群方法总结3
PPTX
静态项目管理工具 - opm
PPT
高性能并发Web服务器实现核心内幕
PPTX
Maven & mongo & sring
PPTX
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
PDF
Kubernetes use-ceph
PDF
主库自动切换 V2.0
PPTX
Ch03 請求與回應
PPT
Database.Cache&Buffer&Lock
PDF
构建基于Lamp的网站架构
PPTX
浅谈电商网站数据访问层(DAL)与 ORM 之适用性
PPTX
如何针对业务做DB优化
PDF
Ceph intro
PPTX
鳳山高中/網頁應用程式開發入門 I
PDF
1号店数据库架构
PDF
豆瓣网技术架构变迁
PDF
淘宝主备数据库自动切换
PPTX
Full stack-development with node js
阿里CDN技术揭秘
移动搜索在慢网速下的优化
Bypat博客出品-服务器运维集群方法总结3
静态项目管理工具 - opm
高性能并发Web服务器实现核心内幕
Maven & mongo & sring
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
Kubernetes use-ceph
主库自动切换 V2.0
Ch03 請求與回應
Database.Cache&Buffer&Lock
构建基于Lamp的网站架构
浅谈电商网站数据访问层(DAL)与 ORM 之适用性
如何针对业务做DB优化
Ceph intro
鳳山高中/網頁應用程式開發入門 I
1号店数据库架构
豆瓣网技术架构变迁
淘宝主备数据库自动切换
Full stack-development with node js
Ad

More from dennis zhuang (17)

PDF
Erlang scheduler
PDF
Java 与 CPU 高速缓存
PDF
Clojure 1.8 Direct-Linking WWH
PDF
Elixir introd
PPTX
CQL 实现
PPTX
我在 Mac 上的常用开发工具
PDF
Hystrix 介绍
PDF
AVOSCloud简介——万象移动云平台
PDF
Avoscloud 2
PPTX
点评新架构
PPTX
Clojure概览
ODP
Ihome inaction 篇外篇之fp介绍
PPTX
Nio trick and trap
PPTX
Aviator——轻量级表达式执行引擎
PPTX
Clojure的魅力
PDF
Erlang简介
PPTX
Java多线程常见陷阱
Erlang scheduler
Java 与 CPU 高速缓存
Clojure 1.8 Direct-Linking WWH
Elixir introd
CQL 实现
我在 Mac 上的常用开发工具
Hystrix 介绍
AVOSCloud简介——万象移动云平台
Avoscloud 2
点评新架构
Clojure概览
Ihome inaction 篇外篇之fp介绍
Nio trick and trap
Aviator——轻量级表达式执行引擎
Clojure的魅力
Erlang简介
Java多线程常见陷阱
Ad

一个 Mongodb command 的前世今生