[JAVA-680] Secondary reads on lists of mongos fail until first write Created: 26/Oct/12  Updated: 08/Jan/13  Resolved: 30/Oct/12

Status: Closed
Project: Java Driver
Component/s: Connection Management
Affects Version/s: 2.9.2
Fix Version/s: 2.9.3, 2.10.0

Type: Bug Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
related to JAVA-656 Don't checkMaster on slaveOk queries Closed

 Description   

The fix for JAVA-656 introduced a regression. In the case where you have use the Mongo constructor that takes a list of ServerAddress, or a MongoURI with a list of server addresses, _masterPortPool starts as null. Before this change, the checkMaster call would block until an initial master was found. Now that it no longer does, there is a race between the background thread and application threads. If an application only does non-primary reads, and the list of servers is a list of mongos, until a write is done, then ensureMaster will never be called and you will keep getting this exception:

10-25-2012 17:01:00 [Thread-427] ERROR com.xgen.svc.mms.svc.ping.BatchPrePopulationSvc [run:136] - Rare case where master=null, probably all servers are down
com.mongodb.MongoException: Rare case where master=null, probably all servers are down
	at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:434) ~[mongo.jar:na]
	at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:276) ~[mongo.jar:na]
	at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:256) ~[mongo.jar:na]
	at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:289) ~[mongo.jar:na]
	at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:274) ~[mongo.jar:na]
	at com.mongodb.DBCursor._check(DBCursor.java:368) ~[mongo.jar:na]
	at com.mongodb.DBCursor._hasNext(DBCursor.java:459) ~[mongo.jar:na]
	at com.mongodb.DBCursor.hasNext(DBCursor.java:484) ~[mongo.jar:na]

This test demonstrates the problem.

public class JAVA656Test {
    public static void main(String[] args) throws UnknownHostException {
        Mongo mongo = new Mongo(Arrays.asList(new ServerAddress("localhost:30999")));
        mongo.setReadPreference(ReadPreference.secondary());
        DBCollection collection = mongo.getDB("test").getCollection("JAVA656Test");
        for (int i = 0; i < 5; i++) {
            try {
                collection.findOne();
            } catch (MongoException e) {
                System.out.println(e.getMessage());
            }
        }
 
        System.out.println("Inserting");
        collection.insert(new BasicDBObject());
 
        System.out.println("Trying again");
        collection.findOne();
        System.out.println("Done");
    }
}

Output is:

Rare case where master=null, probably all servers are down
Rare case where master=null, probably all servers are down
Rare case where master=null, probably all servers are down
Rare case where master=null, probably all servers are down
Rare case where master=null, probably all servers are down
Inserting
Trying again
Done



 Comments   
Comment by auto [ 08/Jan/13 ]

Author:

{u'date': u'2012-10-25T22:15:13Z', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: JAVA-680: Fixing a regression introduced in the previous commit for this issue. In the case where the driver
is not connected to a replica set, like in mongos HA, still may need to ensure there is a master
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/356a633be8ad38760caf165a15045936a08e0a31

Comment by auto [ 08/Jan/13 ]

Author:

{u'date': u'2012-10-25T22:15:13Z', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: JAVA-680: Fixing a regression introduced in the previous commit for this issue. In the case where the driver
is not connected to a replica set, like in mongos HA, still may need to ensure there is a master
Branch: 2.10.x
https://github.com/mongodb/mongo-java-driver/commit/356a633be8ad38760caf165a15045936a08e0a31

Comment by auto [ 27/Oct/12 ]

Author:

{u'date': u'2012-10-25T15:15:13-07:00', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: JAVA-680: Fixing a regression introduced in the previous commit for this issue. In the case where the driver
is not connected to a replica set, like in mongos HA, still may need to ensure there is a master
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/af8066db002fea54854fe96bb240bb7b7501fb96

Comment by auto [ 26/Oct/12 ]

Author:

{u'date': u'2012-10-25T15:15:13-07:00', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: JAVA-680: Fixing a regression introduced in the previous commit for this issue. In the case where the driver
is not connected to a replica set, like in mongos HA, still may need to ensure there is a master
Branch: release-2.9.x
https://github.com/mongodb/mongo-java-driver/commit/356a633be8ad38760caf165a15045936a08e0a31

Comment by Jeffrey Yemin [ 26/Oct/12 ]

This is a regression introduced by JAVA-656

Generated at Thu Feb 08 08:52:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.