CentOS 7 环境下调试 ClickHouse 源码

概述

ClickHouse 的构建可以参考官方文档:How to Build ClickHouse on Linux | ClickHouse Docs

ClickHouse 的构建依赖于以下组件:

  • Git
  • CMake 3.14 or newer
  • Ninja
  • C++ compiler:clang-14 or newer
  • Linked:lld

Note:

  • Git 建议升级一下,版本过低会导致 clone 子模块时报错,笔者这里使用的版本为 2.36.0

编译环境准备

  • 操作系统:Red Hat Enterprise Linux Server release 7.0 (Maipo)
  • gcc 版本:11.2.1
  • make 版本:4.3
  • cmake 版本:3.19.6
  • LLVM 版本:14.0.0

安装或升级 gcc

RHEL 7.0 默认安装的 gcc 版本为 4.8.2 ,需要升级到更高版本,执行下述命令可以将 gcc 升级到 11.2.1 版本:

1sudo yum install -y centos-release-scl
2sudo yum install -y devtoolset-11
3scl enable devtoolset-11 bash
4echo "source /opt/rh/devtoolset-11/enable" >> ~/.bash_profile
5source /opt/rh/devtoolset-11/enable

验证是否安装成功:

1$ gcc --version
2gcc (GCC) 11.2.1 20210728 (Red Hat 11.2.1-1)
3Copyright (C) 2021 Free Software Foundation, Inc.
4This is free software; see the source for copying conditions.  There is NO
5warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

安装或升级 cmake

执行下述命令安装 cmake 3.19.6:

 1mkdir -p ~/env/cmake
 2wget -P=~/env/cmake https://github.com/Kitware/CMake/releases/download/v3.19.6/cmake-3.19.6-Linux-x86_64.tar.gz
 3cd ~/env/cmake && tar -zxf cmake-3.19.6-Linux-x86_64.tar.gz
 4cat <<EOF > ~/.bash_profile
 5# CMake
 6CMAKE_HOME=~/env/cmake/cmake-3.19.6-Linux-x86_64
 7PATH=$CMAKE_HOME/bin:$PATH
 8EOF
 9$ cmake --version
10cmake version 3.19.6
11
12CMake suite maintained and supported by Kitware (kitware.com/cmake).

安装 LLVM

 1wget https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/llvm-project-14.0.0.src.tar.xz
 2tar -Jxvf llvm-project-14.0.0.src.tar.xz
 3cd llvm-project-14.0.0.src
 4mkdir build && cd build
 5cmake \
 6    -DCMAKE_BUILD_TYPE=Release\
 7    -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi;compiler-rt" \
 8    -G "Unix Makefiles" \
 9    ../llvm
10sudo make install

验证是否安装成功:

1$ clang --version
2clang version 14.0.0
3Target: x86_64-unknown-linux-gnu
4Thread model: posix
5InstalledDir: /usr/local/bin

安装 Ninja

Ninja 是一个小型的专注于速度的构建系统,ClickHouse 官方建议使用 Ninja 来进行构建。

笔者这里使用的版本为 v1.11.0,执行如下命令安装:

1wget https://github.com/ninja-build/ninja/releases/download/v1.11.0/ninja-linux.zip
2unzip ninja-linux.zip
3sudo cp ninja /usr/local/bin/

验证是否安装成功:

1$ ninja --version
21.11.0

源码准备

笔者这里使用的 ClickHouse 版本为 v22.3.2.2-lts

首先 clone 源码到本地:

1git clone --recursive https://github.com/ClickHouse/ClickHouse.git

由于 ClickHouse 源码较大,如果下载失败,可以使用如下命令继续下载:

1git submodule update --init --recursive
2git pull --recurse-submodules

源码下载成功后,切换到 ClickHouse 目录下并切换分支为 v22.3.2.2-lts

1cd ClickHouse
2git checkout v22.3.2.2-lts

编译 ClickHouse

1mkdir build && cd build
2export CC=clang-14
3export CXX=clang-14
4cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang -DCMAKE_BUILD_TYPE=Debug ..
5ninja
  • 编译时间可能会有点长,静静等待编译完成即可,编程生成的可执行文件位于 build/programs 目录下:

      1$ tree programs
      2programs/
      3├── bash-completion
      4│   ├── CMakeFiles
      5│   ├── cmake_install.cmake
      6│   ├── completions
      7│   │   ├── CMakeFiles
      8│   │   ├── cmake_install.cmake
      9│   │   └── CTestTestfile.cmake
     10│   └── CTestTestfile.cmake
     11├── benchmark
     12│   ├── CMakeFiles
     13│   │   └── clickhouse-benchmark-lib.dir
     14│   │       └── Benchmark.cpp.o
     15│   ├── cmake_install.cmake
     16│   ├── CTestTestfile.cmake
     17│   └── libclickhouse-benchmark-libd.a
     18├── clickhouse
     19├── clickhouse-benchmark -> clickhouse
     20├── clickhouse-client -> clickhouse
     21├── clickhouse-compressor -> clickhouse
     22├── clickhouse-copier -> clickhouse
     23├── clickhouse-extract-from-config -> clickhouse
     24├── clickhouse-format -> clickhouse
     25├── clickhouse-git-import -> clickhouse
     26├── clickhouse-keeper -> clickhouse
     27├── clickhouse-keeper-converter -> clickhouse
     28├── clickhouse-library-bridge
     29├── clickhouse-local -> clickhouse
     30├── clickhouse-obfuscator -> clickhouse
     31├── clickhouse-odbc-bridge
     32├── clickhouse-server -> clickhouse
     33├── clickhouse-static-files-disk-uploader -> clickhouse
     34├── client
     35│   ├── CMakeFiles
     36│   │   └── clickhouse-client-lib.dir
     37│   │       └── Client.cpp.o
     38│   ├── cmake_install.cmake
     39│   ├── CTestTestfile.cmake
     40│   └── libclickhouse-client-libd.a
     41├── CMakeFiles
     42│   └── clickhouse.dir
     43│       └── main.cpp.o
     44├── cmake_install.cmake
     45├── compressor
     46│   ├── CMakeFiles
     47│   │   └── clickhouse-compressor-lib.dir
     48│   │       └── Compressor.cpp.o
     49│   ├── cmake_install.cmake
     50│   ├── CTestTestfile.cmake
     51│   └── libclickhouse-compressor-libd.a
     52├── copier
     53│   ├── CMakeFiles
     54│   │   └── clickhouse-copier-lib.dir
     55│   │       ├── ClusterCopierApp.cpp.o
     56│   │       ├── ClusterCopier.cpp.o
     57│   │       └── Internals.cpp.o
     58│   ├── cmake_install.cmake
     59│   ├── CTestTestfile.cmake
     60│   └── libclickhouse-copier-libd.a
     61├── CTestTestfile.cmake
     62├── extract-from-config
     63│   ├── CMakeFiles
     64│   │   └── clickhouse-extract-from-config-lib.dir
     65│   │       └── ExtractFromConfig.cpp.o
     66│   ├── cmake_install.cmake
     67│   ├── CTestTestfile.cmake
     68│   └── libclickhouse-extract-from-config-libd.a
     69├── format
     70│   ├── CMakeFiles
     71│   │   └── clickhouse-format-lib.dir
     72│   │       └── Format.cpp.o
     73│   ├── cmake_install.cmake
     74│   ├── CTestTestfile.cmake
     75│   └── libclickhouse-format-libd.a
     76├── git-import
     77│   ├── CMakeFiles
     78│   │   └── clickhouse-git-import-lib.dir
     79│   │       └── git-import.cpp.o
     80│   ├── cmake_install.cmake
     81│   ├── CTestTestfile.cmake
     82│   └── libclickhouse-git-import-libd.a
     83├── install
     84│   ├── CMakeFiles
     85│   │   └── clickhouse-install-lib.dir
     86│   │       └── Install.cpp.o
     87│   ├── cmake_install.cmake
     88│   ├── CTestTestfile.cmake
     89│   └── libclickhouse-install-libd.a
     90├── keeper
     91│   ├── CMakeFiles
     92│   │   ├── clickhouse_keeper_configs.dir
     93│   │   │   ├── keeper_config.xml.S.o
     94│   │   │   └── keeper_embedded.xml.S.o
     95│   │   └── clickhouse-keeper-lib.dir
     96│   │       ├── Keeper.cpp.o
     97│   │       └── TinyContext.cpp.o
     98│   ├── cmake_install.cmake
     99│   ├── CTestTestfile.cmake
    100│   ├── keeper_config.xml.S
    101│   ├── keeper_embedded.xml.S
    102│   ├── libclickhouse_keeper_configsd.a
    103│   └── libclickhouse-keeper-libd.a
    104├── keeper-converter
    105│   ├── CMakeFiles
    106│   │   └── clickhouse-keeper-converter-lib.dir
    107│   │       └── KeeperConverter.cpp.o
    108│   ├── cmake_install.cmake
    109│   ├── CTestTestfile.cmake
    110│   └── libclickhouse-keeper-converter-libd.a
    111├── library-bridge
    112│   ├── CMakeFiles
    113│   │   └── clickhouse-library-bridge.dir
    114│   │       ├── HandlerFactory.cpp.o
    115│   │       ├── Handlers.cpp.o
    116│   │       ├── library-bridge.cpp.o
    117│   │       ├── LibraryBridge.cpp.o
    118│   │       ├── LibraryInterface.cpp.o
    119│   │       ├── SharedLibraryHandler.cpp.o
    120│   │       └── SharedLibraryHandlerFactory.cpp.o
    121│   ├── cmake_install.cmake
    122│   └── CTestTestfile.cmake
    123├── local
    124│   ├── CMakeFiles
    125│   │   └── clickhouse-local-lib.dir
    126│   │       └── LocalServer.cpp.o
    127│   ├── cmake_install.cmake
    128│   ├── CTestTestfile.cmake
    129│   └── libclickhouse-local-libd.a
    130├── obfuscator
    131│   ├── CMakeFiles
    132│   │   └── clickhouse-obfuscator-lib.dir
    133│   │       └── Obfuscator.cpp.o
    134│   ├── cmake_install.cmake
    135│   ├── CTestTestfile.cmake
    136│   └── libclickhouse-obfuscator-libd.a
    137├── odbc-bridge
    138│   ├── CMakeFiles
    139│   │   └── clickhouse-odbc-bridge.dir
    140│   │       ├── ColumnInfoHandler.cpp.o
    141│   │       ├── getIdentifierQuote.cpp.o
    142│   │       ├── HandlerFactory.cpp.o
    143│   │       ├── IdentifierQuoteHandler.cpp.o
    144│   │       ├── MainHandler.cpp.o
    145│   │       ├── ODBCBlockInputStream.cpp.o
    146│   │       ├── ODBCBlockOutputStream.cpp.o
    147│   │       ├── odbc-bridge.cpp.o
    148│   │       ├── ODBCBridge.cpp.o
    149│   │       ├── PingHandler.cpp.o
    150│   │       ├── SchemaAllowedHandler.cpp.o
    151│   │       └── validateODBCConnectionString.cpp.o
    152│   ├── cmake_install.cmake
    153│   ├── CTestTestfile.cmake
    154│   └── tests
    155│       ├── CMakeFiles
    156│       │   └── validate-odbc-connection-string.dir
    157│       │       ├── __
    158│       │       │   └── validateODBCConnectionString.cpp.o
    159│       │       └── validate-odbc-connection-string.cpp.o
    160│       ├── cmake_install.cmake
    161│       ├── CTestTestfile.cmake
    162│       └── validate-odbc-connection-string
    163├── server
    164│   ├── CMakeFiles
    165│   │   ├── clickhouse_server_configs.dir
    166│   │   │   ├── config.xml.S.o
    167│   │   │   ├── embedded.xml.S.o
    168│   │   │   ├── play.html.S.o
    169│   │   │   └── users.xml.S.o
    170│   │   └── clickhouse-server-lib.dir
    171│   │       ├── MetricsTransmitter.cpp.o
    172│   │       └── Server.cpp.o
    173│   ├── cmake_install.cmake
    174│   ├── config.xml.S
    175│   ├── CTestTestfile.cmake
    176│   ├── embedded.xml.S
    177│   ├── libclickhouse_server_configsd.a
    178│   ├── libclickhouse-server-libd.a
    179│   ├── play.html.S
    180│   └── users.xml.S
    181└── static-files-disk-uploader
    182    ├── CMakeFiles
    183    │   └── clickhouse-static-files-disk-uploader-lib.dir
    184    │       └── static-files-disk-uploader.cpp.o
    185    ├── cmake_install.cmake
    186    ├── CTestTestfile.cmake
    187    └── libclickhouse-static-files-disk-uploader-libd.a
    

调试 ClickHouse

笔者这里使用 VS Code + gdb 远程调试 ClickHouse,调试环境如下:

  • VS Code 安装如下插件:

    • C/C++ Extension Pack:该插件包包含了常用的 C++ 开发插件,例如 C++、CMake、Doxygen 文档生成器、远程开发插件包等。
  • 在 VS Code 中配置新增调试文件 launch.json 如下:

     1{
     2    // Use IntelliSense to learn about possible attributes.
     3    // Hover to view descriptions of existing attributes.
     4    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
     5    "version": "0.2.0",
     6    "configurations": [
     7        {
     8            "name": "ClickHouse Client",
     9            "type": "cppdbg",
    10            "request": "launch",
    11            "program": "${workspaceFolder}/build/programs/clickhouse",
    12            "args": [
    13                "client"
    14            ],
    15            "stopAtEntry": false,
    16            "cwd": "${workspaceFolder}",
    17            // "preLaunchTask": "build",
    18            "environment": [],
    19            "externalConsole": false,
    20            "linux": {
    21                "MIMode": "gdb",
    22                "miDebuggerPath": "/opt/rh/devtoolset-11/root/bin/gdb",
    23                "setupCommands": [
    24                    {
    25                        "description": "Enable pretty-printing for gdb",
    26                        "text": "-enable-pretty-printing",
    27                        "ignoreFailures": true
    28                    }
    29                ]
    30            }
    31        },
    32        {
    33            "name": "ClickHouse Server",
    34            "type": "cppdbg",
    35            "request": "launch",
    36            "program": "${workspaceFolder}/build/programs/clickhouse",
    37            "args": [
    38                "server"
    39            ],
    40            "stopAtEntry": false,
    41            "cwd": "${workspaceFolder}",
    42            // "preLaunchTask": "build",
    43            "environment": [],
    44            "externalConsole": false,
    45            "linux": {
    46                "MIMode": "gdb",
    47                "miDebuggerPath": "/opt/rh/devtoolset-11/root/bin/gdb",
    48                "setupCommands": [
    49                    {
    50                        "description": "Enable pretty-printing for gdb",
    51                        "text": "-enable-pretty-printing",
    52                        "ignoreFailures": true
    53                    }
    54                ]
    55            }
    56        }
    57    ]
    58}
    

参考资料

  1. Getting Started Guide for Building ClickHouse | ClickHouse Docs
  2. How to Build ClickHouse on Linux | ClickHouse Docs
  3. Centos7上源码编译安装llvm 11.0.0 - 知乎
  4. https://github.com/ClickHouse/ClickHouse.git
下一页
上一页

相关