MongoDB 使用的一些经验


这是一篇关于 MongoDB 使用经验的一篇文章,MongoDB 相对于 MySQL 简单很多,关于 MySQL 的调优可以看另一篇博文: MYSQL 调优和使用必读



MongoDB 的单进程,多线程模型

读操作可以使用多线程,利用多核心;写操作(Global Locking)和 Map-reduce(JS 解释器的限制)只能使用单线程。

从2.2 版本,MongoDB 部分解决了全局锁问题,可以在写某个库的时候同时写其他库。一般通过在多核心单机上 Sharding 数据库,使用多个 mongod instance, 通过利用多核和缓解全局锁的问题提高读写操作 ops。可以通过 mongostat 命令查看 locking 和 page fault 情况。

另外,使用 htop 可以看到一个进程中的多个线程。Node.js 每个进程其实有两个线程,除了主线程外,还有一个线程池用来处理文件读写等操作。

充分利用 MongoDB 的 oplog

MongoDB 通过 oplog 实现主从同步,但是即使不启用从库,也可以查看和使用 oplog。修改配置文件打开 oplog:

/etc/mongod.conf
# Replication Options
master = true

这样就会在 local database 出现一个名为 oplog.$main 的 collection,(一般 MongoDB 会将这个 collection 中的数据条数保持在 5000 万以下),其中数据类似于:

{
   "ts": {
     "t": 1000,
     "i": 1365409034
  },
   "op": "u",
   "ns": "mydb.mycoll",
   "o2": {
     "_id": ObjectId("50a6718e50e50b4459dcc40e")
  },
   "o": {
     "$set": {
       "myfield": "myfield_value"
    }
  }
}

ts 为自定义的时间戳
op 表示操作类型: insert (i), update (u), delete (d), noop (n)
ns 为操作对应的 collection
o 为操作数据,这里为 $set 操作修改记录

MongoDB 的 MMAP 内存模型

MMAP 的一个缺点很多,比如当读取数据没有在内存中,操作遇到 page fault 的时候也会发生锁操作。

MongoDB 的索引

MongoDB 支持简单的 B-Tree 索引。默认情况下 _id 会自动建索引,如果需要查询其他字段可以自己手动建索引(ensureIndex() )。另外,注意在数据导入导出的时候重建索引。
比 MySQL 好的是 MongoDB 支持多值索引,即使两个字段的顺序是相反的。比如可以支持 .sort({a:1, b:-1}) 这样按不同字段的排序。和 MySQL 类似,使用的时候同样需要避免类似于 skip(BIG_NUM).limit(N) 这样的操作。

MongoDB 需要注意的几点

1. 对线上库的批量操作要控制频率

假如某些读写操作不断占用数据库资源,其他操作将不能很快或者正确完成。可以通过 sleep 操作降低批量操作的频率,为其他操作提供执行空隙。

2. 实时将数据同步到关系型数据库支持复杂查询和数据分析

NoSQL 不支持复杂查询,但是如果需要复杂查询和数据分析,可以将数据同步到关系型数据库中。

MongoDB 原生支持 Streaming,以下 Node.js 代码可以实时获取某个 collection 的数据变化,可以同步到关系型数据库,也可以用来做 Trigger。以下是 MognoDB tail oplog 的核心代码(Node.js):

var options = {
  'ns': self.config.mongodb.db + '.' + self.config.mongodb.collection,
  'ts': {'$gt': new mongo.Timestamp.fromNumber(this.last_timestamp)}
};

var stream = this.mongo.db.collection('oplog.$main')
    .find(options, {tailable: true, awaitdata: true, numberOfRetries: -1}).stream();

stream.on('data', function(item) {
  if (item.op !== 'n' && item.ts.toNumber() !== self.last_timestamp) {
    console.log(adate() + ' ' + JSON.stringify(item)+'\r\n');
    self.process(item, function() {
    });
  }
});

注意读写压力很大的情况下控制 streaming 的速度,具体情况可以见前一篇博文: Node.js 调试 GC 以及内存暴涨的分析。相关代码:

  var stream = this.mongo.db2.collection(self.config.mongodb.collection).find().stream();
  stream.on("data", function(item) {
    stream.pause();
    //console.log(JSON.stringify(item)+'\r\n');
    self.mysql.insert(item, function() {
      stream.resume();
    });
  });

3. 需要为 MongoDB 提供足够的内存空间

如果数据库的数据没有冷热之分,最好配置大于数据大小的内存,防止频繁磁盘操作。
通过将数据记录的键值改短也能明显节约空间。

4. MongoDB 默认操作的异步特性

MongoDB 写操作默认情况下是异步的,所以为了保持一致性,需要加上选项:

{
  safe: {
    fsync: true
  }
}

总之

MongoDB 是一个非常易用,优点和缺点都很明显的数据库。在某些场景下,可以考虑使用 TC,Redis 或者 Postgres,MySQL 替代。

一些参考

http://docs.mongodb.org/manual/faq/concurrency/
https://github.com/mongodb/mongo/blob/master/src/mongo/db/btree.cpp
http://blog.schmichael.com/2011/11/05/failing-with-mongodb/
http://en.wikipedia.org/wiki/Mmap
http://www.polyspot.com/en/blog/2012/understanding-mongodb-storage/

Node.JS

最近做的服务器端组件大部分都在使用 Node.js 。因为 Node.js 库管理模式比较先进,并且依托于 Github 的流行,Node.js 开源的库非常多,一般所需要的第三方库都可以找到。虽然这些库有很多明显的 Bug 但是比从零自己开发要快很多。对于服务器端开发,Node.js 还是个不错的选择,不像 Erlang 更接近底层,业务层面的库相对要少很多。



最近写的一个功能在本地开发的时候没有明显问题,但是到真实环境测试的时候发现内存不断增长,并且增长很快,同时 CPU 占用也很高,接近单核心的 100% 。这对于一个大部分都是 IO 操作的进程显然是有问题的。所以尝试分析内存和 CPU 异常的原因。最终发现是因为生产者和消费者速度差异引起的缓冲区暴增。在 MySQL 连接对象的 Queue 中积压了大量的 Query,而不是内存泄漏。

查看 Node.js 进程的 GC log:

node --trace_gc --trace_gc_verbose test.js
61 ms: Scavenge 2.2 (36.0) -> 1.9 (37.0) MB, 1 ms [Runtime::PerformGC].
Memory allocator,   used: 38780928, available: 1496334336
New space,          used:   257976, available:   790600
Old pointers,       used:  1556224, available:        0, waste:        0
Old data space,     used:  1223776, available:     4768, waste:        0
Code space,         used:  1019904, available:        0, waste:        0
Map space,          used:   131072, available:        0, waste:        0
Cell space,         used:    98304, available:        0, waste:        0
Large object space, used:        0, available: 1495269120
      97 ms: Mark-sweep 11.7 (46.1) -> 5.4 (40.7) MB, 10 ms [Runtime::PerformGC] [GC in old space requested].
Memory allocator,   used: 42717184, available: 1492398080
New space,          used:        0, available:  1048576
Old pointers,       used:  1390648, available:   165576, waste:        0
Old data space,     used:  1225920, available:     2624, waste:        0
Code space,         used:   518432, available:   501472, waste:        0
Map space,          used:    60144, available:    70928, waste:        0
Cell space,         used:    23840, available:    74464, waste:        0
Large object space, used:  3935872, available: 1491332864

关于 Node.js 的 GC

Node.js 的 GC 方式为分代 GC (Generational GC)。对象的生命周期由它的大小决定。对象首先进入占用空间很少的 new space (8MB)。大部分对象会很快失效,会频繁而且快速执行 Young GC (scavenging)*直接*回收这些少量内存。假如有些对象在一段时间内不能被回收,则进入 old space (64-128KB chunks of 8KB pages)。这个区域则执行不频繁的 Old GC/Full GC (mark-sweep, compact or not),并且耗时比较长。(Node.js 的 GC 有两类:Young GC: 频繁的小量的回收;Old GC: 长时间存在的数据)

Node.js 最新增量 GC 方式虽然不能降低总的 GC 时间,但是避免了过大的停顿,一般大停顿也限制在了几十 ms 。

为了减少 Full GC 的停顿,可以限制 new space 的大小

--max-new-space-size=1024 (单位为 KB)

手动在代码中操作 GC (不推荐)

node --expose-gc test.js

修改 Node.js 默认 heap 大小

node --max-old-space-size=2048 test.js (单位为 MB)

Dump 出 heap 的内容到 Chrome 分析:

安装库

https://github.com/bnoordhuis/node-heapdump

在应用的开始位置添加

var heapdump = require('heapdump');

在进程运行一小段时间后执行:

kill -USR2 <pid>

这时候就会在当前目录下生成 heapdump-xxxxxxx.heapsnapshoot 文件。
将这个文件 Down 下来,打开 Chrome 开发者工具中的 Profiles,将这个文件加载进去,就可以看到当前 Node.js heap 中的内容了。

可以看到有很多 MySQL 的 Query 堆积在处理队列中。内存暴涨的原因应该是 MySQL 的处理速度过慢,而 Query 产生速度过快。
所以解决方式很简单,降低 Query 的产生速度。内存暴涨还会引起 GC 持续执行,占用了大量 CPU 资源。

node-mysql 库中的相关代码,其实应该限制 _queue 的 size,size 过大则抛出异常或者阻塞,就不会将错误扩大。

Protocol.prototype._enqueue = function(sequence) {
  if (!this._validateEnqueue(sequence)) {
    return sequence;
  }

  this._queue.push(sequence);

  var self = this;
  sequence
    .on('error', function(err) {
      self._delegateError(err, sequence);
    })
    .on('packet', function(packet) {
      self._emitPacket(packet);
    })
    .on('end', function() {
      self._dequeue();
    });

  if (this._queue.length === 1) {
    this._parser.resetPacketNumber();
    sequence.start();
  }

  return sequence;
};

在不修改 node-mysql 的情况下,加入生产者和消费者的同步,调整之后,内存不再增长,一直保持在不到 100M 左右,CPU 也降低到 10% 左右。

Node.js 调试工具 node-inspector

安装:

npm install -g node-inspector

启动自己的程序:

node --debug test.js

node --debug-brk test.js (在代码第一行加断点)

启动调试器界面:

node-inspector

打开 http://localhost:8080/debug?port=5858 可以看到执行到第一行的断点。
右边为局部变量和全局变量、调用栈和常见的断点调试按钮,查看程序步进执行情况。并且你可以修改正在执行的代码,比如在关键的位置增加 console.log 打印信息。

Node.js 命令行调试工具

以 DEBUG 模式启动 Node.js 程序,类似于 GDB:

node debug test.js

debug> help
Commands: run (r), cont (c), next (n), step (s), out (o), backtrace (bt), setBreakpoint (sb), clearBreakpoint (cb),
watch, unwatch, watchers, repl, restart, kill, list, scripts, breakOnException, breakpoints, version

Node.js 其他常用命令参数

node --max-stack-size 设置栈大小

node --v8-options 打印 V8 相关命令

node --trace-opt test.js

node --trace-bailout test.js 查找不能被优化的函数,重写

node --trace-deopt test.js 查找不能优化的函数

Node.js 的 Profiling

V8 自带的 prof 功能:

npm install profiler

node --prof test.js

会在当前文件夹下生成 v8.log

安装 v8.log 转换工具

sudo npm install tick -g

在当前目录下执行

node-tick-processor v8.log

可以关注其中 Javascript 各个函数的消耗和 GC 部分

[JavaScript]:
ticks  total  nonlib   name
67   18.7%   20.1%  LazyCompile: *makeF /opt/data/app/test/test.js:6
62   17.3%   18.6%  Function: ~ /opt/data/app/test/test.js:9
42   11.7%   12.6%  Stub: FastNewClosureStub
38   10.6%   11.4%  LazyCompile: * /opt/data/app/test/test.js:1

[GC]:
ticks  total  nonlib   name
27    7.5%

参考以及一些有用的链接

https://bugzilla.mozilla.org/show_bug.cgi?id=634503
http://cs.au.dk/~jmi/VM/GC.pdf
http://lifecs.likai.org/2010/02/how-generational-garbage-collector.html
http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Na.C3.AFve_mark-and-sweep
http://en.wikipedia.org/wiki/Cheney’s_algorithm
https://github.com/bnoordhuis/node-heapdump
http://mrale.ph/blog/2011/12/18/v8-optimization-checklist.html
http://es5.github.com/
http://blog.caustik.com/2012/04/08/scaling-node-js-to-100k-concurrent-connections/
https://hacks.mozilla.org/2013/01/building-a-node-js-server-that-wont-melt-a-node-js-holiday-season-part-5/
https://gist.github.com/2000999
http://www.jiangmiao.org/blog/2247.html
http://blog.caustik.com/2012/04/08/scaling-node-js-to-100k-concurrent-connections/
http://blog.caustik.com/2012/04/11/escape-the-1-4gb-v8-heap-limit-in-node-js/
https://developers.google.com/v8/embed#handles
https://hacks.mozilla.org/2012/11/fully-loaded-node-a-node-js-holiday-season-part-2/
https://code.google.com/p/v8/wiki/V8Profiler