[JAVA-1767] listCollections helper fails with direct connection to a secondary Created: 17/Apr/15  Updated: 01/Apr/16  Resolved: 20/Apr/15

Status: Closed
Project: Java Driver
Component/s: Cluster Management
Affects Version/s: 3.0.0
Fix Version/s: 3.0.1

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

Issue Links:
Duplicate
is duplicated by JAVA-2081 MongoClient does not respect ReadPref... Closed

 Description   

According the the server selection specification, when directly connected to a single MongoDB server command helpers like listCollections that intentionally ignore the default read preference on MongoDatabase should still send the command to the server, even if it's a non-primary member of a replica set.

Currently the driver fails to execute listCollections and other similar command when directly connected to a non-primary replica set member.

===============================================
Original Description:

Hi,

I am not able to get to connect to the secondary using secondaryPreferred global preference. Here is my code

MongoClientOptions options = new MongoClientOptions.Builder().connectTimeout(20000).readPreference(ReadPreference.secondaryPreferred()).serverSelectionTimeout(12000).build();
....
m = new MongoClient(new ServerAddress(host, port), creds, options);

When the provided host is a secondary of a replica set the code fails with the following error
Timed out after 12000 ms while waiting for a server that matches ReadPreferenceServerSelector

{readPreference=primary}

.
Client view of cluster state is {type=REPLICA_SET, servers=[

{address=sg-302test6-1063.devservers.mongodirector.com:27017, type=REPLICA_SET_SECONDARY, roundTripTime=2.8 ms, state=C ONNECTED}

]

Looks like the right readPreference is not being preserved in this case.

If I try to connect to the primary it appears to work fine.



 Comments   
Comment by Jeffrey Yemin [ 05/May/15 ]

Closed for 3.0.1 release.

Comment by Jeffrey Yemin [ 30/Apr/15 ]

Sorry about that. This is the proper link: https://oss.sonatype.org/content/repositories/snapshots/org/mongodb/mongo-java-driver/3.1.0-SNAPSHOT/

Comment by Dharshan Rangegowda [ 30/Apr/15 ]

I hit another similar issue which I have filed - https://jira.mongodb.org/browse/JAVA-1786.

Comment by Dharshan Rangegowda [ 30/Apr/15 ]

Thanks. I get a 404 when I try to download the snapshot release

404 - Path /org/mongodb/mongo-java-driver/3.1.0-SNAPSHOT/ not found in local storage of repository "Releases" [id=releases]

Comment by Jeffrey Yemin [ 29/Apr/15 ]

Hi Darshan,

We don't have a release date yet, but I expect it will be in the next couple of weeks. Until then you can use the snapshot release. See http://mongodb.github.io/mongo-java-driver/ for instructions on how to do that.

Thanks for your patience.

Comment by Dharshan Rangegowda [ 29/Apr/15 ]

Hi Jeff - when will 3.0.1 be available at https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongo-java-driver/?

Comment by Githook User [ 20/Apr/15 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: When selecting a server that satisifies a read preference, select any ok server when cluster connection mode is SINGLE.

JAVA-1767
Branch: 3.0.x
https://github.com/mongodb/mongo-java-driver/commit/b1ec0944ce8df1247876ad2f05da643053fb8bee

Comment by Githook User [ 20/Apr/15 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: When selecting a server that satisifies a read preference, select any ok server when cluster connection mode is SINGLE.

JAVA-1767
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/dd5b36bfd1aa63d893edfe4f024fd4831bc2d0fb

Comment by Jeffrey Yemin [ 18/Apr/15 ]

The driver will only discover the other members of the replica set if you use the MongoClient constructor that takes a List of ServerAddress instances.

m = new MongoClient(Arrays.asList(new ServerAddress(host, port)), creds, options);

The way you have it now, the driver will only ever connect to the one MongoDB server running locally.

Comment by Dharshan Rangegowda [ 18/Apr/15 ]

Some of my mongo replicas run locally on the application box and the application connects locally using 127.0.0.1. This is the reason I just use one address instead of multiple hosts. Either way it seems possible that you can discover the rest of the servers even if just one server is specified.

Comment by Jeffrey Yemin [ 18/Apr/15 ]

OK, we can reproduce this. Thanks for the information.

Can you tell us though why you're directly connecting to a secondary rather than specifying multiple hosts?

Comment by Dharshan Rangegowda [ 17/Apr/15 ]

Hi Craig,

The operations I tried was to list collections

MongoDatabase db = m.getDatabase("test");
for(String name: db.listCollectionNames())

{ Logger.info(context + "Listed Collection %s", name); }

Another thing I noticed that it starts to work when I pass a list of hosts instead of a single host. The reason I pass a single host is that I have code running on the secondary machine that connects to 127.0.0.1.

Hope that helps.

Comment by Craig Wilson [ 17/Apr/15 ]

Hi Darshan,

Some operations in MongoDB don't honor read preference, particularly write commands. Could you provide the code for the operation that is failing?

Craig

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