1--source include/have_rocksdb.inc
2
3#
4#  Tests for row checksums feature
5#
6--source include/have_debug.inc
7
8--let LOG=$MYSQLTEST_VARDIR/tmp/rocksdb_checksum.err
9--let $_mysqld_option=--log-error=$LOG
10--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
11--source include/restart_mysqld_with_option.inc
12
13set @save_rocksdb_store_row_debug_checksums=@@global.rocksdb_store_row_debug_checksums;
14set @save_rocksdb_verify_row_debug_checksums=@@global.rocksdb_verify_row_debug_checksums;
15set @save_rocksdb_checksums_pct=@@global.rocksdb_checksums_pct;
16
17show variables like 'rocksdb_%checksum%';
18
19create table t1 (pk int primary key, a int, b int, key(a), key(b)) engine=rocksdb;
20insert into t1 values (1,1,1),(2,2,2),(3,3,3);
21check table t1;
22--let SEARCH_FILE=$LOG
23--let SEARCH_PATTERN=0 table records had checksums
24--let SEARCH_PATTERN=CHECKTABLE t1[^\n]*
25--let SEARCH_OUTPUT=matches
26--source include/search_pattern_in_file.inc
27
28drop table t1;
29
30set session rocksdb_store_row_debug_checksums=on;
31create table t2 (pk int primary key, a int, b int, key(a), key(b)) engine=rocksdb;
32insert into t2 values (1,1,1),(2,2,2),(3,3,3);
33check table t2;
34--let SEARCH_PATTERN=CHECKTABLE t2[^\n]*
35--source include/search_pattern_in_file.inc
36
37--echo # Now, make a table that has both rows with checksums and without
38create table t3 (pk int primary key, a int, b int, key(a), key(b)) engine=rocksdb;
39insert into t3 values (1,1,1),(2,2,2),(3,3,3);
40set session rocksdb_store_row_debug_checksums=off;
41update t3 set b=3 where a=2;
42set session rocksdb_store_row_debug_checksums=on;
43check table t3;
44--let SEARCH_PATTERN=CHECKTABLE t3[^\n]*
45--source include/search_pattern_in_file.inc
46
47set session rocksdb_store_row_debug_checksums=on;
48set session rocksdb_checksums_pct=5;
49create table t4 (pk int primary key, a int, b int, key(a), key(b)) engine=rocksdb;
50--disable_query_log
51let $i=0;
52let $x= 100000;
53while ($i<4000)
54{
55  inc $i;
56  eval insert t4(pk,a,b) values($i, $i, $i div 10);
57  eval update t4 set a= a+$x where a=$i;
58  eval update t4 set pk=pk+$x where pk=$i;
59}
60--enable_query_log
61check table t4;
62perl;
63$total=4000;
64$pct=5;
65@out=();
66
67$filename= "$ENV{LOG}";
68print $filename "\n";
69open(F, '<', $filename) || die("Can't open file $filename: $!");
70while(<F>) {
71  @out=() if /^CURRENT_TEST:/;
72  if (/(\d+) index entries checked \((\d+) had checksums/) {
73    if ($1 == $total and $2 >= $total*($pct-2)/100 and $2 <= $total*($pct+2)/100) {
74      push @out, sprintf "%d index entries had around %d checksums\n", $total, $total*$pct/100;
75    }
76  } elsif (/(\d+) table records had checksums/) {
77    if ($1 >= $total*($pct-2)/100 and $1 <= $total*($pct+2)/100) {
78      push @out, sprintf "Around %d table records had checksums\n", $total*$pct/100;
79    }
80  }
81}
82print @out;
83EOF
84set session rocksdb_checksums_pct=100;
85
86--echo #
87--echo # Ok, table t2 has all rows with checksums. Simulate a few checksum mismatches.
88--echo #
89insert into mtr.test_suppressions values
90 ('Checksum mismatch in key of key-value pair for index'),
91 ('Checksum mismatch in value of key-value pair for index'),
92 ('Data with incorrect checksum');
93
94--echo # 1. Start with mismatch in key checksum of the PK.
95set session debug_dbug= "+d,myrocks_simulate_bad_pk_checksum1";
96set session rocksdb_verify_row_debug_checksums=off;
97select * from t3;
98set session rocksdb_verify_row_debug_checksums=on;
99--error ER_INTERNAL_ERROR
100select * from t3;
101--error ER_INTERNAL_ERROR
102select * from t4;
103set session debug_dbug= "-d,myrocks_simulate_bad_pk_checksum1";
104
105--echo # 2. Continue with mismatch in pk value checksum.
106set session debug_dbug= "+d,myrocks_simulate_bad_pk_checksum2";
107set session rocksdb_verify_row_debug_checksums=off;
108select * from t3;
109set session rocksdb_verify_row_debug_checksums=on;
110--error ER_INTERNAL_ERROR
111select * from t3;
112--error ER_INTERNAL_ERROR
113select * from t4;
114set session debug_dbug= "-d,myrocks_simulate_bad_pk_checksum2";
115
116--echo # 3. Check if we catch checksum mismatches for secondary indexes
117--replace_column 9 #
118explain
119select * from t3 force index(a) where a<4;
120select * from t3 force index(a) where a<4;
121
122set session debug_dbug= "+d,myrocks_simulate_bad_key_checksum1";
123--error ER_INTERNAL_ERROR
124select * from t3 force index(a) where a<4;
125--error ER_INTERNAL_ERROR
126select * from t4 force index(a) where a<1000000;
127set session debug_dbug= "-d,myrocks_simulate_bad_key_checksum1";
128
129--echo # 4. The same for index-only reads?
130--disable_query_log
131set global rocksdb_force_flush_memtable_now=1;
132--enable_query_log
133--replace_column 9 #
134explain
135select a from t3 force index(a) where a<4;
136select a from t3 force index(a) where a<4;
137
138set session debug_dbug= "+d,myrocks_simulate_bad_key_checksum1";
139--error ER_INTERNAL_ERROR
140select a from t3 force index(a) where a<4;
141--error ER_INTERNAL_ERROR
142select a from t4 force index(a) where a<1000000;
143set session debug_dbug= "-d,myrocks_simulate_bad_key_checksum1";
144
145set @@global.rocksdb_store_row_debug_checksums=@save_rocksdb_store_row_debug_checksums;
146set @@global.rocksdb_verify_row_debug_checksums=@save_rocksdb_verify_row_debug_checksums;
147set @@global.rocksdb_checksums_pct=@save_rocksdb_checksums_pct;
148
149--source include/restart_mysqld.inc
150#--remove_file $LOG
151
152drop table t2,t3,t4;
153