1/*
2 Copyright (c) 2015, Facebook, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
17#include <my_global.h>
18
19/* The C++ file's header */
20#include "./event_listener.h"
21
22/* C++ standard header files */
23#include <string>
24#include <vector>
25
26/* MySQL includes */
27#include <mysql/plugin.h>
28
29/* MyRocks includes */
30#include "./ha_rocksdb.h"
31#include "./properties_collector.h"
32#include "./rdb_datadic.h"
33
34namespace myrocks {
35
36static std::vector<Rdb_index_stats>
37extract_index_stats(const std::vector<std::string> &files,
38 const rocksdb::TablePropertiesCollection &props) {
39 std::vector<Rdb_index_stats> ret;
40 for (auto fn : files) {
41 const auto it = props.find(fn);
42 DBUG_ASSERT(it != props.end());
43 std::vector<Rdb_index_stats> stats;
44 Rdb_tbl_prop_coll::read_stats_from_tbl_props(it->second, &stats);
45 ret.insert(ret.end(), stats.begin(), stats.end());
46 }
47 return ret;
48}
49
50void Rdb_event_listener::update_index_stats(
51 const rocksdb::TableProperties &props) {
52 DBUG_ASSERT(m_ddl_manager != nullptr);
53 const auto tbl_props =
54 std::make_shared<const rocksdb::TableProperties>(props);
55
56 std::vector<Rdb_index_stats> stats;
57 Rdb_tbl_prop_coll::read_stats_from_tbl_props(tbl_props, &stats);
58
59 m_ddl_manager->adjust_stats(stats);
60}
61
62void Rdb_event_listener::OnCompactionCompleted(
63 rocksdb::DB *db, const rocksdb::CompactionJobInfo &ci) {
64 DBUG_ASSERT(db != nullptr);
65 DBUG_ASSERT(m_ddl_manager != nullptr);
66
67 if (ci.status.ok()) {
68 m_ddl_manager->adjust_stats(
69 extract_index_stats(ci.output_files, ci.table_properties),
70 extract_index_stats(ci.input_files, ci.table_properties));
71 }
72}
73
74void Rdb_event_listener::OnFlushCompleted(
75 rocksdb::DB *db, const rocksdb::FlushJobInfo &flush_job_info) {
76 DBUG_ASSERT(db != nullptr);
77 update_index_stats(flush_job_info.table_properties);
78}
79
80void Rdb_event_listener::OnExternalFileIngested(
81 rocksdb::DB *db, const rocksdb::ExternalFileIngestionInfo &info) {
82 DBUG_ASSERT(db != nullptr);
83 update_index_stats(info.table_properties);
84}
85
86void Rdb_event_listener::OnBackgroundError(
87 rocksdb::BackgroundErrorReason reason, rocksdb::Status *status) {
88 rdb_log_status_error(*status, "Error detected in background");
89 sql_print_error("RocksDB: BackgroundErrorReason: %d", (int)reason);
90 if (status->IsCorruption()) {
91 rdb_persist_corruption_marker();
92 abort();
93 }
94}
95} // namespace myrocks
96