questions on the clients' shutdown strategy of mongo-c-driver

59 views
Skip to first unread message

asyouknow

unread,
Aug 19, 2016, 4:42:57 AM8/19/16
to mongodb-user
i have briefly read the code of mongo-c-dirver 1.1.0 and mongo-c-driver. i have found that in function mongoc-client_pool_push there is a strategydifferent.

in 1.1.0:
void
mongoc_client_pool_push (mongoc_client_pool_t *pool,
                         mongoc_client_t      *client)
{
   ENTRY;

   bson_return_if_fail(pool);
   bson_return_if_fail(client);

   /*  
    * TODO: Shutdown old client connections.
    */

   /*  
    * TODO: We should try to make a client healthy again if it
    *       is unhealthy since this is typically where a thread
    *       is done with its work.
    */

   mongoc_mutex_lock(&pool->mutex);
   _mongoc_queue_push_head(&pool->queue, client);
   mongoc_cond_signal(&pool->cond);
   mongoc_mutex_unlock(&pool->mutex);

   EXIT;
}

in mongoc-1.3.5
void
mongoc_client_pool_push (mongoc_client_pool_t *pool,
                         mongoc_client_t      *client)
{
   ENTRY;

   BSON_ASSERT (pool);
   BSON_ASSERT (client);

   mongoc_mutex_lock(&pool->mutex);
   if (pool->size > pool->min_pool_size) {
      mongoc_client_t *old_client;
      old_client = (mongoc_client_t *)_mongoc_queue_pop_head (&pool->queue);
      if (old_client) {
          mongoc_client_destroy (old_client);
          pool->size--;
      }   
   }   
   mongoc_mutex_unlock(&pool->mutex);

   mongoc_mutex_lock (&pool->mutex);
   _mongoc_queue_push_tail (&pool->queue, client);

   mongoc_cond_signal(&pool->cond);
   mongoc_mutex_unlock(&pool->mutex);

   EXIT;
}

in a multi-thread scene, when i push a client back it will destroy the oldest client(min_pool_size is 0 default). And it can not be estimated that when to new a client(think 2 threads, it may run simultaneously or one by one to do mongo op), which may cause some  efficiency problem.

I wander may this change is neccessary?

thanks!

A. Jesse Jiryu Davis

unread,
Aug 22, 2016, 9:35:27 PM8/22/16
to mongodb-user
Yes, you pointed out a bug that we've fixed in the most recent release, 1.4.0:


Starting with 1.4.0, the default behavior is to keep mongoc_client_t objects alive in the pool.

asyouknow

unread,
Sep 18, 2016, 11:30:16 PM9/18/16
to mongodb-user
I have read the code of 1.4.0, it make the client pool LIFO and wonder why it close the client at the head? I think the client in tail is the oldest?

在 2016年8月23日星期二 UTC+8上午9:35:27,A. Jesse Jiryu Davis写道:

A. Jesse Jiryu Davis

unread,
Sep 23, 2016, 4:45:54 PM9/23/16
to mongodb-user

A. Jesse Jiryu Davis

unread,
Sep 30, 2016, 5:05:22 PM9/30/16
to mongodb-user
libmongoc 1.4.2 has been released with the fix, thanks for reporting it.
Reply all
Reply to author
Forward
0 new messages