1--source include/have_debug.inc
2--source include/have_debug_sync.inc
3--source include/have_rocksdb.inc
4
5select @@global.rocksdb_table_stats_use_table_scan;
6
7set @old_debug = @@global.debug;
8
9## test1: concurrency between the background index thread and analyze table ##
10create table t (i int auto_increment,
11  j int,
12  k int,
13  primary key(i, j, k), key `secondary_key` (j, k)) engine=rocksdb;
14
15set @@global.debug = '+d,rocksdb_is_bg_thread';
16
17insert into t(j, k)  values (1, 1), (1, 1), (2, 1), (2, 1),
18    (3, 2), (3, 2), (4, 2), (4, 2);
19set @@global.rocksdb_force_flush_memtable_now = true;
20
21set debug_sync = "now signal ready_to_calculate_index_stats";
22
23# Calculate stats on table t
24analyze table t;
25
26show indexes in t;
27select table_name, table_rows from information_schema.tables where table_schema = database() and table_name = "t";
28
29set debug_sync = "now wait_for index_stats_calculation_done";
30
31# Close table t so that the handler's cardinality data will be
32# updated on table re-open
33flush tables t;
34
35# Analyze table and the background thread should get the same index stats
36--echo index stats should not change after the background thread finishes calculation
37show indexes in t;
38select table_name, table_rows from information_schema.tables where table_schema = database() and table_name = "t";
39
40set @@global.debug = @old_debug;
41drop table t;
42
43## test2: concurrency between the background index thread and drop table ##
44create table t (i int auto_increment,
45  j int,
46  k int,
47  primary key(i, j, k), key `secondary_key` (j, k)) engine=rocksdb;
48
49set @@global.debug = '+d,rocksdb_is_bg_thread_drop_table';
50
51# Insert rows to trigger the background thread to calculate
52# index stats for table t
53insert into t(j, k)  values (1, 1), (1, 1), (2, 1), (2, 1),
54    (3, 2), (3, 2), (4, 2), (4, 2);
55set @@global.rocksdb_force_flush_memtable_now = true;
56
57set debug_sync = "now wait_for ready_to_drop_table";
58
59# Drop table t before the index stats are saved
60drop table t;
61set debug_sync = "now signal ready_to_save_table_stats";
62
63--error ER_NO_SUCH_TABLE
64show indexes in t;
65select table_name, table_rows, data_length, index_length from information_schema.tables where table_schema = database() and table_name = "t";
66
67set @@global.debug = @old_debug;
68
69## test3: concurrency between the background index thread and alter table ##
70create table t (i int auto_increment,
71  j int,
72  k int,
73  primary key(i, j, k),  key `secondary_key` (j, k)) engine=rocksdb;
74
75show indexes in t;
76select table_name, table_rows from information_schema.tables where table_schema = database() and table_name = "t";
77
78set @@global.debug = '+d,rocksdb_is_bg_thread';
79set @@global.debug = '+d,rocksdb_calculate_stats';
80insert into t(j, k)  values (1, 1), (1, 1), (2, 1), (2, 1),
81    (3, 2), (3, 2), (4, 2), (4, 2);
82set @@global.rocksdb_force_flush_memtable_now = true;
83
84set debug_sync = "now signal ready_to_calculate_index_stats";
85set debug_sync = "now wait_for ready_to_drop_index";
86
87# Drop secondary_key from table t
88alter table t drop index secondary_key;
89set debug_sync = "now signal ready_to_save_index_stats";
90set debug_sync = "now wait_for index_stats_calculation_done";
91
92# Close table t so that the handler's cardinality data will be
93# updated on table re-open
94flush tables t;
95
96show indexes in t;
97select table_name, table_rows from information_schema.tables where table_schema = database() and table_name = "t";
98
99set @@global.debug = @old_debug;
100drop table t;
101
102