Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with Mongos request in MongoDB

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to deal with Mongos requests in MongoDB. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

After TransportLayer receives a new connection, it creates a special connthread to handle all subsequent requests for the connection. The specific logic is in the _ sessionloop function of the ServiceEntryPointMongos class. The specific logic is in the three steps of "receiving packets, processing, and responding back" according to the type of the request. The transformation relationship of the state machine is summarized as follows:

Specific to the Process business processing, will further distinguish between message types and follow different processing logic, mainly divided into: query class (queryop/getmore), update class (insert/update/delete), killcursors, and other ClientCommand these types.

The processing flow of the first half of these commands is basically similar, including authentication, syntax judgment, finding which shardserver is involved in this operation according to the partition view, and error retry mechanism. The second half is related to the specific business logic, there is a big difference.

Query request processing logic

Because it is a query in Sharding cluster mode, the data of multiple ShardServer may be involved depending on the query conditions. After confirming the ShardServer involved according to the partition view, a query task is generated for each ShardServer to be executed by TaskExecutorPool. TaskExecutorPool is responsible for maintaining a long connection from mongos to ShardServer and includes a pool of network threads to perform network interaction with ShardServer.

After the Conn thread transfers the query task to the TaskExecutorPool task queue, it will wait the semaphore to block waiting. After completing the query request, the network thread in TaskExecutorPool wakes up the network thread to further process the query results.

Mongos maintains a query result cache queue for each Shard Server, and returns the query results after step-by-step processing in the order of Merge, Skip, Limit and RemoveSortKey. The Merge operation is responsible for merging query results from multiple Shard Server, and has different processing logic according to whether the query condition requires sorting:

Need to sort: find the first one among multiple queues according to SortKey to ensure that the global order is returned. After the cache results are processed, the corresponding ShardServer query data will be triggered.

No sorting is required: a ShardServer query result is returned and then the query result of the next ShardServer is returned

Mongos also creates a ClusterCursor for each query and maintains a correspondence with each remote Shard Server cursor for subsequent getmore operations to further fetch data.

The above is how to deal with Mongos requests in MongoDB. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report