Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-812

setting slaveOK() does not result in a secondary read preference through mongos

    XMLWordPrintableJSON

Details

    Description

      Although slaveOk() is deprecated, if it is in use the expectation is it should behave the same as setting ReadPreference.secondaryPreferred();

      Instead the queries are seen with { $readPreference:

      { mode: "primary" }

      } explicitly set when going through a mongos

      Under 2.6 the following java sample will correctly result in a query to a Secondary. The same code in 2.7 or above will result with the primary $readPreference set.

      import com.mongodb.*;
      import java.net.UnknownHostException;
       
      public class TestSlaveOK{
      	public static void main(String[] args){
      		Mongo m = null;
      		DB db = null;
      		DBCollection coll = null;
      		
      		try{
      		
      		MongoOptions mongoOptions = new MongoOptions();
      		mongoOptions.slaveOk = true;
       
      		m = new Mongo("localhost:27017", mongoOptions);
      		db = m.getDB("test");
      		}
      		
      		catch(UnknownHostException uhe){
      		System.out.println("UnknownHostException:" + uhe);
      		}
      		coll = db.getCollection("test");
      		DBObject query = new BasicDBObject("x", "100");
      		DBObject obj = coll.findOne(query, null);	
      	}
      }

      Looking at the highest verbosity logs (or capturing the query in another way) under 2.11.1 will show the query executed with { $readPreference:

      { mode: "primary" }

      } on both the mongos and the Primary node:

      Tue Apr 23 16:37:37 [conn28] shard query: test.test  { $query: { x: "100" }, $readPreference: { mode: "primary" } }

      Attachments

        Activity

          People

            jeff.yemin@mongodb.com Jeffrey Yemin
            andre.defrere@mongodb.com Andre de Frere
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: