TiKV 调试环境搭建,我们根据目的拆分成两篇文章 :
- 第一篇,本文,成功运行 TiKV 单元测试。
- 第二篇,成功运行 TiKV 服务实例。
检查先决条件
构建 TiKV 你将至少需要以下安装:
- git
- rustup
- awk
- cmake
- go
- make
- clang/gcc
1. 安装环境
1.1 IntelliJ IDEA
我是本地电脑用 IntelliJ 家的编辑器编辑源码,然后上传到 linux 服务器编译运行。Intellij 直接有 Rust 的插件,安装就行。

1.2 Rust
curl https://sh.rustup.rs -sSf | sh
日志如下:
info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
/root/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:
/root/.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
/root/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/root/.profile
/root/.bashrc
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1
info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
672.7 KiB / 672.7 KiB (100 %) 221.5 KiB/s in 2s ETA: 0s
info: latest update on 2021-05-06, rust version 1.52.0 (88f19c6da 2021-05-03)
info: downloading component 'cargo'
6.0 MiB / 6.0 MiB (100 %) 558.8 KiB/s in 16s ETA: 0s
info: downloading component 'clippy'
2.4 MiB / 2.4 MiB (100 %) 552.6 KiB/s in 4s ETA: 0s
info: downloading component 'rust-docs'
15.3 MiB / 15.3 MiB (100 %) 583.5 KiB/s in 32s ETA: 0s
info: downloading component 'rust-std'
24.1 MiB / 24.1 MiB (100 %) 1014.4 KiB/s in 25s ETA: 0s
info: downloading component 'rustc'
48.7 MiB / 48.7 MiB (100 %) 1.4 MiB/s in 47s ETA: 0s
info: downloading component 'rustfmt'
3.6 MiB / 3.6 MiB (100 %) 1.4 MiB/s in 3s ETA: 0s
info: installing component 'cargo'
info: using up to 500.0 MiB of RAM to unpack components
6.0 MiB / 6.0 MiB (100 %) 2.8 MiB/s in 2s ETA: 0s
info: installing component 'clippy'
info: installing component 'rust-docs'
15.3 MiB / 15.3 MiB (100 %) 8.6 MiB/s in 1s ETA: 0s
info: installing component 'rust-std'
24.1 MiB / 24.1 MiB (100 %) 3.7 MiB/s in 5s ETA: 0s
info: installing component 'rustc'
48.7 MiB / 48.7 MiB (100 %) 2.6 MiB/s in 16s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
stable-x86_64-unknown-linux-gnu installed - rustc 1.52.0 (88f19c6da 2021-05-03)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, run:
source $HOME/.cargo/env
需要我们手动选择一个安装选项,再下载完成之后将自动安装。我们如果要使其在终端可用的话,还需要设置 PATH
环境变量。
source $HOME/.cargo/env
rustc --version
1.3 Go
在写这篇文章的时候,Go 的最新版为 1.16.4
以 root 或者其他 sudo 用户身份运行下面的命令,下载并且解压 Go 二进制文件到/usr/local目录:
wget -c https://dl.google.com/go/go1.16.4.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local
通过将 Go 目录添加到$PATH环境变量,系统将会知道在哪里可以找到 Go 可执行文件。
这个可以通过添加下面的行到/etc/profile文件(系统范围内安装)或者$HOME/.profile文件(当前用户安装):
export PATH=$PATH:/usr/local/go/bin
保存文件,并且重新加载新的PATH 环境变量到当前的 shell 会话:
source ~/.profile
验证
go version
1.4 下载 TiKV
git clone https://github.com/pingcap/tikv.git
cd tikv
配置你的 Rust 工具链
TiKV 使用 rust-toolchain 中指定的 Rust 工具链版本。 rustup
和 cargo
将自动使用此文件。我们还使用了 rustfmt
和 clippy
组件。
$ rustup component add rustfmt-preview
info: downloading component 'rustfmt-preview'
1.7 MiB / 1.7 MiB (100 %) 214.5 KiB/s ETA: 0 s
info: installing component 'rustfmt-preview'
不知道为什么,反正这里又下载了一堆东西。
构建和测试
虽然 TiKV 包含具有常见工作流程的 Makefile ,但你也可以像在普通 Rust 项目中一样使用 cargo
。
构建 TiKV:
make build
日志比较长,这里就不贴出来了,编译完成之后,最后一行有绿色 Finish
然后安装:
cargo install cargo-watch
然后再运行:
cargo watch -s "cargo check"
最后都 Finish 了,就可以开始代码编写编译工作了。
说下遇到的问题:
- error: failed to resolve patches for `https://github.com/rust-lang/crates.io-index`
这个是 rust 源的问题,解决:更换国内源,查看https://segmentfault.com/a/1190000017483415
使用 nightly 版本时,Crates 源可能会出现 Couldn't resolve host name (Could not resolve host: crates)
错误(见 https://github.com/ustclug/discussions/issues/294)。一个临时的解决方法是在运行 cargo
的时候加入环境变量 CARGO_HTTP_MULTIPLEXING=false
export CARGO_HTTP_MULTIPLEXING=false
2. failed to execute command: No such file or directory (os error 2) is `cmake` not installed?
cmake 没装,直接 sudo apt install cmake
参考资料:
- https://maiyang.me/post/2018-08-02-rust-guide-by-tikv/
- https://www.iocoder.cn/TiKV/build-debugging-environment-first/