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

Query not going to secondary with a slaveDelay

      I am using Morphia 0.99.1-SNAPSHOT with 2.9.3 mongodb java driver.

      I have a replica set with one master and one slave. I want to use the slave for all reads, because to master will be busy enough with writes. The 60 seconds delay is there only so that I can validate my reads are really goind to the secondary (I will remove the delay in production).

      Here is the configuration of my replica set :

      rs.conf()
      {
          "_id" : "rsStat",
          "version" : 3,
          "members" : [
              {
                  "_id" : 0,
                  "host" : "someDNS:27024",
                  "priority" : 2
              },
              {
                  "_id" : 1,
                  "host" : "someDNS:27025",
                  "priority" : 0,
                  "slaveDelay" : 60
              }
          ]
      }
      

      Here is my java code :

      MyClass.java
      Morphia morphia = new Morphia();
      String host = "someDNS";
      int port = 27024;
      String dbName = "someDatabaseName";
      
      // Configure the mongo options
      MongoOptions options = new MongoOptions();
      options.autoConnectRetry = true;
      options.connectionsPerHost = 100;
      options.w = WriteConcern.NONE.getW();
      options.readPreference = ReadPreference.secondaryPreferred();
      // options.slaveOk = true;
      
      // Declare the real Mongo object
      Mongo mongo;
      
      // Look at the system.replset collection to know the replica set status
      Mongo mongoInit = new Mongo(new ServerAddress(host, port), options);
      DB localDb = mongoInit.getDB("local");
      DBCollection replsetCollection = localDb.getCollection("system.replset");
      DBObject replsetInfos = replsetCollection.findOne();
      
      // If the returned object is null, there is no replica set (a single node)
      if (replsetInfos == null)
      {
      	mongo = new Mongo(new ServerAddress(host, port), options);
      }
      else
      {
      	// It's a replica set, gather information of each node
      	List<ServerAddress> addresses = getReplicaSetInfos(host, replsetInfos);
      
      	// The Mongo constructor doesn't work with a list of only one address...
      	if (addresses.size() > 1)
      	{
      		mongo = new Mongo(addresses, options);
      	}
      	else
      	{
      		mongo = new Mongo(addresses.get(0), options);
      	}
      }
      
      // Create the morphia datastore
      Datastore datastore = morphia.createDatastore(mongo, dbName);
      datastore.getMongo().setReadPreference(ReadPreference.secondaryPreferred());
      
      morphia.mapPackage("some.package");
      
      datastore.ensureIndexes();
      
      List<SomeDocument> docs = datastore.find(SomeDocument.class).queryNonPrimary().asList();
      
      

      So, I'm doing a modification on the primary, changing a value in a document from "A" to "B". Few seconds later, I run a find() on the secondary in the mongo shell and I still see "A". Few seconds later, I run this java code and I get "B", the new value. Why is that?

      It looks like my reads are going to the primary : if I would have read from the secondary, I would get the "A" value.
      I'm setting the read preference to secondaryPreferred.
      I'm adding the queryNonPrimay().
      I think it's a bug that my read isn't going to the secondary.

      Is this bug fixed in a later version?

            Assignee:
            justin.lee Justin Lee
            Reporter:
            mchampagne Michael Champagne
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: