In the previous blog Mongo source code learning (3) Request receiving transport layer, I analyzed the role of the transportlayer a little. In this article, I will look at how serviceentrypoint works.
First, serviceentrypoint is defined in the Mongo/src/Mongo/Transport Directory.
Simply put the Code directly.
Service_entry_point.h
Namespace Mongo {/*** this is the entrypoint from the transport layer into MongoDB or mongos. ** the serviceentrypoint accepts new sessions from the transportlayer, and is * responsible for running these sessions in a get-message, run-message, * reply-with-message loop. it may not do this on the transportlayer's thread. * // *** this is the entry point from the transport layer to mongod or mongos. ** Serviceentrypoint receives new sessions from the transportlayer and runs the * Get-message, run-message, and reply-with-message life cycles of these sessions. It may not be in the transportlayer thread. */Class serviceentrypoint {pai_disallow_copying (serviceentrypoint); Public: Virtual ~ Serviceentrypoint () = default;/*** begin running a new session. This method returns immediately. * // *** start a new session. This method will return immediately. */Virtual void startsession (Transport: sessionhandle session) = 0;/*** end all sessions that do not match the mask in tags. * // *** end all sessions that do not match the tags mask. */Virtual void endallsessions (Transport: Session: tagmask tags) = 0;/*** starts the service entry point * // *** start the service entry point. */Virtual status start () = 0;/*** shuts down the service entry point. * // *** close the service entry point. */Virtual bool Shutdown (milliseconds timeout) = 0;/*** append high-level stats to a bsonobjbuilder for serverstatus * // *** append the advanced status to bsonobjbuilder by serverstatus. */Virtual void appendstats (bsonobjbuilder * bob) const = 0;/*** returns the number of sessions currently open. * // *** returns the number of sessions currently opened. */Virtual size_t numopensessions () const = 0;/*** processes a request and fills out a dbresponse. * // *** process a request and write it into dbresponse. * P. S. Knocked on the blackboard. Comrades, this is where the request is handled! */Virtual dbresponse handlerequest (operationcontext * opctx, const message & request) = 0; protected: serviceentrypoint () = default ;};} // namespace Mongo
Well, I think the most important method is handlerequest. The method name in the interface is easy to understand.
Service_entry_pioint_impl.h
Namespace Mongo {class servicecontext; namespace transport {class session;} // namespace transport/*** a basic entry point from the transportlayer into a server. ** the server logic is implemented inside of handlerequest () by a subclass. * startsession () spawns and detaches a new thread for each incoming connection * (Transport: session ). * // *** a basic entry point from the transportlayer to the server. ** The server's request processing logic is implemented through the handlerequest () method of the subclass. * Startsession () Will spawns and allocate a new thread to process each incoming connection (Transport: session) * spawn: (fish, frog, etc.) a large amount of production (eggs, create **/class serviceentrypointimpl: Public serviceentrypoint {partition (serviceentrypointimpl); Public: // constructor explicit partition (servicecontext * svcctx); void startsession (Transport: sessionhandle session) override; void endallsessions (Transport: Session: tagmask tags) Final; Status start () Final; bool Shutdown (milliseconds timeout) Final; void appendstats (bsonobjbuilder * bob) const final; size_t numopensessions () const final {return _ currentconnections. load ();} private: Using ssmlist = stdx: List <STD: shared_ptr <servicestatemachine>; using ssmlistiterator = ssmlist: iterator; servicecontext * const _ svcctx; atomicword <STD: size_t> _ nworkers; mutable stdx: mutex _ sessionsmutex; stdx: condition_variable _ shutdowncondition; ssmlist _ sessions; size_t _ maxnumconnections {worker }; atomicword <size_t> _ currentconnections {0}; atomicword <size_t> _ createdconnections {0}; STD: unique_ptr <transport: serviceexecutorreserved> _ admininternalpool ;}; /** returns true if a session with remote/local addresses shocould be exempted from maxconns * // ** returns true if the remote or local session can be exempted from the maximum number of connections constraint * /bool shouldoverridemaxconns (const transport:: sessionhandle & session, const STD: vector <stdx: variant <CIDR, STD: String >>& exemptions);} // namespace Mongo
It seems that there is not much to say. The next service_entry_point_impl.cpp is the big data. Here we will go deep into the method.
Service_entry_point_impl.cpp
Mongo source code learning (4) Service entry point serviceentrypoint