bigywater.tech
MongoDB · 83

serverStatus 里该盯的指标

serverStatus 输出几百个字段,上线以后到底该盯哪些?每个计数器的口径是什么?这篇每个指标做一个已知的操作,对着增量验证。

+1,000
insert_many 1,000 个文档在 opcounters.insert 里涨的值(记文档数)
49,891 → 499
加了索引后扫描的文档数(scannedObjects)
3 个
一个 pymongo 客户端对象占用的服务端连接数

opcounters

实测 · 本机执行输出
  做了:insert_many 1,000 个文档(1 次调用)、find_one 500 次、update_one 200 次、delete_one 100 次、ping 50 次
  opcounters 的增量: {'opcounters.insert': 1000, 'opcounters.query': 500, 'opcounters.update': 200, 'opcounters.delete': 100, 'opcounters.getmore': 0, 'opcounters.command': 51}
  连续两次 serverStatus,中间什么都不做: opcounters.command 增量 = 1(所以上面 51 = 50 次 ping + 最后那次 serverStatus 自己)
  insert 记的是文档数(insert_many 的 1 个命令记了 1,000)

计数器是累计值,要看增量;insert 记文档数,serverStatus 自己也会让 command 加 1。

metrics.document

实测 · 本机执行输出
  做了:find {k < 10}(1 次)、update_many {k: 1}(1 次)、delete_many {k: 2}(1 次)
  metrics.document 增量: {'metrics.document.returned': 90, 'metrics.document.inserted': 0, 'metrics.document.updated': 9, 'metrics.document.deleted': 9}
  opcounters 增量: {'opcounters.query': 1, 'opcounters.update': 1, 'opcounters.delete': 1}

metrics.document 是服务端处理的文档数(returned 90、updated 9、deleted 9),与 opcounters 的命令数不是一回事。

扫描量

实测 · 本机执行输出
  k = 7,没有 k 索引(全表扫描): 扫描索引键 0,扫描文档 49,891,返回 499
  k = 7,有 k 索引: 扫描索引键 499,扫描文档 499,返回 499
  只要 k(覆盖查询): 扫描索引键 499,扫描文档 0,返回 499

queryExecutor.scanned 是扫描的索引键、scannedObjects 是扫描的文档;加索引后从 49,891 个文档变成 499,覆盖查询为 0。

connections 与 network

实测 · 本机执行输出
  connections 基线: {'current': 5, 'available': 209710, 'totalCreated': 8, 'active': 4}
  再开 20 个客户端并各 ping 一次: {'current': 65, 'available': 209650, 'totalCreated': 68, 'active': 24}
  每个客户端对象占 3 个连接(一个操作连接 + 驱动的 2 个监控连接)
  这 20 个客户端 close 之后 0.5 秒: {'current': 24, 'available': 209691, 'totalCreated': 68, 'active': 23}
  close 之后 3.5 秒: {'current': 3, 'available': 209712, 'totalCreated': 68, 'active': 2}
  返回了 24,941 个文档:network.bytesOut 增加 3,550,481 字节,bytesIn 增加 324 字节,numRequests 增加 3

每个 pymongo 客户端对象占 3 个连接,close 之后连接释放不是立刻的;network.bytesOut 随返回数据量增长。

wiredTiger.cache

实测 · 本机执行输出
  基线: {'bytes currently in the cache': 8474078, 'maximum bytes configured': 268435456, 'tracked dirty bytes in the cache': 50345, 'pages read into cache': 41, 'bytes read into cache': 2565180, 'bytes written from cache': 7465170}
  写入 10 万个 1 KB 文档(约 100 MB)后: {'bytes currently in the cache': 60021792, 'maximum bytes configured': 268435456, 'tracked dirty bytes in the cache': 7805931, 'pages read into cache': 41, 'bytes read into cache': 2565180, 'bytes written from cache': 109527741}
  重启后(缓存是空的): {'bytes currently in the cache': 53515, 'maximum bytes configured': 268435456, 'tracked dirty bytes in the cache': 10652, 'pages read into cache': 21, 'bytes read into cache': 76900, 'bytes written from cache': 31214}
  读完 100,000 个文档后: {'bytes currently in the cache': 113895114, 'maximum bytes configured': 268435456, 'tracked dirty bytes in the cache': 10652, 'pages read into cache': 914, 'bytes read into cache': 104557054, 'bytes written from cache': 31214}
  pages read into cache 增加 893;bytes currently in the cache 增加 113,841,599 字节

写入后缓存涨到约 60 MB,重启后清空,读一遍后涨到约 114 MB;pages read into cache 反映从磁盘读入。

opLatencies

实测 · 本机执行输出
  reads:延迟增加 22,430 μs,操作数增加 2,000 → 平均 11.2 μs
  writes:延迟增加 35,340 μs,操作数增加 2,000 → 平均 17.7 μs
  commands:延迟增加 626 μs,操作数增加 1 → 平均 626.0 μs
  (做了 2,000 次 insert_one 和 2,000 次 find_one;commands 多出的 1 个操作是 b 那次 serverStatus 自己)

累计延迟除以操作数得平均延迟:reads 11.2 μs、writes 17.7 μs。

分步演示

serverStatus 指标
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / MongoDB 9.0.2(Homebrew 的 mongod、mongos)/ mongosh 2.12.0 / pymongo 4.18.3;每个实验在临时目录里起全新的 mongod(独立 dbpath,端口 9900 以上,只监听 127.0.0.1,WiredTiger 缓存 0.25 GB),需要副本集或分片集群时在本机起多个进程;命令输出来自真实的 mongosh,批量写入、计时与多线程用 pymongo;计时用 time.perf_counter,数字每次运行会有小幅抖动,ObjectId、时间戳等每次运行都不同。单机 mongod,wiredTigerCacheSizeGB = 0.25;每个指标先取 serverStatus 再做已知操作再取一次,看增量
    • 没有验证:“每个客户端 3 个连接”是 pymongo 连接池加监控连接的推断,没有逐个确认;副本集特有指标(repl、oplog 窗口);globalLock/queue 在高并发下的表现;内存(mem、tcmalloc)指标;ticket(见后面 WiredTiger ticket 一篇);assert 计数器;不同客户端驱动的连接数差异;连接断开之后 current 的回落时间只测了一次
    京ICP备2026067502号-1京公网安备11010502063487号