1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/*
Copyright (c) 2015, Facebook, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
#pragma once
/* C++ system header files */
#include <map>
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>
/* RocksDB header files */
#include "rocksdb/db.h"
/* MyRocks header files */
#include "./ha_rocksdb.h"
namespace myrocks {
class Rdb_ddl_manager;
class Rdb_key_def;
extern std::atomic<uint64_t> rocksdb_num_sst_entry_put;
extern std::atomic<uint64_t> rocksdb_num_sst_entry_delete;
extern std::atomic<uint64_t> rocksdb_num_sst_entry_singledelete;
extern std::atomic<uint64_t> rocksdb_num_sst_entry_merge;
extern std::atomic<uint64_t> rocksdb_num_sst_entry_other;
extern my_bool rocksdb_compaction_sequential_deletes_count_sd;
struct Rdb_compact_params {
uint64_t m_deletes, m_window, m_file_size;
};
struct Rdb_index_stats {
enum {
INDEX_STATS_VERSION_INITIAL = 1,
INDEX_STATS_VERSION_ENTRY_TYPES = 2,
};
GL_INDEX_ID m_gl_index_id;
int64_t m_data_size, m_rows, m_actual_disk_size;
int64_t m_entry_deletes, m_entry_single_deletes;
int64_t m_entry_merges, m_entry_others;
std::vector<int64_t> m_distinct_keys_per_prefix;
std::string m_name; // name is not persisted
static std::string materialize(const std::vector<Rdb_index_stats> &stats);
static int unmaterialize(const std::string &s,
std::vector<Rdb_index_stats> *const ret);
Rdb_index_stats() : Rdb_index_stats({0, 0}) {}
explicit Rdb_index_stats(GL_INDEX_ID gl_index_id)
: m_gl_index_id(gl_index_id),
m_data_size(0),
m_rows(0),
m_actual_disk_size(0),
m_entry_deletes(0),
m_entry_single_deletes(0),
m_entry_merges(0),
m_entry_others(0) {}
void merge(const Rdb_index_stats &s, const bool increment = true,
const int64_t estimated_data_len = 0);
};
// The helper class to calculate index cardinality
class Rdb_tbl_card_coll {
public:
explicit Rdb_tbl_card_coll(const uint8_t table_stats_sampling_pct);
public:
void ProcessKey(const rocksdb::Slice &key, const Rdb_key_def *keydef,
Rdb_index_stats *stats);
/*
* Resets the state of the collector to start calculating statistics for a
* next index.
*/
void Reset();
/*
* Cardinality statistics might be calculated using some sampling strategy.
* This method adjusts gathered statistics according to the sampling
* strategy used. Note that adjusted cardinality value is just an estimate
* and can return a value exeeding number of rows in a table, so the
* returned value should be capped by row count before using it by
* an optrimizer or displaying it to a clent.
*/
void AdjustStats(Rdb_index_stats *stats);
private:
bool ShouldCollectStats();
bool IsSampingDisabled();
private:
std::string m_last_key;
uint8_t m_table_stats_sampling_pct;
unsigned int m_seed;
};
class Rdb_tbl_prop_coll : public rocksdb::TablePropertiesCollector {
public:
Rdb_tbl_prop_coll(Rdb_ddl_manager *const ddl_manager,
const Rdb_compact_params ¶ms, const uint32_t cf_id,
const uint8_t table_stats_sampling_pct);
/*
Override parent class's virtual methods of interest.
*/
virtual rocksdb::Status AddUserKey(const rocksdb::Slice &key,
const rocksdb::Slice &value,
rocksdb::EntryType type,
rocksdb::SequenceNumber seq,
uint64_t file_size) override;
virtual rocksdb::Status Finish(
rocksdb::UserCollectedProperties *properties) override;
virtual const char *Name() const override { return "Rdb_tbl_prop_coll"; }
rocksdb::UserCollectedProperties GetReadableProperties() const override;
bool NeedCompact() const override;
public:
uint64_t GetMaxDeletedRows() const { return m_max_deleted_rows; }
static void read_stats_from_tbl_props(
const std::shared_ptr<const rocksdb::TableProperties> &table_props,
std::vector<Rdb_index_stats> *out_stats_vector);
private:
static std::string GetReadableStats(const Rdb_index_stats &it);
bool ShouldCollectStats();
void CollectStatsForRow(const rocksdb::Slice &key,
const rocksdb::Slice &value,
const rocksdb::EntryType &type,
const uint64_t file_size);
Rdb_index_stats *AccessStats(const rocksdb::Slice &key);
void AdjustDeletedRows(rocksdb::EntryType type);
private:
uint32_t m_cf_id;
std::shared_ptr<const Rdb_key_def> m_keydef;
Rdb_ddl_manager *m_ddl_manager;
std::vector<Rdb_index_stats> m_stats;
Rdb_index_stats *m_last_stats;
static const char *INDEXSTATS_KEY;
// last added key
std::string m_last_key;
// floating window to count deleted rows
std::vector<bool> m_deleted_rows_window;
uint64_t m_rows, m_window_pos, m_deleted_rows, m_max_deleted_rows;
uint64_t m_file_size;
Rdb_compact_params m_params;
Rdb_tbl_card_coll m_cardinality_collector;
bool m_recorded;
};
class Rdb_tbl_prop_coll_factory
: public rocksdb::TablePropertiesCollectorFactory {
public:
Rdb_tbl_prop_coll_factory(const Rdb_tbl_prop_coll_factory &) = delete;
Rdb_tbl_prop_coll_factory &operator=(const Rdb_tbl_prop_coll_factory &) =
delete;
explicit Rdb_tbl_prop_coll_factory(Rdb_ddl_manager *ddl_manager)
: m_ddl_manager(ddl_manager) {}
/*
Override parent class's virtual methods of interest.
*/
virtual rocksdb::TablePropertiesCollector *CreateTablePropertiesCollector(
rocksdb::TablePropertiesCollectorFactory::Context context) override {
return new Rdb_tbl_prop_coll(m_ddl_manager, m_params,
context.column_family_id,
m_table_stats_sampling_pct);
}
virtual const char *Name() const override {
return "Rdb_tbl_prop_coll_factory";
}
public:
void SetCompactionParams(const Rdb_compact_params ¶ms) {
m_params = params;
}
void SetTableStatsSamplingPct(const uint8_t table_stats_sampling_pct) {
m_table_stats_sampling_pct = table_stats_sampling_pct;
}
private:
Rdb_ddl_manager *const m_ddl_manager;
Rdb_compact_params m_params;
uint8_t m_table_stats_sampling_pct;
};
} // namespace myrocks
|