Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…renames.
  • Loading branch information...
commit 9c268970fe398f8bc13ba923a49b7b0ef0bbea4e 1 parent 76983d0
@jyemin jyemin authored
View
68 src/main/com/mongodb/DBTCPConnector.java
@@ -369,39 +369,39 @@ boolean _error( Throwable t, boolean secondaryOk ){
DBPort get( boolean keep , ReadPreference readPref, ServerAddress hostNeeded ){
- DBPort requestPort = getPinnedRequestPort();
+ DBPort pinnedRequestPort = getPinnedRequestPortForThread();
if ( hostNeeded != null ) {
- if (requestPort != null && requestPort.serverAddress().equals(hostNeeded)) {
- return requestPort;
+ if (pinnedRequestPort != null && pinnedRequestPort.serverAddress().equals(hostNeeded)) {
+ return pinnedRequestPort;
}
// asked for a specific host
return _portHolder.get( hostNeeded ).get();
}
- if ( requestPort != null ){
+ if ( pinnedRequestPort != null ){
// we are within a request, and have a port, should stick to it
- if ( requestPort.getPool() == _masterPortPool || !keep ) {
+ if ( pinnedRequestPort.getPool() == _masterPortPool || !keep ) {
// if keep is false, it's a read, so we use port even if master changed
- return requestPort;
+ return pinnedRequestPort;
}
// it's write and master has changed
// we fall back on new master and try to go on with request
// this may not be best behavior if spec of request is to stick with same server
- requestPort.getPool().done(requestPort);
- pinnedRequestStatusThreadLocal.get().requestPort = null;
+ pinnedRequestPort.getPool().done(pinnedRequestPort);
+ setPinnedRequestPortForThread(null);
}
- DBPort p;
+ DBPort port;
if (getReplicaSetStatus() == null){
if (_masterPortPool == null) {
// this should only happen in rare case that no master was ever found
// may get here at startup if it's a read, slaveOk=true, and ALL servers are down
throw new MongoException("Rare case where master=null, probably all servers are down");
}
- p = _masterPortPool.get();
+ port = _masterPortPool.get();
}
else {
ReplicaSetStatus.ReplicaSet replicaSet = getReplicaSetStatus()._replicaSetHolder.get();
@@ -410,38 +410,38 @@ DBPort get( boolean keep , ReadPreference readPref, ServerAddress hostNeeded ){
if (node == null)
throw new MongoException("No replica set members available in " + replicaSet + " for " + readPref.toDBObject().toString());
- p = _portHolder.get(node.getServerAddress()).get();
+ port = _portHolder.get(node.getServerAddress()).get();
}
// if within request, remember port to stick to same server
- if ( pinnedRequestStatusThreadLocal.get() != null) {
- pinnedRequestStatusThreadLocal.get().requestPort = p;
+ if (threadHasPinnedRequest()) {
+ setPinnedRequestPortForThread(port);
}
- return p;
+ return port;
}
- void done( DBPort p ) {
- DBPort requestPort = getPinnedRequestPort();
+ void done( DBPort port ) {
+ DBPort requestPort = getPinnedRequestPortForThread();
// keep request port
- if (p != requestPort) {
- p.getPool().done(p);
+ if (port != requestPort) {
+ port.getPool().done(port);
}
}
/**
* call this method when there is an IOException or other low level error on port.
- * @param p
+ * @param port
* @param e
*/
- void error( DBPort p , Exception e ){
- p.close();
+ void error( DBPort port , Exception e ){
+ port.close();
pinnedRequestStatusThreadLocal.remove();
// depending on type of error, may need to close other connections in pool
- boolean recoverable = p.getPool().gotError(e);
- if (!recoverable && _connectionStatus != null && _masterPortPool._addr.equals(p.serverAddress())) {
+ boolean recoverable = port.getPool().gotError(e);
+ if (!recoverable && _connectionStatus != null && _masterPortPool._addr.equals(port.serverAddress())) {
ConnectionStatus.Node newMaster = _connectionStatus.ensureMaster();
if (newMaster != null) {
setMaster(newMaster);
@@ -450,13 +450,13 @@ void error( DBPort p , Exception e ){
}
void requestEnsureConnection(){
- if ( pinnedRequestStatusThreadLocal.get() == null )
+ if ( !threadHasPinnedRequest() )
return;
- if ( getPinnedRequestPort() != null )
+ if ( getPinnedRequestPortForThread() != null )
return;
- pinnedRequestStatusThreadLocal.get().requestPort = _masterPortPool.get();
+ setPinnedRequestPortForThread(_masterPortPool.get());
}
void requestStart(){
@@ -464,18 +464,26 @@ void requestStart(){
}
void requestDone(){
- DBPort requestPort = getPinnedRequestPort();
+ DBPort requestPort = getPinnedRequestPortForThread();
if ( requestPort != null )
requestPort.getPool().done( requestPort );
pinnedRequestStatusThreadLocal.remove();
}
- PinnedRequestStatus getPinnedRequestStatus() {
+ PinnedRequestStatus getPinnedRequestStatusForThread() {
return pinnedRequestStatusThreadLocal.get();
}
- DBPort getPinnedRequestPort() {
- return pinnedRequestStatusThreadLocal.get() != null ? pinnedRequestStatusThreadLocal.get().requestPort : null;
+ boolean threadHasPinnedRequest() {
+ return pinnedRequestStatusThreadLocal.get() != null;
+ }
+
+ DBPort getPinnedRequestPortForThread() {
+ return threadHasPinnedRequest() ? pinnedRequestStatusThreadLocal.get().requestPort : null;
+ }
+
+ void setPinnedRequestPortForThread(final DBPort port) {
+ pinnedRequestStatusThreadLocal.get().requestPort = port;
}
private final ThreadLocal<PinnedRequestStatus> pinnedRequestStatusThreadLocal = new ThreadLocal<PinnedRequestStatus>();
View
30 src/test/com/mongodb/DBTCPConnectorTest.java
@@ -64,17 +64,17 @@ public void beforeMethod() throws UnknownHostException {
@Test
public void testRequestReservation() {
final DBTCPConnector.MyPort myPort = _connector.getMyPort();
- assertNull(myPort.getPinnedRequestStatus());
+ assertNull(myPort.getPinnedRequestStatusForThread());
_connector.requestStart();
try {
- assertNull(myPort.getPinnedRequestPort());
- assertNotNull(myPort.getPinnedRequestStatus());
+ assertNull(myPort.getPinnedRequestPortForThread());
+ assertNotNull(myPort.getPinnedRequestStatusForThread());
_connector.requestDone();
- assertNull(myPort.getPinnedRequestStatus());
+ assertNull(myPort.getPinnedRequestStatusForThread());
} finally {
_connector.requestDone();
}
- assertNull(myPort.getPinnedRequestPort());
+ assertNull(myPort.getPinnedRequestPortForThread());
}
/**
@@ -86,10 +86,10 @@ public void testConnectionReservationForWrites() {
_connector.requestStart();
try {
_connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE);
- assertNotNull(myPort.getPinnedRequestStatus());
- DBPort requestPort = myPort.getPinnedRequestPort();
+ assertNotNull(myPort.getPinnedRequestStatusForThread());
+ DBPort requestPort = myPort.getPinnedRequestPortForThread();
_connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE);
- assertEquals(requestPort, myPort.getPinnedRequestPort());
+ assertEquals(requestPort, myPort.getPinnedRequestPortForThread());
} finally {
_connector.requestDone();
}
@@ -104,11 +104,11 @@ public void testConnectionReservationForWriteThenRead() {
_connector.requestStart();
try {
_connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE);
- DBPort requestPort = myPort.getPinnedRequestPort();
+ DBPort requestPort = myPort.getPinnedRequestPortForThread();
_connector.call(_db, _collection,
OutMessage.query(_collection, 0, 0, -1, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary()),
null, 0);
- assertEquals(requestPort, myPort.getPinnedRequestPort());
+ assertEquals(requestPort, myPort.getPinnedRequestPortForThread());
} finally {
_connector.requestDone();
}
@@ -129,11 +129,11 @@ public void testConnectionReservationForReadThenWrite() {
_connector.call(_db, _collection,
OutMessage.query(_collection, 0, 0, -1, new BasicDBObject(), new BasicDBObject(), ReadPreference.secondary()),
null, 0, ReadPreference.secondary(), null);
- DBPort requestPort = myPort.getPinnedRequestPort();
+ DBPort requestPort = myPort.getPinnedRequestPortForThread();
_connector.say(_db, createOutMessageForInsert(), WriteConcern.SAFE);
- assertNotEquals(requestPort, myPort.getPinnedRequestPort());
- DBTCPConnector.PinnedRequestStatus status = myPort.getPinnedRequestStatus();
- assertEquals(_connector.getReplicaSetStatus().getMaster(), myPort.getPinnedRequestPort().serverAddress());
+ assertNotEquals(requestPort, myPort.getPinnedRequestPortForThread());
+ DBTCPConnector.PinnedRequestStatus status = myPort.getPinnedRequestStatusForThread();
+ assertEquals(_connector.getReplicaSetStatus().getMaster(), myPort.getPinnedRequestPortForThread().serverAddress());
} finally {
_connector.requestDone();
}
@@ -150,7 +150,7 @@ public void testConnectionReservationForReads() {
_connector.call(_db, _collection,
OutMessage.query(_collection, 0, 0, -1, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary()),
null, 0);
- assertNotNull(myPort.getPinnedRequestPort());
+ assertNotNull(myPort.getPinnedRequestPortForThread());
} finally {
_connector.requestDone();
}
Please sign in to comment.
Something went wrong with that request. Please try again.