Skip to content

Commit 5941e55

Browse files
author
Ahmed Salama
committed
Add parameter for calculate_stats function of the indexes to recalculate only + testing
Summary: Change calculate_stats() to recalculate statistics for certain set of indexes only; Those that changed. Test Plan: Added new test for asserting index statistics Used gdb to see the new check changing the control flow (some indexes skip and some recalculate). Reviewers: shashanktyagi, mung Differential Revision: https://phabricator.intern.facebook.com/D7075934 Tasks: T15057256
1 parent 2f786e8 commit 5941e55

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

mysql-test/suite/rocksdb/r/add_index_inplace.result

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,24 @@ t1 CREATE TABLE `t1` (
445445
KEY `kb` (`b`(8))
446446
) ENGINE=ROCKSDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
447447
DROP TABLE t1;
448+
SET @prior_rocksdb_table_stats_sampling_pct = @@rocksdb_table_stats_sampling_pct;
449+
set global rocksdb_table_stats_sampling_pct = 100;
450+
CREATE TABLE t1 (a INT, b INT, PRIMARY KEY ka(a)) ENGINE=RocksDB;
451+
INSERT INTO t1 (a, b) VALUES (1, 10);
452+
INSERT INTO t1 (a, b) VALUES (2, 10);
453+
INSERT INTO t1 (a, b) VALUES (3, 20);
454+
INSERT INTO t1 (a, b) VALUES (4, 20);
455+
set global rocksdb_force_flush_memtable_now=1;
456+
analyze table t1;
457+
Table Op Msg_type Msg_text
458+
test.t1 analyze status OK
459+
SHOW INDEX in t1;
460+
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
461+
t1 0 PRIMARY 1 a A 4 NULL NULL LSMTREE
462+
ALTER TABLE t1 ADD INDEX kb(b), ALGORITHM=INPLACE;
463+
SHOW INDEX in t1;
464+
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
465+
t1 0 PRIMARY 1 a A 4 NULL NULL LSMTREE
466+
t1 1 kb 1 b A 2 NULL NULL YES LSMTREE
467+
DROP TABLE t1;
468+
SET global rocksdb_table_stats_sampling_pct = @prior_rocksdb_table_stats_sampling_pct;

mysql-test/suite/rocksdb/t/add_index_inplace.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,24 @@ if ($end_max_index_id <= $start_max_index_id) {
383383
SHOW CREATE TABLE t1;
384384
DROP TABLE t1;
385385

386+
# Cardinality checks for indexes statistics
387+
SET @prior_rocksdb_table_stats_sampling_pct = @@rocksdb_table_stats_sampling_pct;
388+
set global rocksdb_table_stats_sampling_pct = 100;
386389

390+
CREATE TABLE t1 (a INT, b INT, PRIMARY KEY ka(a)) ENGINE=RocksDB;
387391

392+
INSERT INTO t1 (a, b) VALUES (1, 10);
393+
INSERT INTO t1 (a, b) VALUES (2, 10);
394+
INSERT INTO t1 (a, b) VALUES (3, 20);
395+
INSERT INTO t1 (a, b) VALUES (4, 20);
388396

397+
set global rocksdb_force_flush_memtable_now=1;
398+
analyze table t1;
399+
400+
SHOW INDEX in t1;
401+
402+
ALTER TABLE t1 ADD INDEX kb(b), ALGORITHM=INPLACE;
403+
SHOW INDEX in t1;
404+
405+
DROP TABLE t1;
406+
SET global rocksdb_table_stats_sampling_pct = @prior_rocksdb_table_stats_sampling_pct;

storage/rocksdb/ha_rocksdb.cc

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10942,24 +10942,34 @@ int ha_rocksdb::optimize(THD *const thd, HA_CHECK_OPT *const check_opt) {
1094210942
DBUG_RETURN(HA_EXIT_SUCCESS);
1094310943
}
1094410944

10945-
int ha_rocksdb::calculate_stats(const TABLE *const table_arg, THD *const thd,
10946-
HA_CHECK_OPT *const check_opt) {
10945+
int ha_rocksdb::calculate_stats(
10946+
const TABLE *const table_arg,
10947+
const std::unordered_set<GL_INDEX_ID> &to_recalc) {
1094710948
DBUG_ENTER_FUNC();
1094810949

1094910950
// find per column family key ranges which need to be queried
1095010951
std::unordered_map<rocksdb::ColumnFamilyHandle *, std::vector<rocksdb::Range>>
1095110952
ranges;
10952-
std::unordered_set<GL_INDEX_ID> ids_to_check;
10953+
std::unordered_map<GL_INDEX_ID, Rdb_index_stats> stats;
10954+
std::vector<uint> to_recalc_indexes;
1095310955
std::vector<uchar> buf(table_arg->s->keys * 2 *
1095410956
Rdb_key_def::INDEX_NUMBER_SIZE);
10955-
std::unordered_map<GL_INDEX_ID, Rdb_index_stats> stats;
1095610957
for (uint i = 0; i < table_arg->s->keys; i++) {
10958+
const Rdb_key_def &kd = *m_key_descr_arr[i];
10959+
const GL_INDEX_ID index_id = kd.get_gl_index_id();
10960+
if (to_recalc.find(index_id) == to_recalc.end()) {
10961+
continue;
10962+
}
10963+
10964+
to_recalc_indexes.push_back(i);
10965+
}
10966+
10967+
for (uint i : to_recalc_indexes) {
1095710968
const auto bufp = &buf[i * 2 * Rdb_key_def::INDEX_NUMBER_SIZE];
1095810969
const Rdb_key_def &kd = *m_key_descr_arr[i];
1095910970
const GL_INDEX_ID index_id = kd.get_gl_index_id();
1096010971
ranges[kd.get_cf()].push_back(get_range(i, bufp));
1096110972

10962-
ids_to_check.insert(index_id);
1096310973
// Initialize the stats to 0. If there are no files that contain
1096410974
// this gl_index_id, then 0 should be stored for the cached stats.
1096510975
stats[index_id] = Rdb_index_stats(index_id);
@@ -10997,8 +11007,9 @@ int ha_rocksdb::calculate_stats(const TABLE *const table_arg, THD *const thd,
1099711007
other SQL tables, it can be that we're only seeing a small fraction
1099811008
of table's entries (and so we can't update statistics based on that).
1099911009
*/
11000-
if (ids_to_check.find(it1.m_gl_index_id) == ids_to_check.end())
11010+
if (to_recalc.find(it1.m_gl_index_id) == to_recalc.end()) {
1100111011
continue;
11012+
}
1100211013

1100311014
auto kd = ddl_manager.safe_find(it1.m_gl_index_id);
1100411015
DBUG_ASSERT(kd != nullptr);
@@ -11011,8 +11022,9 @@ int ha_rocksdb::calculate_stats(const TABLE *const table_arg, THD *const thd,
1101111022
Rdb_tbl_card_coll cardinality_collector(rocksdb_table_stats_sampling_pct);
1101211023
auto read_opts = rocksdb::ReadOptions();
1101311024
read_opts.read_tier = rocksdb::ReadTier::kMemtableTier;
11014-
for (uint i = 0; i < table_arg->s->keys; i++) {
11025+
for (const uint i : to_recalc_indexes) {
1101511026
const Rdb_key_def &kd = *m_key_descr_arr[i];
11027+
1101611028
Rdb_index_stats &stat = stats[kd.get_gl_index_id()];
1101711029

1101811030
uchar r_buf[Rdb_key_def::INDEX_NUMBER_SIZE * 2];
@@ -11069,8 +11081,18 @@ int ha_rocksdb::calculate_stats(const TABLE *const table_arg, THD *const thd,
1106911081
int ha_rocksdb::analyze(THD *const thd, HA_CHECK_OPT *const check_opt) {
1107011082
DBUG_ENTER_FUNC();
1107111083

11072-
if (table && calculate_stats(table, thd, check_opt) != HA_EXIT_SUCCESS) {
11073-
DBUG_RETURN(HA_ADMIN_FAILED);
11084+
if (table) {
11085+
std::unordered_set<GL_INDEX_ID> ids_to_check;
11086+
for (uint i = 0; i < table->s->keys; i++) {
11087+
const Rdb_key_def &kd = *m_key_descr_arr[i];
11088+
const GL_INDEX_ID index_id = kd.get_gl_index_id();
11089+
ids_to_check.insert(index_id);
11090+
}
11091+
11092+
int res = calculate_stats(table, ids_to_check);
11093+
if (res != HA_EXIT_SUCCESS) {
11094+
DBUG_RETURN(HA_ADMIN_FAILED);
11095+
}
1107411096
}
1107511097

1107611098
DBUG_RETURN(HA_ADMIN_OK);
@@ -11935,7 +11957,7 @@ bool ha_rocksdb::commit_inplace_alter_table(
1193511957
prevents us from updating the stats normally as the ddl_manager cannot
1193611958
find the proper gl_index_ids yet during adjust_stats calls.
1193711959
*/
11938-
if (calculate_stats(altered_table, nullptr, nullptr)) {
11960+
if (calculate_stats(altered_table, create_index_ids)) {
1193911961
/* Failed to update index statistics, should never happen */
1194011962
DBUG_ASSERT(0);
1194111963
}

storage/rocksdb/ha_rocksdb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,8 @@ class ha_rocksdb : public my_core::handler {
13271327
MY_ATTRIBUTE((__warn_unused_result__));
13281328
int analyze(THD *const thd, HA_CHECK_OPT *const check_opt) override
13291329
MY_ATTRIBUTE((__warn_unused_result__));
1330-
int calculate_stats(const TABLE *const table_arg, THD *const thd,
1331-
HA_CHECK_OPT *const check_opt)
1330+
int calculate_stats(const TABLE *const table_arg,
1331+
const std::unordered_set<GL_INDEX_ID> &to_recalc)
13321332
MY_ATTRIBUTE((__warn_unused_result__));
13331333

13341334
enum_alter_inplace_result check_if_supported_inplace_alter(

0 commit comments

Comments
 (0)