<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:38:53 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[GODRIVER-2551] The request may be sent to the secodary node, although primaryPreferred is specified.</title>
                <link>https://jira.mongodb.org/browse/GODRIVER-2551</link>
                <project id="14289" key="GODRIVER">Go Driver</project>
                    <description>&lt;p&gt;I found that although the primaryPreferred is specified, a small number of request will still be sent to the secondary node, even though the primary node is available at the time.&#160;&lt;/p&gt;

&lt;p&gt;mongo-go-driver: v1.10.2&#160;&#160;&lt;/p&gt;

&lt;p&gt;mongodb: 4.0 replicaset&lt;/p&gt;

&lt;p&gt;```&lt;br/&gt;
var url = flag.String(&quot;url&quot;, &quot;127.0.0.1:27031&quot;, &quot;mongodb url&quot;)&lt;br/&gt;
&#160;&lt;br/&gt;
func main() {&lt;br/&gt;
&#160; &#160; &#160; &#160; flag.Parse()&lt;br/&gt;
&#160;&lt;br/&gt;
&#160; &#160; &#160; &#160; f := func() error {&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; clientOptions := options.Client().ApplyURI(*url).SetReadPreference(readpref.PrimaryPreferred())&lt;br/&gt;
&#160;&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; client, err := mongo.NewClient(clientOptions)&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; if err != nil &lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; return fmt.Errorf(&quot;new client failed:%v&quot;, err)
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; }
&lt;p&gt;&#160;&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; if err = client.Connect(context.Background()); err != nil &lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; return fmt.Errorf(&quot;connect failed: %v&quot;, err)
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; }
&lt;p&gt;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; defer client.Disconnect(context.Background())&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;/*&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; if err = client.Ping(context.Background(), readpref.Primary()); err != nil &lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; fmt.Println(&quot;ping failed:&quot;, err)
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; }
&lt;p&gt; else &lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; fmt.Println(&quot;ping...&quot;)
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; }
&lt;p&gt;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; */&lt;br/&gt;
&#160;&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; coll := client.Database(&quot;mydb&quot;).Collection(&quot;coll&quot;)&lt;br/&gt;
&#160;&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; filter := bson.D{{Key: &quot;_id&quot;, Value: &quot;test_val_readpref&quot;}}&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; res := coll.FindOne(context.Background(), filter)&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; if res.Err() != nil &lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; fmt.Println(&quot;err:&quot;, res.Err())
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; return res.Err()
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; }
&lt;p&gt;&#160;&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; return nil&lt;br/&gt;
&#160; &#160; &#160; &#160; }&lt;br/&gt;
&#160;&lt;br/&gt;
&#160; &#160; &#160; &#160; for i := 0; i &amp;lt; 1000; i++ {&lt;br/&gt;
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; if err := f(); err != nil &lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; fmt.Println(&quot;err &quot;, err)
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; }
&lt;p&gt; else &lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; fmt.Println(&quot;success&quot;)
&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; }
&lt;p&gt;&#160; &#160; &#160; &#160; }&lt;br/&gt;
}&lt;br/&gt;
```&lt;/p&gt;

&lt;p&gt;I have to ping the primary node before sending the request to make sure I got data from the primary.&#160;&lt;/p&gt;

&lt;p&gt;From the documentation here: &lt;a href=&quot;https://www.mongodb.com/docs/v5.0/core/read-preference-use-cases/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://www.mongodb.com/docs/v5.0/core/read-preference-use-cases/&lt;/a&gt;&#160;&lt;/p&gt;

&lt;p&gt;The description of primaryPreferred is that&lt;/p&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;operations read from the &lt;a href=&quot;https://www.mongodb.com/docs/v5.0/reference/glossary/#std-term-primary&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;primary&lt;/a&gt;&#160;but if it is unavailable, operations read from&#160;&lt;a href=&quot;https://www.mongodb.com/docs/v5.0/reference/glossary/#std-term-secondary&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;secondary&lt;/a&gt; members in most situations.&lt;/p&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;When the primary node is available and should all operations read from primary members?&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;</description>
                <environment></environment>
        <key id="2140157">GODRIVER-2551</key>
            <summary>The request may be sent to the secodary node, although primaryPreferred is specified.</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="13202">Works as Designed</resolution>
                                        <assignee username="kevin.albertson@mongodb.com">Kevin Albertson</assignee>
                                    <reporter username="1321280378feng@gmail.com">Chao Yin</reporter>
                        <labels>
                    </labels>
                <created>Mon, 19 Sep 2022 08:28:19 +0000</created>
                <updated>Fri, 27 Oct 2023 13:16:22 +0000</updated>
                            <resolved>Tue, 27 Sep 2022 19:51:13 +0000</resolved>
                                    <version>1.10.2</version>
                                                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="4859826" author="kevin.albertson" created="Tue, 27 Sep 2022 19:50:19 +0000"  >&lt;blockquote&gt;
&lt;p&gt;a small number of request will still be sent to the secondary node, even though the primary node is available at the time. &lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;That is likely expected behavior when a MongoClient has not checked all servers in the replica set.&lt;/p&gt;

&lt;p&gt;The driver maintains a description of servers. A primaryPreferred read preference will select a secondary if the description of servers does not include a primary, but includes a secondary.&lt;/p&gt;

&lt;p&gt;When the MongoClient is initially connected, it is only aware of the server &quot;127.0.0.1:27031&quot;. After checking that server, the driver will discover other servers in the replica set.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;From the documentation here: &lt;a href=&quot;https://www.mongodb.com/docs/v5.0/core/read-preference-use-cases/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://www.mongodb.com/docs/v5.0/core/read-preference-use-cases/&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The description of primaryPreferred is that&lt;/p&gt;

&lt;p&gt;```&lt;/p&gt;

&lt;p&gt;operations read from the primary but if it is unavailable, operations read from secondary members in most situations.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;&quot;unavailable&quot; means that the primary is not available to be selected by the driver. That does not suggest the primary server is not responding. The primary may not have yet been discovered.&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                                                <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|i0tj8g:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>9223372036854775807</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>