API to fetch shard_id inside mongo daemon

14 views
Skip to first unread message

Sandeep Joshi

unread,
Aug 26, 2016, 2:02:42 AM8/26/16
to mongodb-dev

Is there an API to fetch the shard-id inside the mongo daemon ?

Something like "mongo::my_shard_id()" ?

https://github.com/mongodb/mongo/blob/master/src/mongo/s/shard_id.h

-Sandeep

Kaloian Manassiev

unread,
Sep 4, 2016, 4:55:30 AM9/4/16
to mongodb-dev
Hi Sandeep,

The way to do this from the shard is to use the ShardingState::getShardName method here - https://github.com/mongodb/mongo/blob/3de973e1fd98473fbf1605e2d6039214aa15b2a4/src/mongo/db/s/sharding_state.h#L100

The return type of this method is std::string, but it is compatible with ShardId.

One thing to keep in mind is that up to mongodb version 3.2, this value will be empty until the first time the shard gets contacted by a mongos or another shard. As of the latest master, this value should always be available once a shard is added to a cluster through the addShard command.

-Kal.

Kaloian Manassiev

unread,
Sep 4, 2016, 4:57:04 AM9/4/16
to mongodb-dev
One thing I forgot to mention is that in order to obtain the ShardingState instance for the server you need to use the static method ShardingState::get, which takes an OperationContext.

Hope this helps.

-Kal.

Sandeep Joshi

unread,
Sep 6, 2016, 9:26:43 AM9/6/16
to mongo...@googlegroups.com

thanks Kal ! appreciate it

-Sandeep

--
You received this message because you are subscribed to a topic in the Google Groups "mongodb-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-dev/tssVTayIl-k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-dev+unsubscribe@googlegroups.com.
To post to this group, send email to mongo...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-dev.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages