@@ -84,7 +84,7 @@ _cluster_fetch_stream_pooled (mongoc_cluster_t *cluster,
84
84
bson_error_t * error );
85
85
86
86
static bool
87
- mongoc_cluster_run_opmsg (mongoc_cluster_t * cluster , mongoc_cmd_t * cmd , bson_t * reply , bson_error_t * error );
87
+ mongoc_cluster_run_opmsg (mongoc_cluster_t * cluster , const mongoc_cmd_t * cmd , bson_t * reply , bson_error_t * error );
88
88
89
89
static void
90
90
_bson_error_message_printf (bson_error_t * error , const char * format , ...) BSON_GNUC_PRINTF (2 , 3 );
@@ -235,7 +235,7 @@ static const int32_t message_header_length = 4u * sizeof (int32_t);
235
235
236
236
static bool
237
237
_mongoc_cluster_run_command_opquery_send (
238
- mongoc_cluster_t * cluster , mongoc_cmd_t * cmd , int32_t compressor_id , mcd_rpc_message * rpc , bson_error_t * error )
238
+ mongoc_cluster_t * cluster , const mongoc_cmd_t * cmd , int32_t compressor_id , mcd_rpc_message * rpc , bson_error_t * error )
239
239
{
240
240
BSON_ASSERT_PARAM (cluster );
241
241
BSON_ASSERT_PARAM (cmd );
@@ -319,7 +319,7 @@ _mongoc_cluster_run_command_opquery_send (
319
319
320
320
static bool
321
321
_mongoc_cluster_run_command_opquery_recv (
322
- mongoc_cluster_t * cluster , mongoc_cmd_t * cmd , mcd_rpc_message * rpc , bson_t * reply , bson_error_t * error )
322
+ mongoc_cluster_t * cluster , const mongoc_cmd_t * cmd , mcd_rpc_message * rpc , bson_t * reply , bson_error_t * error )
323
323
{
324
324
BSON_ASSERT_PARAM (cluster );
325
325
BSON_ASSERT_PARAM (cmd );
@@ -398,7 +398,7 @@ _mongoc_cluster_run_command_opquery_recv (
398
398
399
399
static bool
400
400
mongoc_cluster_run_command_opquery (
401
- mongoc_cluster_t * cluster , mongoc_cmd_t * cmd , int32_t compressor_id , bson_t * reply , bson_error_t * error )
401
+ mongoc_cluster_t * cluster , const mongoc_cmd_t * cmd , int32_t compressor_id , bson_t * reply , bson_error_t * error )
402
402
{
403
403
BSON_ASSERT_PARAM (cluster );
404
404
BSON_ASSERT_PARAM (cmd );
@@ -657,7 +657,10 @@ _should_use_op_msg (const mongoc_cluster_t *cluster)
657
657
*/
658
658
659
659
bool
660
- mongoc_cluster_run_command_private (mongoc_cluster_t * cluster , mongoc_cmd_t * cmd , bson_t * reply , bson_error_t * error )
660
+ mongoc_cluster_run_command_private (mongoc_cluster_t * cluster ,
661
+ const mongoc_cmd_t * cmd ,
662
+ bson_t * reply ,
663
+ bson_error_t * error )
661
664
{
662
665
bool retval ;
663
666
const mongoc_server_stream_t * server_stream ;
@@ -757,25 +760,17 @@ _stream_run_hello (mongoc_cluster_t *cluster,
757
760
bson_t * speculative_auth_response /* OUT */ ,
758
761
bson_error_t * error )
759
762
{
760
- bson_t handshake_command ; /* Initialized by dup_handshake below */
761
- mongoc_cmd_t hello_cmd ;
762
- bson_t reply ;
763
- int64_t start ;
764
- int64_t rtt_msec ;
765
- mongoc_server_description_t empty_sd ;
766
- mongoc_server_description_t * ret_handshake_sd ;
767
- mongoc_server_stream_t * server_stream ;
768
- bool r ;
769
- mongoc_ssl_opt_t * ssl_opts = NULL ;
770
763
mc_shared_tpld td = mc_tpld_take_ref (BSON_ASSERT_PTR_INLINE (cluster )-> client -> topology );
771
764
772
765
ENTRY ;
773
766
774
767
BSON_ASSERT (stream );
775
768
769
+ bson_t handshake_command ;
776
770
_mongoc_topology_dup_handshake_cmd (cluster -> client -> topology , & handshake_command );
777
771
778
772
if (cluster -> requires_auth && speculative_auth_response ) {
773
+ mongoc_ssl_opt_t * ssl_opts = NULL ;
779
774
#ifdef MONGOC_ENABLE_SSL
780
775
ssl_opts = & cluster -> client -> ssl_opts ;
781
776
#endif
@@ -787,7 +782,7 @@ _stream_run_hello (mongoc_cluster_t *cluster,
787
782
_mongoc_handshake_append_sasl_supported_mechs (cluster -> uri , & handshake_command );
788
783
}
789
784
790
- start = bson_get_monotonic_time ();
785
+ const int64_t start = bson_get_monotonic_time ();
791
786
/* TODO CDRIVER-3654: do not use a mongoc_server_stream here.
792
787
* Instead, use a plain stream. If a network error occurs, check the cluster
793
788
* node's generation (which is the generation of the created connection) to
@@ -798,33 +793,40 @@ _stream_run_hello (mongoc_cluster_t *cluster,
798
793
* Then _mongoc_cluster_stream_for_server also handles the error, and
799
794
* invalidates again.
800
795
*/
796
+ mongoc_server_description_t empty_sd ;
801
797
mongoc_server_description_init (& empty_sd , address , server_id );
802
- server_stream = _mongoc_cluster_create_server_stream (td .ptr , & empty_sd , stream );
803
-
798
+ mongoc_server_stream_t * const server_stream = _mongoc_cluster_create_server_stream (td .ptr , & empty_sd , stream );
804
799
mongoc_server_description_cleanup (& empty_sd );
805
800
806
- /* Set up the shared parts of the mongo_cmd_t, which will later be converted
807
- to either an op_msg or op_query: */
808
- memset (& hello_cmd , 0 , sizeof (hello_cmd ));
809
-
801
+ mongoc_query_flags_t query_flags = MONGOC_QUERY_NONE ;
810
802
/* Use OP_QUERY for the handshake, unless the user has specified an
811
803
* API version; the correct hello_cmd has already been selected: */
812
804
if (!_should_use_op_msg (cluster )) {
813
805
/* Complete OPCODE_QUERY setup: */
814
- hello_cmd . query_flags = MONGOC_QUERY_SECONDARY_OK ;
806
+ query_flags | = MONGOC_QUERY_SECONDARY_OK ;
815
807
} else {
816
808
/* We're using OP_MSG, and require some additional doctoring: */
817
809
bson_append_utf8 (& handshake_command , "$db" , 3 , "admin" , 5 );
818
810
}
819
811
820
- hello_cmd .db_name = "admin" ;
821
- hello_cmd .command = & handshake_command ;
822
- hello_cmd .command_name = _mongoc_get_command_name (& handshake_command );
823
- hello_cmd .server_stream = server_stream ;
824
- hello_cmd .is_acknowledged = true;
812
+ /* Set up the shared parts of the mongo_cmd_t, which will later be converted
813
+ to either an op_msg or op_query: */
814
+ const mongoc_cmd_t hello_cmd = {
815
+ .db_name = "admin" ,
816
+ .command = & handshake_command ,
817
+ .command_name = _mongoc_get_command_name (& handshake_command ),
818
+ .server_stream = server_stream ,
819
+ .is_acknowledged = true,
820
+ .query_flags = query_flags ,
821
+ };
825
822
823
+ bson_t reply ;
824
+ // The final resulting server description
825
+ mongoc_server_description_t * ret_handshake_sd = NULL ;
826
826
if (!mongoc_cluster_run_command_private (cluster , & hello_cmd , & reply , error )) {
827
+ // Command execution failed.
827
828
if (negotiate_sasl_supported_mechs ) {
829
+ // Negotiating a new SASL mechanism
828
830
bsonParse (reply ,
829
831
find (allOf (key ("ok" ), isFalse ), //
830
832
do ({
@@ -836,39 +838,35 @@ _stream_run_hello (mongoc_cluster_t *cluster,
836
838
error -> code = MONGOC_ERROR_CLIENT_AUTHENTICATE ;
837
839
})));
838
840
}
841
+ } else {
842
+ // "hello" succeeded
839
843
840
- mongoc_server_stream_cleanup (server_stream );
841
- ret_handshake_sd = NULL ;
842
- goto done ;
843
- }
844
-
845
- rtt_msec = (bson_get_monotonic_time () - start ) / 1000 ;
846
-
847
- ret_handshake_sd = BSON_ALIGNED_ALLOC0 (mongoc_server_description_t );
844
+ // Round-trip time for the hello command
845
+ const int64_t rtt_msec = (bson_get_monotonic_time () - start ) / 1000 ;
848
846
849
- mongoc_server_description_init (ret_handshake_sd , address , server_id );
850
- /* send the error from run_command IN to handle_hello */
851
- mongoc_server_description_handle_hello (ret_handshake_sd , & reply , rtt_msec , error );
847
+ ret_handshake_sd = BSON_ALIGNED_ALLOC0 (mongoc_server_description_t );
848
+ mongoc_server_description_init (ret_handshake_sd , address , server_id );
849
+ /* send the error from run_command IN to handle_hello */
850
+ mongoc_server_description_handle_hello (ret_handshake_sd , & reply , rtt_msec , error );
852
851
853
- if (cluster -> requires_auth && speculative_auth_response ) {
854
- _mongoc_topology_scanner_parse_speculative_authentication (& reply , speculative_auth_response );
855
- }
852
+ if (cluster -> requires_auth && speculative_auth_response ) {
853
+ _mongoc_topology_scanner_parse_speculative_authentication (& reply , speculative_auth_response );
854
+ }
856
855
857
- /* Note: This call will render our copy of the topology description to be
858
- * stale */
859
- r = _mongoc_topology_update_from_handshake (cluster -> client -> topology , ret_handshake_sd );
860
- if (!r ) {
861
- mongoc_server_description_reset (ret_handshake_sd );
862
- bson_set_error (& ret_handshake_sd -> error ,
863
- MONGOC_ERROR_STREAM ,
864
- MONGOC_ERROR_STREAM_NOT_ESTABLISHED ,
865
- "\"%s\" removed from topology" ,
866
- address );
856
+ /* Note: This call will render our copy of the topology description to be
857
+ * stale */
858
+ const bool update_okay = _mongoc_topology_update_from_handshake (cluster -> client -> topology , ret_handshake_sd );
859
+ if (!update_okay ) {
860
+ mongoc_server_description_reset (ret_handshake_sd );
861
+ bson_set_error (& ret_handshake_sd -> error ,
862
+ MONGOC_ERROR_STREAM ,
863
+ MONGOC_ERROR_STREAM_NOT_ESTABLISHED ,
864
+ "\"%s\" removed from topology" ,
865
+ address );
866
+ }
867
867
}
868
868
869
869
mongoc_server_stream_cleanup (server_stream );
870
-
871
- done :
872
870
bson_destroy (& handshake_command );
873
871
bson_destroy (& reply );
874
872
mc_tpld_drop_ref (& td );
@@ -3179,7 +3177,7 @@ mongoc_cluster_try_recv (mongoc_cluster_t *cluster,
3179
3177
3180
3178
3181
3179
static void
3182
- network_error_reply (bson_t * reply , mongoc_cmd_t * cmd )
3180
+ network_error_reply (bson_t * reply , const mongoc_cmd_t * cmd )
3183
3181
{
3184
3182
bson_array_builder_t * labels ;
3185
3183
@@ -3220,7 +3218,7 @@ network_error_reply (bson_t *reply, mongoc_cmd_t *cmd)
3220
3218
3221
3219
static bool
3222
3220
_mongoc_cluster_run_opmsg_send (
3223
- mongoc_cluster_t * cluster , mongoc_cmd_t * cmd , mcd_rpc_message * rpc , bson_t * reply , bson_error_t * error )
3221
+ mongoc_cluster_t * cluster , const mongoc_cmd_t * cmd , mcd_rpc_message * rpc , bson_t * reply , bson_error_t * error )
3224
3222
{
3225
3223
BSON_ASSERT_PARAM (cluster );
3226
3224
BSON_ASSERT_PARAM (cmd );
@@ -3313,7 +3311,7 @@ _mongoc_cluster_run_opmsg_send (
3313
3311
3314
3312
static bool
3315
3313
_mongoc_cluster_run_opmsg_recv (
3316
- mongoc_cluster_t * cluster , mongoc_cmd_t * cmd , mcd_rpc_message * rpc , bson_t * reply , bson_error_t * error )
3314
+ mongoc_cluster_t * cluster , const mongoc_cmd_t * cmd , mcd_rpc_message * rpc , bson_t * reply , bson_error_t * error )
3317
3315
{
3318
3316
BSON_ASSERT_PARAM (cluster );
3319
3317
BSON_ASSERT_PARAM (cmd );
@@ -3421,7 +3419,7 @@ _mongoc_cluster_run_opmsg_recv (
3421
3419
}
3422
3420
3423
3421
static bool
3424
- mongoc_cluster_run_opmsg (mongoc_cluster_t * cluster , mongoc_cmd_t * cmd , bson_t * reply , bson_error_t * error )
3422
+ mongoc_cluster_run_opmsg (mongoc_cluster_t * cluster , const mongoc_cmd_t * cmd , bson_t * reply , bson_error_t * error )
3425
3423
{
3426
3424
BSON_ASSERT_PARAM (cluster );
3427
3425
BSON_ASSERT_PARAM (cmd );
0 commit comments