@@ -369,39 +369,39 @@ class MyPort {
369369
370370 DBPort get ( boolean keep , ReadPreference readPref , ServerAddress hostNeeded ){
371371
372- DBPort requestPort = getPinnedRequestPort ();
372+ DBPort pinnedRequestPort = getPinnedRequestPortForThread ();
373373
374374 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 ;
377377 }
378378
379379 // asked for a specific host
380380 return _portHolder .get ( hostNeeded ).get ();
381381 }
382382
383- if ( requestPort != null ){
383+ if ( pinnedRequestPort != null ){
384384 // 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 ) {
386386 // if keep is false, it's a read, so we use port even if master changed
387- return requestPort ;
387+ return pinnedRequestPort ;
388388 }
389389
390390 // it's write and master has changed
391391 // we fall back on new master and try to go on with request
392392 // 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 ) ;
395395 }
396396
397- DBPort p ;
397+ DBPort port ;
398398 if (getReplicaSetStatus () == null ){
399399 if (_masterPortPool == null ) {
400400 // this should only happen in rare case that no master was ever found
401401 // may get here at startup if it's a read, slaveOk=true, and ALL servers are down
402402 throw new MongoException ("Rare case where master=null, probably all servers are down" );
403403 }
404- p = _masterPortPool .get ();
404+ port = _masterPortPool .get ();
405405 }
406406 else {
407407 ReplicaSetStatus .ReplicaSet replicaSet = getReplicaSetStatus ()._replicaSetHolder .get ();
@@ -410,38 +410,38 @@ DBPort get( boolean keep , ReadPreference readPref, ServerAddress hostNeeded ){
410410 if (node == null )
411411 throw new MongoException ("No replica set members available in " + replicaSet + " for " + readPref .toDBObject ().toString ());
412412
413- p = _portHolder .get (node .getServerAddress ()).get ();
413+ port = _portHolder .get (node .getServerAddress ()).get ();
414414 }
415415
416416 // 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 ) ;
419419 }
420420
421- return p ;
421+ return port ;
422422 }
423423
424- void done ( DBPort p ) {
425- DBPort requestPort = getPinnedRequestPort ();
424+ void done ( DBPort port ) {
425+ DBPort requestPort = getPinnedRequestPortForThread ();
426426
427427 // keep request port
428- if (p != requestPort ) {
429- p .getPool ().done (p );
428+ if (port != requestPort ) {
429+ port .getPool ().done (port );
430430 }
431431 }
432432
433433 /**
434434 * call this method when there is an IOException or other low level error on port.
435- * @param p
435+ * @param port
436436 * @param e
437437 */
438- void error ( DBPort p , Exception e ){
439- p .close ();
438+ void error ( DBPort port , Exception e ){
439+ port .close ();
440440 pinnedRequestStatusThreadLocal .remove ();
441441
442442 // 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 ())) {
445445 ConnectionStatus .Node newMaster = _connectionStatus .ensureMaster ();
446446 if (newMaster != null ) {
447447 setMaster (newMaster );
@@ -450,32 +450,40 @@ void error( DBPort p , Exception e ){
450450 }
451451
452452 void requestEnsureConnection (){
453- if ( pinnedRequestStatusThreadLocal . get () == null )
453+ if ( ! threadHasPinnedRequest () )
454454 return ;
455455
456- if ( getPinnedRequestPort () != null )
456+ if ( getPinnedRequestPortForThread () != null )
457457 return ;
458458
459- pinnedRequestStatusThreadLocal . get (). requestPort = _masterPortPool .get ();
459+ setPinnedRequestPortForThread ( _masterPortPool .get () );
460460 }
461461
462462 void requestStart (){
463463 pinnedRequestStatusThreadLocal .set (new PinnedRequestStatus ());
464464 }
465465
466466 void requestDone (){
467- DBPort requestPort = getPinnedRequestPort ();
467+ DBPort requestPort = getPinnedRequestPortForThread ();
468468 if ( requestPort != null )
469469 requestPort .getPool ().done ( requestPort );
470470 pinnedRequestStatusThreadLocal .remove ();
471471 }
472472
473- PinnedRequestStatus getPinnedRequestStatus () {
473+ PinnedRequestStatus getPinnedRequestStatusForThread () {
474474 return pinnedRequestStatusThreadLocal .get ();
475475 }
476476
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 ;
479487 }
480488
481489 private final ThreadLocal <PinnedRequestStatus > pinnedRequestStatusThreadLocal = new ThreadLocal <PinnedRequestStatus >();
0 commit comments