Skip to content

Commit a2ecf92

Browse files
authored
CDRIVER-5577 add serverMonitoringMode URI option (#1724)
* add sdam-options spec test * add duplication test * implement serverMonitoringMode parsing and logic * add tests to mimic serverMonitoringMode.yml * implement stream/poll monitoring logic based on serverMonitoringMode * add FAAS env tests and change test to only run on live servers * create pool with test_framework fx * add enum in server-monitor.c * add documentation for new public API * validation improvements in mongoc-uri * address test comments
1 parent b88cedc commit a2ecf92

10 files changed

+418
-5
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
:man_page: mongoc_uri_get_server_monitoring_mode
2+
3+
mongoc_uri_get_server_monitoring_mode()
4+
=======================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
const char *
12+
mongoc_uri_get_server_monitoring_mode (const mongoc_uri_t *uri);
13+
14+
Parameters
15+
----------
16+
17+
* ``uri``: A :symbol:`mongoc_uri_t`.
18+
19+
Description
20+
-----------
21+
22+
Fetches the ``serverMonitoringMode`` parameter to an URI if provided.
23+
24+
Returns
25+
-------
26+
27+
A string which should not be modified or freed. Returns "auto" if the ``serverMonitoringMode`` parameter was not provided to ``uri``.
28+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
:man_page: mongoc_uri_set_server_monitoring_mode
2+
3+
mongoc_uri_set_server_monitoring_mode()
4+
=======================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
bool
12+
mongoc_uri_set_server_monitoring_mode (mongoc_uri_t *uri, const char *value);
13+
14+
Parameters
15+
----------
16+
17+
* ``uri``: A :symbol:`mongoc_uri_t`.
18+
* ``value``: The new ``serverMonitoringMode`` value.
19+
20+
Description
21+
-----------
22+
23+
Sets the ``serverMonitoringMode`` URI option to ``value`` after the URI has been parsed from a string.
24+
25+
Updates the option in-place if already set, otherwise appends it to the URI's :symbol:`bson:bson_t` of options.
26+
27+
Returns
28+
-------
29+
30+
Returns false if the ``value`` is not "auto", "poll", or "stream".
31+

src/libmongoc/doc/mongoc_uri_t.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ MONGOC_URI_SAFE safe {tr
308308
mongoc_uri_get_read_prefs
309309
mongoc_uri_get_read_prefs_t
310310
mongoc_uri_get_replica_set
311+
mongoc_uri_get_server_monitoring_mode
311312
mongoc_uri_get_service
312313
mongoc_uri_get_ssl
313314
mongoc_uri_get_string
@@ -336,6 +337,7 @@ MONGOC_URI_SAFE safe {tr
336337
mongoc_uri_set_password
337338
mongoc_uri_set_read_concern
338339
mongoc_uri_set_read_prefs_t
340+
mongoc_uri_set_server_monitoring_mode
339341
mongoc_uri_set_username
340342
mongoc_uri_set_write_concern
341343
mongoc_uri_unescape

src/libmongoc/src/mongoc/mongoc-server-monitor-private.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
#include "mongoc-topology-private.h"
2525

2626
/* For background monitoring of a single server. */
27+
typedef enum {
28+
MONGOC_SERVER_MONITORING_AUTO = 0,
29+
MONGOC_SERVER_MONITORING_POLL,
30+
MONGOC_SERVER_MONITORING_STREAM
31+
} mongoc_server_monitoring_mode_t;
2732

2833
typedef struct _mongoc_server_monitor_t mongoc_server_monitor_t;
2934

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ struct _mongoc_server_monitor_t {
8888
mongoc_server_description_t *description;
8989
uint32_t server_id;
9090
bool is_rtt;
91+
mongoc_server_monitoring_mode_t mode;
9192
};
9293

9394
static BSON_GNUC_PRINTF (3, 4) void _server_monitor_log (mongoc_server_monitor_t *server_monitor,
@@ -789,6 +790,24 @@ _update_topology_description (mongoc_server_monitor_t *server_monitor, mongoc_se
789790
mc_tpld_modify_commit (tdmod);
790791
}
791792

793+
/* Get the mode enum based on the uri
794+
*
795+
* Called during server monitor creation
796+
*/
797+
static mongoc_server_monitoring_mode_t
798+
_server_monitor_get_mode_enum (mongoc_server_monitor_t *server_monitor)
799+
{
800+
const char *mode_str = mongoc_uri_get_server_monitoring_mode (server_monitor->uri);
801+
802+
if (strcmp (mode_str, "poll") == 0) {
803+
return MONGOC_SERVER_MONITORING_POLL;
804+
} else if (strcmp (mode_str, "stream") == 0) {
805+
return MONGOC_SERVER_MONITORING_STREAM;
806+
} else {
807+
return MONGOC_SERVER_MONITORING_AUTO;
808+
}
809+
}
810+
792811
/* Create a new server monitor.
793812
*
794813
* Called during reconcile.
@@ -820,6 +839,7 @@ mongoc_server_monitor_new (mongoc_topology_t *topology,
820839
server_monitor->apm_context = td->apm_context;
821840
server_monitor->initiator = topology->scanner->initiator;
822841
server_monitor->initiator_context = topology->scanner->initiator_context;
842+
server_monitor->mode = _server_monitor_get_mode_enum (server_monitor);
823843
mongoc_cond_init (&server_monitor->shared.cond);
824844
bson_mutex_init (&server_monitor->shared.mutex);
825845
return server_monitor;
@@ -949,11 +969,15 @@ _server_monitor_check_server (mongoc_server_monitor_t *server_monitor,
949969
GOTO (exit);
950970
}
951971

952-
if (!bson_empty (&previous_description->topology_version) &&
953-
_mongoc_handshake_get ()->env == MONGOC_HANDSHAKE_ENV_NONE) {
972+
if (server_monitor->mode != MONGOC_SERVER_MONITORING_POLL && !bson_empty (&previous_description->topology_version) &&
973+
(_mongoc_handshake_get ()->env == MONGOC_HANDSHAKE_ENV_NONE ||
974+
server_monitor->mode == MONGOC_SERVER_MONITORING_STREAM)) {
954975
// Use stream monitoring if:
976+
// - serverMonitoringMode != "poll"
955977
// - Server supports stream monitoring (indicated by `topologyVersion`).
956-
// - Application is not in an FaaS environment (e.g. AWS Lambda).
978+
// - ONE OF:
979+
// - Application is not in an FaaS environment (e.g. AWS Lambda).
980+
// - serverMonitoringMode == "stream"
957981
awaited = true;
958982
_server_monitor_heartbeat_started (server_monitor, awaited);
959983
MONITOR_LOG (server_monitor, "awaitable hello");

src/libmongoc/src/mongoc/mongoc-uri.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,8 @@ bool
749749
mongoc_uri_option_is_utf8 (const char *key)
750750
{
751751
return !strcasecmp (key, MONGOC_URI_APPNAME) || !strcasecmp (key, MONGOC_URI_REPLICASET) ||
752-
!strcasecmp (key, MONGOC_URI_READPREFERENCE) || !strcasecmp (key, MONGOC_URI_SRVSERVICENAME) ||
753-
!strcasecmp (key, MONGOC_URI_TLSCERTIFICATEKEYFILE) ||
752+
!strcasecmp (key, MONGOC_URI_READPREFERENCE) || !strcasecmp (key, MONGOC_URI_SERVERMONITORINGMODE) ||
753+
!strcasecmp (key, MONGOC_URI_SRVSERVICENAME) || !strcasecmp (key, MONGOC_URI_TLSCERTIFICATEKEYFILE) ||
754754
!strcasecmp (key, MONGOC_URI_TLSCERTIFICATEKEYFILEPASSWORD) || !strcasecmp (key, MONGOC_URI_TLSCAFILE) ||
755755
/* deprecated options */
756756
!strcasecmp (key, MONGOC_URI_SSLCLIENTCERTIFICATEKEYFILE) ||
@@ -1142,6 +1142,11 @@ mongoc_uri_apply_options (mongoc_uri_t *uri, const bson_t *options, bool from_dn
11421142
goto UNSUPPORTED_VALUE;
11431143
}
11441144

1145+
} else if (!strcmp (key, MONGOC_URI_SERVERMONITORINGMODE)) {
1146+
if (!mongoc_uri_set_server_monitoring_mode (uri, value)) {
1147+
goto UNSUPPORTED_VALUE;
1148+
}
1149+
11451150
} else if (mongoc_uri_option_is_utf8 (key)) {
11461151
mongoc_uri_bson_append_or_replace_key (&uri->options, canon, value);
11471152

@@ -2350,6 +2355,30 @@ mongoc_uri_get_ssl (const mongoc_uri_t *uri) /* IN */
23502355
return mongoc_uri_get_tls (uri);
23512356
}
23522357

2358+
const char *
2359+
mongoc_uri_get_server_monitoring_mode (const mongoc_uri_t *uri)
2360+
{
2361+
BSON_ASSERT_PARAM (uri);
2362+
2363+
return mongoc_uri_get_option_as_utf8 (uri, MONGOC_URI_SERVERMONITORINGMODE, "auto");
2364+
}
2365+
2366+
2367+
bool
2368+
mongoc_uri_set_server_monitoring_mode (mongoc_uri_t *uri, const char *value)
2369+
{
2370+
BSON_ASSERT_PARAM (uri);
2371+
BSON_ASSERT_PARAM (value);
2372+
2373+
// Check for valid value
2374+
if (strcmp (value, "stream") && strcmp (value, "poll") && strcmp (value, "auto")) {
2375+
return false;
2376+
}
2377+
2378+
mongoc_uri_bson_append_or_replace_key (&uri->options, MONGOC_URI_SERVERMONITORINGMODE, value);
2379+
return true;
2380+
}
2381+
23532382
/*
23542383
*--------------------------------------------------------------------------
23552384
*
@@ -2886,6 +2915,8 @@ mongoc_uri_set_option_as_utf8 (mongoc_uri_t *uri, const char *option_orig, const
28862915
}
28872916
if (!bson_strcasecmp (option, MONGOC_URI_APPNAME)) {
28882917
return mongoc_uri_set_appname (uri, value);
2918+
} else if (!bson_strcasecmp (option, MONGOC_URI_SERVERMONITORINGMODE)) {
2919+
return mongoc_uri_set_server_monitoring_mode (uri, value);
28892920
} else {
28902921
option_lowercase = lowercase_str_new (option);
28912922
mongoc_uri_bson_append_or_replace_key (&uri->options, option_lowercase, value);

src/libmongoc/src/mongoc/mongoc-uri.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#define MONGOC_URI_RETRYREADS "retryreads"
5858
#define MONGOC_URI_RETRYWRITES "retrywrites"
5959
#define MONGOC_URI_SAFE "safe"
60+
#define MONGOC_URI_SERVERMONITORINGMODE "servermonitoringmode"
6061
#define MONGOC_URI_SERVERSELECTIONTIMEOUTMS "serverselectiontimeoutms"
6162
#define MONGOC_URI_SERVERSELECTIONTRYONCE "serverselectiontryonce"
6263
#define MONGOC_URI_SLAVEOK "slaveok"
@@ -197,6 +198,10 @@ MONGOC_EXPORT (const mongoc_read_concern_t *)
197198
mongoc_uri_get_read_concern (const mongoc_uri_t *uri);
198199
MONGOC_EXPORT (void)
199200
mongoc_uri_set_read_concern (mongoc_uri_t *uri, const mongoc_read_concern_t *rc);
201+
MONGOC_EXPORT (const char *)
202+
mongoc_uri_get_server_monitoring_mode (const mongoc_uri_t *uri);
203+
MONGOC_EXPORT (bool)
204+
mongoc_uri_set_server_monitoring_mode (mongoc_uri_t *uri, const char *value);
200205

201206
BSON_END_DECLS
202207

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"tests": [
3+
{
4+
"description": "serverMonitoringMode=auto",
5+
"uri": "mongodb://example.com/?serverMonitoringMode=auto",
6+
"valid": true,
7+
"warning": false,
8+
"hosts": null,
9+
"auth": null,
10+
"options": {
11+
"serverMonitoringMode": "auto"
12+
}
13+
},
14+
{
15+
"description": "serverMonitoringMode=stream",
16+
"uri": "mongodb://example.com/?serverMonitoringMode=stream",
17+
"valid": true,
18+
"warning": false,
19+
"hosts": null,
20+
"auth": null,
21+
"options": {
22+
"serverMonitoringMode": "stream"
23+
}
24+
},
25+
{
26+
"description": "serverMonitoringMode=poll",
27+
"uri": "mongodb://example.com/?serverMonitoringMode=poll",
28+
"valid": true,
29+
"warning": false,
30+
"hosts": null,
31+
"auth": null,
32+
"options": {
33+
"serverMonitoringMode": "poll"
34+
}
35+
},
36+
{
37+
"description": "invalid serverMonitoringMode",
38+
"uri": "mongodb://example.com/?serverMonitoringMode=invalid",
39+
"valid": true,
40+
"warning": true,
41+
"hosts": null,
42+
"auth": null,
43+
"options": {}
44+
}
45+
]
46+
}

0 commit comments

Comments
 (0)