源码编译并部署tidb、tikv
编译部署环境:centos7、git1.8、make3.82、cmake3.14.5
安装配置golang
1 | cd ~ |
安装配置rust
1 | cd ~ |
下载源代码
1 | mkdir -p ~/go/src |
编译pd并启动验证
1 | cd ~/go/src/pd |
编译tikv并启动验证
1 | cd ~/go/src/tikv |
编译tidb并启动验证
1 | cd ~/go/src/tidb |
启动1pd1tidb3tikv
1 | # 方便启停,新建start.sh与stop.sh |
改写源代码,事务启动时,打印日志
寻找事务启动的过程中分别尝试了store/kv.go、store/tikv/txn.go中的Commit实现,都没成功,而且加上之后后台线程一直在不停地打印”hello transaction”
通过test尝试找到了session.go的ExecutePreparedStmt实现,加上日志,写入数据,查看tidb的日志,感觉事务启动是在2pc.go中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26// ExecutePreparedStmt executes a prepared statement.
func (s *session) ExecutePreparedStmt(ctx context.Context, stmtID uint32, args []types.Datum) (sqlexec.RecordSet, error) {
logutil.BgLogger().Info("ExecutePreparedStmt hello transactions")
s.PrepareTxnCtx(ctx)
var err error
s.sessionVars.StartTime = time.Now()
preparedPointer, ok := s.sessionVars.PreparedStmts[stmtID]
if !ok {
err = plannercore.ErrStmtNotFound
logutil.Logger(ctx).Error("prepared statement not found", zap.Uint32("stmtID", stmtID))
return nil, err
}
preparedStmt, ok := preparedPointer.(*plannercore.CachedPrepareStmt)
if !ok {
return nil, errors.Errorf("invalid CachedPrepareStmt type")
}
executor.CountStmtNode(preparedStmt.PreparedAst.Stmt, s.sessionVars.InRestrictedSQL)
ok, err = s.IsCachedExecOk(ctx, preparedStmt)
if err != nil {
return nil, err
}
if ok {
return s.cachedPlanExec(ctx, stmtID, preparedStmt, args)
}
return s.preparedStmtExec(ctx, stmtID, preparedStmt, args)
}修改2pc.go的execute实现,加上日志,写入数据,查看tidb的日志
1
2
3
4
5
6
7
8
9
10
11
12// execute executes the two-phase commit protocol.
func (c *twoPhaseCommitter) execute(ctx context.Context) (err error) {
。。。
binlogChan := c.prewriteBinlog(ctx)
prewriteBo := NewBackofferWithVars(ctx, PrewriteMaxBackoff, c.txn.vars)
start := time.Now()
err = c.prewriteMutations(prewriteBo, c.mutations)
commitDetail := c.getDetail()
logutil.BgLogger().Info("2pc hello transactions")
commitDetail.PrewriteTime = time.Since(start)
。。。
}
以上为,从源码编译部署tidb、tikv、pd,并通过修改源代码在事务开启时打印日志的过程,不确定事务开启是否在2pc.go,go以及tidb初学者,还请指正!