The request may be sent to the secodary node, although primaryPreferred is specified.

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: 1.10.2
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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. 

      mongo-go-driver: v1.10.2  

      mongodb: 4.0 replicaset

      ```
      var url = flag.String("url", "127.0.0.1:27031", "mongodb url")
       
      func main() {
              flag.Parse()
       
              f := func() error {
                      clientOptions := options.Client().ApplyURI(*url).SetReadPreference(readpref.PrimaryPreferred())
       
                      client, err := mongo.NewClient(clientOptions)
                      if err != nil

      {                         return fmt.Errorf("new client failed:%v", err)                 }

       
                      if err = client.Connect(context.Background()); err != nil

      {                         return fmt.Errorf("connect failed: %v", err)                 }

                      defer client.Disconnect(context.Background())
                     
                     /*
                      if err = client.Ping(context.Background(), readpref.Primary()); err != nil

      {                         fmt.Println("ping failed:", err)                 }

      else

      {                         fmt.Println("ping...")                 }

                      */
       
                      coll := client.Database("mydb").Collection("coll")
       
                      filter := bson.D{{Key: "_id", Value: "test_val_readpref"}}
                      res := coll.FindOne(context.Background(), filter)
                      if res.Err() != nil

      {                         fmt.Println("err:", res.Err())                         return res.Err()                 }

       
                      return nil
              }
       
              for i := 0; i < 1000; i++ {
                      if err := f(); err != nil

      {                         fmt.Println("err ", err)                 }

      else

      {                         fmt.Println("success")                 }

              }
      }
      ```

      I have to ping the primary node before sending the request to make sure I got data from the primary. 

      From the documentation here: https://www.mongodb.com/docs/v5.0/core/read-preference-use-cases/ 

      The description of primaryPreferred is that

      ```

      operations read from the primary but if it is unavailable, operations read from secondary members in most situations.

      ```

      When the primary node is available and should all operations read from primary members?

       

            Assignee:
            Kevin Albertson
            Reporter:
            Chao Yin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: