[CXX-875] Database::has_collection has ambiguous error modes Created: 23/Mar/16  Updated: 19/Sep/16  Resolved: 15/Apr/16

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 3.0.2

Type: New Feature Priority: Major - P3
Reporter: Pavel Odintsov Assignee: Samantha Ritter (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hello!

I'm writing application which uses Mongo. But actually sometimes mongo could not run in time due to Linux init manager issues.

So I need some way to check connection to Mongodb. I deeply reviewed API documents about C++ 11 driver but could not find any way to check connection without real requests.

Could you add something like this?

Also function db.has_collection() haven't any way to tell about errors. And case "no connection to mongo" and "we haven't this database" handled in same way. That's not perfect



 Comments   
Comment by Githook User [ 15/Apr/16 ]

Author:

{u'username': u'samantharitter', u'name': u'samantharitter', u'email': u'samantha.ritter@10gen.com'}

Message: CXX-875 database::has_collection throws if operation fails
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/140220923edd45f14f6a7b6b60b8264bf38ee2f7

Comment by Samantha Ritter (Inactive) [ 12/Apr/16 ]

code review: https://github.com/mongodb/mongo-cxx-driver/pull/481

Comment by Pavel Odintsov [ 24/Mar/16 ]

Perfect! Thank you

Comment by Andrew Morrow (Inactive) [ 24/Mar/16 ]

Thanks pavel.odintsov - I've updated the ticket description to capture the work we want to do and scheduled it for a release. We are hoping to release 3.0.1 this week with a number of other accumulated fixes, so I think this will need to wait until the next release after that, 3.0.2.

Comment by Pavel Odintsov [ 24/Mar/16 ]

I've changed ming about separate ping function. But has_collection still a bad code ... We need return code handling here.

Comment by Andrew Morrow (Inactive) [ 24/Mar/16 ]

pavel.odintsov - Have you changed your mind about has_collection as well? There seemed to be a legit complaint there that you couldn't distinguish the two error cases.

Comment by Pavel Odintsov [ 24/Mar/16 ]

So we could close this ticket.

Comment by Pavel Odintsov [ 24/Mar/16 ]

Hello, folks!

Yep, I'm completely agree with your vision I've changed my mind Thanks!

Comment by Andrew Morrow (Inactive) [ 24/Mar/16 ]

I agree. I took a look at has_collection, and yeah, that isn't perfect by any means. pavel.odintsov If you are satisfied with matt.cotter's answer to the first part of your question, do you mind if I retitle this issue to be more about the flaw in has_collection? We will then get it scheduled into an upcoming release.

Also, going back quickly to the comments on ping: we've kept the interface very light on "helpers", because we wanted to build a minimal API first, and then extend it based on popular demand. So your feedback here is incredibly valuable. That said, we are going to lean away from most "helpers" when the intended goal is to achievable by using the existing API, as it is via run_command. But you should feel free to argue with us from a user, rather than maintainer, perspective. We want people to use this driver, after all.

Comment by Matt Cotter [ 23/Mar/16 ]

Hi pavel.odintsov,

You are correct that the only way to check connectivity is to send a command. I think if we made a shortcut for sending a ping command and checking for errors, it would encourage users to just check that method and then not handle future errors. Even two subsequent calls can't be guaranteed to both have connectivity due to network failures, etc.
The ping command works great! If you want to also check cluster status you can send an isMaster command:

#include <iostream>
 
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
 
#include <mongocxx/client.hpp>
#include <mongocxx/exception/operation_exception.hpp>
#include <mongocxx/instance.hpp>
 
int main(int, char**) {
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{}};
 
    bsoncxx::builder::stream::document document{};
 
    auto db = conn["admin"];
    document << "isMaster" << 1;
 
    try {
        auto reply = db.run_command(document.view());
        std::cout << "command succeeded: " << bsoncxx::to_json(reply) << std::endl;
    } catch (mongocxx::operation_exception ex) {
        std::cout << "command failed: " << ex.what() << std::endl;
    }
}

I agree with the critique about has_collection - I think this should throw (like list_collections and list_databases, e.g.) if the underlying operation fails. acm - thoughts on this?

Comment by Pavel Odintsov [ 23/Mar/16 ]

I've implemented this task with following code:

   std::string connection_string = "mongodb://dps_frontend_user:" + mongo_password + "@" +
                                    fastnetmon_global_configuration.mongodb_host + ":" +
                                    convert_int_to_string(fastnetmon_global_configuration.mongodb_port) +
                                   "/?authMechanism=MONGODB-CR&authSource=admin";
 
    mongocxx::client conn{ mongocxx::uri{ connection_string } };
 
 
    auto db = conn[fastnetmon_global_configuration.mongodb_database_name];
 
    logger << log4cpp::Priority::INFO << "PING mongodb";
 
    {
        try {
            bsoncxx::document::value command = document{} << "ping" << 1 << finalize;
            bsoncxx::document::view command_view = command.view();
            auto res = db.run_command(command_view);
        } catch (mongocxx::operation_exception ex) {
            logger << log4cpp::Priority::ERROR << "Could not ping MongoDB";
            return false;
        }
    }
 
    return true;

But will be nice to have short function

Generated at Wed Feb 07 22:00:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.