⭐⭐⭐ Spring Boot 项目实战 ⭐⭐⭐ Spring Cloud 项目实战
《Dubbo 实现原理与源码解析 —— 精品合集》 《Netty 实现原理与源码解析 —— 精品合集》
《Spring 实现原理与源码解析 —— 精品合集》 《MyBatis 实现原理与源码解析 —— 精品合集》
《Spring MVC 实现原理与源码解析 —— 精品合集》 《数据库实体设计合集》
《Spring Boot 实现原理与源码解析 —— 精品合集》 《Java 面试题 + Java 学习指南》

摘要: 原创出处 http://www.iocoder.cn/TiKV/build-debugging-environment-second/ 「芋道源码」欢迎转载,保留摘要,谢谢!


🙂🙂🙂关注**微信公众号:【芋道源码】**有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言将得到认真回复。甚至不知道如何读源码也可以请教噢
  4. 新的源码解析文章实时收到通知。每周更新一篇左右
  5. 认真的源码交流微信群。

本文接 《TiKV 源码解析 —— 调试环境搭建(一)之运行单元测试》 ,主要分享如何搭建运行服务实例的调试环境。

1. 安装 PD

FROM https://github.com/pingcap/pd
PD is the abbreviation for Placement Driver. It is used to manage and schedule the TiKV cluster.
PD supports distribution and fault-tolerance by embedding etcd.

安装命令如下 :

git clone https://github.com/pingcap/pd
cd pd
make build

启动命令如下 :

cd bin
nohup ./pd-server --data-dir=pd --log-file=pd.log &

测试启动是否成功命令如下 :

telnet 127.0.0.1 2379

  • 2379 为 PD 服务的默认端口。

2. 启动 TiKV

如下操作全部在 IntelliJ IDEA 中进行


打开 src/bin/tikv-server.rs 文件,右键 RUN 'tikv-server' 运行。此时,运行报错。原因是 pd endpoints 未配置。修改 Command 的配置文件路径。操作过程如下:

  1. 复制 etc/config-template.toml 文件,生成 etc/config.toml 文件,下面我们会修改该文件自定义配置

  2. 配置 --config 路径,如下图 :

    • 点击 【红框部分】,打开 Run/Debug Configurations 窗口。
    • 修改 【红线部分】,-- --config=${path}请设置成你自己的 etc/config.toml 文件所在路径噢
    • 保存。
  3. 打开 src/bin/tikv-server.rs 文件,右键 RUN 'tikv-server' 运行。

  4. 此时,运行报错。下面,我们一步一步,解决每一个报错。


问题 :please specify pd.endpoints

修改 etc/config.toml 文件,配置 endpoints = ["127.0.0.1:2379"]

参考文章 :《Passing program arguments through Cargo》


问题 :the maximum number of open file descriptors is too small, got {}, expect greater or equal to {}

解决文章 :《increase-the-maximum-number-of-open-file-descriptors-in-snow-leopard》


问题 :failed to create kv engine: "Invalid argument: Compression type ZSTD is not linked with the binary."

  • 方案一 :不使用压缩。

    修改 etc/config.toml 文件,配置 compression-per-level = ["no", "no", "no", "no", "no", "no", "no"] 。有多处,小心别漏了。

  • 方案二 :TODO 【3001】编译安装 zstd 。https://github.com/facebook/zstd


问题 :invalid configuration: StringError("default rocksdb exist, buf raftdb not exist")

  • 修改 etc/config.toml 文件,配置 raftdb-path = "/Users/yunai/pingcap/debugger/raftdb"

  • 修改 etc/config.toml 文件,配置 data-dir = "/Users/yunai/pingcap/debugger/data"


此时,笔者运行 TiKV 运行服务实例成功,开森,希望胖友也顺利。运行成功日志如下 :

[INFO] TiKV is ready to serve

测试启动是否成功命令如下 :

telnet 127.0.0.1 20160

  • 20160 为 TiKV 服务的默认端口。

3. 使用 TiKV

下面,我们要尝试 TiKV 存储数据,有如下两种方式 :

  • 第一种,使用 TiDB 。
  • 第二种,使用 tikv-client-lib-java

笔者优先考虑使用的是第二种,失败,因此后面换成了第一种,成功

3.1 TiDB

安装过程如下 :

  1. 如果胖友未配置 GOPATH 环境变量,参考 《使用Homebrew安装配置golang环境》

  2. 安装命令如下 :

    git clone https://github.com/pingcap/tidb.git $GOPATH/src/github.com/pingcap/tidb
    cd $GOPATH/src/github.com/pingcap/tidb
    make

启动命令如下 :

cd bin
nohup ./tidb-server --store=tikv --path="127.0.0.1:2379" --log-file=tidb.log &

测试启动是否成功命令如下 :

telnet 127.0.0.1 4000

  • 4000 为 TiDB 服务的默认端口。

连接 TiDB Server ,笔者使用 MySQL 图形工具 Navicat ,连接配置如下 :

  • 请愉快的使用 MySQL 进行操作吧。

3.2 tikv-client-lib-java

  1. 克隆项目 tikv-client-lib-java ,地址为 https://github.com/pingcap/tikv-client-lib-java

  2. 命令行输入 :mvn package ,打包出 tikv-client-0.1.0-SNAPSHOT.jar

  3. 创建测试项目,导入 tikv-client-0.1.0-SNAPSHOT.jar ,运行 《 How to use for now 》 提供的示例,报错如下 :

        19:39:42.085 [main] INFO  com.pingcap.tikv.PDClient - Switched to new leader: com.pingcap.tikv.PDClient$LeaderWrapper@4961f6af
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: -1
    at java.util.Collections$EmptyList.get(Collections.java:4454)
    at com.pingcap.tikv.operation.ScanIterator.hasNext(ScanIterator.java:113)
    at com.pingcap.tikv.catalog.CatalogTransaction.hashGetFields(CatalogTransaction.java:115)
    at com.pingcap.tikv.catalog.CatalogTransaction.getDatabases(CatalogTransaction.java:137)
    at com.pingcap.tikv.catalog.Catalog$CatalogCache.loadDatabases(Catalog.java:106)
    at com.pingcap.tikv.catalog.Catalog$CatalogCache.<init>(Catalog.java:49)
    at com.pingcap.tikv.catalog.Catalog$CatalogCache.<init>(Catalog.java:46)
    at com.pingcap.tikv.catalog.Catalog.<init>(Catalog.java:115)
    at com.pingcap.tikv.TiSession.getCatalog(TiSession.java:82)
    at me.yunai.doraemon.tikv.Demo001.main(Demo001.java:22)

TODO 【3002】tikv-client-lib-java

666. 彩蛋

知识星球

又是被狙击的一个过程,折腾了一天,终于完稿!

好啦,胖友,分享一波朋友圈可好!

文章目录
  1. 1. 1. 安装 PD
  2. 2. 2. 启动 TiKV
  3. 3. 3. 使用 TiKV
    1. 3.1. 3.1 TiDB
    2. 3.2. 3.2 tikv-client-lib-java
  4. 4. 666. 彩蛋