[JAVA-1118] Mongo Java Driver Descriptive Exception for No Primary Created: 19/Feb/14  Updated: 03/Jan/18  Resolved: 25/Nov/15

Status: Closed
Project: Java Driver
Component/s: API, Cluster Management
Affects Version/s: 2.11.2
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Linda Qin Assignee: Unassigned
Resolution: Won't Fix Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
related to SERVER-13660 Better Error Response from Shard with... Closed
Case:

 Description   

When a replica set primary is failing and primary reads and writes are performed, a MongoException is thrown. For example:

Caused by: com.mongodb.MongoException: ReplicaSetMonitor no master found for set: XXXX
  at com.mongodb.MongoException.parse(MongoException.java:82) ~[mongo-java-driver-2.11.2.jar:na]
  at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:292) ~[mongo-java-driver-2.11.2.jar:na]
  at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273) ~[mongo-java-driver-2.11.2.jar:na]
  at com.mongodb.DBCursor._check(DBCursor.java:368) ~[mongo-java-driver-2.11.2.jar:na]
  at com.mongodb.DBCursor._hasNext(DBCursor.java:459) ~[mongo-java-driver-2.11.2.jar:na]
  at com.mongodb.DBCursor.hasNext(DBCursor.java:484) ~[mongo-java-driver-2.11.2.jar:na]
  at com.google.code.morphia.query.MorphiaIterator.hasNext(MorphiaIterator.java:43) ~[morphia-0.103.jar:na]
  at com.google.code.morphia.query.QueryImpl.get(QueryImpl.java:416) ~[morphia-0.103.jar:na]

The only way to determine the true cause is to inspect the Exeption's message field. Ideally, we should be able to recognize this exception from its type without having to inspect/pattern match against the Exception's message field.
For example, there could be a new Exception

PrimaryNotFound extends MongoException

that is only thrown in this situation.



 Comments   
Comment by Jeffrey Yemin [ 25/Nov/15 ]

Without server support there's not much a driver can do. Closing as Won't Fix.

Comment by Jeffrey Yemin [ 01/May/14 ]

Without some server support, it's not clear what more we can do.

Comment by Doug Jones [ 18/Apr/14 ]

Ok, I created SERVER-13660 to start a discussion there.

Comment by Jeffrey Yemin [ 16/Apr/14 ]

I can't find anything doing a search. It practice I would think it would be difficult to do it cleanly in all cases, because it may be that multiple shards are involved in some operation and that operation is partially completed before mongos finds a shard with no primary and has to abort.

That said, please feel free to open an issue in the SERVER project with any ideas that you have on this.

Comment by Doug Jones [ 16/Apr/14 ]

Is there a plan to fix this on the server/mongos side? Any Jiras I should look at?

Comment by Jeffrey Yemin [ 16/Apr/14 ]

The server has not publicly documented the error code used to indicate this condition, so the driver just throws a generic error. That "error querying server" is what's returned by mongos.

Comment by Doug Jones [ 15/Apr/14 ]

Does this apply to queries that are routed through mongos? I'm trying to test this behavior in the 2.12.0 driver release and I get the following exception when no primary is available:

2014-04-15 18:25:45:108 EDT [main,,] ERROR RetryUtils - error
com.mongodb.MongoException: error querying server
	at com.mongodb.QueryResultIterator.throwOnQueryFailure(QueryResultIterator.java:214) ~[mongo-java-driver-2.12.0.jar:na]
	at com.mongodb.QueryResultIterator.init(QueryResultIterator.java:198) ~[mongo-java-driver-2.12.0.jar:na]
	at com.mongodb.QueryResultIterator.initFromQueryResponse(QueryResultIterator.java:176) ~[mongo-java-driver-2.12.0.jar:na]
	at com.mongodb.QueryResultIterator.<init>(QueryResultIterator.java:64) ~[mongo-java-driver-2.12.0.jar:na]
	at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:80) ~[mongo-java-driver-2.12.0.jar:na]
	at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:61) ~[mongo-java-driver-2.12.0.jar:na]
	at com.mongodb.DBCursor._check(DBCursor.java:458) ~[mongo-java-driver-2.12.0.jar:na]
	at com.mongodb.DBCursor._hasNext(DBCursor.java:546) ~[mongo-java-driver-2.12.0.jar:na]
	at com.mongodb.DBCursor.hasNext(DBCursor.java:571) ~[mongo-java-driver-2.12.0.jar:na]
	at org.mongodb.morphia.query.MorphiaIterator.hasNext(MorphiaIterator.java:50) ~[morphia-0.107.jar:na]

Comment by Jeffrey Yemin [ 25/Feb/14 ]

In 2.12.0, the driver will throw MongoServerSelectionException: https://github.com/mongodb/mongo-java-driver/blob/master/src/main/com/mongodb/BaseCluster.java#L78-82.

It's not quite as specific as PrimaryNotFound, but I think it will serve the purpose, and it has the advantage of being applicable not only to writes but to reads, regardless of read preference.

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