Skip to content

Commit f39b477

Browse files
ajdavisbjori
authored andcommitted
CDRIVER-1571 simplify async command loop
mongoc_async_run() no longer returns "true" if there is more work to do: it never returns early now since there is only one timeout for the whole function. Also, make timeouts int64_t in all internal functions.
1 parent 55ee43f commit f39b477

9 files changed

+27
-84
lines changed

src/mongoc/mongoc-async-cmd-private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ mongoc_async_cmd_new (mongoc_async_t *async,
8080
const bson_t *cmd,
8181
mongoc_async_cmd_cb_t cb,
8282
void *cb_data,
83-
int32_t timeout_msec);
83+
int64_t timeout_msec);
8484

8585
void
8686
mongoc_async_cmd_destroy (mongoc_async_cmd_t *acmd);

src/mongoc/mongoc-async-cmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ mongoc_async_cmd_new (mongoc_async_t *async,
167167
const bson_t *cmd,
168168
mongoc_async_cmd_cb_t cb,
169169
void *cb_data,
170-
int32_t timeout_msec)
170+
int64_t timeout_msec)
171171
{
172172
mongoc_async_cmd_t *acmd;
173173

src/mongoc/mongoc-async-private.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ mongoc_async_new ();
6363
void
6464
mongoc_async_destroy (mongoc_async_t *async);
6565

66-
bool
66+
void
6767
mongoc_async_run (mongoc_async_t *async,
68-
int32_t timeout_msec);
68+
int64_t timeout_msec);
6969

7070
struct _mongoc_async_cmd *
7171
mongoc_async_cmd (mongoc_async_t *async,
@@ -76,7 +76,7 @@ mongoc_async_cmd (mongoc_async_t *async,
7676
const bson_t *cmd,
7777
mongoc_async_cmd_cb_t cb,
7878
void *cb_data,
79-
int32_t timeout_msec);
79+
int64_t timeout_msec);
8080

8181
BSON_END_DECLS
8282

src/mongoc/mongoc-async.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ mongoc_async_cmd (mongoc_async_t *async,
3333
const bson_t *cmd,
3434
mongoc_async_cmd_cb_t cb,
3535
void *cb_data,
36-
int32_t timeout_msec)
36+
int64_t timeout_msec)
3737
{
3838
return mongoc_async_cmd_new (async, stream, setup, setup_ctx, dbname, cmd, cb,
3939
cb_data, timeout_msec);
@@ -60,9 +60,9 @@ mongoc_async_destroy (mongoc_async_t *async)
6060
bson_free (async);
6161
}
6262

63-
bool
63+
void
6464
mongoc_async_run (mongoc_async_t *async,
65-
int32_t timeout_msec)
65+
int64_t timeout_msec)
6666
{
6767
mongoc_async_cmd_t *acmd, *tmp;
6868
mongoc_stream_poll_t *poller = NULL;
@@ -76,17 +76,15 @@ mongoc_async_run (mongoc_async_t *async,
7676
BSON_ASSERT (timeout_msec > 0);
7777

7878
now = bson_get_monotonic_time ();
79-
expire_at = now + ((int64_t) timeout_msec * 1000);
79+
expire_at = now + timeout_msec * 1000;
8080
poll_size = 0;
8181

82-
for (;;) {
83-
if (!async->ncmds) {
84-
/* work complete */
85-
break;
86-
}
87-
82+
while (async->ncmds) {
83+
/* ncmds grows if we discover a replica & start calling ismaster on it */
8884
if (poll_size < async->ncmds) {
89-
poller = (mongoc_stream_poll_t *)bson_realloc (poller, sizeof (*poller) * async->ncmds);
85+
poller = (mongoc_stream_poll_t *) bson_realloc (
86+
poller, sizeof (*poller) * async->ncmds);
87+
9088
poll_size = async->ncmds;
9189
}
9290

@@ -145,7 +143,4 @@ mongoc_async_run (mongoc_async_t *async,
145143
acmd->data, &acmd->error);
146144
mongoc_async_cmd_destroy (acmd);
147145
}
148-
149-
/* cancel the loop in the caller, mongoc_topology_scanner_work() */
150-
return false;
151146
}

src/mongoc/mongoc-topology-scanner-private.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ mongoc_topology_scanner_node_destroy (mongoc_topology_scanner_node_t *node,
110110

111111
void
112112
mongoc_topology_scanner_start (mongoc_topology_scanner_t *ts,
113-
int32_t timeout_msec,
113+
int64_t timeout_msec,
114114
bool obey_cooldown);
115115

116-
bool
116+
void
117117
mongoc_topology_scanner_work (mongoc_topology_scanner_t *ts,
118-
int32_t timeout_msec);
118+
int64_t timeout_msec);
119119

120120
void
121121
mongoc_topology_scanner_sum_errors (mongoc_topology_scanner_t *ts,

src/mongoc/mongoc-topology-scanner.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ mongoc_topology_scanner_node_setup (mongoc_topology_scanner_node_t *node,
532532

533533
void
534534
mongoc_topology_scanner_start (mongoc_topology_scanner_t *ts,
535-
int32_t timeout_msec,
535+
int64_t timeout_msec,
536536
bool obey_cooldown)
537537
{
538538
mongoc_topology_scanner_node_t *node, *tmp;
@@ -583,19 +583,11 @@ mongoc_topology_scanner_start (mongoc_topology_scanner_t *ts,
583583
*--------------------------------------------------------------------------
584584
*/
585585

586-
bool
586+
void
587587
mongoc_topology_scanner_work (mongoc_topology_scanner_t *ts,
588-
int32_t timeout_msec)
588+
int64_t timeout_msec)
589589
{
590-
bool r;
591-
592-
r = mongoc_async_run (ts->async, timeout_msec);
593-
594-
if (! r) {
595-
ts->in_progress = false;
596-
}
597-
598-
return r;
590+
mongoc_async_run (ts->async, timeout_msec);
599591
}
600592

601593
/*

src/mongoc/mongoc-topology.c

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -273,43 +273,6 @@ mongoc_topology_destroy (mongoc_topology_t *topology)
273273
bson_free(topology);
274274
}
275275

276-
/*
277-
*--------------------------------------------------------------------------
278-
*
279-
* _mongoc_topology_run_scanner --
280-
*
281-
* Not threadsafe, the expectation is that we're either single
282-
* threaded or only the background thread runs scans.
283-
*
284-
* Crank the underlying scanner until we've timed out or finished.
285-
*
286-
* Returns:
287-
* true if there is more work to do, false otherwise
288-
*
289-
*--------------------------------------------------------------------------
290-
*/
291-
static bool
292-
_mongoc_topology_run_scanner (mongoc_topology_t *topology,
293-
int64_t work_msec)
294-
{
295-
int64_t now;
296-
int64_t expire_at;
297-
bool keep_going = true;
298-
299-
now = bson_get_monotonic_time ();
300-
expire_at = now + (work_msec * 1000);
301-
302-
/* while there is more work to do and we haven't timed out */
303-
while (keep_going && now <= expire_at) {
304-
keep_going = mongoc_topology_scanner_work (topology->scanner, (expire_at - now) / 1000);
305-
306-
if (keep_going) {
307-
now = bson_get_monotonic_time ();
308-
}
309-
}
310-
311-
return keep_going;
312-
}
313276

314277
/*
315278
*--------------------------------------------------------------------------
@@ -327,8 +290,8 @@ _mongoc_topology_do_blocking_scan (mongoc_topology_t *topology, bson_error_t *er
327290
topology->connect_timeout_msec,
328291
true);
329292

330-
while (_mongoc_topology_run_scanner (topology,
331-
topology->connect_timeout_msec)) {}
293+
mongoc_topology_scanner_work (topology->scanner,
294+
topology->connect_timeout_msec);
332295

333296
/* Aggregate all scanner errors, if any */
334297
mongoc_topology_scanner_sum_errors (topology->scanner, error);
@@ -808,9 +771,8 @@ void * _mongoc_topology_run_background (void *data)
808771

809772
/* scanning locks and unlocks the mutex itself until the scan is done */
810773
mongoc_mutex_unlock (&topology->mutex);
811-
812-
while (_mongoc_topology_run_scanner (topology,
813-
topology->connect_timeout_msec)) {}
774+
mongoc_topology_scanner_work (topology->scanner,
775+
topology->connect_timeout_msec);
814776

815777
mongoc_mutex_lock (&topology->mutex);
816778

tests/test-mongoc-async.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ test_ismaster_impl (bool with_ssl)
134134
TIMEOUT);
135135
}
136136

137-
while (mongoc_async_run (async, TIMEOUT)) {
138-
}
137+
mongoc_async_run (async, TIMEOUT);
139138

140139
for (i = 0; i < NSERVERS; i++) {
141140
if (!results[i].finished) {

tests/test-mongoc-topology-scanner.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ _test_topology_scanner(bool with_ssl)
5757
int i;
5858
bson_t q = BSON_INITIALIZER;
5959
int finished = NSERVERS * 3;
60-
bool more_to_do;
6160

6261
#ifdef MONGOC_ENABLE_SSL
6362
mongoc_ssl_opt_t sopt = { 0 };
@@ -100,11 +99,7 @@ _test_topology_scanner(bool with_ssl)
10099

101100
for (i = 0; i < 3; i++) {
102101
mongoc_topology_scanner_start (topology_scanner, TIMEOUT, false);
103-
104-
more_to_do = mongoc_topology_scanner_work (topology_scanner, TIMEOUT);
105-
106-
assert(! more_to_do);
107-
102+
mongoc_topology_scanner_work (topology_scanner, TIMEOUT);
108103
mongoc_topology_scanner_reset (topology_scanner);
109104
}
110105

0 commit comments

Comments
 (0)