[JAVA-2544] Messy way to determine if a Client connection is successful Created: 13/Jun/17  Updated: 27/Oct/23  Resolved: 27/Jun/17

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

Type: Improvement Priority: Major - P3
Reporter: Andrew Harris Assignee: Ross Lawley
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Currently, when you try to connect to a remote MongoDB server, the connection will appear to be successful even though the remote server is down. The only indication is from the log messages but the Java API continues as normal.

To make a connection and then check the connection is successful then Java programs need do something similar to the following...

MongoClient mongoClient = new MongoClient();
mongoClient.getAddress();

I'd expect the first line to throw an exception if a connection to any server (in a server list say) failed but it doesn't so the second line is required in order to do it. Very annoying.

I propose that either an immediate MongoException is thrown by the MongoClient or you introduce a method where the connection can be tested (e.g. an isClosed or whatever method on the MongoClient class). Both would be good though. One for immediate connection problems and the other for subsequent checks by the application.



 Comments   
Comment by Ross Lawley [ 27/Jun/17 ]

Marking this as "works as designed" for the reasons discussed.

As mentioned the Listener inferfaces can also be used to determine the state of the cluster.

All the best,

Ross

Comment by Jeffrey Yemin [ 26/Jun/17 ]

There's also ClusterListener, which is a bit higher level that ServerMonitorListener. The reference documentation has a simple example of its use: http://mongodb.github.io/mongo-java-driver/3.4/driver/reference/monitoring/.

Comment by Ross Lawley [ 26/Jun/17 ]

ServerMonitorListener is available in 3.3. We are also improving the usability for the ConnectionListener and ConnectionPoolListener in 3.5 - see JAVA-805 for more information.

Comment by Andrew Harris [ 26/Jun/17 ]

That listener idea is interesting. Is that only possible in 3.5+?

Regards,

Andy

Comment by Ross Lawley [ 26/Jun/17 ]

Hi aharris,

Apologies if I was unclear in my response. When I mentioned its a complicated issue, this is because without knowing the exact read preference or write concern of any given operation, it can be impossible to determine if the "connection" is successful or not. For example when connecting to a replicaset - if for some reason there is no primary (due to an election) then writes will fail, where as secondary reads would succeed. In that scenario, the definition of a successful connection can change depending on the users action.

An alternative example, is when connecting to a Sharded Cluster, if a user provides multiple MongoS to connect to then the driver is resilient. If any MongoS is down for any reason then operations can continue without impacting the system. In fact, it is this design that allows for rolling server upgrades without downtime for users.

One alternative, would be to use the Listener API to detect server state, The ServerMonitorListener which can be configured in the MongoClientOptions.

I hope helps,

Ross

Comment by Andrew Harris [ 26/Jun/17 ]

That's no different to how I do it now but I don't accept your argument
that it is a complicated issue. Surely the driver must know when it is
connected to something (a socket connection is established) and so that
state can be read through the java driver api?

Comment by Ross Lawley [ 26/Jun/17 ]

Hi aharris,

Many thanks for your ticket. When you create a MongoClient in the Java driver, a connection pool is created in the background. The process follows the Server Discovery and Monitoring Specifcation. As you might expect in a distributed system, discovery and monitoring of services can be a non trivial process. As distributed systems are complex, they are designed to be fault tolerant, a great example of that is MongoDB's ReplicaSet system that provides fault tolerance and automatic failover.

The notion of a successful connection is further complicated in MongoDB. As MongoDB is a replicated fault tolerant distributed system users are given the flexibility about where reads come from and where writes occur. This is specified in the Server Selection specification.

All in all, this makes the question about whether or not any given MongoClient is successful non-trivial. It can depend on many factors and how the client is intended to be used. Perhaps the best way to ensure that the connection is valid at a given point in time, for a given read / write preference is to issue a command to the server. I have seen users issue a command such as MongoClient#listDatabases or MongoDatabase#listCollections, to achieve this aim.

I hope that helps,

Ross

Comment by Mark Agarunov [ 22/Jun/17 ]

Hello aharris,

Thank you for the report. I've set the fixVersion to "Needs Triage" for this new feature to be scheduled against our currently planned work. Updates will be posted on this ticket as they happen.

Thanks,
Mark

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