Skip to content

Commit 2b84411

Browse files
committed
CDRIVER-2811 set heartbeat event durations
mongoc_apm_server_heartbeat_succeeded_t, mongoc_apm_server_heartbeat_failed_t duration fields had been uninitialized. Test that they are set, and the same command monitoring structs (which were implemented correctly).
1 parent a436f27 commit 2b84411

File tree

7 files changed

+175
-29
lines changed

7 files changed

+175
-29
lines changed

NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ Features:
1313
Bug fixes:
1414

1515
* mongoc_gridfs_create_file_from_stream ignored errors while writing chunks
16-
to the server
16+
to the server.
1717
* The following functions should not have taken a "bypassDocumentValidation"
1818
option in bson_t *opts, the option is now prohibited:
1919
- mongoc_bulk_operation_insert_with_opts
2020
- mongoc_bulk_operation_update_one_with_opts
2121
- mongoc_bulk_operation_update_many_with_opts
2222
- mongoc_bulk_operation_replace_one_with_opts
23+
* The heartbeat-succeeded and heartbeat-failed events (part of SDAM
24+
Monitoring) had uninitialized "duration" fields, they are now set correctly.
2325

2426
mongo-c-driver 1.12.0
2527
=====================

src/libmongoc/src/mongoc/mongoc-async-cmd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ bool
9999
mongoc_async_cmd_run (mongoc_async_cmd_t *acmd)
100100
{
101101
mongoc_async_cmd_result_t result;
102-
int64_t rtt_msec;
102+
int64_t duration_usec;
103103
_mongoc_async_cmd_phase_t phase_callback;
104104

105105
BSON_ASSERT (acmd);
@@ -120,13 +120,13 @@ mongoc_async_cmd_run (mongoc_async_cmd_t *acmd)
120120
return true;
121121
}
122122

123-
rtt_msec = (bson_get_monotonic_time () - acmd->cmd_started) / 1000;
123+
duration_usec = bson_get_monotonic_time () - acmd->cmd_started;
124124

125125
if (result == MONGOC_ASYNC_CMD_SUCCESS) {
126-
acmd->cb (acmd, result, &acmd->reply, rtt_msec);
126+
acmd->cb (acmd, result, &acmd->reply, duration_usec);
127127
} else {
128128
/* we're in ERROR, TIMEOUT, or CANCELED */
129-
acmd->cb (acmd, result, NULL, rtt_msec);
129+
acmd->cb (acmd, result, NULL, duration_usec);
130130
}
131131

132132
mongoc_async_cmd_destroy (acmd);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef enum {
4545
typedef void (*mongoc_async_cmd_cb_t) (struct _mongoc_async_cmd *acmd,
4646
mongoc_async_cmd_result_t result,
4747
const bson_t *bson,
48-
int64_t rtt_msec);
48+
int64_t duration_usec);
4949

5050
typedef mongoc_stream_t *(*mongoc_async_cmd_initiate_t) (
5151
struct _mongoc_async_cmd *);

src/libmongoc/src/mongoc/mongoc-topology-scanner.c

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ _async_connected (mongoc_async_cmd_t *acmd);
5050
static void
5151
_async_success (mongoc_async_cmd_t *acmd,
5252
const bson_t *ismaster_response,
53-
int64_t rtt_msec);
53+
int64_t duration_usec);
5454

5555
static void
5656
_async_error_or_timeout (mongoc_async_cmd_t *acmd,
57-
int64_t rtt_msec,
57+
int64_t duration_usec,
5858
const char *default_err_msg);
5959

6060
static void
6161
_async_handler (mongoc_async_cmd_t *acmd,
6262
mongoc_async_cmd_result_t async_status,
6363
const bson_t *ismaster_response,
64-
int64_t rtt_msec);
64+
int64_t duration_usec);
6565

6666
static void
6767
_mongoc_topology_scanner_monitor_heartbeat_started (
@@ -71,13 +71,15 @@ static void
7171
_mongoc_topology_scanner_monitor_heartbeat_succeeded (
7272
const mongoc_topology_scanner_t *ts,
7373
const mongoc_host_list_t *host,
74-
const bson_t *reply);
74+
const bson_t *reply,
75+
int64_t duration_usec);
7576

7677
static void
7778
_mongoc_topology_scanner_monitor_heartbeat_failed (
7879
const mongoc_topology_scanner_t *ts,
7980
const mongoc_host_list_t *host,
80-
const bson_error_t *error);
81+
const bson_error_t *error,
82+
int64_t duration_usec);
8183

8284

8385
/* reset "retired" nodes that failed or were removed in the previous scan */
@@ -433,7 +435,7 @@ _async_connected (mongoc_async_cmd_t *acmd)
433435
static void
434436
_async_success (mongoc_async_cmd_t *acmd,
435437
const bson_t *ismaster_response,
436-
int64_t rtt_msec)
438+
int64_t duration_usec)
437439
{
438440
void *data = acmd->data;
439441
mongoc_topology_scanner_node_t *node =
@@ -452,7 +454,7 @@ _async_success (mongoc_async_cmd_t *acmd,
452454
node->last_failed = -1;
453455

454456
_mongoc_topology_scanner_monitor_heartbeat_succeeded (
455-
ts, &node->host, ismaster_response);
457+
ts, &node->host, ismaster_response, duration_usec);
456458

457459
/* set our successful stream. */
458460
BSON_ASSERT (!node->stream);
@@ -464,12 +466,17 @@ _async_success (mongoc_async_cmd_t *acmd,
464466
ismaster_response, &node->sasl_supported_mechs);
465467
}
466468

467-
ts->cb (node->id, ismaster_response, rtt_msec, ts->cb_data, &acmd->error);
469+
/* mongoc_topology_scanner_cb_t takes rtt_msec, not usec */
470+
ts->cb (node->id,
471+
ismaster_response,
472+
duration_usec / 1000,
473+
ts->cb_data,
474+
&acmd->error);
468475
}
469476

470477
static void
471478
_async_error_or_timeout (mongoc_async_cmd_t *acmd,
472-
int64_t rtt_msec,
479+
int64_t duration_usec,
473480
const char *default_err_msg)
474481
{
475482
void *data = acmd->data;
@@ -516,10 +523,11 @@ _async_error_or_timeout (mongoc_async_cmd_t *acmd,
516523
node->host.host_and_port);
517524

518525
_mongoc_topology_scanner_monitor_heartbeat_failed (
519-
ts, &node->host, &node->last_error);
526+
ts, &node->host, &node->last_error, duration_usec);
520527

521-
/* call the topology scanner callback. cannot connect to this node. */
522-
ts->cb (node->id, NULL, rtt_msec, ts->cb_data, error);
528+
/* call the topology scanner callback. cannot connect to this node.
529+
* callback takes rtt_msec, not usec. */
530+
ts->cb (node->id, NULL, duration_usec / 1000, ts->cb_data, error);
523531
} else {
524532
/* there are still more commands left for this node or it succeeded
525533
* with another stream. skip the topology scanner callback. */
@@ -540,7 +548,7 @@ static void
540548
_async_handler (mongoc_async_cmd_t *acmd,
541549
mongoc_async_cmd_result_t async_status,
542550
const bson_t *ismaster_response,
543-
int64_t rtt_msec)
551+
int64_t duration_usec)
544552
{
545553
BSON_ASSERT (acmd->data);
546554

@@ -549,13 +557,13 @@ _async_handler (mongoc_async_cmd_t *acmd,
549557
_async_connected (acmd);
550558
return;
551559
case MONGOC_ASYNC_CMD_SUCCESS:
552-
_async_success (acmd, ismaster_response, rtt_msec);
560+
_async_success (acmd, ismaster_response, duration_usec);
553561
return;
554562
case MONGOC_ASYNC_CMD_TIMEOUT:
555-
_async_error_or_timeout (acmd, rtt_msec, "connection timeout");
563+
_async_error_or_timeout (acmd, duration_usec, "connection timeout");
556564
return;
557565
case MONGOC_ASYNC_CMD_ERROR:
558-
_async_error_or_timeout (acmd, rtt_msec, "connection error");
566+
_async_error_or_timeout (acmd, duration_usec, "connection error");
559567
return;
560568
case MONGOC_ASYNC_CMD_IN_PROGRESS:
561569
default:
@@ -773,8 +781,10 @@ mongoc_topology_scanner_node_setup (mongoc_topology_scanner_node_t *node,
773781
{
774782
bool success = false;
775783
mongoc_stream_t *stream;
784+
int64_t start;
776785

777786
_mongoc_topology_scanner_monitor_heartbeat_started (node->ts, &node->host);
787+
start = bson_get_monotonic_time ();
778788

779789
/* if there is already a working stream, push it back to be re-scanned. */
780790
if (node->stream) {
@@ -803,7 +813,10 @@ mongoc_topology_scanner_node_setup (mongoc_topology_scanner_node_t *node,
803813

804814
if (!success) {
805815
_mongoc_topology_scanner_monitor_heartbeat_failed (
806-
node->ts, &node->host, error);
816+
node->ts,
817+
&node->host,
818+
error,
819+
(bson_get_monotonic_time () - start) / 1000);
807820

808821
node->ts->setup_err_cb (node->id, node->ts->cb_data, error);
809822
return;
@@ -1053,13 +1066,15 @@ static void
10531066
_mongoc_topology_scanner_monitor_heartbeat_succeeded (
10541067
const mongoc_topology_scanner_t *ts,
10551068
const mongoc_host_list_t *host,
1056-
const bson_t *reply)
1069+
const bson_t *reply,
1070+
int64_t duration_usec)
10571071
{
10581072
if (ts->apm_callbacks.server_heartbeat_succeeded) {
10591073
mongoc_apm_server_heartbeat_succeeded_t event;
10601074
event.host = host;
10611075
event.context = ts->apm_context;
10621076
event.reply = reply;
1077+
event.duration_usec = duration_usec;
10631078
ts->apm_callbacks.server_heartbeat_succeeded (&event);
10641079
}
10651080
}
@@ -1069,13 +1084,15 @@ static void
10691084
_mongoc_topology_scanner_monitor_heartbeat_failed (
10701085
const mongoc_topology_scanner_t *ts,
10711086
const mongoc_host_list_t *host,
1072-
const bson_error_t *error)
1087+
const bson_error_t *error,
1088+
int64_t duration_usec)
10731089
{
10741090
if (ts->apm_callbacks.server_heartbeat_failed) {
10751091
mongoc_apm_server_heartbeat_failed_t event;
10761092
event.host = host;
10771093
event.context = ts->apm_context;
10781094
event.error = error;
1095+
event.duration_usec = duration_usec;
10791096
ts->apm_callbacks.server_heartbeat_failed (&event);
10801097
}
10811098
}

src/libmongoc/tests/test-mongoc-async.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void
5656
test_ismaster_helper (mongoc_async_cmd_t *acmd,
5757
mongoc_async_cmd_result_t result,
5858
const bson_t *bson,
59-
int64_t rtt_msec)
59+
int64_t duration_usec)
6060
{
6161
struct result *r = (struct result *) acmd->data;
6262
bson_iter_t iter;
@@ -220,7 +220,7 @@ static void
220220
test_large_ismaster_helper (mongoc_async_cmd_t *acmd,
221221
mongoc_async_cmd_result_t result,
222222
const bson_t *bson,
223-
int64_t rtt_msec)
223+
int64_t duration_usec)
224224
{
225225
bson_iter_t iter;
226226
bson_error_t *error = &acmd->error;
@@ -307,7 +307,7 @@ static void
307307
test_ismaster_delay_callback (mongoc_async_cmd_t *acmd,
308308
mongoc_async_cmd_result_t result,
309309
const bson_t *bson,
310-
int64_t rtt_msec)
310+
int64_t duration_usec)
311311
{
312312
((stream_with_result_t *) acmd->data)->finished = true;
313313
}

src/libmongoc/tests/test-mongoc-command-monitoring.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ static void
787787
cmd_succeeded_cb (const mongoc_apm_command_succeeded_t *event)
788788
{
789789
cmd_test_t *test;
790+
int64_t duration;
790791

791792
if (!strcmp (mongoc_apm_command_succeeded_get_command_name (event),
792793
"endSessions")) {
@@ -797,18 +798,27 @@ cmd_succeeded_cb (const mongoc_apm_command_succeeded_t *event)
797798
test->succeeded_calls++;
798799
ASSERT_CMPSTR (test->cmd_name,
799800
mongoc_apm_command_succeeded_get_command_name (event));
801+
802+
duration = mongoc_apm_command_succeeded_get_duration (event);
803+
ASSERT_CMPINT64 (duration, >=, (int64_t) 0);
804+
ASSERT_CMPINT64 (duration, <=, (int64_t) 10000000); /* ten seconds */
800805
}
801806

802807

803808
static void
804809
cmd_failed_cb (const mongoc_apm_command_failed_t *event)
805810
{
806811
cmd_test_t *test;
812+
int64_t duration;
807813

808814
test = (cmd_test_t *) mongoc_apm_command_failed_get_context (event);
809815
test->failed_calls++;
810816
ASSERT_CMPSTR (test->cmd_name,
811817
mongoc_apm_command_failed_get_command_name (event));
818+
819+
duration = mongoc_apm_command_failed_get_duration (event);
820+
ASSERT_CMPINT64 (duration, >=, (int64_t) 0);
821+
ASSERT_CMPINT64 (duration, <=, (int64_t) 10000000); /* ten seconds */
812822
}
813823

814824

0 commit comments

Comments
 (0)