set @save_rocksdb_store_row_debug_checksums=@@global.rocksdb_store_row_debug_checksums;
set @save_rocksdb_verify_row_debug_checksums=@@global.rocksdb_verify_row_debug_checksums;
set @save_rocksdb_checksums_pct=@@global.rocksdb_checksums_pct;
show variables like 'rocksdb_%checksum%';
Variable_name	Value
rocksdb_checksums_pct	100
rocksdb_store_row_debug_checksums	OFF
rocksdb_verify_row_debug_checksums	OFF
create table t1 (pk int primary key, a int, b int, key(a), key(b)) engine=rocksdb;
insert into t1 values (1,1,1),(2,2,2),(3,3,3);
check table t1;
Table	Op	Msg_type	Msg_text
test.t1	check	status	OK
CHECKTABLE t1: Checking table t1
CHECKTABLE t1:   Checking index a
CHECKTABLE t1:   ... 3 index entries checked (0 had checksums)
CHECKTABLE t1:   Checking index b
CHECKTABLE t1:   ... 3 index entries checked (0 had checksums)
CHECKTABLE t1:   0 table records had checksums
drop table t1;
set session rocksdb_store_row_debug_checksums=on;
create table t2 (pk int primary key, a int, b int, key(a), key(b)) engine=rocksdb;
insert into t2 values (1,1,1),(2,2,2),(3,3,3);
check table t2;
Table	Op	Msg_type	Msg_text
test.t2	check	status	OK
CHECKTABLE t2: Checking table t2
CHECKTABLE t2:   Checking index a
CHECKTABLE t2:   ... 3 index entries checked (3 had checksums)
CHECKTABLE t2:   Checking index b
CHECKTABLE t2:   ... 3 index entries checked (3 had checksums)
CHECKTABLE t2:   3 table records had checksums
# Now, make a table that has both rows with checksums and without
create table t3 (pk int primary key, a int, b int, key(a), key(b)) engine=rocksdb;
insert into t3 values (1,1,1),(2,2,2),(3,3,3);
set session rocksdb_store_row_debug_checksums=off;
update t3 set b=3 where a=2;
set session rocksdb_store_row_debug_checksums=on;
check table t3;
Table	Op	Msg_type	Msg_text
test.t3	check	status	OK
CHECKTABLE t3: Checking table t3
CHECKTABLE t3:   Checking index a
CHECKTABLE t3:   ... 3 index entries checked (3 had checksums)
CHECKTABLE t3:   Checking index b
CHECKTABLE t3:   ... 3 index entries checked (2 had checksums)
CHECKTABLE t3:   2 table records had checksums
set session rocksdb_store_row_debug_checksums=on;
set session rocksdb_checksums_pct=5;
create table t4 (pk int primary key, a int, b int, key(a), key(b)) engine=rocksdb;
check table t4;
Table	Op	Msg_type	Msg_text
test.t4	check	status	OK
4000 index entries had around 200 checksums
4000 index entries had around 200 checksums
Around 200 table records had checksums
set session rocksdb_checksums_pct=100;
#
# Ok, table t2 has all rows with checksums. Simulate a few checksum mismatches.
#
insert into mtr.test_suppressions values 
('Checksum mismatch in key of key-value pair for index'),
('Checksum mismatch in value of key-value pair for index'),
('Data with incorrect checksum');
# 1. Start with mismatch in key checksum of the PK.
set session debug_dbug= "+d,myrocks_simulate_bad_pk_checksum1";
set session rocksdb_verify_row_debug_checksums=off;
select * from t3;
pk	a	b
1	1	1
2	2	3
3	3	3
set session rocksdb_verify_row_debug_checksums=on;
select * from t3;
ERROR HY000: Internal error: Record checksum mismatch
select * from t4;
ERROR HY000: Internal error: Record checksum mismatch
set session debug_dbug= "-d,myrocks_simulate_bad_pk_checksum1";
# 2. Continue with mismatch in pk value checksum.
set session debug_dbug= "+d,myrocks_simulate_bad_pk_checksum2";
set session rocksdb_verify_row_debug_checksums=off;
select * from t3;
pk	a	b
1	1	1
2	2	3
3	3	3
set session rocksdb_verify_row_debug_checksums=on;
select * from t3;
ERROR HY000: Internal error: Record checksum mismatch
select * from t4;
ERROR HY000: Internal error: Record checksum mismatch
set session debug_dbug= "-d,myrocks_simulate_bad_pk_checksum2";
# 3. Check if we catch checksum mismatches for secondary indexes
explain
select * from t3 force index(a) where a<4;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t3	range	a	a	5	NULL	#	Using index condition
select * from t3 force index(a) where a<4;
pk	a	b
1	1	1
2	2	3
3	3	3
set session debug_dbug= "+d,myrocks_simulate_bad_key_checksum1";
select * from t3 force index(a) where a<4;
ERROR HY000: Internal error: Record checksum mismatch
select * from t4 force index(a) where a<1000000;
ERROR HY000: Internal error: Record checksum mismatch
set session debug_dbug= "-d,myrocks_simulate_bad_key_checksum1";
# 4. The same for index-only reads?
explain 
select a from t3 force index(a) where a<4;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t3	range	a	a	5	NULL	#	Using where; Using index
select a from t3 force index(a) where a<4;
a
1
2
3
set session debug_dbug= "+d,myrocks_simulate_bad_key_checksum1";
select a from t3 force index(a) where a<4;
ERROR HY000: Internal error: Record checksum mismatch
select a from t4 force index(a) where a<1000000;
ERROR HY000: Internal error: Record checksum mismatch
set session debug_dbug= "-d,myrocks_simulate_bad_key_checksum1";
set @@global.rocksdb_store_row_debug_checksums=@save_rocksdb_store_row_debug_checksums;
set @@global.rocksdb_verify_row_debug_checksums=@save_rocksdb_verify_row_debug_checksums;
set @@global.rocksdb_checksums_pct=@save_rocksdb_checksums_pct;
# restart
drop table t2,t3,t4;
