ClickHouse源码阅读(0000 0001) —— main.cpp分析

C++项目在启动时都是从main()方法开始执行的。今天来分析一下ClickHouse源码的main.cpp。

先找到main()方法:

int main(int argc_, char **argv_) {
    /// Reset new handler to default (that throws std::bad_alloc)
    /// It is needed because LLVM library clobbers it.
    std::set_new_handler(nullptr);

#if USE_EMBEDDED_COMPILER
    if (argc_ >= 2 && 0 == strcmp(argv_[1], "-cc1"))
        return mainEntryClickHouseClang(argc_, argv_);
#endif

#if USE_TCMALLOC
    /** Without this option, tcmalloc returns memory to OS too frequently for medium-sized memory allocations
      *  (like IO buffers, column vectors, hash tables, etc.),
      *  that lead to page faults and significantly hurts performance.
      */
    MallocExtension::instance()->SetNumericProperty("tcmalloc.aggressive_memory_decommit", false);
#endif

    std::vector<char *> argv(argv_, argv_ + argc_);

    /// Print a basic help if nothing was matched
    MainFunc main_func = printHelp;

    for (auto &application : clickhouse_applications) {
        if (isClickhouseApp(application.first, argv)) {
            main_func = application.second;
            break;
        }
    }

    return main_func(static_cast<int>(argv.size()), argv.data());
}

主要步骤包括:(重点在4、5步)

1、重置handler;

2、条件编译命令 USE_EMBEDDED_COMPILER;

3、条件编译命令 USE_TCMALLOC;

4、遍历已经定义好的clickhouse_applications[]数组,根据输入参数判断需要启动哪个application;

5、进入application对应的主函数,并执行。

如输入为clickhouse server .....,则启动server这个应用,执行mainEntryClickHouseServer这个方法:

int mainEntryClickHouseServer(int argc, char **argv) {
    DB::Server app;
    try {
        return app.run(argc, argv);
    }
    catch (...) {
        std::cerr << DB::getCurrentExceptionMessage(true) << "\n";
        auto code = DB::getCurrentExceptionCode();
        return code ? code : 1;
    }
}

如输入为clickhouse client .....,则启动client这个应用,执行mainEntryClickHouseClient这个方法:

int mainEntryClickHouseClient(int argc, char **argv) {
    try {
        DB::Client client;
        client.init(argc, argv);//注意客户端需要先执行这里的client.init()方法
        return client.run();
    }
    catch (const boost::program_options::error &e) {
        std::cerr << "Bad arguments: " << e.what() << std::endl;
        return 1;
    }
    catch (...) {
        std::cerr << DB::getCurrentExceptionMessage(true) << std::endl;
        return 1;
    }
}

不同应用的具体的启动过程在后面的文章中进行分析。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值