From 1a0f5813eb64b40367d81bc5c1824344238036a2 Mon Sep 17 00:00:00 2001 From: Chun Ni Date: Fri, 28 Apr 2023 09:41:25 -0700 Subject: [PATCH] Add dict_register_dd_table_id api support for rocksdb dd (#1298) Summary: We are adding support for data dictionary related rocksdb_hton functions. This will include the current diff and couple of following diffs. Each diff will add support for one or several rocksdb_hton function. This current adds support for rocksdb_hton->dict_register_dd_table_id. It contains several part 1. Add a rdb_native_dd file. The file is dedicated to rocksdb data dictionary related function and objects. 2. Add dd table check in rename_table/delete_table/truncate_table. These table operations are not allowed for dd tables. Fail the operation if it's operated on dd tables. 3. Maintain a dd table id set. Pull Request resolved: https://github.com/facebook/mysql-5.6/pull/1298 Reviewed By: luqun Differential Revision: D45332365 Pulled By: sunshine-Chun fbshipit-source-id: 95aebf8d6a4de03f37bb15438fadfe19670108ba --- storage/rocksdb/CMakeLists.txt | 1 + storage/rocksdb/ha_rocksdb.cc | 38 +++++++++++++++++------ storage/rocksdb/ha_rocksdb.h | 6 ++-- storage/rocksdb/rdb_native_dd.cc | 53 ++++++++++++++++++++++++++++++++ storage/rocksdb/rdb_native_dd.h | 47 ++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 storage/rocksdb/rdb_native_dd.cc create mode 100644 storage/rocksdb/rdb_native_dd.h diff --git a/storage/rocksdb/CMakeLists.txt b/storage/rocksdb/CMakeLists.txt index 4f58e95e6b0e..b9bb20611608 100644 --- a/storage/rocksdb/CMakeLists.txt +++ b/storage/rocksdb/CMakeLists.txt @@ -159,6 +159,7 @@ SET(ROCKSDB_SE_SOURCES rdb_cf_options.cc rdb_cf_options.h rdb_cf_manager.cc rdb_cf_manager.h rdb_converter.cc rdb_converter.h + rdb_native_dd.cc rdb_native_dd.h properties_collector.cc properties_collector.h event_listener.cc event_listener.h rdb_i_s.cc rdb_i_s.h diff --git a/storage/rocksdb/ha_rocksdb.cc b/storage/rocksdb/ha_rocksdb.cc index 8f1ac6511056..d0ab517a4823 100644 --- a/storage/rocksdb/ha_rocksdb.cc +++ b/storage/rocksdb/ha_rocksdb.cc @@ -101,6 +101,7 @@ #include "./rdb_index_merge.h" #include "./rdb_iterator.h" #include "./rdb_mutex_wrapper.h" +#include "./rdb_native_dd.h" #include "./rdb_psi.h" #include "./rdb_sst_partitioner_factory.h" #include "./rdb_threads.h" @@ -7432,6 +7433,8 @@ static int rocksdb_init_internal(void *const p) { rocksdb_hton->clone_interface.clone_apply = rocksdb_clone_apply; rocksdb_hton->clone_interface.clone_apply_end = rocksdb_clone_apply_end; + rocksdb_hton->dict_register_dd_table_id = rocksdb_dict_register_dd_table_id; + rocksdb_hton->flags = HTON_SUPPORTS_EXTENDED_KEYS | HTON_CAN_RECREATE; rocksdb_hton->partition_flags = rocksdb_partition_flags; @@ -10132,6 +10135,9 @@ int ha_rocksdb::truncate_table(Rdb_tbl_def *tbl_def_arg, dd::Table *table_def) { DBUG_ENTER_FUNC(); + int err = native_dd::reject_if_dd_table(table_def); + if (err != 0) DBUG_RETURN(err); + /* Fast table truncation involves deleting the table and then recreating it. However, it is possible recreating the table fails. In this case, a @@ -10155,8 +10161,8 @@ int ha_rocksdb::truncate_table(Rdb_tbl_def *tbl_def_arg, should be locked via MDL, no other process thread be able to access this table. */ - int err = rdb_split_normalized_tablename(orig_tablename, &dbname, &tblname, - &partition); + err = rdb_split_normalized_tablename(orig_tablename, &dbname, &tblname, + &partition); assert(err == 0); if (err != HA_EXIT_SUCCESS) DBUG_RETURN(err); tblname = std::string(TRUNCATE_TABLE_PREFIX) + tblname; @@ -14065,12 +14071,14 @@ int ha_rocksdb::delete_table(Rdb_tbl_def *const tbl) { */ int ha_rocksdb::delete_table(const char *const tablename, - const dd::Table *table_def - MY_ATTRIBUTE((__unused__))) { + const dd::Table *table_def) { DBUG_ENTER_FUNC(); assert(tablename != nullptr); + int err = native_dd::reject_if_dd_table(table_def); + if (err != 0) DBUG_RETURN(err); + /* Find the table in the hash */ Rdb_tbl_def *const tbl = get_table_if_exists(tablename); if (!tbl) { @@ -14085,17 +14093,19 @@ int ha_rocksdb::delete_table(const char *const tablename, HA_EXIT_SUCCESS OK other HA_ERR error code (cannot be SE-specific) */ -int ha_rocksdb::rename_table( - const char *const from, const char *const to, - const dd::Table *from_table_def MY_ATTRIBUTE((__unused__)), - dd::Table *to_table_def MY_ATTRIBUTE((__unused__))) { +int ha_rocksdb::rename_table(const char *const from, const char *const to, + const dd::Table *from_table_def, + [[maybe_unused]] dd::Table *to_table_def) { DBUG_ENTER_FUNC(); + int rc; + rc = native_dd::reject_if_dd_table(from_table_def); + if (rc != 0) DBUG_RETURN(rc); + std::string from_str; std::string to_str; std::string from_db; std::string to_db; - int rc; if (rdb_is_tablename_normalized(from)) { from_str = from; @@ -19020,6 +19030,16 @@ static bool parse_fault_injection_params( return false; } +bool ha_rocksdb::get_se_private_data(dd::Table *, bool reset) { + // TODO: Remove the assert once we fully migrate to using RocksDB DD API. + assert(false); + if (reset) { + native_dd::clear_dd_table_ids(); + } + + return false; +} + } // namespace myrocks /* diff --git a/storage/rocksdb/ha_rocksdb.h b/storage/rocksdb/ha_rocksdb.h index 01d43a3e2713..8d5171e3bf48 100644 --- a/storage/rocksdb/ha_rocksdb.h +++ b/storage/rocksdb/ha_rocksdb.h @@ -493,8 +493,8 @@ class ha_rocksdb : public my_core::handler, public blob_buffer { bool should_store_row_debug_checksums() const; int rename_table(const char *const from, const char *const to, - const dd::Table *from_table_def MY_ATTRIBUTE((__unused__)), - dd::Table *to_table_def MY_ATTRIBUTE((__unused__))) override + const dd::Table *from_table_def, + dd::Table *to_table_def) override MY_ATTRIBUTE((__warn_unused_result__, __nonnull__(2, 3))); int convert_record_from_storage_format(const rocksdb::Slice *const key, @@ -670,6 +670,8 @@ class ha_rocksdb : public my_core::handler, public blob_buffer { static bool can_use_bloom_filter(THD *thd, const Rdb_key_def &kd, const rocksdb::Slice &eq_cond); + bool get_se_private_data(dd::Table *, bool reset) override; + private: bool mrr_sorted_mode; // true <=> we are in ordered-keys, ordered-results // RANGE_SEQ_IF is stored in handler::mrr_funcs diff --git a/storage/rocksdb/rdb_native_dd.cc b/storage/rocksdb/rdb_native_dd.cc new file mode 100644 index 000000000000..9e0e3d1fc598 --- /dev/null +++ b/storage/rocksdb/rdb_native_dd.cc @@ -0,0 +1,53 @@ +/* + Copyright (c) 2023 Meta, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* This C++ file's header file */ +#include "rdb_native_dd.h" + +/* MySQL header files */ +#include "sql/dd/types/table.h" // dd::Table + +/* MyRocks header files */ +#include "ha_rocksdb.h" + +namespace myrocks { +std::unordered_set native_dd::s_dd_table_ids = {}; + +bool native_dd::is_dd_table_id(dd::Object_id id) { + return (native_dd::s_dd_table_ids.find(id) != + native_dd::s_dd_table_ids.end()); +} + +int native_dd::reject_if_dd_table(const dd::Table *table_def) { + if (table_def != nullptr && is_dd_table_id(table_def->se_private_id())) { + my_error(ER_NOT_ALLOWED_COMMAND, MYF(0)); + return HA_ERR_UNSUPPORTED; + } + + return (0); +} + +void native_dd::insert_dd_table_ids(dd::Object_id dd_table_id) { + s_dd_table_ids.insert(dd_table_id); +} + +void native_dd::clear_dd_table_ids() { s_dd_table_ids.clear(); } + +void rocksdb_dict_register_dd_table_id(dd::Object_id dd_table_id) { + native_dd::insert_dd_table_ids(dd_table_id); +}; + +} // namespace myrocks diff --git a/storage/rocksdb/rdb_native_dd.h b/storage/rocksdb/rdb_native_dd.h new file mode 100644 index 000000000000..72673689b0c6 --- /dev/null +++ b/storage/rocksdb/rdb_native_dd.h @@ -0,0 +1,47 @@ +/* + Copyright (c) 2023 Meta, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#pragma once + +/* C++ standard header files */ +#include + +/* MySQL header files */ +#include "sql/dd/object_id.h" + +namespace dd { +class Table; +} + +namespace myrocks { + +void rocksdb_dict_register_dd_table_id(dd::Object_id dd_table_id); + +class native_dd { + private: + /* Set of ids of DD tables */ + static std::unordered_set s_dd_table_ids; + + public: + static bool is_dd_table_id(dd::Object_id id); + + static void insert_dd_table_ids(dd::Object_id dd_table_id); + + static void clear_dd_table_ids(); + + static int reject_if_dd_table(const dd::Table *table_def); +}; + +} // namespace myrocks