Skip to content

Commit fe583a7

Browse files
committed
CDRIVER-1558 fix minPoolSize logic
1 parent ac82971 commit fe583a7

File tree

7 files changed

+132
-43
lines changed

7 files changed

+132
-43
lines changed

src/mongoc/mongoc-client-pool-private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
BSON_BEGIN_DECLS
2929

30-
size_t mongoc_client_pool_get_size(mongoc_client_pool_t *pool);
30+
size_t mongoc_client_pool_get_size (mongoc_client_pool_t *pool);
31+
size_t mongoc_client_pool_num_pushed (mongoc_client_pool_t *pool);
3132

3233
BSON_END_DECLS
3334

src/mongoc/mongoc-client-pool.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,19 @@ mongoc_client_pool_push (mongoc_client_pool_t *pool,
266266
BSON_ASSERT (client);
267267

268268
mongoc_mutex_lock(&pool->mutex);
269-
if (pool->min_pool_size && pool->size > pool->min_pool_size) {
269+
_mongoc_queue_push_head (&pool->queue, client);
270+
271+
if (pool->min_pool_size &&
272+
_mongoc_queue_get_length (&pool->queue) > pool->min_pool_size) {
273+
270274
mongoc_client_t *old_client;
271-
old_client = (mongoc_client_t *)_mongoc_queue_pop_head (&pool->queue);
275+
old_client = (mongoc_client_t *)_mongoc_queue_pop_tail (&pool->queue);
272276
if (old_client) {
273277
mongoc_client_destroy (old_client);
274278
pool->size--;
275279
}
276280
}
277281

278-
_mongoc_queue_push_head (&pool->queue, client);
279-
280282
mongoc_cond_signal(&pool->cond);
281283
mongoc_mutex_unlock(&pool->mutex);
282284

@@ -297,6 +299,21 @@ mongoc_client_pool_get_size (mongoc_client_pool_t *pool)
297299
RETURN (size);
298300
}
299301

302+
303+
size_t
304+
mongoc_client_pool_num_pushed (mongoc_client_pool_t *pool)
305+
{
306+
size_t num_pushed = 0;
307+
308+
ENTRY;
309+
310+
mongoc_mutex_lock (&pool->mutex);
311+
num_pushed = pool->queue.length;
312+
mongoc_mutex_unlock (&pool->mutex);
313+
314+
RETURN (num_pushed);
315+
}
316+
300317
void
301318
mongoc_client_pool_max_size(mongoc_client_pool_t *pool,
302319
uint32_t max_pool_size)

src/mongoc/mongoc-queue-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct _mongoc_queue_t
4040
{
4141
mongoc_queue_item_t *head;
4242
mongoc_queue_item_t *tail;
43+
uint32_t length;
4344
};
4445

4546

@@ -52,6 +53,7 @@ struct _mongoc_queue_item_t
5253

5354
void _mongoc_queue_init (mongoc_queue_t *queue);
5455
void *_mongoc_queue_pop_head (mongoc_queue_t *queue);
56+
void *_mongoc_queue_pop_tail (mongoc_queue_t *queue);
5557
void _mongoc_queue_push_head (mongoc_queue_t *queue,
5658
void *data);
5759
void _mongoc_queue_push_tail (mongoc_queue_t *queue,

src/mongoc/mongoc-queue.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ _mongoc_queue_push_head (mongoc_queue_t *queue,
4747
if (!queue->tail) {
4848
queue->tail = item;
4949
}
50+
51+
queue->length++;
5052
}
5153

5254

@@ -69,6 +71,7 @@ _mongoc_queue_push_tail (mongoc_queue_t *queue,
6971
}
7072

7173
queue->tail = item;
74+
queue->length++;
7275
}
7376

7477

@@ -87,23 +90,52 @@ _mongoc_queue_pop_head (mongoc_queue_t *queue)
8790
queue->head = item->next;
8891
data = item->data;
8992
bson_free(item);
93+
queue->length--;
9094
}
9195

9296
return data;
9397
}
9498

9599

96-
uint32_t
97-
_mongoc_queue_get_length (const mongoc_queue_t *queue)
100+
void *
101+
_mongoc_queue_pop_tail (mongoc_queue_t *queue)
98102
{
99103
mongoc_queue_item_t *item;
100-
uint32_t count = 0;
104+
void *data = NULL;
101105

102106
BSON_ASSERT (queue);
103107

104-
for (item = queue->head; item; item = item->next) {
105-
count++;
108+
if (queue->length == 0) {
109+
return NULL;
110+
}
111+
112+
data = queue->tail->data;
113+
114+
if (queue->length == 1) {
115+
bson_free (queue->tail);
116+
queue->head = queue->tail = NULL;
117+
} else {
118+
/* find item pointing at tail */
119+
for (item = queue->head; item; item = item->next) {
120+
if (item->next == queue->tail) {
121+
item->next = NULL;
122+
bson_free (queue->tail);
123+
queue->tail = item;
124+
break;
125+
}
126+
}
106127
}
107128

108-
return count;
129+
queue->length--;
130+
131+
return data;
132+
}
133+
134+
135+
uint32_t
136+
_mongoc_queue_get_length (const mongoc_queue_t *queue)
137+
{
138+
BSON_ASSERT (queue);
139+
140+
return queue->length;
109141
}

tests/TestSuite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ extern "C" {
133133
#define ASSERT_CMPUINT64(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, PRIu64)
134134
#define ASSERT_CMPSIZE_T(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, "zd")
135135
#define ASSERT_CMPSSIZE_T(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, "zx")
136+
#define ASSERT_CMPVOID(a, eq, b) ASSERT_CMPINT_HELPER(a, eq, b, "p")
136137

137138
#define ASSERT_MEMCMP(a, b, n) \
138139
do { \

tests/test-mongoc-client-pool.c

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,47 +79,59 @@ test_mongoc_client_pool_min_size_dispose (void)
7979
mongoc_client_pool_t *pool;
8080
mongoc_client_t *client;
8181
mongoc_uri_t *uri;
82-
mongoc_array_t conns;
83-
int i;
82+
mongoc_client_t *c0, *c1, *c2, *c3;
8483

85-
_mongoc_array_init (&conns, sizeof client);
86-
87-
uri = mongoc_uri_new ("mongodb://127.0.0.1?maxpoolsize=10&minpoolsize=3");
84+
uri = mongoc_uri_new ("mongodb://127.0.0.1?minpoolsize=2");
8885
pool = mongoc_client_pool_new (uri);
8986

90-
for (i = 0; i < 10; i++) {
91-
client = mongoc_client_pool_pop (pool);
92-
assert (client);
93-
_mongoc_array_append_val (&conns, client);
94-
assert (mongoc_client_pool_get_size (pool) == i + 1);
95-
}
87+
c0 = mongoc_client_pool_pop (pool);
88+
assert (c0);
89+
ASSERT_CMPSIZE_T (mongoc_client_pool_get_size (pool), ==, (size_t) 1);
9690

97-
for (i = 0; i < 10; i++) {
98-
client = _mongoc_array_index (&conns, mongoc_client_t *, i);
99-
assert (client);
100-
mongoc_client_pool_push (pool, client);
101-
}
91+
c1 = mongoc_client_pool_pop (pool);
92+
assert (c1);
93+
ASSERT_CMPSIZE_T (mongoc_client_pool_get_size (pool), ==, (size_t) 2);
10294

103-
assert (mongoc_client_pool_get_size (pool) == 3);
95+
c2 = mongoc_client_pool_pop (pool);
96+
assert (c2);
97+
ASSERT_CMPSIZE_T (mongoc_client_pool_get_size (pool), ==, (size_t) 3);
10498

105-
/* assert oldest clients were destroyed, newest were stored */
106-
for (i = 9; i >= 7; i--) {
107-
client = mongoc_client_pool_pop (pool);
108-
assert (client);
109-
assert (client == _mongoc_array_index (&conns, mongoc_client_t *, i));
110-
}
99+
c3 = mongoc_client_pool_pop (pool);
100+
assert (c3);
101+
ASSERT_CMPSIZE_T (mongoc_client_pool_get_size (pool), ==, (size_t) 4);
111102

112-
/* clean up */
113-
for (i = 7; i < 10; i++) {
114-
client = _mongoc_array_index (&conns, mongoc_client_t *, i);
115-
assert (client);
116-
mongoc_client_pool_push (pool, client);
117-
}
103+
mongoc_client_pool_push (pool, c0); /* queue is [c0] */
104+
ASSERT_CMPSIZE_T (mongoc_client_pool_num_pushed (pool), ==, (size_t) 1);
105+
ASSERT_CMPSIZE_T (mongoc_client_pool_get_size (pool), ==, (size_t) 4);
118106

119-
_mongoc_array_clear (&conns);
120-
_mongoc_array_destroy (&conns);
121-
mongoc_uri_destroy (uri);
107+
mongoc_client_pool_push (pool, c1); /* queue is [c1, c0] */
108+
ASSERT_CMPSIZE_T (mongoc_client_pool_num_pushed (pool), ==, (size_t) 2);
109+
ASSERT_CMPSIZE_T (mongoc_client_pool_get_size (pool), ==, (size_t) 4);
110+
111+
mongoc_client_pool_push (pool, c2); /* queue is [c2, c1] */
112+
ASSERT_CMPSIZE_T (mongoc_client_pool_num_pushed (pool), ==, (size_t) 2);
113+
ASSERT_CMPSIZE_T (mongoc_client_pool_get_size (pool), ==, (size_t) 3);
114+
115+
mongoc_client_pool_push (pool, c3); /* queue is [c3, c2] */
116+
ASSERT_CMPSIZE_T (mongoc_client_pool_num_pushed (pool), ==, (size_t) 2);
117+
ASSERT_CMPSIZE_T (mongoc_client_pool_get_size (pool), ==, (size_t) 2);
118+
119+
/* assert oldest client was destroyed, newest were stored */
120+
client = mongoc_client_pool_pop (pool);
121+
assert (client);
122+
assert (client == c3);
123+
124+
client = mongoc_client_pool_pop (pool);
125+
assert (client);
126+
assert (client == c2);
127+
128+
ASSERT_CMPSIZE_T (mongoc_client_pool_get_size (pool), ==, (size_t) 2);
129+
130+
/* clean up */
131+
mongoc_client_pool_push (pool, c2);
132+
mongoc_client_pool_push (pool, c3);
122133
mongoc_client_pool_destroy (pool);
134+
mongoc_uri_destroy (uri);
123135
}
124136

125137
static void

tests/test-mongoc-queue.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,32 @@ test_mongoc_queue_basic (void)
2626
}
2727

2828

29+
static void
30+
test_mongoc_queue_pop_tail (void)
31+
{
32+
mongoc_queue_t q = MONGOC_QUEUE_INITIALIZER;
33+
34+
_mongoc_queue_push_head(&q, (void *) 1);
35+
ASSERT_CMPUINT32 (_mongoc_queue_get_length (&q), ==, (uint32_t) 1);
36+
ASSERT_CMPVOID (_mongoc_queue_pop_tail (&q), ==, (void *) 1);
37+
38+
ASSERT_CMPUINT32 (_mongoc_queue_get_length (&q), ==, (uint32_t) 0);
39+
ASSERT_CMPVOID (_mongoc_queue_pop_tail (&q), ==, (void *) NULL);
40+
41+
_mongoc_queue_push_tail(&q, (void *)2);
42+
_mongoc_queue_push_head(&q, (void *)3);
43+
ASSERT_CMPUINT32 (_mongoc_queue_get_length (&q), ==, (uint32_t) 2);
44+
ASSERT_CMPVOID (_mongoc_queue_pop_tail (&q), ==, (void *) 2);
45+
ASSERT_CMPUINT32 (_mongoc_queue_get_length (&q), ==, (uint32_t) 1);
46+
ASSERT_CMPVOID (_mongoc_queue_pop_tail (&q), ==, (void *) 3);
47+
ASSERT_CMPUINT32 (_mongoc_queue_get_length (&q), ==, (uint32_t) 0);
48+
ASSERT_CMPVOID (_mongoc_queue_pop_tail (&q), ==, (void *) NULL);
49+
}
50+
51+
2952
void
3053
test_queue_install (TestSuite *suite)
3154
{
3255
TestSuite_Add (suite, "/Queue/basic", test_mongoc_queue_basic);
56+
TestSuite_Add (suite, "/Queue/pop_tail", test_mongoc_queue_pop_tail);
3357
}

0 commit comments

Comments
 (0)