Skip to content

Commit fe574ad

Browse files
author
Brendan W. McAdams
committed
JAVA-428 - Support new ReadPreference semantics, deprecate SlaveOK
* Pass basic readPreference information down to the API layer; doesn't support dealing w/ tags yet just "Primary or Secondary" * We are not yet implementing the proposed "bucketing" behavior for pings, but using the existing ping selection of secondary code from the existing library
1 parent b9a115e commit fe574ad

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

src/main/com/mongodb/DBApiLayer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int
298298

299299
if ( willTrace() ) trace( "find: " + _fullNameSpace + " " + JSON.serialize( ref ) );
300300

301-
OutMessage query = OutMessage.query( _mongo , options , _fullNameSpace , numToSkip , chooseBatchSize(batchSize, limit, 0) , ref , fields );
301+
OutMessage query = OutMessage.query( _mongo , options , _fullNameSpace , numToSkip , chooseBatchSize(batchSize, limit, 0) , ref , fields, readPref);
302302

303303
Response res = _connector.call( _db , this , query , null , 2 );
304304

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public WriteResult say( DB db , OutMessage m , WriteConcern concern , ServerAddr
139139
checkMaster( false , true );
140140

141141
MyPort mp = _myPort.get();
142-
DBPort port = mp.get( true , false , hostNeeded );
142+
DBPort port = mp.get( true , ReadPreference.PRIMARY, hostNeeded );
143143

144144
try {
145145
port.checkAuth( db );
@@ -188,12 +188,17 @@ public Response call( DB db , DBCollection coll , OutMessage m , ServerAddress h
188188

189189
public Response call( DB db , DBCollection coll , OutMessage m , ServerAddress hostNeeded , int retries )
190190
throws MongoException {
191-
boolean slaveOk = m.hasOption( Bytes.QUERYOPTION_SLAVEOK );
191+
ReadPreference readPref = m.getReadPreference();
192+
if (readPref == ReadPreference.PRIMARY && m.hasOption( Bytes.QUERYOPTION_SLAVEOK ))
193+
readPref = ReadPreference.SECONDARY;
194+
195+
boolean secondaryOk = !(readPref == ReadPreference.PRIMARY);
196+
192197
_checkClosed();
193-
checkMaster( false , !slaveOk );
198+
checkMaster( false, !secondaryOk );
194199

195200
final MyPort mp = _myPort.get();
196-
final DBPort port = mp.get( false , slaveOk, hostNeeded );
201+
final DBPort port = mp.get( false , readPref, hostNeeded );
197202

198203
Response res = null;
199204
boolean retry = false;
@@ -206,7 +211,7 @@ public Response call( DB db , DBCollection coll , OutMessage m , ServerAddress h
206211
catch ( IOException ioe ){
207212
mp.error( port , ioe );
208213
retry = retries > 0 && !coll._name.equals( "$cmd" )
209-
&& !(ioe instanceof SocketTimeoutException) && _error( ioe, slaveOk );
214+
&& !(ioe instanceof SocketTimeoutException) && _error( ioe, secondaryOk );
210215
if ( !retry ){
211216
throw new MongoException.Network( "can't call something : " + port.host() + "/" + db,
212217
ioe );
@@ -282,11 +287,11 @@ public String getConnectPoint(){
282287
* This method is called in case of an IOException.
283288
* It will potentially trigger a checkMaster() to check the status of all servers.
284289
* @param t the exception thrown
285-
* @param slaveOk slaveOk flag
290+
* @param secondaryOk secondaryOk flag
286291
* @return true if the request should be retried, false otherwise
287292
* @throws MongoException
288293
*/
289-
boolean _error( Throwable t, boolean slaveOk )
294+
boolean _error( Throwable t, boolean secondaryOk )
290295
throws MongoException {
291296
if (_rsStatus == null) {
292297
// single server, no need to retry
@@ -297,14 +302,14 @@ boolean _error( Throwable t, boolean slaveOk )
297302
// if no server is up, we wont retry until the updater thread finds one
298303
// this is to cut down the volume of requests/errors when all servers are down
299304
if ( _rsStatus.hasServerUp() ){
300-
checkMaster( true , !slaveOk );
305+
checkMaster( true , !secondaryOk );
301306
}
302307
return _rsStatus.hasServerUp();
303308
}
304309

305310
class MyPort {
306311

307-
DBPort get( boolean keep , boolean slaveOk , ServerAddress hostNeeded ){
312+
DBPort get( boolean keep , ReadPreference readPref, ServerAddress hostNeeded ){
308313

309314
if ( hostNeeded != null ){
310315
// asked for a specific host
@@ -325,8 +330,8 @@ DBPort get( boolean keep , boolean slaveOk , ServerAddress hostNeeded ){
325330
_requestPort = null;
326331
}
327332

328-
if ( slaveOk && _rsStatus != null ){
329-
// if slaveOk, try to use a secondary
333+
if ( !(readPref == ReadPreference.PRIMARY) && _rsStatus != null ){
334+
// if not a primary read set, try to use a secondary
330335
ServerAddress slave = _rsStatus.getASecondary();
331336
if ( slave != null ){
332337
return _portHolder.get( slave ).get();

src/main/com/mongodb/OutMessage.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,22 @@
2626
import java.io.OutputStream;
2727
import java.util.concurrent.atomic.AtomicInteger;
2828

29-
import org.bson.BSONEncoder;
30-
import org.bson.BSONObject;
29+
import org.bson.*;
3130
import org.bson.io.PoolOutputBuffer;
3231
import org.bson.types.ObjectId;
3332

34-
class OutMessage extends BSONEncoder {
33+
class OutMessage extends BasicBSONEncoder {
3534

3635
static AtomicInteger ID = new AtomicInteger(1);
37-
36+
3837
static OutMessage query( Mongo m , int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields ){
38+
return query( m, options, ns, numToSkip, batchSize, query, fields, ReadPreference.PRIMARY );
39+
}
40+
41+
static OutMessage query( Mongo m , int options , String ns , int numToSkip , int batchSize , DBObject query , DBObject fields, ReadPreference readPref ){
3942
OutMessage out = new OutMessage( m , 2004 );
4043
out._appendQuery( options , ns , numToSkip , batchSize , query , fields );
44+
out.setReadPreference( readPref );
4145
return out;
4246
}
4347

@@ -198,9 +202,19 @@ public int putObject(BSONObject o) {
198202
return sz;
199203
}
200204

205+
206+
public ReadPreference getReadPreference(){
207+
return _readPref;
208+
}
209+
210+
public void setReadPreference( ReadPreference readPref ){
211+
_readPref = readPref;
212+
}
213+
201214
private Mongo _mongo;
202215
private PoolOutputBuffer _buffer;
203216
private int _id;
204217
private int _queryOptions = 0;
218+
private ReadPreference _readPref = ReadPreference.PRIMARY;
205219

206220
}

0 commit comments

Comments
 (0)