[JAVA-2269] Unable to connect to cluster DB in AWS using Java Async driver Created: 01/Aug/16  Updated: 11/Sep/19  Resolved: 04/Aug/16

Status: Closed
Project: Java Driver
Component/s: API, Async, Connection Management
Affects Version/s: 3.2.2
Fix Version/s: None

Type: Task Priority: Blocker - P1
Reporter: KRISHNA KUMAR Assignee: Unassigned
Resolution: Done Votes: 0
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to JAVA-2271 ConnectionString.getCredentialList sh... Closed

 Description   

Hi,
We are not able to connect to the MongoDB cluster environment in AWS using Async Java driver.

The issue is mentioned here: http://stackoverflow.com/questions/38472781/replica-set-connection-issue-mongodb-writableserverselector

We spent lot of time writing code using Async Java driver finally having a blocker in AWS environment. We are able to connect using Spring Mongo though.

Regards
Krishna



 Comments   
Comment by Chandramouli Putta [ 05/Aug/16 ]

Thank you so much, Ross..!!

Comment by Ross Lawley [ 04/Aug/16 ]

Hi Chandra,

I'm really pleased that has fixed it.

In response to your comments:

1. I had to upgrade to the async jar verison 3.3.0 since .serverSettings(ServerSettings.builder().applyConnectionString(hrvuConnectionString).build()) this line is not working with 3.2.2.

Sorry, I had just taken the code from master - if you aren't using heartbeat frequency then it wouldn't matter but upgrading to 3.3.0. Here are the changes for 3.3 http://mongodb.github.io/mongo-java-driver/3.3/whats-new/.

2. I am still curious about the connection opening and closing messages with the earlier situation. How was the driver obtaining the connections to the servers if there was a credentials issue as in the fix.

You can connect to MongoDB without requiring authentication, for example calling isMaster to understand the toplogy. However, the connection must be authenticated to read or write to a database / collection.

3. I see that credentials part of the connectionString is final and there were no errors when we used it. We were not able to figure out where the problem was and expecting something about the error to be logged somewhere.

I fully appreciate that was confusing and I'm please to say because of your report we've added JAVA-2271. This will ensure in the future it is clear that the ConnectionString class is immutable and getCredentials will return an immutable list.

All the best,

The JVM drivers team

Comment by Chandramouli Putta [ 04/Aug/16 ]

Hello Ross,

The fix is working now. Thank you very much for the heads-up.

A few observations:
1. I had to upgrade to the async jar verison 3.3.0 since .serverSettings(ServerSettings.builder().applyConnectionString(hrvuConnectionString).build()) this line is not working with 3.2.2.
2. I am still curious about the connection opening and closing messages with the earlier situation. How was the driver obtaining the connections to the servers if there was a credentials issue as in the fix.
3. I see that credentials part of the connectionString is final and there were no errors when we used it. We were not able to figure out where the problem was and expecting something about the error to be logged somewhere.

Thanks again for the help.

Best Regards,
Chandra.

Comment by Chandramouli Putta [ 02/Aug/16 ]

Thank you, Ross...!! I will try and get back to you soon.

Comment by KRISHNA KUMAR [ 02/Aug/16 ]

Thanks!

Comment by Ross Lawley [ 02/Aug/16 ]

No problems - I updated the pseudo code example - as I hadn't null checked previously. There may still be an issue - so please double check!

Comment by KRISHNA KUMAR [ 02/Aug/16 ]

Thanks a lot Ross, we shall try the suggestion and get back to you soon. Appreciate your timely help.

Comment by Ross Lawley [ 02/Aug/16 ]

Hi p.moulich,

Username and password can be set via the connection string - please see the connection string documentation for more information and the mongo java driver authentication documentation .

The bug is in your code not in the driver and can be fixed by providing the correct connection string or by setting the credentials via MongoClientSettings eg:

MongoClientSettings.Builder builder = MongoClientSettings.builder()
    .clusterSettings(ClusterSettings.builder().applyConnectionString(connectionString).build())
    .connectionPoolSettings(ConnectionPoolSettings.builder().applyConnectionString(connectionString).build())
    .serverSettings(ServerSettings.builder().applyConnectionString(connectionString).build())
    .sslSettings(SslSettings.builder().applyConnectionString(connectionString).build())
    .socketSettings(SocketSettings.builder().applyConnectionString(connectionString).build());
 
if (connectionString.getReadPreference() != null) {
    builder.readPreference(connectionString.getReadPreference());
}
if (connectionString.getReadConcern() != null) {
    builder.readConcern(connectionString.getReadConcern());
}
if (connectionString.getWriteConcern() != null) {
    builder.writeConcern(connectionString.getWriteConcern());
}
 
builder.credentialList(** Insert Credentials List Here **);
 
MongoClients.create(builder.build());

Ross

Comment by Chandramouli Putta [ 02/Aug/16 ]

Hello Ross,

Thanks for the update.
connectionString.getCredentialList().add(credentials);

If the above line is a bug, what is the preferred way of adding credentials? Is there a new jar version where this is fixed? We have used 3.2.0 and 3.2.2 versions of the Async driver.

Comment by Chandramouli Putta [ 02/Aug/16 ]

2016-08-02 10:31:04.826 [W] [ ] [org.springframework.core.io.support.PathMatchingResourcePatternResolver] | Skipping [/opt/vgt/high-resolution-vehicle-utilization-server-deployable/70/runtime/jar/application/high-resolution-vehicle-utilization-server-app-70.war] because it does not denote a directory
2016-08-02 10:31:06.223 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | MongoConf::Getting High Res Collection
2016-08-02 10:31:06.223 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | MongoConf::Getting Database
2016-08-02 10:31:06.245 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | getMongoClient::Replica Set Exists::rst-hrvu-shared-qa
2016-08-02 10:31:06.245 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Auth DB: admin
2016-08-02 10:31:06.246 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo port: 27017
2016-08-02 10:31:06.246 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Max Pool Size: 30
2016-08-02 10:31:06.246 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Min Pool Size: 5
2016-08-02 10:31:06.246 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Auth Max Idle Time In MS: 60000
2016-08-02 10:31:06.246 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Replica Set: rst-hrvu-shared-qa
2016-08-02 10:31:06.246 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo host property: hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io,hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io,hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io
2016-08-02 10:31:06.246 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo port: 27017
2016-08-02 10:31:06.247 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo server: hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.248 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo server: hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.248 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo server: hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27
 
2016-08-02 10:31:06.258 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | getMongoConnectionString::finalConnString::hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017,hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017,hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.275 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | getMongoConnectionString::ConnectionString::mongodb://hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017,hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017,hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017/?replicaSet=rst-hrvu-shared-qa&w=0&readPreference=secondaryPreferred&maxPoolSize=30&minpoolsize=5&maxidletimems=60000
2016-08-02 10:31:06.275 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo User: hrvu
2016-08-02 10:31:06.275 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Password: 
2016-08-02 10:31:06.275 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo host property: hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io,hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io,hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io
2016-08-02 10:31:06.277 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo User in MongoCreds: hrvu
2016-08-02 10:31:06.277 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Password in MongoCreds: [C@5c31e31e
2016-08-02 10:31:06.332 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Auth DB: admin
2016-08-02 10:31:06.333 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo port: 27017
2016-08-02 10:31:06.333 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Max Pool Size: 30
2016-08-02 10:31:06.333 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Min Pool Size: 5
2016-08-02 10:31:06.333 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Auth Max Idle Time In MS: 60000
2016-08-02 10:31:06.334 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Replica Set: rst-hrvu-shared-qa
2016-08-02 10:31:06.334 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo host property: hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io,hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io,hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io
2016-08-02 10:31:06.334 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo port: 27017
2016-08-02 10:31:06.334 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo server: hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.334 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo server: hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.334 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo server: hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.335 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | getMongoConnectionString::finalConnString::hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017,hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017,hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.335 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | getMongoConnectionString::ConnectionString::mongodb://hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017,hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017,hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017/?replicaSet=rst-hrvu-shared-qa&w=0&readPreference=secondaryPreferred&maxPoolSize=30&minpoolsize=5&maxidletimems=60000
2016-08-02 10:31:06.335 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo User: hrvu
2016-08-02 10:31:06.335 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Password: 
2016-08-02 10:31:06.335 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo host property: hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io,hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io,hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io
2016-08-02 10:31:06.336 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo User in MongoCreds: hrvu
2016-08-02 10:31:06.336 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Mongo Password in MongoCreds: [C@53ea38ff
2016-08-02 10:31:06.372 [I] [ ] [org.mongodb.driver.cluster] | Cluster created with settings {hosts=[hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='120000 ms', maxWaitQueueSize=150, requiredReplicaSetName='rst-hrvu-shared-qa'}
2016-08-02 10:31:06.372 [I] [ ] [org.mongodb.driver.cluster] | Adding discovered server hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017 to client view of cluster
2016-08-02 10:31:06.419 [I] [ ] [org.mongodb.driver.cluster] | Adding discovered server hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017 to client view of cluster
2016-08-02 10:31:06.421 [I] [ ] [org.mongodb.driver.cluster] | Adding discovered server hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017 to client view of cluster
2016-08-02 10:31:06.425 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | MongoConf::Returning Cluster Client
2016-08-02 10:31:06.453 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | getHighResVehicleUtilizationCollection::Creating Indexes for Collection
2016-08-02 10:31:06.480 [I] [ ] [org.mongodb.driver.cluster] | No server chosen by WritableServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, all=[ServerDescription{address=hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 120000 ms before timing out
2016-08-02 10:31:06.482 [I] [ ] [org.mongodb.driver.cluster] | No server chosen by WritableServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, all=[ServerDescription{address=hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 120000 ms before timing out
2016-08-02 10:31:06.483 [I] [ ] [org.mongodb.driver.cluster] | 
 chosen by WritableServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, all=[ServerDescription{address=hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 120000 ms before timing out
2016-08-02 10:31:06.529 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | MongoConf::::Update Options::Bypass Document Validation::true
2016-08-02 10:31:06.529 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | MongoConf::::Update Options::Upsert::false
2016-08-02 10:31:06.577 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:1, serverValue:74873}] to hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.582 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:5, serverValue:73891}] to hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.577 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:2, serverValue:74874}] to hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.585 [I] [ ] [org.mongodb.driver.cluster] | Monitor thread successfully connected to server with description ServerDescription{address=hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 6]}, minWireVersion=0, maxWireVersion=4, electionId=7fffffff0000000000000001, maxDocumentSize=16777216, roundTripTimeNanos=1387725, setName='rst-hrvu-shared-qa', canonicalAddress=hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, hosts=[hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017], passives=[], arbiters=[], primary='hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017', tagSet=TagSet{[]}}
2016-08-02 10:31:06.588 [I] [ ] [org.mongodb.driver.cluster] | Setting max election id to 7fffffff0000000000000001 from replica set primary hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.591 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:8, serverValue:73892}] to hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.591 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:7, serverValue:74875}] to hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.592 [I] [ ] [org.mongodb.driver.cluster] | Discovered replica set primary hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.592 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:4, serverValue:73996}] to hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.593 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:3, serverValue:73995}] to hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.595 [I] [ ] [org.mongodb.driver.cluster] | Monitor thread successfully connected to server with description ServerDescription{address=hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 6]}, minWireVersion=0, maxWireVersion=4, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=2481088, setName='rst-hrvu-shared-qa', canonicalAddress=hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelessc:
 
2016-08-02 10:31:06.604 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:6, serverValue:73890}] to hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.609 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:11, serverValue:73997}] to hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.610 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:10, serverValue:74876}] to hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.613 [I] [ ] [org.mongodb.driver.cluster] | Monitor thread successfully connected to server with description ServerDescription{address=hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 6]}, minWireVersion=0, maxWireVersion=4, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=7918207, setName='rst-hrvu-shared-qa', canonicalAddress=hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, hosts=[hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017, hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017], passives=[], arbiters=[], primary='hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017', tagSet=TagSet{[]}}
2016-08-02 10:31:06.636 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:12, serverValue:73998}] to hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.649 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:9, serverValue:73893}] to hrvu-c.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.653 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:13, serverValue:74877}] to hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.655 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:14, serverValue:73999}] to hrvu-b.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.679 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Created an Index for the field VIN....!null
2016-08-02 10:31:06.680 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Created an Index for the field driveSessionId....!null
2016-08-02 10:31:06.683 [I] [ ] [com.wirelesscar.componentbase.ping.PingRegistry] | Registering pingable with identifier com.wirelesscar.subscriptionrepository.client.SubscriptionRepositoryImpl. Type: com.wirelesscar.subscriptionrepository.client.SubscriptionRepositoryImpl
2016-08-02 10:31:06.683 [I] [ ] [com.wirelesscar.highresvu.conf.MongoConf] | Created an Index for the field Chassis ID....!null
2016-08-02 10:31:06.693 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:16, serverValue:74878}] to hrvu-a.singlenode-mongodb.qa.shared.eu-west-1.qa.aws.wirelesscar.io:27017
2016-08-02 10:31:06.700 [I] [ ] [org.mongodb.driver.connection] | Opened connection [connectionId{localValue:17, serverValue:74000}] to hrvu-b.singlenode-mongodb:

Comment by Ross Lawley [ 02/Aug/16 ]

Theres quite a lot happening in the code sample but one line immediately stands out as a bug:

connectionString.getCredentialList().add(credentials);

This will not work as expected - the connectionString is immutable and as such that line won't add the credentials to it. This would explain a failure to connect to the database.

Ross

Comment by Chandramouli Putta [ 02/Aug/16 ]

Hello Ross,

Below is the code we have at this moment. I have also added comments (COMMENT is the initial code we tried with) and logging statements in all the methods in which we are trying to connect to the Mongo DB server.

@Bean
  public MongoClient getMongoClient() throws Exception {
    //MongoClient mongoClient = MongoClients.create(getMongoConnectionString());  -- COMMENT - 1 -- Used this as well - without the below if-else
    String replicaSet = config().getPlatformString("mongodb.replicaSetName", "");
    LOGGER.info("getMongoClient::Replica Set Exists::"+replicaSet);
    if(null==replicaSet || "".equals(replicaSet.trim())) {
      LOGGER.info("MongoConf::Returning Standalone Client");
      return MongoClients.create(getMongoConnectionString());
    }
    else {
      ConnectionString hrvuConnectionString = getMongoConnectionString();
      MongoClient mongoClusterClient = MongoClients.create(MongoClientSettings.builder()
                                    .clusterSettings(ClusterSettings.builder()
                                          .applyConnectionString(hrvuConnectionString)
                                          .serverSelectionTimeout(120, TimeUnit.SECONDS)
                                          .build())
                                          .connectionPoolSettings(ConnectionPoolSettings.builder()
                                                        .applyConnectionString(hrvuConnectionString)
                                                        .build())
                                                        .serverSettings(ServerSettings.builder().build())
                                                        .credentialList(getMongoConnectionString().getCredentialList())
                                                        .sslSettings(SslSettings.builder()
                                                            .applyConnectionString(hrvuConnectionString)
                                                            .build())
                                                            .socketSettings(SocketSettings.builder()
                                                                .applyConnectionString(hrvuConnectionString)
                                                                .build())
                                                                .build());
      LOGGER.info("MongoConf::Returning Cluster Client");
      return mongoClusterClient;
    }
  }
  
  private List<ServerAddress> getMongoServerAddresses() throws UnknownHostException {
    String hostsString = config().getPlatformString("mongodb.host", "localhost");
    LOGGER.info("Mongo host property: " + hostsString);
    int port = (int) config().getPlatformLong("mongodb.port", 27017L);
    LOGGER.info("Mongo port: " + port);
 
    List<String> hosts = Arrays.asList(hostsString.split(","));
    ArrayList<ServerAddress> serverAddresses = new ArrayList<>();
    for (String host : hosts) {
      LOGGER.info(String.format("Mongo server: %s:%d", host, port));
      serverAddresses.add(new ServerAddress(host, port));
    }
 
    return serverAddresses;
  }
  
  private ConnectionString getMongoConnectionString() throws Exception {
 
    String authdb = config().getPlatformString("mongodb.authdb", null);
    int port = (int) config().getPlatformLong("mongodb.port", 27017L);
    String maxPoolSize = config().getPlatformString("mongodb.maxPoolSize", "100");
    String minPoolSize = config().getPlatformString("mongodb.minPoolSize", "30");
    String maxIdleTimeMs = config().getPlatformString("mongodb.maxidletimems", "60000");
    String replicaSet = config().getPlatformString("mongodb.replicaSetName", ""); //COMMENT - 3 -- Added after started getting the timeout error
    LOGGER.info("Mongo Auth DB: " + authdb);
    LOGGER.info("Mongo port: " + port);
    LOGGER.info("Mongo Max Pool Size: " + maxPoolSize);
    LOGGER.info("Mongo Min Pool Size: " + minPoolSize);
    LOGGER.info("Mongo Auth Max Idle Time In MS: " + maxIdleTimeMs);
    LOGGER.info("Mongo Replica Set: " + replicaSet);
 
    List<ServerAddress> serverAddresses = getMongoServerAddresses();
    String finalConnString = serverAddresses.stream().map(serverAddr -> serverAddr.getHost() + ":" + serverAddr.getPort())
        .collect(Collectors.joining(",", "", ""));
    LOGGER.info("getMongoConnectionString::finalConnString::" + finalConnString);
 
    ConnectionString connectionString = null;
    
    /*connectionString = new ConnectionString("mongodb://" + finalConnString + "/?maxPoolSize="+ maxPoolSize 
        + "&minpoolsize=" + minPoolSize + "&maxidletimems=" + maxIdleTimeMs);*/ -- COMMENT - 2 -- Initial attempt - without the below if-else
    if(null == replicaSet || "".equals(replicaSet.trim())) {
      connectionString = new ConnectionString("mongodb://" + finalConnString + "/?maxPoolSize="+ maxPoolSize 
          + "&minpoolsize=" + minPoolSize + "&maxidletimems=" + maxIdleTimeMs);
    } else {
      connectionString = new ConnectionString("mongodb://" + finalConnString + "/?replicaSet="+replicaSet
          +"&w=0&readPreference=secondaryPreferred&maxPoolSize="+ maxPoolSize + "&minpoolsize=" + minPoolSize + "&maxidletimems=" + maxIdleTimeMs);
    }
 
    LOGGER.info("getMongoConnectionString::ConnectionString::" + connectionString.getConnectionString());
    
 
    String user = config().getPlatformString("mongodb.user", null);
    String password = config().getPlatformString("mongodb.password", null);
    String hostsString = config().getPlatformString("mongodb.host", "localhost");
    LOGGER.info("Mongo User: " + user);
    LOGGER.info("Mongo Password: " + password);
    LOGGER.info("Mongo host property: " + hostsString);
 
    MongoCredential credentials = MongoCredential.createCredential(user, authdb, password.toCharArray());
    LOGGER.info("Mongo User in MongoCreds: " + credentials.getUserName());
    LOGGER.info("Mongo Password in MongoCreds: " + credentials.getPassword().toString());
 
    connectionString.getCredentialList().add(credentials);
 
    return connectionString;
 
  }

I am trying to extract the full logs again.....but couldn't extract due to some other errors. Will share once available.

Comment by Ross Lawley [ 02/Aug/16 ]

With the Spring Data, we didn't need any of these configurations except having the db nodes listed in the connection string. And the application is also able to access the database.

This should also be the case with the async driver - I'm trying to understand what in your configuration is failing.

Does simplifying and just supplying the connection string to MongoClients#create work or are you still seeing these timeout messages?

Please try that and attach the logs from the application showing the output. eg: MongoClients.create(connectionString);

You should not have to create or set the selector at all and the driver will handle that based on the read preference in case of reads or the primary in case of writes. If using just the same connection string (with no customised settings) on the same application server fails then there looks to be an issue. I want to ensure that there are no differences between the environments they are run on.

Ross

Comment by Chandramouli Putta [ 02/Aug/16 ]

Hi,

I have modified the SOF question.
http://stackoverflow.com/questions/38472781/replica-set-connection-issue-mongodb-writableserverselector

I have tried with multiple options (Creating a new writable selector and pass it in the cluster settings, and specifying replica set as the cluster type with the connection mode). Every time the error is same.
About the independent node connections, we are able to see in the logs that it is creating and closing the connections to the independent servers, but our application is not working.

With the Spring Data, we didn't need any of these configurations except having the db nodes listed in the connection string. And the application is also able to access the database.

Comment by Ross Lawley [ 01/Aug/16 ]

Hi Krishna,

Interesting, although I confess to being confused by the issue.

Perhaps there is some other issue at hand, in stackoverflow you mention:

What is more interesting is after reporting this, the application is obtaining the connections independently.

Can you clarify what you mean here? Does the application run as expected?

You also mention supplying a new instance of WritableServerSelector:

I see that it needs to get the primary node for db writes (node-a.singlenode-mongodb.qa.shared.eu.qa.aws.app.io in this case). My understanding is supplying a new instance of WritableServerSelector in the ClusterSettings while creating the client will be enough to select primaries using the selector's select method and it is proving wrong with this problem.

I couldn't see that in your code example? I'm unsure as to your intent here but does simplifying and just supplying the connection string to MongoClients#create work or are you still seeing these timeout messages?

Ross

Comment by KRISHNA KUMAR [ 01/Aug/16 ]

Hi Ross,

We had this checked with our clound infra team and confirmed that there is no network related issues. Today we tested with the Spring Mongo and it is working fine in the same environment. We initially considered this Async driver since the architecture team told us that it would be give some 20% more performance than Spring Mongo. We are curious why it fails though.

regards
Krishna

Comment by Ross Lawley [ 01/Aug/16 ]

Hi kkumarc@gmail.com,

Thanks for the ticket. From looking at your stackoverflow issue I can see the driver is waiting to connect to the MongoD's:

ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, all=[
ServerDescription{address=node-a.singlenode-mongodb.qa.shared.eu.qa.aws.app.io:27017, type=UNKNOWN, state=CONNECTING}, 
ServerDescription{address=node-b.singlenode-mongodb.qa.shared.eu.qa.aws.app.io:27017, type=UNKNOWN, state=CONNECTING}, 
ServerDescription{address=node-c.singlenode-mongodb.qa.shared.eu.qa.aws.app.io:27017, type=UNKNOWN, state=CONNECTING}]}. 
Waiting for 30000 ms before timing out

This state occurs when the Java application cannot reach the MongoD's via the network.

You will need to confirm that the application and the MongoD machines have the correct access / privledges / firewall rules so that each other to communicate over the network.

Ross

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