发牌SO 发表于 2015-7-6 10:04:43

MongoDB源码概述——启动处理

         在启动MongoDB后,程序会对相应的参数,上次遗留的锁文件,日志文件等等进行相应的处理,同时也会开启一些支撑其他部分运行的服务线程,为了精读MongoDB的代码,领会其全局设计理念,所以我对这些不是特别核心的部分,也通过博文给自己来做一个总结,方便自己以后查阅。
  
  程序在mian函数里进行了对输入参数的所有处理,程序使用Boost库实现了跨平台的命令行参数的兼容性,这部分的代码非常庞大,也非常的乱,所以也没有必要太过记载,在main函数的底部进行了initAndListen(cmdLine.port, appsrvPath);调用,这个函数就是我们的重点部分。
  
  在void _initAndListen(int listenPort, const char*appserverLoc = NULL)内函数首先进行了一些运行环境的相关检验,如:判断当前系统是不是32bit系统:bool is32bit =sizeof(int*) == 4;接着输出一些版本信息(这部分代码就不贴了)后面进行了acquirePathLock()方法的调用。


   void acquirePathLock() {  string name = ( boost::filesystem::path( dbpath ) / "mongod.lock" ).native_file_string();
      bool oldFile = false;
      //oldFile为true的概念是存在mongod.lock并且它的filesize大于0,。 正常结束的进程会将mongodb.lock大小设置为0(包括CTRL+C)
      if ( boost::filesystem::exists( name ) && boost::filesystem::file_size( name ) > 0 ) {
            oldFile = true;
      }
      // we check this here because we want to see if we can get the lock
      // if we can't, then its probably just another mongod running

      lockFile = open( name.c_str(), O_RDWR | O_CREAT , S_IRWXU | S_IRWXG | S_IRWXO );
      if( lockFile
页: [1]
查看完整版本: MongoDB源码概述——启动处理