@@ -2707,6 +2707,7 @@ _mongoc_cluster_select_server_id (mongoc_client_session_t *cs,
2707
2707
mongoc_topology_t * topology ,
2708
2708
mongoc_ss_optype_t optype ,
2709
2709
const mongoc_read_prefs_t * read_prefs ,
2710
+ bool * must_use_primary ,
2710
2711
bson_error_t * error )
2711
2712
{
2712
2713
uint32_t server_id ;
@@ -2715,14 +2716,14 @@ _mongoc_cluster_select_server_id (mongoc_client_session_t *cs,
2715
2716
server_id = cs -> server_id ;
2716
2717
if (!server_id ) {
2717
2718
server_id = mongoc_topology_select_server_id (
2718
- topology , optype , read_prefs , error );
2719
+ topology , optype , read_prefs , must_use_primary , error );
2719
2720
if (server_id ) {
2720
2721
_mongoc_client_session_pin (cs , server_id );
2721
2722
}
2722
2723
}
2723
2724
} else {
2724
- server_id =
2725
- mongoc_topology_select_server_id ( topology , optype , read_prefs , error );
2725
+ server_id = mongoc_topology_select_server_id (
2726
+ topology , optype , read_prefs , must_use_primary , error );
2726
2727
/* Transactions Spec: Additionally, any non-transaction operation using a
2727
2728
* pinned ClientSession MUST unpin the session and the operation MUST
2728
2729
* perform normal server selection. */
@@ -2763,13 +2764,14 @@ _mongoc_cluster_stream_for_optype (mongoc_cluster_t *cluster,
2763
2764
mongoc_server_stream_t * server_stream ;
2764
2765
uint32_t server_id ;
2765
2766
mongoc_topology_t * topology = cluster -> client -> topology ;
2767
+ bool must_use_primary = false;
2766
2768
2767
2769
ENTRY ;
2768
2770
2769
2771
BSON_ASSERT (cluster );
2770
2772
2771
2773
server_id = _mongoc_cluster_select_server_id (
2772
- cs , topology , optype , read_prefs , error );
2774
+ cs , topology , optype , read_prefs , & must_use_primary , error );
2773
2775
2774
2776
if (!server_id ) {
2775
2777
_mongoc_bson_init_with_transient_txn_error (cs , reply );
@@ -2779,7 +2781,7 @@ _mongoc_cluster_stream_for_optype (mongoc_cluster_t *cluster,
2779
2781
if (!mongoc_cluster_check_interval (cluster , server_id )) {
2780
2782
/* Server Selection Spec: try once more */
2781
2783
server_id = _mongoc_cluster_select_server_id (
2782
- cs , topology , optype , read_prefs , error );
2784
+ cs , topology , optype , read_prefs , & must_use_primary , error );
2783
2785
2784
2786
if (!server_id ) {
2785
2787
_mongoc_bson_init_with_transient_txn_error (cs , reply );
@@ -2790,6 +2792,9 @@ _mongoc_cluster_stream_for_optype (mongoc_cluster_t *cluster,
2790
2792
/* connect or reconnect to server if necessary */
2791
2793
server_stream = _mongoc_cluster_stream_for_server (
2792
2794
cluster , server_id , true /* reconnect_ok */ , cs , reply , error );
2795
+ if (server_stream ) {
2796
+ server_stream -> must_use_primary = must_use_primary ;
2797
+ }
2793
2798
2794
2799
RETURN (server_stream );
2795
2800
}
@@ -2799,6 +2804,7 @@ mongoc_cluster_stream_for_reads (mongoc_cluster_t *cluster,
2799
2804
const mongoc_read_prefs_t * read_prefs ,
2800
2805
mongoc_client_session_t * cs ,
2801
2806
bson_t * reply ,
2807
+ bool has_write_stage ,
2802
2808
bson_error_t * error )
2803
2809
{
2804
2810
const mongoc_read_prefs_t * prefs_override = read_prefs ;
@@ -2808,7 +2814,13 @@ mongoc_cluster_stream_for_reads (mongoc_cluster_t *cluster,
2808
2814
}
2809
2815
2810
2816
return _mongoc_cluster_stream_for_optype (
2811
- cluster , MONGOC_SS_READ , prefs_override , cs , reply , error );
2817
+ cluster ,
2818
+ /* Narrow down the optype if this is an aggregate op with a write stage */
2819
+ has_write_stage ? MONGOC_SS_AGGREGATE_WITH_WRITE : MONGOC_SS_READ ,
2820
+ prefs_override ,
2821
+ cs ,
2822
+ reply ,
2823
+ error );
2812
2824
}
2813
2825
2814
2826
mongoc_server_stream_t *
0 commit comments