2

How can I change the timeout duration for different operations that can fail due to server inaccessibility? (start_session, insert, find, delete, update, ...)

...
auto pool = mongocxx::pool(mongocxx::uri("bad_uri"), pool_options);
auto connection = pool.try_acquire();
auto db = (*(connection.value()))["test_db"];
auto collection = db["test_collection"];

// This does not help
mongocxx::write_concern wc;
wc.timeout(std::chrono::milliseconds(1000));
mongocxx::options::insert insert_options;
insert_options.write_concern(wc);

// takes about 30 seconds to fail
collection.insert_one(from_json(R"({"name": "john doe", "occupation": "_redacted_", "skills" : "a certain set"})"), insert_options);

[Edit]
Here is the exception message:

C++ exception with description "No suitable servers found: serverSelectionTimeoutMS expired: [connection timeout calling ismaster on '127.0.0.1:27017']

pooya13
  • 2,060
  • 2
  • 23
  • 29

1 Answers1

3

It would be helpful to see the actual error message from the insert_one() operation, but "takes about 30 seconds to fail" suggests that this may be due to the default server selection timeout. You can configure that via the serverSelectionTimeoutMS connection string option.

If you are connecting to a replica set, I would suggest keeping that timeout a bit above the expected time for a failover to complete. Replica Set Elections states:

The median time before a cluster elects a new primary should not typically exceed 12 seconds

You may find that is shorter in practice. By keeping the server selection timeout above the expected failover time, you'll allow the driver to insulate your application from an error (at the expense of wait time).

If you are not connecting to a replica set, feel free to lower serverSelectionTimeoutMS to a lower value, albeit still greater than the expected latency to your mongod (standalone) or mongos (sharded cluster) node.

Do note that since server selection occurs within a loop, the connectTimeoutMS connection string option won't affect the delay you're seeing. Lower the connection timeout will allow the driver to internally give up when attempting to connect to an inaccessible server, but the server selection will still block for up to serverSelectionTimeoutMS (and likely retry connections to the server during that loop).

jmikola
  • 6,892
  • 1
  • 31
  • 61
  • Thank you. This helped me a lot. I am holding out on accepting for now to see if someone can help with changing the configuration via the driver since that is the focus of the question. (or is this not possible to do via the driver?) – pooya13 Jun 13 '19 at 16:45
  • Sorry I just realized that you did say it is set via the "connection string option". Thank you. I will edit your answer with an example of an appropriate URI. – pooya13 Jun 13 '19 at 16:58
  • 1
    Since my edit was rejected I will add a comment instead. An example of doing this using the C++ driver: `auto pool = mongocxx::pool(mongocxx::uri("mongodb://localhost:27017/?serverSelectionTimeoutMS=1000"));` – pooya13 Jun 17 '19 at 22:02