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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
/* Copyright (c) 2015, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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 St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <algorithm>
#include "my_dbug.h" // DBUG_PRINT
#include "my_inttypes.h" // uint32
#include "sql/dd/cache/dictionary_client.h" // dd::Dictionary_client
#include "sql/dd/collection.h" // dd::Collection
#include "sql/dd/dd_tablespace.h" // dd::get_tablespace_name
#include "sql/dd/impl/bootstrap/bootstrap_ctx.h" // dd::bootstrap::SERVER_VERSION_80016
#include "sql/dd/impl/sdi.h" // dd::serialize
#include "sql/dd/impl/sdi_utils.h" // sdi_utils::checked_return
#include "sql/dd/properties.h" // dd::Properties
#include "sql/dd/string_type.h" // dd::String_type
#include "sql/dd/tablespace_id_owner_visitor.h" // dd::visit_tablespace_id_owner
#include "sql/dd/types/index.h" // dd::Index
#include "sql/dd/types/partition.h" // dd::Partition
#include "sql/dd/types/partition_index.h" // dd::Partition_index
#include "sql/dd/types/schema.h" // dd::Schema
#include "sql/dd/types/table.h" // dd::Table
#include "sql/dd/types/tablespace.h" // dd::Tablespace
#include "sql/handler.h" // sdi_set
#include "sql/mdl.h" // MDL_key::TABLESPACE
#include "sql/sql_class.h" // THD
#include "template_utils.h" // ReturnValueOrError
/**
@file
@ingroup sdi
Storage and retrieval of SDIs to/form tablespaces. This allows SDIs
to be stored transactionally.
*/
using namespace dd::sdi_utils;
namespace {
using DC = dd::cache::Dictionary_client;
#ifndef NDEBUG
const char *ge_type(const dd::Table &) { return "TABLE"; }
const char *ge_type(const dd::Index &) { return "INDEX"; }
const char *ge_type(const dd::Partition &) { return "PARTITION"; }
const char *ge_type(const dd::Partition_index &) { return "PARTITION_INDEX"; }
#endif /* NDEBUG */
/**
Traverses Table object with sub objects. Returns when the first
valid (not INVALID_OBJECT_ID) tablespace id is found.
*/
template <typename DDT>
dd::Object_id fetch_first_tablespace_id(const DDT &t) {
DBUG_PRINT("ddsdi", ("fetch_first_tablespace_id(%s)", t.name().c_str()));
dd::Object_id tspid = dd::INVALID_OBJECT_ID;
visit_tablespace_id_owners(t, [&](auto &ge) {
dd::Object_id tid = ge.tablespace_id();
DBUG_PRINT("ddsdi", ("Checking %s '%s'", ge_type(ge), ge.name().c_str()));
if (tid != dd::INVALID_OBJECT_ID) {
DBUG_PRINT("ddsdi", ("Found id:%llu, source:%s", tid, ge_type(ge)));
tspid = tid;
return true; // Returning true to terminate visitation
}
return false;
});
return tspid;
}
/**
Traverses Table object with sub objects. Returns when the
Tablespace object corresponding to the first valid (not
INVALID_OBJECT_ID) tablespace id is found.
*/
ReturnValueOrError<const dd::Tablespace *> fetch_tablespace(
THD *thd, dd::Object_id tsid) {
if (tsid == dd::INVALID_OBJECT_ID) {
return {nullptr, false};
}
// The tablespace object may not have MDL
// Need to use acquire_uncached_uncommitted to get name for MDL
std::unique_ptr<dd::Tablespace> tblspc_;
if (thd->dd_client()->acquire_uncached_uncommitted(tsid, &tblspc_)) {
return {nullptr, true};
}
if (tblspc_ == nullptr) {
// When dropping a table in an implicit tablespace, the
// referenced tablespace may already have been removed. This
// is ok since this means that the sdis in the tablespace
// have been removed also. Note that since tsids is only used
// to check for duplicates, it makes sense to leave tsid
// there and return false so that we can proceed.
return {nullptr, false};
}
if (mdl_lock(thd, MDL_key::TABLESPACE, "", tblspc_->name(),
MDL_INTENTION_EXCLUSIVE)) {
return {nullptr, true};
}
// Re-acquire after getting lock to make sure it is still there...
const dd::Tablespace *tblspc = nullptr;
if (thd->dd_client()->acquire(tsid, &tblspc)) {
return {nullptr, true};
}
return {tblspc, false};
}
} // namespace
namespace dd {
namespace sdi_tablespace {
bool store_tbl_sdi(THD *thd, handlerton *hton, const dd::Sdi_type &sdi,
const dd::Table &table,
const dd::Schema &schema [[maybe_unused]]) {
auto res = fetch_tablespace(thd, fetch_first_tablespace_id(table));
if (res.error) {
return true;
}
if (res.value == nullptr) {
return false;
}
DBUG_PRINT("ddsdi", ("store_sdi_with_schema[](Schema" ENTITY_FMT
", Table" ENTITY_FMT ")",
ENTITY_VAL(schema), ENTITY_VAL(table)));
sdi_key_t key = {SDI_TYPE_TABLE, table.id()};
if (hton->sdi_set(hton, *res.value, &table, &key, sdi.c_str(),
sdi.length())) {
return checked_return(true);
}
DBUG_PRINT("ddsdi", ("Successful return from hton->sdi_set() with "
"sdi=\n%s\nStored in Tablespace (" ENTITY_FMT ")",
sdi.c_str(), ENTITY_VAL(*res.value)));
return false;
}
bool store_tsp_sdi(handlerton *hton, const Sdi_type &sdi,
const Tablespace &tblspc) {
DBUG_PRINT("ddsdi", ("store_tsp_sdi(" ENTITY_FMT ")", ENTITY_VAL(tblspc)));
sdi_key_t key = {SDI_TYPE_TABLESPACE, tblspc.id()};
if (hton->sdi_set(hton, tblspc, nullptr, &key, sdi.c_str(), sdi.length())) {
return checked_return(true);
}
return false;
}
bool drop_tbl_sdi(THD *thd, const handlerton &hton, const Table &table,
const Schema &schema [[maybe_unused]]) {
DBUG_PRINT("ddsdi",
("drop_tbl_sdi(Schema" ENTITY_FMT ", Table" ENTITY_FMT ")",
ENTITY_VAL(schema), ENTITY_VAL(table)));
auto res = fetch_tablespace(thd, fetch_first_tablespace_id(table));
if (res.error) {
return true;
}
if (res.value == nullptr) {
return false;
}
sdi_key_t key = {SDI_TYPE_TABLE, table.id()};
if (table.subpartition_type() == Table::ST_NONE ||
table.last_checked_for_upgrade_version_id() >
dd::bootstrap::SERVER_VERSION_80016) {
return checked_return(hton.sdi_delete(*res.value, &table, &key));
}
// Sub-partitioned tables from older versions which have not yet
// been checked for upgrade may not have an SDI record in the SDI
// index in the tablespace, so we need to install an error handler which
// catches the resulting error.
bool error_suppressed = false;
if (sdi_utils::handle_errors(
thd,
[&](uint errnum, const char *,
Sql_condition::enum_severity_level *level, const char *) {
if (errnum == ER_SDI_OPERATION_FAILED_MISSING_RECORD) {
(*level) = Sql_condition::SL_WARNING;
error_suppressed = true;
}
return false;
},
[&]() { return hton.sdi_delete(*res.value, &table, &key); })) {
return checked_return(!error_suppressed);
}
return false;
}
template <typename DDT>
bool drop_all_sdi_impl(THD *thd, const handlerton &hton, const DDT &t) {
return visit_tablespace_id_owners(t, [&](auto &tsp_id_owner) {
dd::Object_id tid = tsp_id_owner.tablespace_id();
DBUG_PRINT("ddsdi", ("Checking %s '%s'", ge_type(tsp_id_owner),
tsp_id_owner.name().c_str()));
if (tid == dd::INVALID_OBJECT_ID) {
return false;
}
DBUG_PRINT("ddsdi", ("Proceeding with id:%llu, source:%s", tid,
ge_type(tsp_id_owner)));
auto res = fetch_tablespace(thd, tid);
if (res.error) {
return true;
}
if (res.value == nullptr) {
return false;
}
if (res.value->se_private_data().exists("discarded")) {
my_error(ER_TABLESPACE_DISCARDED, MYF(0), t.name().c_str());
return true;
}
sdi_vector_t sdi_keys;
if (hton.sdi_get_keys(*res.value, sdi_keys)) {
return checked_return(true);
}
for (const auto &k : sdi_keys.m_vec) {
if (k.type == SDI_TYPE_TABLESPACE && k.id == res.value->id()) {
continue;
}
if (hton.sdi_delete(*res.value, nullptr, &k)) {
return checked_return(true);
}
}
return false;
}); // visitor lambda
}
bool drop_all_sdi(THD *thd, const handlerton &hton, const Table &t) {
return drop_all_sdi_impl(thd, hton, t);
}
bool drop_all_sdi(THD *thd, const handlerton &hton, const Partition &p) {
return drop_all_sdi_impl(thd, hton, p);
}
} // namespace sdi_tablespace
} // namespace dd
|