Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

JAVA-595: Cleaned up the code a bit with helper methods and variable …

…renames.
  • Loading branch information...
commit 9c268970fe398f8bc13ba923a49b7b0ef0bbea4e 1 parent 76983d0
Jeff Yemin authored March 12, 2013
68  src/main/com/mongodb/DBTCPConnector.java
@@ -369,39 +369,39 @@ boolean _error( Throwable t, boolean secondaryOk ){
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,13 +450,13 @@ 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(){
@@ -464,18 +464,26 @@ void requestStart(){
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>();
30  src/test/com/mongodb/DBTCPConnectorTest.java
@@ -64,17 +64,17 @@ public void beforeMethod() throws UnknownHostException {
64 64
     @Test
65 65
     public void testRequestReservation() {
66 66
         final DBTCPConnector.MyPort myPort = _connector.getMyPort();
67  
-        assertNull(myPort.getPinnedRequestStatus());
  67
+        assertNull(myPort.getPinnedRequestStatusForThread());
68 68
         _connector.requestStart();
69 69
         try {
70  
-            assertNull(myPort.getPinnedRequestPort());
71  
-            assertNotNull(myPort.getPinnedRequestStatus());
  70
+            assertNull(myPort.getPinnedRequestPortForThread());
  71
+            assertNotNull(myPort.getPinnedRequestStatusForThread());
72 72
             _connector.requestDone();
73  
-            assertNull(myPort.getPinnedRequestStatus());
  73
+            assertNull(myPort.getPinnedRequestStatusForThread());
74 74
         } finally {
75 75
             _connector.requestDone();
76 76
         }
77  
-        assertNull(myPort.getPinnedRequestPort());
  77
+        assertNull(myPort.getPinnedRequestPortForThread());
78 78
     }
79 79
 
80 80
     /**
@@ -86,10 +86,10 @@ public void testConnectionReservationForWrites() {
86 86
         _connector.requestStart();
87 87
         try {
88 88
             _connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE);
89  
-            assertNotNull(myPort.getPinnedRequestStatus());
90  
-            DBPort requestPort = myPort.getPinnedRequestPort();
  89
+            assertNotNull(myPort.getPinnedRequestStatusForThread());
  90
+            DBPort requestPort = myPort.getPinnedRequestPortForThread();
91 91
             _connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE);
92  
-            assertEquals(requestPort, myPort.getPinnedRequestPort());
  92
+            assertEquals(requestPort, myPort.getPinnedRequestPortForThread());
93 93
         } finally {
94 94
             _connector.requestDone();
95 95
         }
@@ -104,11 +104,11 @@ public void testConnectionReservationForWriteThenRead() {
104 104
         _connector.requestStart();
105 105
         try {
106 106
             _connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE);
107  
-            DBPort requestPort = myPort.getPinnedRequestPort();
  107
+            DBPort requestPort = myPort.getPinnedRequestPortForThread();
108 108
             _connector.call(_db, _collection,
109 109
                     OutMessage.query(_collection, 0, 0, -1, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary()),
110 110
                     null, 0);
111  
-            assertEquals(requestPort, myPort.getPinnedRequestPort());
  111
+            assertEquals(requestPort, myPort.getPinnedRequestPortForThread());
112 112
         } finally {
113 113
             _connector.requestDone();
114 114
         }
@@ -129,11 +129,11 @@ public void testConnectionReservationForReadThenWrite() {
129 129
             _connector.call(_db, _collection,
130 130
                     OutMessage.query(_collection, 0, 0, -1, new BasicDBObject(), new BasicDBObject(), ReadPreference.secondary()),
131 131
                     null, 0, ReadPreference.secondary(), null);
132  
-            DBPort requestPort = myPort.getPinnedRequestPort();
  132
+            DBPort requestPort = myPort.getPinnedRequestPortForThread();
133 133
             _connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE);
134  
-            assertNotEquals(requestPort, myPort.getPinnedRequestPort());
135  
-            DBTCPConnector.PinnedRequestStatus status = myPort.getPinnedRequestStatus();
136  
-            assertEquals(_connector.getReplicaSetStatus().getMaster(), myPort.getPinnedRequestPort().serverAddress());
  134
+            assertNotEquals(requestPort, myPort.getPinnedRequestPortForThread());
  135
+            DBTCPConnector.PinnedRequestStatus status = myPort.getPinnedRequestStatusForThread();
  136
+            assertEquals(_connector.getReplicaSetStatus().getMaster(), myPort.getPinnedRequestPortForThread().serverAddress());
137 137
         } finally {
138 138
             _connector.requestDone();
139 139
         }
@@ -150,7 +150,7 @@ public void testConnectionReservationForReads() {
150 150
             _connector.call(_db, _collection,
151 151
                     OutMessage.query(_collection, 0, 0, -1, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary()),
152 152
                     null, 0);
153  
-            assertNotNull(myPort.getPinnedRequestPort());
  153
+            assertNotNull(myPort.getPinnedRequestPortForThread());
154 154
         } finally {
155 155
             _connector.requestDone();
156 156
         }

0 notes on commit 9c26897

Please sign in to comment.
Something went wrong with that request. Please try again.