Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - massive amount of MongoCleaner threads

Tags:

mongodb

Somehow my java application talking to mongodb ended up with a huge number of parked (sleeping) threads named MongoCleanerXXX which I presume come from the driver. Number of those was ~600. Apparently there were some connection problems with the database which did recover after some time after mongod was restarted.

MongoDB Java driver version is 2.10.1 MongoDB versions is 2.2.0

What could be the reason for this and what I should be doing wrong to cause this as a client app of MongoDB?

like image 612
Dima Avatar asked Jun 26 '13 18:06

Dima


2 Answers

Sometimes we see a lot of these cleanup threads if MongoClient.close() is not called at the appropriate times.

For example:

  • When undeploying a webapp (see JAVA-817)
  • When authentication fails (see JAVA-831)

There were originally some race conditions around cleaning up these threads, but this was fixed in version 2.6 of the Java driver.

like image 115
Trisha Avatar answered Nov 15 '22 16:11

Trisha


See http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/

The MongoClient class is designed to be thread safe and shared among threads. Typically you create only 1 instance for a given database cluster and use it across your application. If for some reason you decide to create many MongoClient instances, note that:

  • all resource usage limits (max connections, etc) apply per MongoClient instance
  • to dispose of an instance, make sure you call MongoClient.close() to clean up resources

Switch your MongoClient invocation to a singleton:

static public MongoClient mongoClientInstance = null;
public static synchronized MongoClient getMongoClientInstance(String host) {
    if (mongoClientInstance == null) {
        try {
            mongoClientInstance = new MongoClient(host);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    return mongoClientInstance;
}

Seems to work great.

like image 45
Nicholas DiPiazza Avatar answered Nov 15 '22 17:11

Nicholas DiPiazza