文档菜单
文档首页
/ / /
PHP库手册

服务器选择与监控

本页内容

  • 服务器选择与监控
  • 连接字符串选项
  • connectTimeoutMS
  • heartbeatFrequencyMS
  • serverSelectionTimeoutMS
  • serverSelectionTryOnce
  • socketCheckIntervalMS
  • socketTimeoutMS

在执行任何操作之前,MongoDB PHP 库必须首先从拓扑结构(例如副本集、分片集群)中选择一个服务器。选择服务器需要准确查看拓扑结构,因此扩展程序定期监控它所连接的服务器。

在其他大多数驱动程序中,服务器发现和监控由后台线程处理;然而,PHP 驱动程序是单线程的,因此必须在应用程序启动的操作之间进行监控。

考虑以下示例应用程序

<?php
/**
* When constructing a Client, the library creates a MongoDB\Driver\Manager
* object from the extension. In turn, the extension will either create a
* libmongoc client object (and persist it according to the constructor
* parameters) or re-use a previously persisted client.
*
* Assuming a new libmongoc client was created, the host name(s) in the
* connection string must be resolved via DNS. Likewise, if the connection
* string includes a mongodb+srv scheme, SRV/TXT records must be resolved.
* Following DNS resolution, the driver should then have a list of one or
* more hosts to which it can connect. This is referred to as the seed list.
*
* If a previously persisted client was re-used, no DNS resolution is needed
* and there will likely already be connections and topology state associated
* with the client.
*
* Drivers perform no further IO when constructing a client, so control is
* returned the the PHP script.
*/
$client = new MongoDB\Client('mongodb://a.example.com:27017/?replicaSet=rs0');
/**
* The library creates a MongoDB\Database object from the Client. This does
* not entail any IO, as the Database and Collection objects only associate
* a database or namespace with a Client object, respectively.
*/
$database = $client->test;
/**
* The library creates an internal object for this operation and must select
* a server to use for executing that operation.
*
* If this is the first operation on the underlying libmongoc client, it must
* first discover the topology. It does so by establishing connections to any
* host(s) in the seed list (this may entail TLS and OCSP verification) and
* issuing "hello" commands.
*
* In the case of a replica set, connecting to a single host in the seed list
* should allow the driver to discover all other members in the replica set.
* In the case of a sharded cluster, the driver will start with an initial
* seed list of mongos hosts and, if SRV polling is utilized, may discover
* additional mongos hosts over time.
*
* If the topology was already initialized (i.e. this is not the first
* operation on the client), the driver may still need to perform monitoring
* (i.e. "hello" commands) and refresh its view of the topology. This process
* may entail adding or removing hosts from the topology.
*
* Once the topology has been discovered and any necessary monitoring has
* been performed, the driver may select a server according to the rules
* outlined in the server selection specification (e.g. applying a read
* preference, filtering hosts by latency).
*/
$database->command(['ping' => 1]);

尽管该应用程序只包含几行 PHP 代码,但实际上后台有相当多的工作!感兴趣的读者可以在以下文档中找到更详细的讨论

  • 单线程模式在libmongoc文档中

  • 服务器发现和监控 规范

  • 服务器选择 规范

存在一些与服务器选择和监控相关的连接字符串选项。

connectTimeoutMS 指定了与服务器建立连接以及服务器监控(hello 命令)的套接字超时的限制。对于PHP等单线程驱动程序,默认为10秒。

当服务器在监控时超时,它将不会在至少五秒(冷却时间(cooldownMS))过去后再次检查。此超时旨在避免在每次后续扫描发生错误后,单线程驱动程序在 connectTimeoutMS 上阻塞。

应用程序可以考虑将此选项设置为略高于集群中服务器之间最大的延迟。例如,如果PHP应用服务器和数据库服务器之间最大的 ping 时间为200ms,指定一秒的超时可能是合理的。这将允许有足够的时间建立连接并监控可访问的服务器,同时显著减少检测不可访问服务器的时间。

heartbeatFrequencyMS 确定了监控应发生的频率。默认为单线程驱动程序为60秒,最低可设置为500ms。

serverSelectionTimeoutMS 确定了在服务器选择循环中花费的最大时间。默认为30秒,但如果 serverSelectionTryOncetrueconnectTimeoutMS 的值较小,应用程序通常会在更早的时间内失败。

原始默认值是在副本集选举完成时间较长的时候设定的。应用程序可以考虑将此选项设置为主选举预期完成时间稍长一些。例如,副本集选举 指出,选举通常不会超过12秒,因此15秒的超时时间可能是合理的。连接到分片集群的应用程序可以考虑更小的值,因为 mongos 会将驱动程序与选举隔离开。

不过,通常不应将 serverSelectionTimeoutMS 设置为小于 connectTimeoutMS 的值。

serverSelectionTryOnce 用于确定驱动器在第一次尝试选择服务器失败后是否应该放弃,或者继续等待直到 serverSelectionTimeoutMS 超时。PHP 默认为 true,允许驱动器在无法选择服务器时(例如,故障转移期间没有主服务器)快速失败。

对于高流量的Web应用程序,默认行为通常是希望的行为,因为它意味着工作进程不会被阻塞在服务器选择循环中,而可以返回错误响应并立即继续服务另一个请求。此外,其他驱动器功能,如可重试的读写操作,仍可以使应用程序避免诸如故障转移之类的瞬态错误。

尽管如此,如果应用程序优先考虑弹性而不是响应时间(和工作者池利用率),则可能希望将 serverSelectionTryOnce 的值指定为 false

socketCheckIntervalMS 用于确定在最近未使用的情况下,应该多长时间检查一次套接字(使用 ping 命令)。默认为5秒,且故意低于 heartbeatFrequencyMS,以更好地允许单线程驱动器恢复丢失的连接。

socketTimeoutMS 用于确定在套接字上进行读取或写入操作的最大时间。由于服务器监控使用 connectTimeoutMS 进行其套接字超时,因此 socketTimeoutMS 仅适用于应用程序执行的操作。

socketTimeoutMS 默认为5分钟;然而,由于 最大执行时间,对于Web SAPIs默认为30秒,PHP网络请求可能会因此更早终止。在CLI环境中,max_execution_time默认无限制,因此socketTimeoutMS可能更容易达到。

注意:

socketTimeoutMS与服务器选择和监控无直接关系;然而,它经常与其他选项相关联,因此需要提及。

下一页

MongoDB PHP 库