最简单版本的mongocxx driver测试

2019-04-03

一个最简单版本的mongocxx driver测试

安装

  • 安装新版cmake3.1以上

    1
    2
    3
    wget https://cmake.org/files/v3.10/cmake-3.10.1-Linux-x86_64.tar.gz
    tar fxz cmake-3.10.1-Linux-x86_64.tar.gz
    ln -sf /home/xxxxx/cmake-3.10.1-Linux-x86_64/bin/cmake /usr/bin/cmake
  • 安装libbson

    1
    2
    3
    4
    5
    6
    wget https://github.com/mongodb/libbson/releases/download/1.9.0/libbson-1.9.0.tar.gz
    tar -xzf libbson-1.9.0.tar.gz
    cd libbson-1.9.0/
    ./configure --enable-static
    make -j8
    sudo make install
  • 安装mongo c driver

    1
    2
    3
    4
    5
    6
    wget https://github.com/mongodb/mongo-c-driver/releases/download/1.9.0/mongo-c-driver-1.9.0.tar.gz
    tar xzf mongo-c-driver-1.9.0.tar.gz
    cd mongo-c-driver-1.9.0
    ./configure --enable-static --disable-automatic-init-and-cleanup
    make -j8
    sudo make install
  • 安装mongo cxx driver 3.1

    1
    2
    3
    4
    5
    6
    7
    wget https://github.com/mongodb/mongo-cxx-driver/archive/r3.1.3.tar.gz -O
    mongo-cxx-driver-r3.1.3.tar.gz
    tar fxz mongo-cxx-driver-r3.1.3.tar.gz
    cd mongo-cxx-driver-r3.1.3/build
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DLIBBSON_DIR=/usr/local -DLIBMONGOC_DIR=/usr/local ..
    sudo make -j8
    sudo make install

最简单版本的main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
typedef std::shared_ptr<mongocxx::client> MongoConnPtr;

int main( int argc, char * argv[] )
{
mongocxx::instance instance{};
mongocxx::uri uri("mongodb://username:pass@127.0.0.1:30000/?authSource=db_name&authMechanism=SCRAM-SHA-1");
//mongocxx::client client(uri);
MongoConnPtr pConn( new mongocxx::client(uri) );
auto db = (*pConn)["tw2_commondb"];
auto collection = db["test"];

auto cursor = collection.find({});
for(auto doc : cursor)
std::cout << bsoncxx::to_json(doc) << std::endl;
std::cout << "end" << std::endl;
}

这个简单的测试程序是为了调试某个crash引起的

  • 看起来很诡异,一开始以为是mongo连接不上,用户名密码authMechanism不对之类的引起的,但是用上面的最简单的代码测试又没问题

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #0  0x00000000009452a8 in _mongoc_client_new_from_uri ()
    (gdb) bt
    #0 0x00000000009452a8 in _mongoc_client_new_from_uri ()
    #1 0x000000000090c445 in mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&) ()
    #2 0x000000000081862c in __base_dtor () at /usr/local/include/bsoncxx/v_noabi/bsoncxx/third_party/mnmlstc/core/optional.hpp:81
    #3 __base_dtor () at /usr/local/include/bsoncxx/v_noabi/bsoncxx/third_party/mnmlstc/core/optional.hpp:194
    #4 __base_dtor () at /usr/local/include/bsoncxx/v_noabi/bsoncxx/view_or_value.hpp:30
    #5 __base_dtor () at /usr/local/include/bsoncxx/v_noabi/bsoncxx/string/view_or_value.hpp:36
    #6 MongoDatabase::initMongoConnection() (this=0x152892c0) at mongo_database.cpp:667
    #7 0x0000000000589884 in _M_dispose () at /usr/include/c++/4.9/bits/basic_string.h:240
    #8 __base_dtor () at /usr/include/c++/4.9/bits/basic_string.h:547
    #9 CommonMongoDatabase::startup(EntityDefs const&, bool, bool) (this=0x7ffcd0f7fc60, entityDefs=...) at mongo_database.cpp:3016
    #10 0x0000000000502d42 in Database::init(bool, bool, DefsSyncMode) (this=0x7ffcd0f80b90, isRecover=<optimized out>, isUpgrade=<optimized out>, defsSyncMode=<optimized out>) at database.cpp:974
    #11 0x00000000005048df in bwMain(int, char**) (argc=<optimized out>, argv=<optimized out>) at main.cpp:69
    #12 0x00007f1dd39b6b45 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
    #13 0x00000000004d00cf in _start ()
  • 最后发现这句mongocxx::instance instance{};在多线程中单例没有在所有链接初始化之前调用,就必定出现这个crash,出现在_mongoc_client_new_from_uri