mongo源码学习(四)服务入口点ServiceEntryPoint

在上一篇博客mongo源码学习(三)请求接收传输层中,稍微分析了一下TransportLayer的作用,这篇来看下ServiceEntryPoint是怎么做的。

首先ServiceEntryPoint的定义在mongo/src/mongo/transport目录下。

废话不过说,直接上代码。

service_entry_point.h

namespace mongo {

/**
 * This is the entrypoint from the transport layer into mongod 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.
 */
/**
 * 这是从transport layer到mongod或者mongos的入口点。
 *
 * ServiceEntryPoint从TransportLayer中接收新的Session,并且负责运行这些Sessions的
 * get-Message,run-Message和reply-with-Message生命周期。它可能不会在TransportLayer的线程
 * 中这些事情。
 */
class ServiceEntryPoint {
    MONGO_DISALLOW_COPYING(ServiceEntryPoint);

public:
    virtual ~ServiceEntryPoint() = default;

    /**
     * Begin running a new Session. This method returns immediately.
     */
    /**
     * 开始一个新的Session。这个方法会马上返回。
     */
    virtual void startSession(transport::SessionHandle session) = 0;

    /**
     * End all sessions that do not match the mask in tags.
     */
    /**
     * 结束所有和tags中掩码不匹配的sessions。
     */
    virtual void endAllSessions(transport::Session::TagMask tags) = 0;

    /**
     * Starts the service entry point
     */
    /**
     * 启动服务入口点。
     */
    virtual Status start() = 0;

    /**
    * Shuts down the service entry point.
    */
    /**
     * 关闭服务入口点。
     */
    virtual bool shutdown(Milliseconds timeout) = 0;

    /**
     * Append high-level stats to a BSONObjBuilder for serverStatus
     */
    /**
     * 为serverStatus向BSONObjBuilder追加高级状态。
     */
    virtual void appendStats(BSONObjBuilder* bob) const = 0;


    /**
    * Returns the number of sessions currently open.
    */
    /**
     * 返回当前打开的sessions数量。
     */
    virtual size_t numOpenSessions() const = 0;

    /**
     * Processes a request and fills out a DbResponse.
     */
    /**
     * 处理一个请求并写入DbResponse。
     * P. S. 敲黑板了, 同志们,这里就是处理请求的地方了啊!
     */
    virtual DbResponse handleRequest(OperationContext* opCtx, const Message& request) = 0;

protected:
    ServiceEntryPoint() = default;
};

}  // namespace mongo

嗯,我觉得最重要的方法就是handleRequest了,接口中方法名字取的通俗易懂,没毛病。

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).
 */
/**
 * 从TransportLayer到server的一个基本入口点。
 *
 * 服务器处理请求的逻辑是通过子类的handleRequest()方法实现的。
 * startSession()会spawns并且分配一个新的线程来处理每个到来的连接(transport:Session)
 * spawn: (鱼、蛙等)大量产(卵);引起,酿成
 *
 */
class ServiceEntryPointImpl : public ServiceEntryPoint {
    MONGO_DISALLOW_COPYING(ServiceEntryPointImpl);

public:
    // 构造函数
    explicit ServiceEntryPointImpl(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{DEFAULT_MAX_CONN};
    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 should be exempted from maxConns
 */
/*
 * 如果远程或本地的session可以从最大连接数约束中豁免则返回true
 */
bool shouldOverrideMaxConns(const transport::SessionHandle& session,
                            const std::vector<stdx::variant<CIDR, std::string>>& exemptions);

}  // namespace mongo

似乎也没有太多好说的了。接下来的service_entry_point_impl.cpp是大头,这里开始要深入到方法内部去了。

service_entry_point_impl.cpp

猜你喜欢

转载自www.cnblogs.com/tuhooo/p/9860692.html