mongodb / mongo-php-driver

The Official MongoDB PHP driver
https://pecl.php.net/package/mongodb
Apache License 2.0
889 stars 204 forks source link

Randomly getting "socket timeout calling ismaster on" #970

Closed bujardeari closed 4 years ago

bujardeari commented 5 years ago

Description

Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set): [socket timeout calling ismaster on 'gcp-atlas-prod-shard-00-00-gz8ag.gcp.mongodb.net:27017'] [socket timeout calling ismaster on 'gcp-atlas-prod-shard-00-01-gz8ag.gcp.mongodb.net:27017'] [socket timeout calling ismaster on 'gcp-atlas-prod-shard-00-02-gz8ag.gcp.mongodb.net:27017'] in /var/www/html/vendor/mongodb/mongodb/src/Collection.php:645
Stack trace:
#0 /var/www/html/vendor/mongodb/mongodb/src/Collection.php(645): MongoDB\Driver\Manager->selectServer(Object(MongoDB\Driver\ReadPreference))
#1 /var/www/html/App/settings.php(53): MongoDB\Collection->findOne(Array)
#2 /var/www/html/public/index.php(5): require_once('/var/www/html/A...')
#3 {main} thrown in /var/www/html/vendor/mongodb/mongodb/src/Collection.php on line 645

The problem comes randomly mostly every second time i start the pod.

Environment

I have a PHP Version 7.2.14 running on kubernetes deployment on google cloud.

libbson bundled version => 1.13.0
libmongoc bundled version => 1.13.0
libmongoc SSL => enabled
libmongoc SSL library => OpenSSL
libmongoc crypto => enabled
libmongoc crypto library => libcrypto
libmongoc crypto system profile => disabled
libmongoc SASL => enabled
libmongoc ICU => disabled
libmongoc compression => enabled
libmongoc compression snappy => disabled
libmongoc compression zlib => enabled
mongodb.debug => no value => no value
<?php

 $mongo_password = "xxxxxx";
  $mongo_database = "db_test";
  $mongo_connectionstring = "mongodb+srv://username:".$mongo_password."@gcp-atlas-prod-gz8ag.gcp.mongodb.net";

   $mongo = new MongoDB\Client($mongo_connectionstring);
   $collection = $mongo->$mongo_database->config;
   $_CFG =$collection->findOne(['id'=> 'system']);
jmikola commented 5 years ago

Cross-referencing with the previous two comments related to this issue:

To better help you understand the exception message, I can break down the ConnectionTimeoutException details as follows:

Unfortunately, the information in this exception doesn't reveal any additional information that would indicate a bug in the driver itself. I believe my analysis in https://github.com/mongodb/mongo-php-library/issues/546#issuecomment-482139743 is still applicable and the real culprit here may be a network connectivity issue in your deployment. As you noted in the linked comments above, you were unable to reproduce this in a local environment and a subsequent deployment in the affected production environment apparently resolves the connection problem.

Regarding serverSelectionTryOnce

PHP driver's default behavior only attempts server selection once and allows the application to fail fast rather than block for an extended amount of time and possibly allow requests to build up on the server. The behavior can be modified by the serverSelectionTryOnce URI option. If you'd like to try disabling serverSelectionTryOnce and allow the driver to make additional attempts to connect after a socket failure, I would strongly suggest tweaking serverSelectionTimeoutMS below its default value of 30 seconds. Ideally, the connectionTimeoutMS option should also be tweaked closer to (but still greater than) the expected latency between your application server and the database server. More details about the driver's internal behavior can be found in the server selection and server discovery and monitoring specifications.

SashaDesigN commented 4 years ago

for me just helped updating of php mongodb driver actually on prod all worked good, just wanted to edit website quick on local version and got same issue

Herodesh commented 4 years ago

How about checking your connection ips?