目录
一、测试版本
二、模拟锁表场景
1、查看自动提交参数
2、关闭自动提交
3、测试表结构
4、测试数据
5、会话一更新数据不提交
6、会话二更新数据卡住
7、会话三查看连接信息
8、会话三查看锁信息
9、解决方法
10、会话一插入数据不提交
11、会话二更新报错
三、总结
名称 | 值 |
CPU | Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz |
操作系统 | CentOS Linux release 7.9.2009 (Core) |
内存 | 3G |
逻辑核数 | 2 |
Gbase8a版本 | 8.6.2-R43 |
打开三个会话。
gbase> show variables like'%commit%';
+-----------------------------------------------------------+----------+
| Variable_name | Value |
+-----------------------------------------------------------+----------+
| _t_gcluster_commit_revert_flag_test | 1 |
| autocommit | ON |
| gbase_tcmalloc_aggressive_decommit_threadhold_load_factor | 0.900000 |
| gbase_tcmalloc_balanced_decommit_threadhold_load_factor | 0.600000 |
| gcluster_kafka_batch_commit_dml_count | 100000 |
| gcluster_kafka_parallel_commit | 1 |
+-----------------------------------------------------------+----------+
6 rows in set (Elapsed: 00:00:00.00)
gbase> set autocommit = 0;
Query OK, 0 rows affected (Elapsed: 00:00:00.00)
前两个会话设置非自动提交。
gbase> desc czg.testtab;
+-------+---------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+-------------------+-----------------------------+
| a | int(11) | YES | | NULL | |
| b | double | YES | | NULL | |
| c | varchar(100) | YES | MUL | NULL | |
| d | text | YES | | NULL | |
| e | blob | YES | | NULL | |
| f | longblob | YES | | NULL | |
| g | date | YES | | NULL | |
| h | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| i | decimal(10,2) | YES | | NULL | |
+-------+---------------+------+-----+-------------------+-----------------------------+
9 rows in set (Elapsed: 00:00:00.05)
gbase> select * from czg.testtab limit 10;
+------+------+------+--------------------+---------------------+--------------------------+------------+---------------------+------+
| a | b | c | d | e | f | g | h | i |
+------+------+------+--------------------+---------------------+--------------------------+------------+---------------------+------+
| 1 | 1.1 | czg | 快乐的小天使 | qwertasdsdfzxczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 1.00 |
| 1 | 1.1 | czg | 快乐的小天使 | qwertasdsdfzxczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 1.00 |
| 1 | 1.1 | czg | 快乐的小天使 | qwertasdsdfzxczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 1.00 |
| 1 | 1.1 | czg | 快乐的小天使 | qwertasdsdfzxczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 1.00 |
| 1 | 1.1 | czg | 快乐的小天使 | qwertasdsdfzxczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 1.00 |
| 2 | 1.1 | czg | 快乐的小天使 | qwertasdsdfzxczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 2.00 |
| 2 | 4.1 | czg | 快乐的小天使 | qwertasdsdfzxczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 1.10 |
| 2 | 4.1 | czg | 快乐的小天使 | qwertasdsdfz\xczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 1.10 |
| 2 | 4.1 | czg | 快乐的小天使 | qwertasdsdfz.xczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 1.10 |
| 2 | 4.1 | czg | 快乐的小天使 | qwertasdsdfz.xczxxv | gregergjsfishfuieehfuiew | 1995-09-18 | 2022-08-03 09:24:00 | 1.10 |
+------+------+------+--------------------+---------------------+--------------------------+------------+---------------------+------+
10 rows in set (Elapsed: 00:00:02.51)
gbase> update czg.testtab set d = 'JJLGG' where a = 2;
Query OK, 1310720 rows affected (Elapsed: 03:28:21.03)
Rows matched: 1310720 Changed: 1310720 Warnings: 0
gbase> update czg.testtab set d = 'HappySunshine' where a = 1;
我们更新的是不同的行,却锁住了,说明Gbase8a不支持行锁,这里加的是表锁。
[gbase@czg2 ~]$ gccli -e "show detail processlist;"
+-----+-------+-----------------+----------------------+--------------------+---------+--------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+--------------------------------------------------------+
| Id | Tid | User | Host | db | Command | Time | State | Lock | Wait | Info |
+-----+-------+-----------------+----------------------+--------------------+---------+--------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+--------------------------------------------------------+
| 1 | 5752 | event_scheduler | localhost | NULL | Daemon | 80337 | Waiting for next activation | NULL | NULL | NULL |
| 276 | 78263 | root | localhost | NULL | Sleep | 172223 | | NULL | NULL | NULL |
| 277 | 78372 | root | 192.168.142.12:35329 | zxj | Sleep | 172223 | | NULL | NULL | NULL |
| 585 | 94012 | root | localhost | information_schema | Query | 205 | checking permissions | czg | czg.testtab02076d53-3c94-4aa4-b020-b9df0c802739 | update czg.testtab set d = 'HappySunshine' where a = 1 |
| 589 | 95886 | root | localhost | NULL | Sleep | 19292 | | czg;czg.testtab02076d53-3c94-4aa4-b020-b9df0c802739;czg.testtab580D5F90-B287-4199-B057-E6FBD44B5BFA;czg.testtab6ef6f8a9-87f0-4d6c-8043-899367d02df3 | NULL | NULL |
| 642 | 2049 | root | localhost | NULL | Query | 0 | NULL | NULL | NULL | show detail processlist |
+-----+-------+-----------------+----------------------+--------------------+---------+--------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------+--------------------------------------------------------+
这些字段的含义为:
字段 | 描述 |
Id | 连接编号。 |
Tid | 线程号。 |
User | 登录数据库用户。 |
Host | 连接源端的ip和端口。 |
db | 连接的数据库。 |
Command | 当前执行的命令。 |
Time | 上面命令执行的时间。(单位:秒) |
State | SQL的运行状态。 |
Lock | 持有的锁。 |
Wait | 等待的锁。 |
Info | 执行的SQL语句。 |
我们主要看589和585。
585的State:checking permissions,表示在检查权限。Wait:czg.testtab02076d53-3c94-4aa4-b020-b9df0c802739,表示等待锁czg.testtab02076d53-3c94-4aa4-b020-b9df0c802739。
589的Lock:czg;czg.testtab02076d53-3c94-4aa4-b020-b9df0c802739;czg.testtab580D5F90-B287-4199-B057-E6FBD44B5BFA;czg.testtab6ef6f8a9-87f0-4d6c-8043-899367d02df3,表示持有锁:
(1)czg
(2)czg.testtab02076d53-3c94-4aa4-b020-b9df0c802739(和585等待的锁一样)
(3)czg.testtab580D5F90-B287-4199-B057-E6FBD44B5BFA
(4)czg.testtab6ef6f8a9-87f0-4d6c-8043-899367d02df3
说明589一直占有锁,未释放,导致585一直等待。再看589的Command:Sleep,表示为空闲状态。那我们可以断定是589没有执行提交操作,导致的锁等待。如果589是Query,那就有可能是执行慢,导致其他会话卡住。具体情况具体分析吧。
[gbase@czg2 ~]$ gcadmin showlock
+========================================================================================================================+
| GCLUSTER LOCK |
+========================================================================================================================+
+-----------------------------------------------+--------------+------------------------------+--------------+------+----+
| Lock name | owner | content | create time |orphan|type|
+-----------------------------------------------+--------------+------------------------------+--------------+------+----+
| czg |192.168.142.12|LOCK_Reserved: 589(LWP:95886) |20231109105742|FALSE | S |
+-----------------------------------------------+--------------+------------------------------+--------------+------+----+
| czg |192.168.142.12|LOCK_Reserved: 585(LWP:94012) |20231109161549|FALSE | S |
+-----------------------------------------------+--------------+------------------------------+--------------+------+----+
|czg.testtab02076d53-3c94-4aa4-b020-b9df0c802739|192.168.142.12|LOCK_Rebalance: 589(LWP:95886)|20231109142557|FALSE | E |
+-----------------------------------------------+--------------+------------------------------+--------------+------+----+
|czg.testtab580d5f90-b287-4199-b057-e6fbd44b5bfa|192.168.142.12| LOCK_DMLEX: 589(LWP:95886) |20231109142557|FALSE | E |
+-----------------------------------------------+--------------+------------------------------+--------------+------+----+
|czg.testtab6ef6f8a9-87f0-4d6c-8043-899367d02df3|192.168.142.12| LOCK_DMLEX: 589(LWP:95886) |20231109142557|FALSE | E |
+-----------------------------------------------+--------------+------------------------------+--------------+------+----+
Total : 5
参数 | 描述 |
Lock name | 锁的名称。 |
owner | 持有锁的节点IP。 |
content | 第一个数字是Id连接编号。 第二个数字是线程号。 |
create time | 锁创建日期。 |
orphan | 是否为孤儿锁。 |
type | S:共享锁。E:排他锁。 |
(1)方法一:589对应的会话进行提交。
(2)方法二:开一个新会话kill 589。
(3)方法三:通过官方的python gcware接口去释放。
查看锁demo
#encoding:utf-8
import gcware
SUCCESS_FLAG = 1
FAIL_FLAG = 0
def ReleaseLock(TableName, LockId):
if gcware.unlocktableforce(TableName,LockId) != 0:
print("ReleaseLock : OK, TableName : %s, LockId : %s"%(TableName,LockId))
return SUCCESS_FLAG
else:
print("ReleaseLock : Fail, TableName : %s, LockId : %s"%(TableName,LockId))
return FAIL_FLAG
if __name__ == '__main__':
for i in gcware.getlocks():
for j in i:
print("%-20s : %s"%(j,i[j]))
print("=========================")
TableName就是下面的name,LockId就是下面的lockid。大家可以自己调用ReleaseLock尝试释放锁,建议大家不要用gcware接口。
运行效果
create time : 20231109105742
name : czg
lockid : 9114802754034860036
orphan : FALSE
content : LOCK_Reserved: 589(LWP:95886)
owner : 192.168.142.12
type : SHARE
=========================
create time : 20231109161549
name : czg
lockid : 171802565900500995
orphan : FALSE
content : LOCK_Reserved: 585(LWP:94012)
owner : 192.168.142.12
type : SHARE
=========================
create time : 20231109142557
name : czg.testtab02076d53-3c94-4aa4-b020-b9df0c802739
lockid : 8869015163097841664
orphan : FALSE
content : LOCK_Rebalance: 589(LWP:95886)
owner : 192.168.142.12
type : EXCLUSIVE
=========================
create time : 20231109142557
name : czg.testtab580d5f90-b287-4199-b057-e6fbd44b5bfa
lockid : 2937847747467804673
orphan : FALSE
content : LOCK_DMLEX: 589(LWP:95886)
owner : 192.168.142.12
type : EXCLUSIVE
=========================
create time : 20231109142557
name : czg.testtab6ef6f8a9-87f0-4d6c-8043-899367d02df3
lockid : 6284283086788100098
orphan : FALSE
content : LOCK_DMLEX: 589(LWP:95886)
owner : 192.168.142.12
type : EXCLUSIVE
=========================
gbase> insert into czg.testtab values(1,1.1,'asdasd','zxj','dfd','asd','1995-09-18','2023-08-03 09:24:00',1.00);
Query OK, 1 row affected (Elapsed: 00:00:00.14)
gbase> update czg.testtab set d = 'JJLGG' where a = 2;
ERROR 1727 (HY000): try to lock in gcluster failed: (GBA-02LO-0002) Can't lock file(already exists).
序号 | 描述 |
1 | Gbase8a锁支持粒度为表级。 |
2 | 并发更新或删除同一张表时,其中一个会话执行较慢,导致其他会话等待。 |
3 | 会话一更新或删除A表,会话二插入A表,无论会话一二的执行顺序,都会提示报错:ERROR 1727 (HY000): try to lock in gcluster failed: (GBA-02LO-0002) Can't lock file(already exists).。 |