Skip to content

Commit acffb01

Browse files
AliSQLAliSQL
authored andcommitted
[Feature] issue #33 HINT SOLUTION FOR INVENTORY
Description ----------- Usually Inventory Center has some hot commodity, corresponding to MySQL table records. So update the record will cause serious contention simultaneously. This patch supply a queue for every hot commondity to serialize update according to statement hint. Hints: QUEUE_ON_PK COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL TARGET_AFFECT_ROW Usage ----- update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL QUEUE_ON_PK 1 TARGET_AFFECT_ROW 1 t1 set c=c-1 where id=1;
1 parent c38a957 commit acffb01

19 files changed

Lines changed: 541 additions & 25 deletions

mysql-test/r/mysqld--help-notwin.result

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,17 @@ The following options may be given as the first argument:
719719
--range-alloc-block-size=#
720720
Allocation block size for storing ranges during
721721
optimization
722+
--rds-allow-unsafe-stmt-with-gtid
723+
Allow executing CREATE TABLE AS SELECT or mixed engine
724+
transactions if enabled.
722725
--rds-filter-key-cmp-in-order
723726
If enabled, then match keys stored in filter list in
724727
order
728+
--rds-gtid-precommit
729+
Add gtid into gtid_executed before flushing binlog from
730+
cache to file.
731+
--rds-ic-reduce-hint-enable
732+
enable the ic_reduce strategy when using hint
725733
--rds-indexstat Control INDEX_STATISTICS
726734
--rds-reset-all-filter
727735
Delete all sql filters immediately
@@ -740,12 +748,6 @@ The following options may be given as the first argument:
740748
--rds-threads-running-high-watermark=#
741749
When threads_running exceeds this limit, query that isn't
742750
in active transaction should quit.
743-
--rds-allow-unsafe-stmt-with-gtid
744-
Allow executing CREATE TABLE AS SELECT or mixed engine
745-
transactions if enabled.
746-
--rds-gtid-precommit
747-
Add gtid into gtid_executed before flushing binlog from
748-
cache to file.
749751
--read-buffer-size=#
750752
Each thread that does a sequential scan allocates a
751753
buffer of this size for each table it scans. If you do
@@ -1205,10 +1207,10 @@ block-encryption-mode aes-128-ecb
12051207
bulk-insert-buffer-size 8388608
12061208
character-set-client-handshake TRUE
12071209
character-set-filesystem binary
1208-
character-set-server latin1
1210+
character-set-server gbk
12091211
character-sets-dir MYSQL_CHARSETSDIR/
12101212
chroot (No default value)
1211-
collation-server latin1_swedish_ci
1213+
collation-server gbk_chinese_ci
12121214
completion-type NO_CHAIN
12131215
concurrent-insert AUTO
12141216
connect-timeout 10
@@ -1397,7 +1399,10 @@ query-cache-type OFF
13971399
query-cache-wlock-invalidate FALSE
13981400
query-prealloc-size 8192
13991401
range-alloc-block-size 4096
1402+
rds-allow-unsafe-stmt-with-gtid FALSE
14001403
rds-filter-key-cmp-in-order FALSE
1404+
rds-gtid-precommit FALSE
1405+
rds-ic-reduce-hint-enable FALSE
14011406
rds-indexstat FALSE
14021407
rds-reset-all-filter FALSE
14031408
rds-sql-delete-filter (No default value)
@@ -1407,8 +1412,6 @@ rds-sql-update-filter (No default value)
14071412
rds-tablestat FALSE
14081413
rds-threads-running-ctl-mode SELECTS
14091414
rds-threads-running-high-watermark 151
1410-
rds-allow-unsafe-stmt-with-gtid FALSE
1411-
rds-gtid-precommit FALSE
14121415
read-buffer-size 131072
14131416
read-only FALSE
14141417
read-rnd-buffer-size 262144
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
drop table if exists t1;
2+
create table t1 (id int primary key, c int) engine=innodb;
3+
update TARGET_AFFECT_ROW 1 t1 set c=c+1 where id=1;
4+
ERROR HY000: The affected row number does not match that of user specified.
5+
begin;
6+
insert into t1 values(1,1);
7+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL TARGET_AFFECT_ROW 1 t1 set c=c+1 where id=1;
8+
select * from t1;
9+
id c
10+
1 2
11+
begin;
12+
insert into t1 values(2,1);
13+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL TARGET_AFFECT_ROW 2 t1 set c=c+1 where id=2;
14+
ERROR HY000: The affected row number does not match that of user specified.
15+
select * from t1;
16+
id c
17+
1 2
18+
set @a=3;
19+
start transaction;
20+
insert into t1 values(3,1);
21+
prepare stmt from "update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL TARGET_AFFECT_ROW 1 t1 set c=c+1 where id=?";
22+
execute stmt using @a;
23+
prepare stmt from "insert COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL into t1 values(?,1)";
24+
execute stmt using @a;
25+
ERROR 23000: Duplicate entry '3' for key 'PRIMARY'
26+
select * from t1;
27+
id c
28+
1 2
29+
3 2
30+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL QUEUE_ON_PK 1 TARGET_AFFECT_ROW 1 t1 set c=c+1 where id=1;
31+
select * from t1;
32+
id c
33+
1 3
34+
3 2
35+
set global rds_ic_reduce_hint_enable=OFF;
36+
insert QUEUE_ON_PK 2 into t1 values(4,sleep(2));;
37+
set @tbegin=unix_timestamp(now());
38+
insert QUEUE_ON_PK 2 into t1 values(5,sleep(2));
39+
set @tend=unix_timestamp(now());
40+
select @tend - @tbegin cost;
41+
cost
42+
2
43+
delete from t1 where id in (4,5);
44+
set global rds_ic_reduce_hint_enable=ON;
45+
insert QUEUE_ON_PK 2 into t1 values(4,sleep(2));;
46+
set @tbegin=unix_timestamp(now());
47+
insert QUEUE_ON_PK 2 into t1 values(5,sleep(2));
48+
set @tend=unix_timestamp(now());
49+
select @tend - @tbegin cost;
50+
cost
51+
4
52+
begin;
53+
insert QUEUE_ON_PK 2 into t1 values(6,1);
54+
insert QUEUE_ON_PK 2 into t1 values(7,1);
55+
commit;
56+
insert COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL into t1 values(7,1);
57+
ERROR 23000: Duplicate entry '7' for key 'PRIMARY'
58+
insert COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL into t1 values(8,1);
59+
create table t2 like t1;
60+
insert into t2 value (1,1),(2,2);
61+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL t1,t2 set t2.c=3 where t2.id=t1.id and t1.id=1;
62+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL t1,t2 set t2.id=2 where t2.id=t1.id and t1.id=1;
63+
ERROR 23000: Duplicate entry '2' for key 'PRIMARY'
64+
SET DEBUG_SYNC='after_unlock_queue_lock_in_unregist WAIT_FOR unlock_in_check';
65+
insert QUEUE_ON_PK 2 into t1 values(9,1);;
66+
SET DEBUG_SYNC='after_unlock_queue_lock_in_check SIGNAL unlock_in_check WAIT_FOR go';
67+
insert QUEUE_ON_PK 2 into t1 values(10,1);;
68+
SET DEBUG_SYNC='now SIGNAL go';
69+
set global rds_ic_reduce_hint_enable=OFF;
70+
drop table t1,t2;
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#
2+
# Test of update hints
3+
#
4+
--source include/have_debug_sync.inc
5+
source include/have_innodb.inc;
6+
7+
--disable_warnings
8+
drop table if exists t1;
9+
--enable_warnings
10+
11+
create table t1 (id int primary key, c int) engine=innodb;
12+
connect (user1,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK);
13+
14+
#test target_affect_rows
15+
--error ER_UNMATCH_AFFECTED_ROWS
16+
update TARGET_AFFECT_ROW 1 t1 set c=c+1 where id=1;
17+
#-------------------------------------------------------------#
18+
19+
#test commit_on_success
20+
21+
begin;
22+
insert into t1 values(1,1);
23+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL TARGET_AFFECT_ROW 1 t1 set c=c+1 where id=1;
24+
25+
connection user1;
26+
select * from t1;
27+
#-------------------------------------------------------------#
28+
29+
#test rollback_on_fail
30+
connection default;
31+
begin;
32+
insert into t1 values(2,1);
33+
--error ER_UNMATCH_AFFECTED_ROWS
34+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL TARGET_AFFECT_ROW 2 t1 set c=c+1 where id=2;
35+
36+
connection user1;
37+
select * from t1;
38+
#-------------------------------------------------------------#
39+
40+
#test prepare-execute useage
41+
connection default;
42+
set @a=3;
43+
start transaction;
44+
insert into t1 values(3,1);
45+
prepare stmt from "update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL TARGET_AFFECT_ROW 1 t1 set c=c+1 where id=?";
46+
execute stmt using @a;
47+
48+
prepare stmt from "insert COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL into t1 values(?,1)";
49+
--error ER_DUP_ENTRY
50+
execute stmt using @a;
51+
52+
connection user1;
53+
select * from t1;
54+
#-------------------------------------------------------------#
55+
56+
#total hints
57+
connection default;
58+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL QUEUE_ON_PK 1 TARGET_AFFECT_ROW 1 t1 set c=c+1 where id=1;
59+
select * from t1;
60+
#-------------------------------------------------------------#
61+
62+
63+
#
64+
# Test for queue_on_pk
65+
#
66+
67+
#wait for pk
68+
#rds_ic_reduce_hint_enable=OFF
69+
set global rds_ic_reduce_hint_enable=OFF;
70+
connection default;
71+
--send insert QUEUE_ON_PK 2 into t1 values(4,sleep(2));
72+
73+
connection user1;
74+
set @tbegin=unix_timestamp(now());
75+
insert QUEUE_ON_PK 2 into t1 values(5,sleep(2));
76+
set @tend=unix_timestamp(now());
77+
select @tend - @tbegin cost;
78+
79+
connection default;
80+
--reap
81+
delete from t1 where id in (4,5);
82+
83+
#rds_ic_reduce_hint_enable=ON
84+
set global rds_ic_reduce_hint_enable=ON;
85+
connection default;
86+
--send insert QUEUE_ON_PK 2 into t1 values(4,sleep(2));
87+
88+
connection user1;
89+
set @tbegin=unix_timestamp(now());
90+
insert QUEUE_ON_PK 2 into t1 values(5,sleep(2));
91+
set @tend=unix_timestamp(now());
92+
select @tend - @tbegin cost;
93+
94+
connection default;
95+
--reap
96+
97+
#for cover
98+
connection default;
99+
begin;
100+
insert QUEUE_ON_PK 2 into t1 values(6,1);
101+
insert QUEUE_ON_PK 2 into t1 values(7,1);
102+
commit;
103+
104+
--error ER_DUP_ENTRY
105+
insert COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL into t1 values(7,1);
106+
107+
insert COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL into t1 values(8,1);
108+
109+
create table t2 like t1;
110+
insert into t2 value (1,1),(2,2);
111+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL t1,t2 set t2.c=3 where t2.id=t1.id and t1.id=1;
112+
--error ER_DUP_ENTRY
113+
update COMMIT_ON_SUCCESS ROLLBACK_ON_FAIL t1,t2 set t2.id=2 where t2.id=t1.id and t1.id=1;
114+
115+
#sync for left_thread_num
116+
connection default;
117+
SET DEBUG_SYNC='after_unlock_queue_lock_in_unregist WAIT_FOR unlock_in_check';
118+
--send insert QUEUE_ON_PK 2 into t1 values(9,1);
119+
120+
connection user1;
121+
SET DEBUG_SYNC='after_unlock_queue_lock_in_check SIGNAL unlock_in_check WAIT_FOR go';
122+
--sleep 1
123+
--send insert QUEUE_ON_PK 2 into t1 values(10,1);
124+
125+
connection default;
126+
--reap
127+
--sleep 1
128+
SET DEBUG_SYNC='now SIGNAL go';
129+
130+
connection user1;
131+
--reap
132+
133+
#
134+
#clean up
135+
#
136+
set global rds_ic_reduce_hint_enable=OFF;
137+
138+
drop table t1,t2;
139+
140+
disconnect user1;
141+
disconnect default;

mysql-test/suite/sys_vars/r/all_vars.result

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ INNODB_RDS_MIN_CONCURRENCY_TICKETS
2828
INNODB_RDS_MIN_CONCURRENCY_TICKETS
2929
INNODB_RDS_READ_VIEW_CACHE
3030
INNODB_RDS_READ_VIEW_CACHE
31+
RDS_ALLOW_UNSAFE_STMT_WITH_GTID
32+
RDS_ALLOW_UNSAFE_STMT_WITH_GTID
3133
RDS_FILTER_KEY_CMP_IN_ORDER
3234
RDS_FILTER_KEY_CMP_IN_ORDER
35+
RDS_GTID_PRECOMMIT
36+
RDS_GTID_PRECOMMIT
37+
RDS_IC_REDUCE_HINT_ENABLE
38+
RDS_IC_REDUCE_HINT_ENABLE
3339
RDS_INDEXSTAT
3440
RDS_INDEXSTAT
3541
RDS_RESET_ALL_FILTER
@@ -158,9 +164,5 @@ TRX_IDLE_TIMEOUT
158164
TRX_IDLE_TIMEOUT
159165
TRX_READONLY_IDLE_TIMEOUT
160166
TRX_READONLY_IDLE_TIMEOUT
161-
RDS_ALLOW_UNSAFE_STMT_WITH_GTID
162-
RDS_ALLOW_UNSAFE_STMT_WITH_GTID
163-
RDS_GTID_PRECOMMIT
164-
RDS_GTID_PRECOMMIT
165167
drop table t1;
166168
drop table t2;

0 commit comments

Comments
 (0)