Skip to content

Commit 945c363

Browse files
jkedgarGunnar Kudrjavets
authored andcommitted
Convert index_id into (cf_id, index_id).
Summary: Eventually we want to be able to import tables and the index_id values from the other tables may collide with existing index_ids. Thus we are converting the global index identifier from a single integer to the combination of a column family and index id. @update-submodule: rocksdb Test Plan: MTR Reviewers: yoshinorim, spetrunia, hermanlee4, maykov Reviewed By: maykov Differential Revision: https://reviews.facebook.net/D48477
1 parent 504c219 commit 945c363

File tree

9 files changed

+313
-220
lines changed

9 files changed

+313
-220
lines changed

storage/rocksdb/ha_rocksdb.cc

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
/* The C++ file's header */
2222
#include "./ha_rocksdb.h"
2323

24+
/* C++ standard header files */
25+
#include <map>
26+
2427
/* MySQL includes */
2528
#include "./my_bit.h"
2629
#include "./my_stacktrace.h"
@@ -979,7 +982,7 @@ static bool stop_drop_index_thread;
979982
class Rdb_CompactionFilter : public rocksdb::CompactionFilter
980983
{
981984
public:
982-
Rdb_CompactionFilter() {}
985+
explicit Rdb_CompactionFilter(uint32_t _cf_id) : cf_id(_cf_id) {}
983986
~Rdb_CompactionFilter()
984987
{
985988
print_compaction_status();
@@ -996,18 +999,21 @@ class Rdb_CompactionFilter : public rocksdb::CompactionFilter
996999
bool* value_changed) const override {
9971000

9981001
DBUG_ASSERT(key.size() >= sizeof(uint32));
999-
uint32 index= read_big_uint4((const uchar*)key.data());
1000-
DBUG_ASSERT(index >= 1);
10011002

1002-
if (index != prev_index) // processing new index id
1003+
GL_INDEX_ID gl_index_id;
1004+
gl_index_id.cf_id= cf_id;
1005+
gl_index_id.index_id= read_big_uint4((const uchar*)key.data());
1006+
DBUG_ASSERT(gl_index_id.index_id >= 1);
1007+
1008+
if (gl_index_id != prev_index) // processing new index id
10031009
{
10041010
if (num_deleted > 0)
10051011
{
10061012
print_compaction_status();
10071013
num_deleted= 0;
10081014
}
1009-
should_delete= dict_manager.is_drop_index_ongoing(index);
1010-
prev_index= index;
1015+
should_delete= dict_manager.is_drop_index_ongoing(gl_index_id);
1016+
prev_index= gl_index_id;
10111017
}
10121018

10131019
if (should_delete)
@@ -1024,11 +1030,14 @@ class Rdb_CompactionFilter : public rocksdb::CompactionFilter
10241030
{
10251031
if (num_deleted > 0)
10261032
sql_print_information("RocksDB: Compacting away elements from dropped "
1027-
"index %u: %llu", prev_index, num_deleted);
1033+
"index (%u,%u): %llu", prev_index.cf_id,
1034+
prev_index.index_id, num_deleted);
10281035
}
10291036

1037+
// Column family for this compaction filter
1038+
const uint32_t cf_id;
10301039
// Index id of the previous record
1031-
mutable uint32 prev_index= 0;
1040+
mutable GL_INDEX_ID prev_index= {0, 0};
10321041
// Number of rows deleted for the same index id
10331042
mutable uint64 num_deleted= 0;
10341043
// Current index id should be deleted or not (should be deleted if true)
@@ -1049,7 +1058,7 @@ class Rdb_CompactionFilterFactory : public rocksdb::CompactionFilterFactory
10491058
std::unique_ptr<rocksdb::CompactionFilter> CreateCompactionFilter(
10501059
const rocksdb::CompactionFilter::Context& context) {
10511060
return std::unique_ptr<rocksdb::CompactionFilter>(
1052-
new Rdb_CompactionFilter);
1061+
new Rdb_CompactionFilter(context.column_family_id));
10531062
}
10541063
};
10551064

@@ -3041,18 +3050,16 @@ int ha_rocksdb::create_key_defs(TABLE *table_arg, const char *db_table,
30413050
for (i= 0; i < table_arg->s->keys; i++)
30423051
{
30433052
uint index_id= ddl_manager.get_and_update_next_number(&dict_manager);
3044-
uint16_t index_dict_version;
3053+
uint16_t index_dict_version= RDBSE_KEYDEF::INDEX_INFO_VERSION_GLOBAL_ID;
30453054
uchar index_type;
30463055
uint16_t kv_version;
30473056
if (i == table_arg->s->primary_key)
30483057
{
3049-
index_dict_version= RDBSE_KEYDEF::INDEX_CF_MAPPING_VERSION_KV_FORMAT;
30503058
index_type= RDBSE_KEYDEF::INDEX_TYPE_PRIMARY;
30513059
kv_version= RDBSE_KEYDEF::PRIMARY_FORMAT_VERSION_INITIAL;
30523060
}
30533061
else
30543062
{
3055-
index_dict_version= RDBSE_KEYDEF::INDEX_CF_MAPPING_VERSION_KV_FORMAT;
30563063
index_type= RDBSE_KEYDEF::INDEX_TYPE_SECONDARY;
30573064
kv_version= RDBSE_KEYDEF::SECONDARY_FORMAT_VERSION_INITIAL;
30583065
}
@@ -5549,43 +5556,31 @@ void* drop_index_thread(void*)
55495556
assert(ret == 0 || ret == ETIMEDOUT);
55505557
mysql_mutex_unlock(&drop_index_interrupt_mutex);
55515558

5552-
std::vector<uint32> indices;
5553-
dict_manager.get_drop_indexes_ongoing(indices);
5559+
std::vector<GL_INDEX_ID> indices;
5560+
dict_manager.get_drop_indexes_ongoing(&indices);
55545561
if (!indices.empty()) {
5555-
std::unordered_set<uint32> finished;
5562+
std::unordered_set<GL_INDEX_ID> finished;
55565563
rocksdb::ReadOptions read_opts;
55575564
read_opts.total_order_seek = true; // disable bloom filter
55585565

55595566
for (auto d : indices) {
5560-
uint16 index_dict_version=0;
5561-
uchar index_type= 0;
5562-
uint16 kv_version= 0;
5563-
uint32 cf_id= 0;
55645567
uint32 cf_flags= 0;
5565-
if (!dict_manager.get_cf_id(d, &index_dict_version, &index_type,
5566-
&kv_version, &cf_id))
5567-
{
5568-
sql_print_error("RocksDB: Failed to get column family id "
5569-
"from index id %u. MyRocks data dictionary may "
5570-
"get corrupted.", d);
5571-
abort_with_stack_traces();
5572-
}
5573-
if (!dict_manager.get_cf_flags(cf_id, &cf_flags))
5568+
if (!dict_manager.get_cf_flags(d.cf_id, &cf_flags))
55745569
{
55755570
sql_print_error("RocksDB: Failed to get column family flags "
55765571
"from cf id %u. MyRocks data dictionary may "
5577-
"get corrupted.", cf_id);
5572+
"get corrupted.", d.cf_id);
55785573
abort_with_stack_traces();
55795574
}
5580-
rocksdb::ColumnFamilyHandle* cfh= cf_manager.get_cf(cf_id);
5575+
rocksdb::ColumnFamilyHandle* cfh= cf_manager.get_cf(d.cf_id);
55815576
DBUG_ASSERT(cfh);
55825577
bool is_reverse_cf= cf_flags & RDBSE_KEYDEF::REVERSE_CF_FLAG;
55835578
std::unique_ptr<rocksdb::Iterator> it(
55845579
rdb->NewIterator(read_opts, cfh));
55855580

55865581
bool index_removed= false;
55875582
uchar key_buf[RDBSE_KEYDEF::INDEX_NUMBER_SIZE]= {0};
5588-
store_big_uint4(key_buf, d);
5583+
store_big_uint4(key_buf, d.index_id);
55895584
rocksdb::Slice key = rocksdb::Slice((char*)key_buf, sizeof(key_buf));
55905585
it->Seek(key);
55915586
if (is_reverse_cf)
@@ -5938,7 +5933,7 @@ void compute_optimizer_stats()
59385933
}
59395934
}
59405935

5941-
std::map<uint32_t, MyRocksTablePropertiesCollector::IndexStats>
5936+
std::map<GL_INDEX_ID, MyRocksTablePropertiesCollector::IndexStats>
59425937
statsFromProps;
59435938
for (auto cf : cfs) {
59445939
rocksdb::TablePropertiesCollection props;
@@ -5953,13 +5948,13 @@ void compute_optimizer_stats()
59535948
std::vector<MyRocksTablePropertiesCollector::IndexStats> stats;
59545949
for (auto& keydef : keydefs)
59555950
{
5956-
auto it = statsFromProps.find(keydef->get_index_number());
5951+
auto it = statsFromProps.find(keydef->get_gl_index_id());
59575952
if (it != statsFromProps.end()) {
59585953
stats.push_back(it->second);
59595954
} else {
59605955
// This index didn't have any associated stats in SST properties.
59615956
// It might be an empty table, for example.
5962-
stats.emplace_back(keydef->get_index_number());
5957+
stats.emplace_back(keydef->get_gl_index_id());
59635958
}
59645959

59655960
uchar buf[2*RDBSE_KEYDEF::INDEX_NUMBER_SIZE];

storage/rocksdb/ha_rocksdb.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,53 @@ class Rdb_transaction;
115115

116116
const char *const rocksdb_hton_name= "ROCKSDB";
117117

118+
typedef struct _gl_index_id_s {
119+
uint32_t cf_id;
120+
uint32_t index_id;
121+
bool operator==(const struct _gl_index_id_s& other) const
122+
{
123+
return cf_id == other.cf_id && index_id == other.index_id;
124+
}
125+
bool operator!=(const struct _gl_index_id_s& other) const
126+
{
127+
return cf_id != other.cf_id || index_id != other.index_id;
128+
}
129+
bool operator<(const struct _gl_index_id_s& other) const
130+
{
131+
return cf_id < other.cf_id ||
132+
(cf_id == other.cf_id && index_id < other.index_id);
133+
}
134+
bool operator<=(const struct _gl_index_id_s& other) const
135+
{
136+
return cf_id < other.cf_id ||
137+
(cf_id == other.cf_id && index_id <= other.index_id);
138+
}
139+
bool operator>(const struct _gl_index_id_s& other) const
140+
{
141+
return cf_id > other.cf_id ||
142+
(cf_id == other.cf_id && index_id > other.index_id);
143+
}
144+
bool operator>=(const struct _gl_index_id_s& other) const
145+
{
146+
return cf_id > other.cf_id ||
147+
(cf_id == other.cf_id && index_id >= other.index_id);
148+
}
149+
} GL_INDEX_ID;
150+
151+
/* Provide hash function for GL_INDEX_ID so we can include it in sets */
152+
namespace std {
153+
template <>
154+
struct hash<GL_INDEX_ID>
155+
{
156+
std::size_t operator()(const GL_INDEX_ID& gl_index_id) const
157+
{
158+
uint64_t val = ((uint64_t) gl_index_id.cf_id << 32 |
159+
(uint64_t) gl_index_id.index_id);
160+
return std::hash<uint64_t>()(val);
161+
}
162+
};
163+
} // namespace std
164+
118165
/** @brief
119166
Class definition for the storage engine
120167
*/

storage/rocksdb/properties_collector.cc

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
/* This C++ file's header file */
1818
#include "./properties_collector.h"
1919

20+
/* Standard C++ header files */
21+
#include <map>
22+
2023
/* MySQL header files */
2124
#include "./sql_array.h"
2225

@@ -31,8 +34,10 @@ uint64_t rocksdb_num_sst_entry_other = 0;
3134

3235
MyRocksTablePropertiesCollector::MyRocksTablePropertiesCollector(
3336
Table_ddl_manager* ddl_manager,
34-
CompactionParams params
37+
CompactionParams params,
38+
uint32_t cf_id
3539
) :
40+
cf_id_(cf_id),
3641
ddl_manager_(ddl_manager),
3742
rows_(0l), deleted_rows_(0l), max_deleted_rows_(0l),
3843
params_(params)
@@ -70,15 +75,17 @@ MyRocksTablePropertiesCollector::AddUserKey(
7075
break;
7176
}
7277

73-
74-
uint32_t index_number = read_big_uint4((const uchar*)key.data());
75-
if (stats_.empty() || index_number != stats_.back().index_number) {
78+
GL_INDEX_ID gl_index_id;
79+
gl_index_id.cf_id = cf_id_;
80+
gl_index_id.index_id = read_big_uint4((const uchar*)key.data());
81+
if (stats_.empty() || gl_index_id != stats_.back().gl_index_id)
82+
{
7683
keydef_ = NULL;
7784
// starting a new table
7885
// add the new element into stats_
79-
stats_.push_back(IndexStats(index_number));
86+
stats_.push_back(IndexStats(gl_index_id));
8087
if (ddl_manager_) {
81-
keydef_ = ddl_manager_->get_copy_of_keydef(index_number);
88+
keydef_ = ddl_manager_->get_copy_of_keydef(gl_index_id);
8289
}
8390
if (keydef_) {
8491
// resize the array to the number of columns.
@@ -154,12 +161,12 @@ rocksdb::Status
154161
MyRocksTablePropertiesCollector::Finish(
155162
rocksdb::UserCollectedProperties* properties
156163
) {
157-
std::vector<uint32_t> changed_indexes;
164+
std::vector<GL_INDEX_ID> changed_indexes;
158165
changed_indexes.resize(stats_.size());
159166
std::transform(
160167
stats_.begin(), stats_.end(),
161168
changed_indexes.begin(),
162-
[](const IndexStats& s) {return s.index_number;}
169+
[](const IndexStats& s) {return s.gl_index_id;}
163170
);
164171
ddl_manager_->add_changed_indexes(changed_indexes);
165172
properties->insert({INDEXSTATS_KEY, IndexStats::materialize(stats_)});
@@ -203,11 +210,12 @@ MyRocksTablePropertiesCollector::GetReadableStats(
203210
const MyRocksTablePropertiesCollector::IndexStats& it
204211
) {
205212
std::string s;
206-
s.append(std::to_string(it.index_number));
207-
s.append(":{name:");
213+
s.append("(");
214+
s.append(std::to_string(it.gl_index_id.cf_id));
215+
s.append(", ");
216+
s.append(std::to_string(it.gl_index_id.index_id));
217+
s.append("):{name:");
208218
s.append(it.name);
209-
s.append(", number:");
210-
s.append(std::to_string(it.index_number));
211219
s.append(", size:");
212220
s.append(std::to_string(it.data_size));
213221
s.append(", rows:");
@@ -244,8 +252,8 @@ MyRocksTablePropertiesCollector::GetStatsFromTableProperties(
244252
*/
245253
void MyRocksTablePropertiesCollector::GetStats(
246254
const rocksdb::TablePropertiesCollection& collection,
247-
const std::unordered_set<uint32_t>& index_numbers,
248-
std::map<uint32_t, MyRocksTablePropertiesCollector::IndexStats>& ret
255+
const std::unordered_set<GL_INDEX_ID>& index_numbers,
256+
std::map<GL_INDEX_ID, MyRocksTablePropertiesCollector::IndexStats>& ret
249257
) {
250258
for (auto it : collection) {
251259
const auto& user_properties = it.second->user_collected_properties;
@@ -254,8 +262,8 @@ void MyRocksTablePropertiesCollector::GetStats(
254262
std::vector<IndexStats> stats;
255263
if (IndexStats::unmaterialize(it2->second, stats) == 0) {
256264
for (auto it3 : stats) {
257-
if (index_numbers.count(it3.index_number) != 0) {
258-
ret[it3.index_number].merge(it3);
265+
if (index_numbers.count(it3.gl_index_id) != 0) {
266+
ret[it3.gl_index_id].merge(it3);
259267
}
260268
}
261269
}
@@ -272,7 +280,8 @@ std::string MyRocksTablePropertiesCollector::IndexStats::materialize(
272280
String ret;
273281
write_short(&ret, INDEX_STATS_VERSION);
274282
for (auto i : stats) {
275-
write_int(&ret, i.index_number);
283+
write_int(&ret, i.gl_index_id.cf_id);
284+
write_int(&ret, i.gl_index_id.index_id);
276285
assert(sizeof i.data_size <= 8);
277286
write_int64(&ret, i.data_size);
278287
write_int64(&ret, i.rows);
@@ -302,15 +311,17 @@ int MyRocksTablePropertiesCollector::IndexStats::unmaterialize(
302311
while (p < p2) {
303312
IndexStats stats;
304313
if (p+
305-
sizeof(stats.index_number)+
314+
sizeof(stats.gl_index_id.cf_id)+
315+
sizeof(stats.gl_index_id.index_id)+
306316
sizeof(stats.data_size)+
307317
sizeof(stats.rows)+
308318
sizeof(stats.approximate_size)+
309319
sizeof(uint64) > p2)
310320
{
311321
return 1;
312322
}
313-
stats.index_number = read_int(&p);
323+
stats.gl_index_id.cf_id = read_int(&p);
324+
stats.gl_index_id.index_id = read_int(&p);
314325
stats.data_size = read_int64(&p);
315326
stats.rows = read_int64(&p);
316327
stats.approximate_size = read_int64(&p);
@@ -334,7 +345,7 @@ int MyRocksTablePropertiesCollector::IndexStats::unmaterialize(
334345
for the index based on stats for each sst
335346
*/
336347
void MyRocksTablePropertiesCollector::IndexStats::merge(const IndexStats& s) {
337-
index_number = s.index_number;
348+
gl_index_id = s.gl_index_id;
338349
rows += s.rows;
339350
data_size += s.data_size;
340351
if (distinct_keys_per_prefix.size() < s.distinct_keys_per_prefix.size()) {

0 commit comments

Comments
 (0)