Skip to content

Commit b6e30a0

Browse files
authored
CDRIVER-5643 Remove cluster time gossiping on SDAM commands (#2149)
1 parent a7c78f6 commit b6e30a0

13 files changed

+444
-156
lines changed

src/libmongoc/src/mongoc/mongoc-server-monitor.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -252,18 +252,6 @@ _server_monitor_heartbeat_failed(mongoc_server_monitor_t *server_monitor,
252252
bson_mutex_unlock(&log_and_monitor->apm_mutex);
253253
}
254254

255-
static void
256-
_server_monitor_append_cluster_time(mongoc_server_monitor_t *server_monitor, bson_t *cmd)
257-
{
258-
mc_shared_tpld td = mc_tpld_take_ref(BSON_ASSERT_PTR_INLINE(server_monitor)->topology);
259-
260-
/* Cluster time is updated on every reply. */
261-
if (!bson_empty(&td.ptr->cluster_time)) {
262-
bson_append_document(cmd, "$clusterTime", 12, &td.ptr->cluster_time);
263-
}
264-
mc_tpld_drop_ref(&td);
265-
}
266-
267255
static bool
268256
_server_monitor_send_and_recv_hello_opmsg(mongoc_server_monitor_t *server_monitor,
269257
const bson_t *cmd,
@@ -521,8 +509,6 @@ _server_monitor_polling_hello(mongoc_server_monitor_t *server_monitor,
521509
hello = _mongoc_topology_scanner_get_monitoring_cmd(server_monitor->topology->scanner, hello_ok);
522510
bson_copy_to(hello, &cmd);
523511

524-
_server_monitor_append_cluster_time(server_monitor, &cmd);
525-
526512
ret = _server_monitor_send_and_recv(server_monitor, &cmd, hello_response, error);
527513

528514
bson_destroy(&cmd);
@@ -782,7 +768,6 @@ _server_monitor_awaitable_hello(mongoc_server_monitor_t *server_monitor,
782768
hello = _mongoc_topology_scanner_get_monitoring_cmd(server_monitor->topology->scanner, description->hello_ok);
783769
bson_copy_to(hello, &cmd);
784770

785-
_server_monitor_append_cluster_time(server_monitor, &cmd);
786771
bson_append_document(&cmd, "topologyVersion", 15, &description->topology_version);
787772
bson_append_int64(&cmd, "maxAwaitTimeMS", 14, server_monitor->heartbeat_frequency_ms);
788773
bson_append_utf8(&cmd, "$db", 3, "admin", 5);
@@ -823,10 +808,6 @@ _update_topology_description(mongoc_server_monitor_t *server_monitor, mongoc_ser
823808
hello_response = &description->last_hello_response;
824809
}
825810

826-
if (hello_response) {
827-
_mongoc_topology_update_cluster_time(topology, hello_response);
828-
}
829-
830811
if (mcommon_atomic_int_fetch(&topology->scanner_state, mcommon_memory_order_relaxed) ==
831812
MONGOC_TOPOLOGY_SCANNER_SHUTTING_DOWN) {
832813
return;
@@ -841,6 +822,7 @@ _update_topology_description(mongoc_server_monitor_t *server_monitor, mongoc_ser
841822
server_monitor->server_id,
842823
hello_response,
843824
description->round_trip_time_msec,
825+
MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_IGNORE,
844826
&description->error);
845827
/* Reconcile server monitors. */
846828
_mongoc_topology_background_monitoring_reconcile(topology, tdmod.new_td);
@@ -965,7 +947,6 @@ _server_monitor_setup_connection(mongoc_server_monitor_t *server_monitor,
965947
/* Perform handshake. */
966948
bson_destroy(&cmd);
967949
_mongoc_topology_dup_handshake_cmd(server_monitor->topology, &cmd);
968-
_server_monitor_append_cluster_time(server_monitor, &cmd);
969950
bson_destroy(hello_response);
970951

971952
ret = _server_monitor_send_and_recv(server_monitor, &cmd, hello_response, error);

src/libmongoc/src/mongoc/mongoc-topology-description-private.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,20 @@ _mongoc_topology_description_copy_to(const mongoc_topology_description_t *src, m
106106
void
107107
mongoc_topology_description_cleanup(mongoc_topology_description_t *description);
108108

109+
typedef enum {
110+
MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_IGNORE,
111+
MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_UPDATE,
112+
} mongoc_topology_description_hello_cluster_time_strategy_t;
113+
109114
void
110-
mongoc_topology_description_handle_hello(mongoc_topology_description_t *topology,
111-
const mongoc_log_and_monitor_instance_t *log_and_monitor,
112-
uint32_t server_id,
113-
const bson_t *hello_response,
114-
int64_t rtt_msec,
115-
const bson_error_t *error /* IN */);
115+
mongoc_topology_description_handle_hello(
116+
mongoc_topology_description_t *topology,
117+
const mongoc_log_and_monitor_instance_t *log_and_monitor,
118+
uint32_t server_id,
119+
const bson_t *hello_response,
120+
int64_t rtt_msec,
121+
mongoc_topology_description_hello_cluster_time_strategy_t cluster_time_strategy,
122+
const bson_error_t *error /* IN */);
116123

117124
mongoc_server_description_t const *
118125
mongoc_topology_description_select(const mongoc_topology_description_t *description,

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,8 @@ mongoc_topology_description_invalidate_server(mongoc_topology_description_t *td,
13531353
}
13541354

13551355
/* send NULL hello reply */
1356-
mongoc_topology_description_handle_hello(td, log_and_monitor, id, NULL, MONGOC_RTT_UNSET, error);
1356+
mongoc_topology_description_handle_hello(
1357+
td, log_and_monitor, id, NULL, MONGOC_RTT_UNSET, MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_IGNORE, error);
13571358
}
13581359

13591360
/*
@@ -2129,12 +2130,14 @@ _mongoc_topology_description_check_compatible(mongoc_topology_description_t *td)
21292130
*/
21302131

21312132
void
2132-
mongoc_topology_description_handle_hello(mongoc_topology_description_t *topology,
2133-
const mongoc_log_and_monitor_instance_t *log_and_monitor,
2134-
uint32_t server_id,
2135-
const bson_t *hello_response,
2136-
int64_t rtt_msec,
2137-
const bson_error_t *error /* IN */)
2133+
mongoc_topology_description_handle_hello(
2134+
mongoc_topology_description_t *topology,
2135+
const mongoc_log_and_monitor_instance_t *log_and_monitor,
2136+
uint32_t server_id,
2137+
const bson_t *hello_response,
2138+
int64_t rtt_msec,
2139+
mongoc_topology_description_hello_cluster_time_strategy_t cluster_time_strategy,
2140+
const bson_error_t *error /* IN */)
21382141
{
21392142
mongoc_topology_description_t *prev_td = NULL;
21402143
mongoc_server_description_t *prev_sd = NULL;
@@ -2218,7 +2221,9 @@ mongoc_topology_description_handle_hello(mongoc_topology_description_t *topology
22182221
}
22192222
}
22202223

2221-
mongoc_topology_description_update_cluster_time(topology, hello_response);
2224+
if (cluster_time_strategy == MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_UPDATE) {
2225+
mongoc_topology_description_update_cluster_time(topology, hello_response);
2226+
}
22222227

22232228
if (prev_sd) {
22242229
sd_changed = !_mongoc_server_description_equal(prev_sd, sd);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ typedef struct mongoc_topology_scanner {
117117
bson_mutex_t handshake_cmd_mtx;
118118
bson_t *handshake_cmd;
119119
handshake_state_t handshake_state;
120-
bson_t cluster_time;
121120
bson_oid_t topology_id;
122121
const char *appname;
123122

@@ -242,8 +241,6 @@ void
242241
mongoc_topology_scanner_set_stream_initiator(mongoc_topology_scanner_t *ts, mongoc_stream_initiator_t si, void *ctx);
243242
bool
244243
_mongoc_topology_scanner_set_appname(mongoc_topology_scanner_t *ts, const char *name);
245-
void
246-
_mongoc_topology_scanner_set_cluster_time(mongoc_topology_scanner_t *ts, const bson_t *cluster_time);
247244

248245
void
249246
_mongoc_topology_scanner_set_dns_cache_timeout(mongoc_topology_scanner_t *ts, int64_t timeout_ms);

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ _init_hello(mongoc_topology_scanner_t *ts)
169169
{
170170
bson_init(&ts->hello_cmd);
171171
bson_init(&ts->legacy_hello_cmd);
172-
bson_init(&ts->cluster_time);
173172
ts->handshake_cmd = NULL;
174173

175174
_add_hello(ts);
@@ -441,10 +440,6 @@ _begin_hello_cmd(mongoc_topology_scanner_node_t *node,
441440
bson_free(oidc_access_token);
442441
}
443442

444-
if (!bson_empty(&ts->cluster_time)) {
445-
bson_append_document(&cmd, "$clusterTime", 12, &ts->cluster_time);
446-
}
447-
448443
/* if the node should connect with a TCP socket, stream will be null, and
449444
* dns_result will be set. The async loop is responsible for calling the
450445
* _tcp_initiator to construct TCP sockets. */
@@ -533,7 +528,6 @@ mongoc_topology_scanner_destroy(mongoc_topology_scanner_t *ts)
533528
bson_destroy(&ts->hello_cmd);
534529
bson_destroy(&ts->legacy_hello_cmd);
535530
bson_destroy(ts->handshake_cmd);
536-
bson_destroy(&ts->cluster_time);
537531
mongoc_server_api_destroy(ts->api);
538532
bson_mutex_destroy(&ts->handshake_cmd_mtx);
539533

@@ -1343,17 +1337,6 @@ _mongoc_topology_scanner_set_appname(mongoc_topology_scanner_t *ts, const char *
13431337
return false;
13441338
}
13451339

1346-
/*
1347-
* Set the scanner's clusterTime unconditionally: don't compare with prior
1348-
* @cluster_time is like {clusterTime: <timestamp>}
1349-
*/
1350-
void
1351-
_mongoc_topology_scanner_set_cluster_time(mongoc_topology_scanner_t *ts, const bson_t *cluster_time)
1352-
{
1353-
bson_destroy(&ts->cluster_time);
1354-
bson_copy_to(cluster_time, &ts->cluster_time);
1355-
}
1356-
13571340
/* SDAM Monitoring Spec: send HeartbeatStartedEvent */
13581341
static void
13591342
_mongoc_topology_scanner_monitor_heartbeat_started(const mongoc_topology_scanner_t *ts, const mongoc_host_list_t *host)

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ static bool
9595
_mongoc_topology_update_no_lock(uint32_t id,
9696
const bson_t *hello_response,
9797
int64_t rtt_msec,
98+
mongoc_topology_description_hello_cluster_time_strategy_t cluster_time_strategy,
9899
mongoc_topology_description_t *td,
99100
const mongoc_log_and_monitor_instance_t *log_and_monitor,
100101
const bson_error_t *error /* IN */)
101102
{
102-
mongoc_topology_description_handle_hello(td, log_and_monitor, id, hello_response, rtt_msec, error);
103+
mongoc_topology_description_handle_hello(
104+
td, log_and_monitor, id, hello_response, rtt_msec, cluster_time_strategy, error);
103105

104106
/* return false if server removed from topology */
105107
return mongoc_topology_description_server_by_id(td, id, NULL) != NULL;
@@ -130,8 +132,13 @@ _mongoc_topology_scanner_setup_err_cb(uint32_t id, void *data, const bson_error_
130132
// Use `mc_tpld_unsafe_get_mutable` to get a mutable topology description
131133
// without locking. This function only applies to single-threaded clients.
132134
mongoc_topology_description_t *td = mc_tpld_unsafe_get_mutable(topology);
133-
mongoc_topology_description_handle_hello(
134-
td, &topology->log_and_monitor, id, NULL /* hello reply */, -1 /* rtt_msec */, error);
135+
mongoc_topology_description_handle_hello(td,
136+
&topology->log_and_monitor,
137+
id,
138+
NULL /* hello reply */,
139+
-1 /* rtt_msec */,
140+
MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_IGNORE,
141+
error);
135142
}
136143
}
137144

@@ -181,13 +188,25 @@ _mongoc_topology_scanner_cb(
181188
* client MUST change its type to Unknown only after it has retried the
182189
* server once." */
183190
if (!hello_response && sd && sd->type != MONGOC_SERVER_UNKNOWN) {
184-
_mongoc_topology_update_no_lock(id, hello_response, rtt_msec, td, &topology->log_and_monitor, error);
191+
_mongoc_topology_update_no_lock(id,
192+
hello_response,
193+
rtt_msec,
194+
MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_IGNORE,
195+
td,
196+
&topology->log_and_monitor,
197+
error);
185198

186199
/* add another hello call to the current scan - the scan continues
187200
* until all commands are done */
188201
mongoc_topology_scanner_scan(topology->scanner, sd->id);
189202
} else {
190-
_mongoc_topology_update_no_lock(id, hello_response, rtt_msec, td, &topology->log_and_monitor, error);
203+
_mongoc_topology_update_no_lock(id,
204+
hello_response,
205+
rtt_msec,
206+
MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_IGNORE,
207+
td,
208+
&topology->log_and_monitor,
209+
error);
191210

192211
/* The processing of the hello results above may have added, changed, or
193212
* removed server descriptions. We need to reconcile that with our
@@ -1444,8 +1463,13 @@ _mongoc_topology_update_from_handshake(mongoc_topology_t *topology, const mongoc
14441463
tdmod = mc_tpld_modify_begin(topology);
14451464

14461465
/* return false if server was removed from topology */
1447-
has_server = _mongoc_topology_update_no_lock(
1448-
sd->id, &sd->last_hello_response, sd->round_trip_time_msec, tdmod.new_td, &topology->log_and_monitor, NULL);
1466+
has_server = _mongoc_topology_update_no_lock(sd->id,
1467+
&sd->last_hello_response,
1468+
sd->round_trip_time_msec,
1469+
MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_UPDATE,
1470+
tdmod.new_td,
1471+
&topology->log_and_monitor,
1472+
NULL);
14491473

14501474
/* if pooled, wake threads waiting in mongoc_topology_server_by_id */
14511475
mongoc_cond_broadcast(&topology->cond_client);
@@ -1557,7 +1581,6 @@ _mongoc_topology_update_cluster_time(mongoc_topology_t *topology, const bson_t *
15571581
_mongoc_cluster_time_greater(&cluster_time, &tdmod.new_td->cluster_time)) {
15581582
bson_destroy(&tdmod.new_td->cluster_time);
15591583
bson_copy_to(&cluster_time, &tdmod.new_td->cluster_time);
1560-
_mongoc_topology_scanner_set_cluster_time(topology->scanner, &tdmod.new_td->cluster_time);
15611584
mc_tpld_modify_commit(tdmod);
15621585
} else {
15631586
mc_tpld_modify_drop(tdmod);

src/libmongoc/tests/json-test.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,13 @@ process_sdam_test_hello_responses(bson_t *phase, mongoc_topology_t *topology)
224224

225225
/* send hello through the topology description's handler */
226226
capture_logs(true);
227-
mongoc_topology_description_handle_hello(tdmod.new_td, &topology->log_and_monitor, sd->id, &response, 1, NULL);
227+
mongoc_topology_description_handle_hello(tdmod.new_td,
228+
&topology->log_and_monitor,
229+
sd->id,
230+
&response,
231+
1,
232+
MONGOC_TOPOLOGY_DESCRIPTION_HELLO_CLUSTER_TIME_IGNORE,
233+
NULL);
228234
if (mc_tpld_servers_const(tdmod.new_td)->items_len == 0) {
229235
ASSERT_CAPTURED_LOG("topology", MONGOC_LOG_LEVEL_WARNING, "Last server removed from topology");
230236
}

src/libmongoc/tests/test-mongoc-client-session.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,12 @@ started(const mongoc_apm_command_started_t *event)
10431043
}
10441044

10451045
has_cluster_time = bson_iter_init_find(&iter, cmd, "$clusterTime");
1046-
if (test->acknowledged && !has_cluster_time) {
1046+
1047+
// Since $clusterTime is no longer used with SDAM, there may not be a cluster time until the first successful
1048+
// command completes.
1049+
const bool should_have_cluster_time = test->acknowledged && test->n_succeeded >= 1;
1050+
1051+
if (should_have_cluster_time && !has_cluster_time) {
10471052
test_error("no $clusterTime sent with command %s", cmd_name);
10481053
}
10491054

0 commit comments

Comments
 (0)