Frage

Does anyone know how mongos select a mongod to perform a query/insert/... ?

We have 3 shards. Every shard have 3 mongod(s) which were configurated as a replica set.


[More detail] After mongos determined which shard(maybe many shards) to perform the request by using shard key, how mongos select the right mongod(in the shard, one is primary, the other two are secondary)? Or how to direct the mongos to read from the secondary member in the shard? Is it possible? Is something like ReadPreference.SECONDARY_PREFERRED can be used?

War es hilfreich?

Lösung 2

I think the mongos will query the slave if you do not request it to only query the master.

So if you create your MongoClient instance (to talk to mongos) like this:

MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
...
builder.readPreference(ReadPreference.nearest()); // not the primary
...
MongoClientOptions clientOptions = builder.build();
mongoClient = new MongoClient(serverAddr, clientOptions);

I think you actually don't need to worry about it. The mongos will wisely choose a right mongod in a determined shard. (Actually, our DBA tell me he see the query to the slave mongod)

This opition is passed to mongos through the "flags" value in the OP_QUERY struct. I debugged into the mongodb java drive, see the value is 4 --- which means "SlaveOK" bit is set. Please see: http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/?pageVersion=106#op-query for more detail.

Andere Tipps

Your mongos instance looks at the shard key in your query or insert. It knows which shard holds which range of the shard key. There is a technical explanation here. You can look at the output of sh.status() to see which shard holds which range. From each shard it will connect you to the primary replication set member.

It takes the primary member, the only one you can write to, much the same way as any other driver would.

Edit

To clarify I was talking about writes. All writes to a replica set whether it is inited as a shard or not will be to primary only.

This answer was given in response to the insert bit of:

query/insert/...

And was given before any editing.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top