Skip to content

Commit d08ef11

Browse files
authored
CDRIVER-3947 Use "hello" command when an API version is declared (#784)
* Use hello command for handshake with versioned API * Support handling hello command responses * Deprecate legacy hello terminology * Test handling hello command responses * Refactor internal methods to use hello terminology * Change mock server to also handle hello * Replace deprecated terminology in test expectations * Use hello in tests * Fix speculative auth responder * Fix legacy hello command usage * Add test for behaviour when server does not support hello command * Use legacy hello in tests to avoid failures on old server versions * Update SDAM spec tests * Fix memory leak * Fix wrong command expectations for handshake * Refactor handshake autoresponders * Fix wrong comment style * Rename mongoc_server_description_hello_response * Use method to retrieve hello response * Improve deprecation declaration * Rename methods for hello callbacks * Fix various test issues * Rename hello_response method
1 parent 3e9adec commit d08ef11

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1436
-1122
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
:man_page: mongoc_server_description_hello_response
2+
3+
mongoc_server_description_hello_response()
4+
===============================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
const bson_t *
12+
mongoc_server_description_hello_response (
13+
const mongoc_server_description_t *description);
14+
15+
Parameters
16+
----------
17+
18+
* ``description``: A :symbol:`mongoc_server_description_t`.
19+
20+
Description
21+
-----------
22+
23+
The client or client pool periodically runs a
24+
`"hello" <https://docs.mongodb.org/manual/reference/command/isMaster/>`_
25+
command on each server, to update its view of the MongoDB deployment. Use
26+
:symbol:`mongoc_client_get_server_descriptions()` and
27+
``mongoc_server_description_hello_response()`` to get the most recent "hello"
28+
response.
29+
30+
Returns
31+
-------
32+
33+
A reference to a BSON document, owned by the server description. The document is empty if the driver is not connected to the server.
34+

src/libmongoc/doc/mongoc_server_description_ismaster.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ Synopsis
1212
mongoc_server_description_ismaster (
1313
const mongoc_server_description_t *description);
1414
15+
Deprecated
16+
----------
17+
18+
.. warning::
19+
20+
This function is deprecated and should not be used in new code.
21+
22+
Please use :doc:`mongoc_server_description_hello_response() <mongoc_server_description_hello_response>` instead.
23+
1524
Parameters
1625
----------
1726

src/libmongoc/doc/mongoc_server_description_t.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Applications receive a temporary reference to a ``mongoc_server_description_t``
3333
:maxdepth: 1
3434

3535
mongoc_server_description_destroy
36+
mongoc_server_description_hello_response
3637
mongoc_server_description_host
3738
mongoc_server_description_id
3839
mongoc_server_description_ismaster

src/libmongoc/src/mongoc/mongoc-cluster.c

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,11 @@ mongoc_cluster_run_command_opquery (mongoc_cluster_t *cluster,
285285
_mongoc_rpc_gather (&rpc, &cluster->iov);
286286
_mongoc_rpc_swab_to_le (&rpc);
287287

288-
if (compressor_id != -1 && IS_NOT_COMMAND ("ismaster") &&
289-
IS_NOT_COMMAND ("saslstart") && IS_NOT_COMMAND ("saslcontinue") &&
290-
IS_NOT_COMMAND ("getnonce") && IS_NOT_COMMAND ("authenticate") &&
291-
IS_NOT_COMMAND ("createuser") && IS_NOT_COMMAND ("updateuser")) {
288+
if (compressor_id != -1 && IS_NOT_COMMAND (HANDSHAKE_CMD_LEGACY_HELLO) &&
289+
IS_NOT_COMMAND ("hello") && IS_NOT_COMMAND ("saslstart") &&
290+
IS_NOT_COMMAND ("saslcontinue") && IS_NOT_COMMAND ("getnonce") &&
291+
IS_NOT_COMMAND ("authenticate") && IS_NOT_COMMAND ("createuser") &&
292+
IS_NOT_COMMAND ("updateuser")) {
292293
output = _mongoc_rpc_compress (cluster, compressor_id, &rpc, error);
293294
if (output == NULL) {
294295
GOTO (done);
@@ -730,11 +731,11 @@ mongoc_cluster_run_command_parts (mongoc_cluster_t *cluster,
730731
/*
731732
*--------------------------------------------------------------------------
732733
*
733-
* _mongoc_stream_run_ismaster --
734+
* _mongoc_stream_run_hello --
734735
*
735-
* Run an ismaster command on the given stream. If
736+
* Run a hello command on the given stream. If
736737
* @negotiate_sasl_supported_mechs is true, then saslSupportedMechs is
737-
* added to the ismaster command.
738+
* added to the hello command.
738739
*
739740
* Returns:
740741
* A mongoc_server_description_t you must destroy or NULL. If the call
@@ -743,18 +744,18 @@ mongoc_cluster_run_command_parts (mongoc_cluster_t *cluster,
743744
*--------------------------------------------------------------------------
744745
*/
745746
static mongoc_server_description_t *
746-
_mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
747-
mongoc_stream_t *stream,
748-
const char *address,
749-
uint32_t server_id,
750-
bool negotiate_sasl_supported_mechs,
751-
mongoc_scram_cache_t *scram_cache,
752-
mongoc_scram_t *scram,
753-
bson_t *speculative_auth_response /* OUT */,
754-
bson_error_t *error)
747+
_mongoc_stream_run_hello (mongoc_cluster_t *cluster,
748+
mongoc_stream_t *stream,
749+
const char *address,
750+
uint32_t server_id,
751+
bool negotiate_sasl_supported_mechs,
752+
mongoc_scram_cache_t *scram_cache,
753+
mongoc_scram_t *scram,
754+
bson_t *speculative_auth_response /* OUT */,
755+
bson_error_t *error)
755756
{
756757
const bson_t *command;
757-
mongoc_cmd_t ismaster_cmd;
758+
mongoc_cmd_t hello_cmd;
758759
bson_t reply;
759760
int64_t start;
760761
int64_t rtt_msec;
@@ -813,15 +814,15 @@ _mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
813814
* last known ismaster indicates the server supports a newer wire protocol.
814815
*/
815816
server_stream->sd->max_wire_version = WIRE_VERSION_MIN;
816-
memset (&ismaster_cmd, 0, sizeof (ismaster_cmd));
817-
ismaster_cmd.db_name = "admin";
818-
ismaster_cmd.command = command;
819-
ismaster_cmd.command_name = _mongoc_get_command_name (command);
820-
ismaster_cmd.query_flags = MONGOC_QUERY_SLAVE_OK;
821-
ismaster_cmd.server_stream = server_stream;
817+
memset (&hello_cmd, 0, sizeof (hello_cmd));
818+
hello_cmd.db_name = "admin";
819+
hello_cmd.command = command;
820+
hello_cmd.command_name = _mongoc_get_command_name (command);
821+
hello_cmd.query_flags = MONGOC_QUERY_SLAVE_OK;
822+
hello_cmd.server_stream = server_stream;
822823

823824
if (!mongoc_cluster_run_command_private (
824-
cluster, &ismaster_cmd, &reply, error)) {
825+
cluster, &hello_cmd, &reply, error)) {
825826
if (negotiate_sasl_supported_mechs) {
826827
if (bson_iter_init_find (&iter, &reply, "ok") &&
827828
!bson_iter_as_bool (&iter)) {
@@ -845,8 +846,8 @@ _mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
845846
sizeof (mongoc_server_description_t));
846847

847848
mongoc_server_description_init (sd, address, server_id);
848-
/* send the error from run_command IN to handle_ismaster */
849-
mongoc_server_description_handle_ismaster (sd, &reply, rtt_msec, error);
849+
/* send the error from run_command IN to handle_hello */
850+
mongoc_server_description_handle_hello (sd, &reply, rtt_msec, error);
850851

851852
if (cluster->requires_auth && speculative_auth_response) {
852853
_mongoc_topology_scanner_parse_speculative_authentication (
@@ -877,28 +878,28 @@ _mongoc_stream_run_ismaster (mongoc_cluster_t *cluster,
877878
/*
878879
*--------------------------------------------------------------------------
879880
*
880-
* _mongoc_cluster_run_ismaster --
881+
* _mongoc_cluster_run_hello --
881882
*
882-
* Run an initial ismaster command for the given node and handle result.
883+
* Run an initial hello command for the given node and handle result.
883884
*
884885
* Returns:
885886
* mongoc_server_description_t on success, NULL otherwise.
886887
* the mongoc_server_description_t MUST BE DESTROYED BY THE CALLER.
887888
*
888889
* Side effects:
889890
* Makes a blocking I/O call, updates cluster->topology->description
890-
* with ismaster result.
891+
* with hello result.
891892
*
892893
*--------------------------------------------------------------------------
893894
*/
894895
static mongoc_server_description_t *
895-
_mongoc_cluster_run_ismaster (mongoc_cluster_t *cluster,
896-
mongoc_cluster_node_t *node,
897-
uint32_t server_id,
898-
mongoc_scram_cache_t *scram_cache,
899-
mongoc_scram_t *scram /* OUT */,
900-
bson_t *speculative_auth_response /* OUT */,
901-
bson_error_t *error /* OUT */)
896+
_mongoc_cluster_run_hello (mongoc_cluster_t *cluster,
897+
mongoc_cluster_node_t *node,
898+
uint32_t server_id,
899+
mongoc_scram_cache_t *scram_cache,
900+
mongoc_scram_t *scram /* OUT */,
901+
bson_t *speculative_auth_response /* OUT */,
902+
bson_error_t *error /* OUT */)
902903
{
903904
mongoc_server_description_t *sd;
904905

@@ -908,7 +909,7 @@ _mongoc_cluster_run_ismaster (mongoc_cluster_t *cluster,
908909
BSON_ASSERT (node);
909910
BSON_ASSERT (node->stream);
910911

911-
sd = _mongoc_stream_run_ismaster (
912+
sd = _mongoc_stream_run_hello (
912913
cluster,
913914
node->stream,
914915
node->connection_address,
@@ -2095,18 +2096,18 @@ _mongoc_cluster_add_node (mongoc_cluster_t *cluster,
20952096
cluster_node =
20962097
_mongoc_cluster_node_new (stream, generation, host->host_and_port);
20972098

2098-
sd = _mongoc_cluster_run_ismaster (cluster,
2099-
cluster_node,
2100-
server_id,
2101-
cluster->scram_cache,
2102-
&scram,
2103-
&speculative_auth_response,
2104-
error);
2099+
sd = _mongoc_cluster_run_hello (cluster,
2100+
cluster_node,
2101+
server_id,
2102+
cluster->scram_cache,
2103+
&scram,
2104+
&speculative_auth_response,
2105+
error);
21052106
if (!sd) {
21062107
GOTO (error);
21072108
}
21082109

2109-
_mongoc_handshake_parse_sasl_supported_mechs (&sd->last_is_master,
2110+
_mongoc_handshake_parse_sasl_supported_mechs (&sd->last_hello_response,
21102111
&sasl_supported_mechs);
21112112

21122113
if (cluster->requires_auth) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,8 @@ mongoc_cmd_is_compressible (mongoc_cmd_t *cmd)
10731073
BSON_ASSERT (cmd);
10741074
BSON_ASSERT (cmd->command_name);
10751075

1076-
return !!strcasecmp (cmd->command_name, "ismaster") &&
1076+
return !!strcasecmp (cmd->command_name, "hello") &&
1077+
!!strcasecmp (cmd->command_name, HANDSHAKE_CMD_LEGACY_HELLO) &&
10771078
!!strcasecmp (cmd->command_name, "authenticate") &&
10781079
!!strcasecmp (cmd->command_name, "getnonce") &&
10791080
!!strcasecmp (cmd->command_name, "saslstart") &&

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ BSON_BEGIN_DECLS
3535
#define HANDSHAKE_OS_ARCHITECTURE_MAX 32
3636
#define HANDSHAKE_DRIVER_NAME_MAX 64
3737
#define HANDSHAKE_DRIVER_VERSION_MAX 32
38+
39+
#define HANDSHAKE_CMD_LEGACY_HELLO "isMaster"
40+
#define HANDSHAKE_RESPONSE_LEGACY_HELLO "ismaster"
3841
/* platform has no fixed max size. It can just occupy the remaining
3942
* available space in the document. */
4043

src/libmongoc/src/mongoc/mongoc-handshake.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ BSON_BEGIN_DECLS
6161
* Returns true if the given fields are set successfully. Otherwise, it returns
6262
* false and logs an error.
6363
*
64-
* The default handshake data the driver sends with "isMaster" looks something
64+
* The default handshake data the driver sends with "hello" looks something
6565
* like:
6666
* client: {
6767
* driver: {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ struct _mongoc_server_description_t {
6262
mongoc_host_list_t host;
6363
int64_t round_trip_time_msec;
6464
int64_t last_update_time_usec;
65-
bson_t last_is_master;
66-
bool has_is_master;
65+
bson_t last_hello_response;
66+
bool has_hello_response;
6767
const char *connection_address;
6868
/* SDAM dictates storing me/hosts/passives/arbiters after being "normalized
6969
* to lower-case" Instead, they are stored in the casing they are received,
@@ -149,10 +149,10 @@ mongoc_server_description_update_rtt (mongoc_server_description_t *server,
149149
int64_t rtt_msec);
150150

151151
void
152-
mongoc_server_description_handle_ismaster (mongoc_server_description_t *sd,
153-
const bson_t *reply,
154-
int64_t rtt_msec,
155-
const bson_error_t *error /* IN */);
152+
mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
153+
const bson_t *hello_response,
154+
int64_t rtt_msec,
155+
const bson_error_t *error /* IN */);
156156

157157
void
158158
mongoc_server_description_filter_stale (mongoc_server_description_t **sds,

0 commit comments

Comments
 (0)