serverSelectionTimeoutMS expired when debugging the threading example in C Driver

136 views
Skip to first unread message

dingho...@gmail.com

unread,
Nov 8, 2016, 11:54:32 PM11/8/16
to mongodb-user
Hi!
I am testing the threading example shown in 

the code is as follows:


#include <mongoc.h>
#include <pthread.h>
//set scheduler-locking on
#define N_THREADS 10

static void *
worker(void *data)
{
    mongoc_client_pool_t *pool = data;
    mongoc_client_t *client;
    mongoc_collection_t *collection;
    bson_error_t error;
    bson_oid_t oid;
    bson_t *doc;

    mongoc_init ();

    client = mongoc_client_pool_pop(pool);
    collection = mongoc_client_get_collection (client, "mydb", "mycoll");

    doc = bson_new ();
    bson_oid_init (&oid, NULL);
    BSON_APPEND_OID (doc, "_id", &oid);
    BSON_APPEND_UTF8 (doc, "hello", "world");

    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
    }

    bson_destroy (doc);
    mongoc_collection_destroy (collection);
    mongoc_cleanup ();
    mongoc_client_pool_push(pool, client);
    return NULL;
}

int main(int argc, char *argv[])
{
mongoc_client_pool_t *pool;
mongoc_uri_t *uri;
pthread_t threads[N_THREADS];

mongoc_init();

uri = mongoc_uri_new("mongodb://localhost/");
pool = mongoc_client_pool_new(uri);
int i;
for (i = 0; i < N_THREADS; i++)
{
pthread_create(&threads[i], NULL, worker, pool);
}

for (i = 0; i < N_THREADS; i++)
{
pthread_join(threads[i], NULL);
}

mongoc_client_pool_destroy(pool);
mongoc_uri_destroy(uri);
mongoc_cleanup();

return 0;
}


When I run this code it works fine, but when I debug it it shows the error message
No suitable servers found: `serverSelectionTimeoutMS` expired 
when steping over   
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
    } 

I am running this code in eclipse CDT.

A. Jesse Jiryu Davis

unread,
Nov 9, 2016, 8:42:34 PM11/9/16
to mongodb-user
Hello, I have two responses.

1. I'm curious, why are you calling mongoc_init and mongoc_cleanup in "worker()"? This is a mistake, these functions should only be called once in your program, and after mongoc_cleanup you must not call any other C Driver functions. So I'm curious why you made this mistake. Is there something in our documentation that encouraged you to call mongoc_init and mongoc_cleanup multiple times? We'd like to make our documentation as clear as we can.

2. When you run code in the debugger, you can pause it for a while on a particular line of code. When you resume it, various timeouts that the C Driver has been tracking may have expired. In this case, you may pause your program for more than 10 seconds, and one of the threads that was waiting for the driver to connect to the server cancels its operation, since the default server selection timeout is 10 seconds. Avoid this when you're debugging with a connection string like:

mongodb://localhost/?serverSelectionTimeoutMS=999999

Raymond Xie

unread,
Nov 9, 2016, 10:05:35 PM11/9/16
to mongod...@googlegroups.com
Hello,

I am new to mongo db, through a video tutorial I installed mongovue hoping to have a GUI for the database management, for unknown reason, although I can see the collection in my database in command line, but the mongovue doesn't show any collection, can anyone shed me any light?

Thank you very much.

Raymond

A. Jesse Jiryu Davis

unread,
Nov 9, 2016, 11:51:32 PM11/9/16
to mongodb-user
Raymond, please ask your question as a NEW topic instead of replying to this unrelated conversation. Thanks.

Hongyu Ding

unread,
Nov 10, 2016, 1:53:07 AM11/10/16
to mongodb-user
Thank you for answering.
1. Sorry about the mistake, the documentation is really helpful. Actually I just copied the example code in "Inserting a Document" into the code in "Threading", really should have noticed this. 

2. It seems like the problem is that I used "set scheduler-locking on" when debugging. I just tried "set scheduler-locking off" and "set scheduler-locking step", and they all worked fine. But I don't know why.

And about your response, I tried to replace
uri = mongoc_uri_new("mongodb://localhost/");
with
 uri = mongoc_uri_new ("mongodb://localhost/?serverSelectionTimeoutMS=999999");
and then when I step over 
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
    } 
It just paused there. 
Then I tried 50999 instead of 999999, it paused for a while and then showed the error
No suitable servers found: `serverSelectionTimeoutMS` expired 
 
The latest code:
#include <mongoc.h>
#include <pthread.h>
//set scheduler-locking on
#define N_THREADS 10

static void *
worker(void *data)
{
    mongoc_client_pool_t *pool = data;
    mongoc_client_t *client;
    mongoc_collection_t *collection;
    bson_error_t error;
    bson_oid_t oid;
    bson_t *doc;
    client = mongoc_client_pool_pop(pool);
    collection = mongoc_client_get_collection (client, "mydb", "mycoll");
    doc = bson_new ();
    bson_oid_init (&oid, NULL);
    BSON_APPEND_OID (doc, "_id", &oid);
    BSON_APPEND_UTF8 (doc, "hello", "world");
    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
    }
    bson_destroy (doc);
    mongoc_collection_destroy (collection);
    mongoc_client_pool_push(pool, client);
    return NULL;
}

int main(int argc, char *argv[])
{
mongoc_client_pool_t *pool;
mongoc_uri_t *uri;
pthread_t threads[N_THREADS];
mongoc_init();
uri = mongoc_uri_new ("mongodb://localhost/?serverSelectionTimeoutMS=999999");
pool = mongoc_client_pool_new(uri);
int i;
for (i = 0; i < N_THREADS; i++)
{
pthread_create(&threads[i], NULL, worker, pool);
}
for (i = 0; i < N_THREADS; i++)
{
pthread_join(threads[i], NULL);
}
mongoc_client_pool_destroy(pool);
mongoc_uri_destroy(uri);
mongoc_cleanup();
return 0;
}

Thanks again.


在 2016年11月10日星期四 UTC+8上午9:42:34,A. Jesse Jiryu Davis写道:

A. Jesse Jiryu Davis

unread,
Nov 10, 2016, 7:22:51 AM11/10/16
to mongod...@googlegroups.com
Interesting, I didn't know about gdb's scheduler-locking mode. 

The client pool makes its initial connection to MongoDB in response to the first mongoc_client_t operation you do. The pool spawns a background thread to do this work, and the other threads in your program wait (in mongoc_collection_insert) for the background thread to connect. I explain the C Driver's connection code here.

According to the docs for scheduler-locking:

"If it is off, then there is no locking and any thread may run at any time." -> so when you pause a thread in gdb, the client pool's background thread can keep running and connect to the server

"If on, then only the current thread may run when the inferior is resumed." -> so when you pause a thread in gdb, and even when you resume it, the client pool's background thread is still paused, so it never connects

"The step mode prevents other threads from preempting the current thread while you are stepping, so that the focus of debugging does not change unexpectedly. Other threads are completely free to run when you use commands like ‘continue’, ‘until’, or ‘finish’" -> so when you do "continue" in gdb, that lets the pool's background thread finish connecting to MongoDB

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/Dss4mFP_T68/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/1053c3f9-f426-41fa-b0ab-c3b4a99e688e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages