globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 230 forks source link

Read preference tag sets not prioritised #300

Open mdevar opened 5 years ago

mdevar commented 5 years ago

Hello globalsign team I need help with how to specify empty read preference tags using mgo.DialInfo.ReadPreference.TagSets According to mongodb documentation: https://docs.mongodb.com/manual/reference/method/Mongo.setReadPref/

If the replica set cannot satisfy the first tag set, the client will attempt to use the second read preference. Each tag set can contain zero or more field/value tag pairs, with an "empty" document acting as a wildcard which matches a replica set member with any tag set or no tag set.

So IF the empty tag set is provided, the fail over logic should match any repl set member, but ONLY if it’s unable to match the first tag set

I am specifying the tag sets as follows: o.DialInfo.ReadPreference.TagSets = []bson.D{{bson.DocElem{Name: "foo", Value: "bar"}}, {}} it seems that with the above configuration, your BestFit function always returns default server (primary in my case, which does not have tags foo:bar), because it is matched on the empty tag, even though the actual server with tags foo:bar is reachable, but it is not returned as BestFit. I hope, I’m just not specifying the tagSets correctly. Could someone please clarify this for me. Thanks.

eminano commented 5 years ago

Hi @mdevar,

Really sorry for the delay!

From the implementation, it seems the tag sets are not prioritised. All the servers are checked against all the tag sets, and if they satisfy at least one, they are selected as the best fit.

https://github.com/globalsign/mgo/blob/master/server.go#L562 https://github.com/globalsign/mgo/blob/master/server.go#L359

Which means, if you have primary preferred, like in your case, that one will always be selected, since it matches the empty tag set. A quick test to verify this behaviour would be to remove the empty tag set and check that the right server is selected (the one that satisfies foo:var tag set).

We are not implementing new features/enhancements at the moment, but we'd be happy to get a PR improving this feature!

Alternatively, have a look at the official mongodb driver, which was recently released and it's actively maintained: https://github.com/mongodb/mongo-go-driver

Hope this helps, and sorry again for the delay!

Esther