Skip to content

Commit 742d9c8

Browse files
committed
[JAVA-237]: Should only have 1 DBCleanerThread per Mongo instance
1 parent 892869f commit 742d9c8

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

src/main/com/mongodb/DB.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,9 @@ public int getOptions(){
656656
return _options.get();
657657
}
658658

659+
public abstract void cleanCursors( boolean force ) throws MongoException;
660+
661+
659662
final Mongo _mongo;
660663
final String _name;
661664

src/main/com/mongodb/DBApiLayer.java

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class DBApiLayer extends DB {
3737
/** The maximum number of cursors allowed */
3838
static final int NUM_CURSORS_BEFORE_KILL = 100;
3939
static final int NUM_CURSORS_PER_BATCH = 20000;
40-
static final int CLEANER_INTERVAL = 1000;
4140

4241
// --- show
4342

@@ -68,8 +67,6 @@ protected DBApiLayer( Mongo mongo, String name , DBConnector connector ){
6867
_rootPlusDot = _root + ".";
6968

7069
_connector = connector;
71-
_cleaner = new DBCleanerThread(CLEANER_INTERVAL);
72-
_cleaner.start();
7370
}
7471

7572
public void requestStart(){
@@ -107,7 +104,7 @@ String _removeRoot( String ns ){
107104
return ns.substring( _root.length() + 1 );
108105
}
109106

110-
void _cleanCursors( boolean force )
107+
public void cleanCursors( boolean force )
111108
throws MongoException {
112109

113110
int sz = _deadCursorIds.size();
@@ -481,41 +478,10 @@ static class DeadCursor {
481478
final long id;
482479
final ServerAddress host;
483480
}
484-
485-
class DBCleanerThread implements Runnable {
486-
487-
Thread _thread;
488-
int interval;
489-
490-
DBCleanerThread(int interval) {
491-
this.interval = interval;
492-
}
493-
494-
void start() {
495-
// start thread as daemon, it's only a cleaner
496-
_thread = new Thread(this);
497-
_thread.setName(getName() + " - cleaner");
498-
_thread.setDaemon(true);
499-
_thread.start();
500-
}
501-
502-
public void run() {
503-
while (_connector.isOpen()) {
504-
try {
505-
Thread.sleep(interval);
506-
_cleanCursors(true);
507-
} catch (Throwable t) {
508-
// thread must never die, print full stack
509-
TRACE_LOGGER.log(Level.WARNING, t.getMessage(), t);
510-
}
511-
}
512-
}
513-
}
514481

515482
final String _root;
516483
final String _rootPlusDot;
517484
final DBConnector _connector;
518-
final DBCleanerThread _cleaner;
519485
final Map<String,MyCollection> _collections = Collections.synchronizedMap( new HashMap<String,MyCollection>() );
520486

521487
ConcurrentLinkedQueue<DeadCursor> _deadCursorIds = new ConcurrentLinkedQueue<DeadCursor>();

src/main/com/mongodb/Mongo.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public class Mongo {
105105
*/
106106
public static final int MINOR_VERSION = 4;
107107

108+
static int cleanerIntervalMS;
109+
static {
110+
cleanerIntervalMS = Integer.parseInt(System.getProperty("com.mongodb.cleanerIntervalMS", "1000"));
111+
}
112+
108113
/**
109114
* returns a database object
110115
* @param addr the database address
@@ -185,6 +190,8 @@ public Mongo( ServerAddress addr , MongoOptions options )
185190
_options = options;
186191
_applyMongoOptions();
187192
_connector = new DBTCPConnector( this , _addr );
193+
_cleaner = new DBCleanerThread();
194+
_cleaner.start();
188195
}
189196

190197
/**
@@ -220,6 +227,9 @@ public Mongo( ServerAddress left , ServerAddress right , MongoOptions options )
220227
_options = options;
221228
_applyMongoOptions();
222229
_connector = new DBTCPConnector( this , _addrs );
230+
231+
_cleaner = new DBCleanerThread();
232+
_cleaner.start();
223233
}
224234

225235
/**
@@ -252,6 +262,9 @@ public Mongo( List<ServerAddress> replicaSetSeeds , MongoOptions options )
252262
_options = options;
253263
_applyMongoOptions();
254264
_connector = new DBTCPConnector( this , _addrs );
265+
266+
_cleaner = new DBCleanerThread();
267+
_cleaner.start();
255268
}
256269

257270
/**
@@ -288,6 +301,8 @@ public Mongo( MongoURI uri )
288301
_connector = new DBTCPConnector( this , replicaSetSeeds );
289302
}
290303

304+
_cleaner = new DBCleanerThread();
305+
_cleaner.start();
291306
}
292307

293308
/**
@@ -494,6 +509,7 @@ DBTCPConnector getConnector() {
494509
final ConcurrentMap<String,DB> _dbs = new ConcurrentHashMap<String,DB>();
495510
private WriteConcern _concern = WriteConcern.NORMAL;
496511
final Bytes.OptionHolder _netOptions = new Bytes.OptionHolder( null );
512+
final DBCleanerThread _cleaner;
497513

498514
org.bson.util.SimplePool<PoolOutputBuffer> _bufferPool =
499515
new org.bson.util.SimplePool<PoolOutputBuffer>( 1000 ){
@@ -559,4 +575,24 @@ String _toKey( MongoURI uri ){
559575

560576
}
561577

578+
class DBCleanerThread extends Thread {
579+
580+
DBCleanerThread() {
581+
setDaemon(true);
582+
setName("MongoCleaner" + hashCode());
583+
}
584+
585+
public void run() {
586+
while (_connector.isOpen()) {
587+
try {
588+
Thread.sleep(cleanerIntervalMS);
589+
for (DB db : _dbs.values()) {
590+
db.cleanCursors(true);
591+
}
592+
} catch (Throwable t) {
593+
// thread must never die
594+
}
595+
}
596+
}
597+
}
562598
}

0 commit comments

Comments
 (0)