@@ -369,39 +369,39 @@ class MyPort {
369
369
370
370
DBPort get ( boolean keep , ReadPreference readPref , ServerAddress hostNeeded ){
371
371
372
- DBPort requestPort = getPinnedRequestPort ();
372
+ DBPort pinnedRequestPort = getPinnedRequestPortForThread ();
373
373
374
374
if ( hostNeeded != null ) {
375
- if (requestPort != null && requestPort .serverAddress ().equals (hostNeeded )) {
376
- return requestPort ;
375
+ if (pinnedRequestPort != null && pinnedRequestPort .serverAddress ().equals (hostNeeded )) {
376
+ return pinnedRequestPort ;
377
377
}
378
378
379
379
// asked for a specific host
380
380
return _portHolder .get ( hostNeeded ).get ();
381
381
}
382
382
383
- if ( requestPort != null ){
383
+ if ( pinnedRequestPort != null ){
384
384
// we are within a request, and have a port, should stick to it
385
- if ( requestPort .getPool () == _masterPortPool || !keep ) {
385
+ if ( pinnedRequestPort .getPool () == _masterPortPool || !keep ) {
386
386
// if keep is false, it's a read, so we use port even if master changed
387
- return requestPort ;
387
+ return pinnedRequestPort ;
388
388
}
389
389
390
390
// it's write and master has changed
391
391
// we fall back on new master and try to go on with request
392
392
// this may not be best behavior if spec of request is to stick with same server
393
- requestPort .getPool ().done (requestPort );
394
- pinnedRequestStatusThreadLocal . get (). requestPort = null ;
393
+ pinnedRequestPort .getPool ().done (pinnedRequestPort );
394
+ setPinnedRequestPortForThread ( null ) ;
395
395
}
396
396
397
- DBPort p ;
397
+ DBPort port ;
398
398
if (getReplicaSetStatus () == null ){
399
399
if (_masterPortPool == null ) {
400
400
// this should only happen in rare case that no master was ever found
401
401
// may get here at startup if it's a read, slaveOk=true, and ALL servers are down
402
402
throw new MongoException ("Rare case where master=null, probably all servers are down" );
403
403
}
404
- p = _masterPortPool .get ();
404
+ port = _masterPortPool .get ();
405
405
}
406
406
else {
407
407
ReplicaSetStatus .ReplicaSet replicaSet = getReplicaSetStatus ()._replicaSetHolder .get ();
@@ -410,38 +410,38 @@ DBPort get( boolean keep , ReadPreference readPref, ServerAddress hostNeeded ){
410
410
if (node == null )
411
411
throw new MongoException ("No replica set members available in " + replicaSet + " for " + readPref .toDBObject ().toString ());
412
412
413
- p = _portHolder .get (node .getServerAddress ()).get ();
413
+ port = _portHolder .get (node .getServerAddress ()).get ();
414
414
}
415
415
416
416
// if within request, remember port to stick to same server
417
- if ( pinnedRequestStatusThreadLocal . get () != null ) {
418
- pinnedRequestStatusThreadLocal . get (). requestPort = p ;
417
+ if (threadHasPinnedRequest () ) {
418
+ setPinnedRequestPortForThread ( port ) ;
419
419
}
420
420
421
- return p ;
421
+ return port ;
422
422
}
423
423
424
- void done ( DBPort p ) {
425
- DBPort requestPort = getPinnedRequestPort ();
424
+ void done ( DBPort port ) {
425
+ DBPort requestPort = getPinnedRequestPortForThread ();
426
426
427
427
// keep request port
428
- if (p != requestPort ) {
429
- p .getPool ().done (p );
428
+ if (port != requestPort ) {
429
+ port .getPool ().done (port );
430
430
}
431
431
}
432
432
433
433
/**
434
434
* call this method when there is an IOException or other low level error on port.
435
- * @param p
435
+ * @param port
436
436
* @param e
437
437
*/
438
- void error ( DBPort p , Exception e ){
439
- p .close ();
438
+ void error ( DBPort port , Exception e ){
439
+ port .close ();
440
440
pinnedRequestStatusThreadLocal .remove ();
441
441
442
442
// depending on type of error, may need to close other connections in pool
443
- boolean recoverable = p .getPool ().gotError (e );
444
- if (!recoverable && _connectionStatus != null && _masterPortPool ._addr .equals (p .serverAddress ())) {
443
+ boolean recoverable = port .getPool ().gotError (e );
444
+ if (!recoverable && _connectionStatus != null && _masterPortPool ._addr .equals (port .serverAddress ())) {
445
445
ConnectionStatus .Node newMaster = _connectionStatus .ensureMaster ();
446
446
if (newMaster != null ) {
447
447
setMaster (newMaster );
@@ -450,32 +450,40 @@ void error( DBPort p , Exception e ){
450
450
}
451
451
452
452
void requestEnsureConnection (){
453
- if ( pinnedRequestStatusThreadLocal . get () == null )
453
+ if ( ! threadHasPinnedRequest () )
454
454
return ;
455
455
456
- if ( getPinnedRequestPort () != null )
456
+ if ( getPinnedRequestPortForThread () != null )
457
457
return ;
458
458
459
- pinnedRequestStatusThreadLocal . get (). requestPort = _masterPortPool .get ();
459
+ setPinnedRequestPortForThread ( _masterPortPool .get () );
460
460
}
461
461
462
462
void requestStart (){
463
463
pinnedRequestStatusThreadLocal .set (new PinnedRequestStatus ());
464
464
}
465
465
466
466
void requestDone (){
467
- DBPort requestPort = getPinnedRequestPort ();
467
+ DBPort requestPort = getPinnedRequestPortForThread ();
468
468
if ( requestPort != null )
469
469
requestPort .getPool ().done ( requestPort );
470
470
pinnedRequestStatusThreadLocal .remove ();
471
471
}
472
472
473
- PinnedRequestStatus getPinnedRequestStatus () {
473
+ PinnedRequestStatus getPinnedRequestStatusForThread () {
474
474
return pinnedRequestStatusThreadLocal .get ();
475
475
}
476
476
477
- DBPort getPinnedRequestPort () {
478
- return pinnedRequestStatusThreadLocal .get () != null ? pinnedRequestStatusThreadLocal .get ().requestPort : null ;
477
+ boolean threadHasPinnedRequest () {
478
+ return pinnedRequestStatusThreadLocal .get () != null ;
479
+ }
480
+
481
+ DBPort getPinnedRequestPortForThread () {
482
+ return threadHasPinnedRequest () ? pinnedRequestStatusThreadLocal .get ().requestPort : null ;
483
+ }
484
+
485
+ void setPinnedRequestPortForThread (final DBPort port ) {
486
+ pinnedRequestStatusThreadLocal .get ().requestPort = port ;
479
487
}
480
488
481
489
private final ThreadLocal <PinnedRequestStatus > pinnedRequestStatusThreadLocal = new ThreadLocal <PinnedRequestStatus >();
0 commit comments