本课相关信息
课前学习资料
教学视频
课程作业
题目描述
本地下载 TiDB,TiKV,PD 源代码,改写源码并编译部署以下环境:
- 1 TiDB
- 1 PD
- 3 TiKV
改写后:使得 TiDB 启动事务时,能打印出一个 hello transaction
的日志
下载编译 TiDB, TiKV, PD 源码
1. 下载源码
$ git clone https://github.com/pingcap/tidb.git
$ git clone https://github.com/pingcap/pd.git
$ git clone https://github.com/tikv/tikv.git
2. 编译 TiDB, PD 源码
TiDB 和 PD 都是 Go 语言写的,本地之前已经有 Go 的环境,所以直接 make
就顺利地编译完成了:
3. 编译 TiKV 源码
TiKV 是 Rust 写的,需要先安装 Rust 相关的环境才能编译。
Rust 安装 直接通过 rustup
:
$ curl https://sh.rustup.rs -sSf | sh
安装完成后也是直接通过 make
对 TiKV 进行编译,拉依赖和编译的过程及其漫长。。。总之经过数小时的等待,最终还是顺利地编译好了 TiKV:
使用 TiUP 部署本地测试环境
由于 TiUP playground 支持使用本地编译好的二进制文件启动集群,所以这里直接选择使用 TiUP 来部署编译好的 PD、TiKV 和 TiDB。
TiUP 的安装也十分方便,参考官方指南即可:
$ curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
使用 TiUP 部署 TiDB 集群:
$ tiup playground --kv 3 --tiflash 0 --db.binpath tidb/bin/tidb-server --pd.binpath pd/bin/pd-server --kv.binpath tikv/target/release/tikv-server
这样就成功使用之前编译好的 PD、TiKV、TiDB 在本地部署了个 1 TiDB + 1 PD + 3 TiKV 的集群。
改写源码打印日志
作业要求在启动事务时打印日志,按我自己的理解,可能就是在 [BEGIN|START TRANSACTION]
时打印日志(不确定是否理解正确)。所以在 TiDB 代码里找到了 executeBegin
这个函数添加上了日志(commit):
func (e *SimpleExec) executeBegin(ctx context.Context, s *ast.BeginStmt) error {
logutil.Logger(ctx).Info("hello transaction")
// If BEGIN is the first statement in TxnCtx, we can reuse the existing transaction, without the
// need to call NewTxn, which commits the existing transaction and begins a new one.
txnCtx := e.ctx.GetSessionVars().TxnCtx
if txnCtx.History != nil {
err := e.ctx.NewTxn(ctx)
if err != nil {
return err
}
}
...
}
添加好日志后重新编译 TiDB 并进行部署,使用 TiUP 部署的 TiDB 可以在 ~/.tiup/data
中找到日志:
能看到已经持续地有 hello transaction
的日志被打印出来,但奇怪的是我还并没有执行什么 SQL。而且日志是以 3s 一次的频率被打印的,应该是有什么周期性的事务在执行。
总之还是验证下 [BEGIN|START TRANSACTION]
时是否会打印日志:
能看到当执行 BEGIN
或者 START TRANSACTION
时,TiDB 确实有打印出 hello transaction
的日志。