1

我正在尝试理解 mongodb 读取语义。根据文档,除非您更改读取首选项,否则它始终是副本集中的主服务器。

我的设置:Linux VM:4GB RAM,1 个核心,在同一个 VM 上运行 3 个 mongo 实例(我知道这是一个糟糕的设置!但这就是我现在从 ops 获得的方式)

当我从 Java 驱动程序运行以下 mongo 查询时,我只是在监视内存使用情况、日志等。

这是我看到的:

ReadPreference.PrimaryPreferred:

最佳

PID                USER      PR  NI VIRT  RES  SHR  S %CPU %MEM    TIME+  COMMAND 
17874(primary)     mongod    20   0 43.0g 1.3g 1.1g S 14.3 36.0 109:06.71 mongod                                                                                                                                                                    
18048(secondary-1) mongod    20   0 42.7g 690m  88m S  0.3 18.0  28:45.09 mongod                                                                                                                                                                    
18207(secondary-2) mongod    20   0 42.7g 641m  60m S  0.3 16.7  28:53.13 mongod   

日志

小学一

Fri Aug  2 21:35:39.416 [initandlisten] connection accepted from 10.10.1.57:54306 #11927 (40 connections now open)
Fri Aug  2 21:44:16.332 [initandlisten] connection accepted from 10.10.1.57:54376 #11963 (41 connections now open)
Fri Aug  2 21:44:16.348 [initandlisten] connection accepted from 10.10.1.57:54377 #11964 (42 connections now open)
Fri Aug  2 21:44:16.414 [initandlisten] connection accepted from 10.10.1.57:54380 #11965 (43 connections now open)

中学一

Fri Aug  2 20:03:43.066 [conn11270] end connection 10.10.1.57:53547 (20 connections now open)
Fri Aug  2 20:03:43.066 [conn11271] end connection 10.10.1.57:53551 (19 connections now open)
Fri Aug  2 21:44:16.331 [initandlisten] connection accepted from 10.10.1.57:54374 #11776 (19 connections now open)
Fri Aug  2 21:44:16.350 [initandlisten] connection accepted from 10.10.1.57:54378 #11777 (20 connections now open)

中学二

Fri Aug  2 20:03:43.065 [conn11271] end connection 10.10.1.57:53552 (20 connections now open)
Fri Aug  2 20:03:43.066 [conn11270] end connection 10.10.1.57:53549 (19 connections now open)
Fri Aug  2 21:44:16.331 [initandlisten] connection accepted from 10.10.1.57:54375 #11776 (19 connections now open)
Fri Aug  2 21:44:16.352 [initandlisten] connection accepted from 10.10.1.57:54379 #11777 (20 connections now open)

ReadPreference.Secondary:

最佳

PID                USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                                                  
18048(secondary-1) mongod    20   0 42.7g 1.1g 517m S  7.3 29.2  29:28.00 mongod                                                                                                                                                                    
17874(primary)     mongod    20   0 43.0g 1.0g 777m S  0.3 26.7 112:57.05 mongod                                                                                                                                                                    
18207(secondary-2) mongod    20   0 42.7g 617m  37m S  0.3 16.1  28:59.08 mongod

日志

==> mongod_rs0-0.log <==

Fri Aug  2 22:19:01.056 [conn12118] end connection 10.10.10.201:52558 (34 connections now open)
Fri Aug  2 22:19:01.057 [initandlisten] connection accepted from 10.10.10.201:52564 #12120 (35 connections now open)

==> mongod_rs0-1.log <==

Fri Aug  2 22:19:04.038 [conn11925] end connection 10.10.10.201:46443 (15 connections now open)
Fri Aug  2 22:19:04.039 [initandlisten] connection accepted from 10.10.10.201:46449 #11927 (16 connections now open)

==> mongod_rs0-2.log <==

Fri Aug  2 22:19:04.050 [conn11925] end connection 10.10.10.201:37641 (14 connections now open)
Fri Aug  2 22:19:04.050 [initandlisten] connection accepted from 10.10.10.201:37647 #11927 (15 connections now open)

我的问题是:

  1. 在这两种情况下,CPU 使用率都反映了您的阅读偏好,但我不太了解日志。为什么在我查询时它会在每个实例上打开连接?
  2. 为什么primary保持更多no。即使 readpreference 是 SecondaryPreferred 的连接数。
  3. 每个实例上的 tcpdump 都处于活动状态。其中2个应该是空闲的,对吧?没有其他客户端/进程调用此集群。
4

1 回答 1

1

与主服务器和辅助服务器的连接甚至可能不是您的客户端连接和查询服务器。主服务器和辅助服务器需要保持相互连接,以便 a) 主服务器上的写入可以复制到辅助服务器上,并且 b) 所有服务器都可以确保仍然有主服务器可用。

例如,我现在在我的笔记本电脑上设置并运行了一个三服务器副本,但是没有进程连接到它或查询它,主服务器上的日志显示:

[conn15939] end connection 127.0.0.1:50584 (6 connections now open)
[initandlisten] connection accepted from 127.0.0.1:50597 #15941 (7 connections now open)

在辅助节点上,我有一半的连接数。这是因为从节点只需要连接到它的主节点,但主节点将拥有来自所有从节点的连接。

于 2013-08-11T11:56:16.263 回答