TiDB 的事务和一致性校验工具 BANK

文章介绍了BANK程序,一个用于测试分布式数据库在高可用场景下事务一致性的工具。该程序模拟转账操作,验证多表事务和跨节点的高可用性。通过校验账户总金额和TSO的时间戳来评估事务破裂、RPO=0的容灾效果以及历史数据一致性。
摘要由CSDN通过智能技术生成

作者: pepezzzz 原文来源: https://tidb.net/blog/f80362b4

背景

在分布式数据库的选型和测试过程中,通常需要关注分布式事务在高可用场景下的一致性和 RPO=0 的容灾技术实现。分布式事务需要能影响多张表的多条记录,实现多表事务和跨节点高可用的验证。

BANK 程序

BANK 程序(链接: https://pan.baidu.com/s/1M14Kf0ULGdbkoMZj0iOvDA?pwd=p93k 提取码: p93k)是一个简化转账模型的并发程序。初始化后,会创建流水 record 表和账务 account 表,流水表对应转账过程中的金额变化和事务时间( tso 列,实际函数为 time.Now().UnixNano() ,即执行节点的纳秒时间),account 表记录最终的账户金额和事务时间(tso 列),record 表和 account 表的同一事务(tso 列) 的交易记录和交易账户能对应。

MySQL [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| accounts       |
| record         |
+----------------+
2 rows in set (0.00 sec)

MySQL [test]> select * from record order by tso desc limit 1; 
+---------+-------+--------------+--------------------+------------+------------------+--------+---------------------+
| from_id | to_id | from_balance | from_balance_after | to_balance | to_balance_after | amount | tso                 |
+---------+-------+--------------+--------------------+------------+------------------+--------+---------------------+
|     384 |   172 |         1226 |                284 |       1915 |             2857 |    942 | 1661398293659372259 |

MySQL [test]> select * from accounts order by tso desc limit 10; 
+-----+---------+---------------------------------------------------------------------------------------------------+---------------------+
| id  | balance | remark                                                                                            | tso                 |
+-----+---------+---------------------------------------------------------------------------------------------------+---------------------+
| 384 |     284 | abcdefghijklmnopqrs                                                                               | 1661398293659372259 |
| 172 |    2857 | abcdefg                                                                                           | 1661398293659372259 |

使用方法

程序的参数如下:

[root@iZuf6d7xln13sovvijl68rZ ~]# ./bank --help
Usage of ./bank:
-accounts int
the number of the bank accounts (default 1000000)
-create
create new tables for test
-driver string
database driver name, support 'mysql', 'postgres' (default "mysql")
-dsn string
data source name
MySQL: root:@tcp(127.0.0.1:4000)/test
PostgreSQL: postgres://root:@127.0.0.1:5432/postgres?sslmode=disable
(default "root:@tcp(127.0.0.1:4000)/test")
-insert
insert data for test
-interval duration
verify interval (default 2s)
-threads int
threads count (default 10)

初次执行

./bank -dsn 'root:password@tcp(172.16.5.31:34000)/test' -create -insert -accounts 100

配好 DSN 后,第一次执行程序添加 -create -insert 参数会自动初始化数据,初始完成后开始转账,就随机挑选两个账户,select for update 锁定账户,然后 update 账户并记录流水,然后提交事务。

中断后可后续执行

./bank -dsn 'root:password@tcp(172.16.5.31:34000)/test' -accounts 100

bank 程序除转账交易的并发线程(默认10个)外,有一个 goroutine,到达校验 interval (默认 2 秒)时间就会算一下账户总金额是不是对的,不是预期的就退出。

使用场景

  1. 节点高可用情况等情况下的事务一致性。如果总金额不一致,说明发生事务破裂。

  2. RPO=0 容灾场景的数据一致性。如果最新的 TSO 不一致,说明不是 RPO=0。

  3. 逻辑复制场景的历史数据一致性。如果历史 TSO 的数据不一致,说明复制过程数据不一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值