Clickhouse在Mac OS上的编译、运行及在VSCode中的调试

系统环境

MacOS Monterey 12.2.1
Intel i9 2.3 GHz 8 Cores
32GB DDR4 RAM
1TB SSD Storage

官方教程地址

https://clickhouse.com/docs/en/development/build-osx

下载代码

配置Github IP加速

如果你没有配置过Github IP,访问Github可能会很慢。

你可以访问以下四个网址,将页面上的IP配置到/etc/hosts里:
https://www.ipaddress.com/site/github.global.ssl.fastly.net#i...
https://www.ipaddress.com/site/github.com#ipinfo
https://www.ipaddress.com/site/raw.githubusercontent.com#ipinfo
https://www.ipaddress.com/site/pkg-containers.githubuserconte...

作为参考,我的hosts配置如下:

# https://www.ipaddress.com/site/github.global.ssl.fastly.net#ipinfo
# 151.101.1.194
# 151.101.65.194
# 151.101.129.194
# 151.101.193.194
151.101.1.194 github.global.ssl.fastly.net

# https://www.ipaddress.com/site/github.com#ipinfo
# 140.82.113.4
140.82.113.4 github.com

# https://www.ipaddress.com/site/raw.githubusercontent.com#ipinfo
# 185.199.108.133
# 185.199.109.133
# 185.199.110.133
# 185.199.111.133
185.199.108.133 githubusercontent.com

# https://www.ipaddress.com/site/pkg-containers.githubusercontent.com#ipinfo
# 185.199.108.154
# 185.199.109.154
# 185.199.110.154
# 185.199.111.154
185.199.108.154 pkg-containers.githubusercontent.com

如果你有自己的代理服务器,你也可以通过proxychains4或其他方式来访问git。

Fork代码并下载

不建议直接下载官方代码,而是自己fork一份到自己的Github账号,这样修改代码会更方便。
访问https://github.com/ClickHouse/ClickHouse即可创建自己的fork。
以从我的代码库clone为例:

git clone https://github.com/paxoscn/ClickHouse.git

环境准备

安装Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install)"
brew update

安装脚本作者捐赠地址:
https://brew.idayer.com/reward/
安装脚本作者点赞地址:
https://github.com/ineo6/homebrew-install
https://gitee.com/ineo6/homebrew-install
Homebrew捐赠地址:
https://github.com/Homebrew/brew#donations

安装工具库

brew install ccache cmake ninja libtool gettext llvm gcc gdb binutils grep findutils

编译代码

cd ClickHouse
mkdir build
export PATH=$(brew --prefix llvm)/bin:$PATH
export CC=$(brew --prefix llvm)/bin/clang
export CXX=$(brew --prefix llvm)/bin/clang++
git submodule update --init
cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -S . -B build
cmake --build build

编译可能遇到的问题

Clang版本问题

注意编译器需要使用Homebrew's vanilla Clang。cmake正确的输出开头应为类似“Homebrew clang version 15.0.7”的文字,而不是“Apple clang version ... ”之类。若版本错误请检查是否成功通过brew install安装了必要的工具库,并清空build目录后重试。

缺失cctz/testdata目录

可以从另一目录中找到并拷贝:

cp -r contrib/abseil-cpp/absl/time/internal/cctz/testdata contrib/cctz/
某个头文件缺失错误

当看到某个头文件缺失的错误提示时,请检查contrib目录下是否存在空文件夹,空文件夹可能是因为“git submodule update --init”命令失败导致的。你可以在.git/config里查看缺失的子模块的git地址,并手动clone到contrib目录下。

运行

修改系统最大文件数配置

执行:

ulimit -n

来查看系统最大文件数,若为2560或更小则需要进行配置修改:

sudo vi /Library/LaunchDaemons/limit.maxfiles.plist

文件内容如下:




  
    Label
    limit.maxfiles
    ProgramArguments
    
      launchctl
      limit
      maxfiles
      524288
      524288
    
    RunAtLoad
    
    ServiceIPC
    
  

随后对该配置文件进行权限设置:

sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist

检查该配置文件是否正确:

plutil /Library/LaunchDaemons/limit.maxfiles.plist

最后重启系统并查看“ulimit -n”命令返回的系统最大文件数是否已经改变为524288。

启动进程

./build/programs/clickhouse-server --config-file ./programs/server/config.xml

使用客户端进行连接

./build/programs/clickhouse-client

可以执行以下命令来验证服务是否正常:

:) select 1;

SELECT 1

Query id: 3477a16a-894c-4c7b-8290-4874b6f4ed21

┌─1─┐
│ 1 │
└───┘

1 row in set. Elapsed: 0.001 sec. 

VSCode设置

Kit设置

在VSCode中打开Clickhouse目录后,若你没有安装过C++插件,则VSCode会自动提示安装。安装完后,在菜单选择“查看”>“命令面板...”,并输入“CMake: Edit User-Local CMake Kits”来打开文件“~/.local/share/CMakeTools/cmake-tools-kits.json”,将自己的Clang编译器路径加进去:

[
  {
    "name": "Homebrew clang version 15.0.7",
    "compilers": {
      "C": "/usr/local/opt/llvm/bin/clang",
      "CXX": "/usr/local/opt/llvm/bin/clang++"
    }
  }
]

拷贝lldb-mi

若文件“/usr/local/opt/llvm/bin/lldb-mi”不存在,可执行以下命令从~/.vscode中拷贝一份:

cp ~/.vscode/extensions/ms-vscode.cpptools-1.14.4-darwin-x64/debugAdapters/lldb-mi/bin/lldb-mi /usr/local/opt/llvm/bin/

启动配置

在项目目录下编辑文件.vscode/launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "ClickHouse目录/build/programs/clickhouse",
            "args": ["server",  "--config-file=ClickHouse目录/programs/server/config.xml"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "sourceFileMap": { "build/src/": "ClickHouse目录/src/" },
            "setupCommands": [
                {
                    "description": "pretty",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

其中的“ClickHouse目录”是绝对路径。
如果在后续调试断点时VSCode无法跳到正确的源码,并提示“找不到源码 ... SourceRequest not supported”,则可能是sourceFileMap配置有误,请检查路径或尝试去掉该配置。

确认配置选择

保存后,请在下方活动栏确认配置选择是否正确:
调试配置名称:(lldb) Launch
build variant:RelWithDebInfo
active kit:Homebrew clang version 15.0.7

开始调试

在你关心的代码中打一个断点,例如Disks/DiskLocal.cpp的tryReserve处。随后在菜单点击“运行”>“启动调试”开始调试。

你可能感兴趣的:(Clickhouse在Mac OS上的编译、运行及在VSCode中的调试)