Docs Menu
Docs Home
/ / /
PHP ライブラリ マニュアル

サーバーの選択と監視

操作を実行する前に、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 ドキュメントの

  • サーバーの検出と監視 仕様

  • サーバーの選択 仕様

サーバーの選択と監視に関連するいくつかの接続stringオプションがあります。

connectTimeoutMS は、サーバーへの接続を確立するための両方の制限、サーバー監視用のソケット タイムアウトを指定します( helloコマンド)。 PHP などの単一スレッド ドライバーの場合、これはデフォルトで 10 秒になります。

監視中にサーバーがタイムアウトすると、少なくとも 5 秒( クールダウンMS )が経過しているこのタイムアウトは、エラー後のスキャンでconnectTimeoutMSのシングルスレッド ドライバーをブロックするのを避けるためのものです。

アプリケーションは、クラスター内のサーバー間で最大レイテンシよりわずかに大きな値にこのオプションを設定することを検討できます。 たとえば、PHP アプリケーション サーバーとデータベース サーバー間の最大ping時間が200ミリ秒の場合、タイムアウトを 1 秒に指定するのが適切な場合があります。 これにより、接続を確立し、アクセス可能なサーバーを監視するために十分な時間が確保できますが、アクセスできないサーバーを検出する時間も大幅に短縮されます。

heartbeatFrequencyMS モニタリングの頻度を決定します。 これはデフォルトで60秒に設定され、 500ミリ秒まで低く設定できます。

serverSelectionTimeoutMS サーバー選択ループで費やする最大時間を決定します。 これのデフォルトは 30 秒ですが、 serverSelectionTryOncetrueで、 connectTimeoutMSの値が小さい場合、アプリケーションは通常、より早く失敗します。

元のデフォルトは、レプリカセットの選挙が完了するまでにはるかに時間がかかるときに確立されました。 アプリケーションは、このオプションを選挙の予想完了時間よりもわずかに長く設定することを検討できます。 たとえば、「レプリカセットの選挙」では、選挙は通常12秒を超えないことが記載されているため、 15秒のタイムアウトが合理的と考えられます。 シャーディングされたクラスターに接続するアプリケーションでは、 mongosがドライバーを選挙から分離するため、値はさらに小さな値とみなされる場合があります。

つまり、 serverSelectionTimeoutMSは通常、 connectTimeoutMSより小さい値に設定しないでください。

serverSelectionTryOnce は、サーバー選択の最初の試行後にドライバーが中断するか、 serverSelectionTimeoutMSに達するまで待機し続けるかを決定します。 PHP のデフォルトはtrueです。これにより、サーバーを選択できない場合(例: フェイルオーバー中にプライマリが存在しない場合)。

デフォルトの動作は通常、高トラフィックのウェブアプリケーションに適していGo 。これにより、ワーカー プロセスはサーバー選択ループでブロックされず、代わりにエラー応答が返され、別のリクエストを処理するためにすぐにオンになることができるためです。 さらに、再試行可能な読み取りや書き込みなどの他のドライバー機能によって、アプリケーションはフェイルオーバーなどの一時的なエラーを回避できるようになります。

つまり、応答時間(およびワーカー プールの使用率)よりも回復力を優先するアプリケーションでは、 serverSelectionTryOncefalseを指定する必要があるかもしれません。

socketCheckIntervalMS 最近使用されていない場合に、ソケットをチェックする頻度( pingコマンドを使用)を決定します。 これはデフォルトで 5 秒で、シングルスレッド ドライバーが切断された接続を回復できるようにするために、 heartbeatFrequencyMSよりも意図的に低くされています。

socketTimeoutMS ソケットへの読み取りまたは書き込みに費やされる最大時間を決定します。 サーバー監視ではソケット タイムアウトにconnectTimeoutMSが使用されるため、 socketTimeoutMSはアプリケーションによって実行される操作にのみ適用されます。

socketTimeoutMS 5のデフォルトは 分で、ただし、 max_execution_time により、PHP Web リクエストはより早く終了される可能性があります 、ウェブ30 SAPI のデフォルトは 秒です。デフォルトでmax_execution_timeが制限されている CLI 環境では、 socketTimeoutMSに達する可能性が高くなります。

注意

socketTimeoutMS は、サーバーの選択と監視には直接関係しません。ただし、他のオプションに関連付けられることが多いため、参照が必要です。

次へ

MongoDB PHP ライブラリ

項目一覧