Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use heartbeats?

Mongodb java driver recently added (in version 2.12) the following methods to MongoClientOptions.Builder class:

  • heartbeatConnectRetryFrequency(int heartbeatConnectRetryFrequency)
  • heartbeatConnectTimeout(int heartbeatConnectTimeout)
  • heartbeatFrequency(int heartbeatFrequency)
  • heartbeatThreadCount(int heartbeatThreadCount)

As per this bug, it looks like these methods expose properties using which the MongoDB Java driver will try to regenerate connection pool, in case the connections in the driver connection pool are dead due to network outage or mongodb-server restart.

I have already read the concerned javadocs and searched on web, but could not find any detailed example or good article explaining how to use them.

Does anyone here know how to use these properties?

like image 580
Amit Sharma Avatar asked Nov 01 '22 20:11

Amit Sharma


1 Answers

The description of these fields is given in javadoc for MongoClientOptions as follows:

  • heartbeatConnectRetryFrequency(int heartbeatConnectRetryFrequency) :
    • This is the frequency that a background thread will attempt to connect to each MongoDB server that the MongoClient is connected to, when that server is currently unreachable.
  • heartbeatConnectTimeout(int heartbeatConnectTimeout) :
    • This is the socket connect timeout for sockets used by the background thread that is monitoring each MongoDB server that the MongoClient is connected to.
  • heartbeatFrequency(int heartbeatFrequency) :
    • This is the frequency that a background thread will attempt to connect to each MongoDB server that the MongoClient is connected to.
  • heartbeatThreadCount(int heartbeatThreadCount) :
    • This is the number of threads that will be used to monitor the MongoDB servers that the MongoClient is connected to.

Some observations I made

  • These methods became part of mongodb java driver api in v2.12.2.
  • As per my test code,
    • pre v2.12.2, mongodb-java-driver could not recover from mongodb-server restarts
    • in v2.12.2, the first call after server restart fails and all consequent calls go through (i.e., mongodb-java-driver recovers from server restarts, but with this bug.
    • The bug mentioned above has been fixed in v2.12.3, which has not been released as of date of writing this answer.
like image 145
Amit Sharma Avatar answered Nov 08 '22 12:11

Amit Sharma