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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/* 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 "sql/dd/impl/cache/shared_dictionary_cache.h"
#include <assert.h>
#include <atomic>
#include "sql/dd/impl/cache/shared_multi_map.h"
#include "sql/dd/impl/cache/storage_adapter.h" // Storage_adapter
#include "sql/mysqld.h"
#include "sql/sql_class.h" // THD::is_error()
namespace dd {
namespace cache {
template <typename T>
class Cache_element;
Shared_dictionary_cache *Shared_dictionary_cache::instance() {
static Shared_dictionary_cache s_cache;
return &s_cache;
}
void Shared_dictionary_cache::init() {
instance()->m_map<Collation>()->set_capacity(collation_capacity);
instance()->m_map<Charset>()->set_capacity(charset_capacity);
// Set capacity to have room for all connections to leave an element
// unused in the cache to avoid frequent cache misses while e.g.
// opening a table.
instance()->m_map<Abstract_table>()->set_capacity(max_connections);
instance()->m_map<Event>()->set_capacity(event_capacity);
instance()->m_map<Routine>()->set_capacity(stored_program_def_size);
instance()->m_map<Schema>()->set_capacity(schema_def_size);
instance()->m_map<Column_statistics>()->set_capacity(
column_statistics_capacity);
instance()->m_map<Spatial_reference_system>()->set_capacity(
spatial_reference_system_capacity);
instance()->m_map<Tablespace>()->set_capacity(tablespace_def_size);
instance()->m_map<Resource_group>()->set_capacity(resource_group_capacity);
}
void Shared_dictionary_cache::shutdown() {
instance()->m_map<Abstract_table>()->shutdown();
instance()->m_map<Collation>()->shutdown();
instance()->m_map<Column_statistics>()->shutdown();
instance()->m_map<Charset>()->shutdown();
instance()->m_map<Event>()->shutdown();
instance()->m_map<Routine>()->shutdown();
instance()->m_map<Schema>()->shutdown();
instance()->m_map<Spatial_reference_system>()->shutdown();
instance()->m_map<Tablespace>()->shutdown();
instance()->m_map<Resource_group>()->shutdown();
}
// Don't call this function anywhere except upgrade scenario.
void Shared_dictionary_cache::reset(bool keep_dd_entities) {
shutdown();
if (!keep_dd_entities) Storage_adapter::instance()->erase_all();
init();
}
// Workaround to be used during recovery at server restart.
bool Shared_dictionary_cache::reset_tables_and_tablespaces(THD *thd) {
return (instance()->m_map<Abstract_table>()->reset(thd) ||
instance()->m_map<Tablespace>()->reset(thd));
}
// Get an element from the cache, given the key.
template <typename K, typename T>
bool Shared_dictionary_cache::get(THD *thd, const K &key,
Cache_element<T> **element) {
bool error = false;
assert(element);
if (m_map<T>()->get(key, element)) {
// Handle cache miss.
const T *new_object = nullptr;
error = get_uncached(thd, key, ISO_READ_COMMITTED, &new_object);
// Add the new object, and assign the output element, even in the case of
// a miss error (needed to remove the missed key).
m_map<T>()->put(&key, new_object, element);
}
return error;
}
// Read an object directly from disk, given the key.
template <typename K, typename T>
bool Shared_dictionary_cache::get_uncached(THD *thd, const K &key,
enum_tx_isolation isolation,
const T **object) const {
assert(object);
bool error = Storage_adapter::get(thd, key, isolation, false, object);
assert(!error || thd->is_system_thread() || thd->killed || thd->is_error());
return error;
}
// Add an object to the shared cache.
template <typename T>
void Shared_dictionary_cache::put(const T *object, Cache_element<T> **element) {
assert(object);
// Cast needed to help the compiler choose the correct template instance..
m_map<T>()->put(static_cast<const typename T::Id_key *>(nullptr), object,
element);
}
// Explicitly instantiate the types for the various usages.
template bool
Shared_dictionary_cache::get<Abstract_table::Id_key, Abstract_table>(
THD *thd, const Abstract_table::Id_key &, Cache_element<Abstract_table> **);
template bool
Shared_dictionary_cache::get<Abstract_table::Name_key, Abstract_table>(
THD *thd, const Abstract_table::Name_key &,
Cache_element<Abstract_table> **);
template bool
Shared_dictionary_cache::get<Abstract_table::Aux_key, Abstract_table>(
THD *thd, const Abstract_table::Aux_key &,
Cache_element<Abstract_table> **);
template bool
Shared_dictionary_cache::get_uncached<Abstract_table::Id_key, Abstract_table>(
THD *thd, const Abstract_table::Id_key &, enum_tx_isolation,
const Abstract_table **) const;
template bool
Shared_dictionary_cache::get_uncached<Abstract_table::Name_key, Abstract_table>(
THD *thd, const Abstract_table::Name_key &, enum_tx_isolation,
const Abstract_table **) const;
template bool
Shared_dictionary_cache::get_uncached<Abstract_table::Aux_key, Abstract_table>(
THD *thd, const Abstract_table::Aux_key &, enum_tx_isolation,
const Abstract_table **) const;
template void Shared_dictionary_cache::put<Abstract_table>(
const Abstract_table *, Cache_element<Abstract_table> **);
template bool Shared_dictionary_cache::get<Charset::Id_key, Charset>(
THD *thd, const Charset::Id_key &, Cache_element<Charset> **);
template bool Shared_dictionary_cache::get<Charset::Name_key, Charset>(
THD *thd, const Charset::Name_key &, Cache_element<Charset> **);
template bool Shared_dictionary_cache::get<Charset::Aux_key, Charset>(
THD *thd, const Charset::Aux_key &, Cache_element<Charset> **);
template bool Shared_dictionary_cache::get_uncached<Charset::Id_key, Charset>(
THD *thd, const Charset::Id_key &, enum_tx_isolation,
const Charset **) const;
template bool Shared_dictionary_cache::get_uncached<Charset::Name_key, Charset>(
THD *thd, const Charset::Name_key &, enum_tx_isolation,
const Charset **) const;
template bool Shared_dictionary_cache::get_uncached<Charset::Aux_key, Charset>(
THD *thd, const Charset::Aux_key &, enum_tx_isolation,
const Charset **) const;
template void Shared_dictionary_cache::put<Charset>(const Charset *,
Cache_element<Charset> **);
template bool Shared_dictionary_cache::get<Collation::Id_key, Collation>(
THD *thd, const Collation::Id_key &, Cache_element<Collation> **);
template bool Shared_dictionary_cache::get<Collation::Name_key, Collation>(
THD *thd, const Collation::Name_key &, Cache_element<Collation> **);
template bool Shared_dictionary_cache::get<Collation::Aux_key, Collation>(
THD *thd, const Collation::Aux_key &, Cache_element<Collation> **);
template bool Shared_dictionary_cache::get_uncached<
Collation::Id_key, Collation>(THD *thd, const Collation::Id_key &,
enum_tx_isolation, const Collation **) const;
template bool
Shared_dictionary_cache::get_uncached<Collation::Name_key, Collation>(
THD *thd, const Collation::Name_key &, enum_tx_isolation,
const Collation **) const;
template bool Shared_dictionary_cache::get_uncached<
Collation::Aux_key, Collation>(THD *thd, const Collation::Aux_key &,
enum_tx_isolation, const Collation **) const;
template void Shared_dictionary_cache::put<Collation>(
const Collation *, Cache_element<Collation> **);
template bool Shared_dictionary_cache::get<Event::Id_key, Event>(
THD *thd, const Event::Id_key &, Cache_element<Event> **);
template bool Shared_dictionary_cache::get<Event::Name_key, Event>(
THD *thd, const Event::Name_key &, Cache_element<Event> **);
template bool Shared_dictionary_cache::get<Event::Aux_key, Event>(
THD *thd, const Event::Aux_key &, Cache_element<Event> **);
template bool Shared_dictionary_cache::get_uncached<Event::Id_key, Event>(
THD *thd, const Event::Id_key &, enum_tx_isolation, const Event **) const;
template bool Shared_dictionary_cache::get_uncached<Event::Name_key, Event>(
THD *thd, const Event::Name_key &, enum_tx_isolation, const Event **) const;
template bool Shared_dictionary_cache::get_uncached<Event::Aux_key, Event>(
THD *thd, const Event::Aux_key &, enum_tx_isolation, const Event **) const;
template void Shared_dictionary_cache::put<Event>(const Event *,
Cache_element<Event> **);
template bool Shared_dictionary_cache::get<Routine::Id_key, Routine>(
THD *thd, const Routine::Id_key &, Cache_element<Routine> **);
template bool Shared_dictionary_cache::get<Routine::Name_key, Routine>(
THD *thd, const Routine::Name_key &, Cache_element<Routine> **);
template bool Shared_dictionary_cache::get<Routine::Aux_key, Routine>(
THD *thd, const Routine::Aux_key &, Cache_element<Routine> **);
template bool Shared_dictionary_cache::get_uncached<Routine::Id_key, Routine>(
THD *thd, const Routine::Id_key &, enum_tx_isolation,
const Routine **) const;
template bool Shared_dictionary_cache::get_uncached<Routine::Name_key, Routine>(
THD *thd, const Routine::Name_key &, enum_tx_isolation,
const Routine **) const;
template bool Shared_dictionary_cache::get_uncached<Routine::Aux_key, Routine>(
THD *thd, const Routine::Aux_key &, enum_tx_isolation,
const Routine **) const;
template void Shared_dictionary_cache::put<Routine>(const Routine *,
Cache_element<Routine> **);
template bool Shared_dictionary_cache::get<Schema::Id_key, Schema>(
THD *thd, const Schema::Id_key &, Cache_element<Schema> **);
template bool Shared_dictionary_cache::get<Schema::Name_key, Schema>(
THD *thd, const Schema::Name_key &, Cache_element<Schema> **);
template bool Shared_dictionary_cache::get<Schema::Aux_key, Schema>(
THD *thd, const Schema::Aux_key &, Cache_element<Schema> **);
template bool Shared_dictionary_cache::get_uncached<Schema::Id_key, Schema>(
THD *thd, const Schema::Id_key &, enum_tx_isolation, const Schema **) const;
template bool Shared_dictionary_cache::get_uncached<Schema::Name_key, Schema>(
THD *thd, const Schema::Name_key &, enum_tx_isolation,
const Schema **) const;
template bool Shared_dictionary_cache::get_uncached<Schema::Aux_key, Schema>(
THD *thd, const Schema::Aux_key &, enum_tx_isolation,
const Schema **) const;
template void Shared_dictionary_cache::put<Schema>(const Schema *,
Cache_element<Schema> **);
template bool Shared_dictionary_cache::get<Spatial_reference_system::Id_key,
Spatial_reference_system>(
THD *thd, const Spatial_reference_system::Id_key &,
Cache_element<Spatial_reference_system> **);
template bool Shared_dictionary_cache::get<Spatial_reference_system::Name_key,
Spatial_reference_system>(
THD *thd, const Spatial_reference_system::Name_key &,
Cache_element<Spatial_reference_system> **);
template bool Shared_dictionary_cache::get<Spatial_reference_system::Aux_key,
Spatial_reference_system>(
THD *thd, const Spatial_reference_system::Aux_key &,
Cache_element<Spatial_reference_system> **);
template bool Shared_dictionary_cache::get_uncached<
Spatial_reference_system::Id_key, Spatial_reference_system>(
THD *thd, const Spatial_reference_system::Id_key &, enum_tx_isolation,
const Spatial_reference_system **) const;
template bool Shared_dictionary_cache::get_uncached<
Spatial_reference_system::Name_key, Spatial_reference_system>(
THD *thd, const Spatial_reference_system::Name_key &, enum_tx_isolation,
const Spatial_reference_system **) const;
template bool Shared_dictionary_cache::get_uncached<
Spatial_reference_system::Aux_key, Spatial_reference_system>(
THD *thd, const Spatial_reference_system::Aux_key &, enum_tx_isolation,
const Spatial_reference_system **) const;
template void Shared_dictionary_cache::put<Spatial_reference_system>(
const Spatial_reference_system *,
Cache_element<Spatial_reference_system> **);
template bool
Shared_dictionary_cache::get<Column_statistics::Id_key, Column_statistics>(
THD *thd, const Column_statistics::Id_key &,
Cache_element<Column_statistics> **);
template bool
Shared_dictionary_cache::get<Column_statistics::Name_key, Column_statistics>(
THD *thd, const Column_statistics::Name_key &,
Cache_element<Column_statistics> **);
template bool
Shared_dictionary_cache::get<Column_statistics::Aux_key, Column_statistics>(
THD *thd, const Column_statistics::Aux_key &,
Cache_element<Column_statistics> **);
template bool Shared_dictionary_cache::get_uncached<Column_statistics::Id_key,
Column_statistics>(
THD *thd, const Column_statistics::Id_key &, enum_tx_isolation,
const Column_statistics **) const;
template bool Shared_dictionary_cache::get_uncached<Column_statistics::Name_key,
Column_statistics>(
THD *thd, const Column_statistics::Name_key &, enum_tx_isolation,
const Column_statistics **) const;
template bool Shared_dictionary_cache::get_uncached<Column_statistics::Aux_key,
Column_statistics>(
THD *thd, const Column_statistics::Aux_key &, enum_tx_isolation,
const Column_statistics **) const;
template void Shared_dictionary_cache::put<Column_statistics>(
const Column_statistics *, Cache_element<Column_statistics> **);
template bool Shared_dictionary_cache::get<Tablespace::Id_key, Tablespace>(
THD *thd, const Tablespace::Id_key &, Cache_element<Tablespace> **);
template bool Shared_dictionary_cache::get<Tablespace::Name_key, Tablespace>(
THD *thd, const Tablespace::Name_key &, Cache_element<Tablespace> **);
template bool Shared_dictionary_cache::get<Tablespace::Aux_key, Tablespace>(
THD *thd, const Tablespace::Aux_key &, Cache_element<Tablespace> **);
template bool
Shared_dictionary_cache::get_uncached<Tablespace::Id_key, Tablespace>(
THD *thd, const Tablespace::Id_key &, enum_tx_isolation,
const Tablespace **) const;
template bool
Shared_dictionary_cache::get_uncached<Tablespace::Name_key, Tablespace>(
THD *thd, const Tablespace::Name_key &, enum_tx_isolation,
const Tablespace **) const;
template bool
Shared_dictionary_cache::get_uncached<Tablespace::Aux_key, Tablespace>(
THD *thd, const Tablespace::Aux_key &, enum_tx_isolation,
const Tablespace **) const;
template void Shared_dictionary_cache::put<Tablespace>(
const Tablespace *, Cache_element<Tablespace> **);
template bool
Shared_dictionary_cache::get<Resource_group::Id_key, Resource_group>(
THD *thd, const Resource_group::Id_key &, Cache_element<Resource_group> **);
template bool
Shared_dictionary_cache::get<Resource_group::Name_key, Resource_group>(
THD *thd, const Resource_group::Name_key &,
Cache_element<Resource_group> **);
template bool
Shared_dictionary_cache::get<Resource_group::Aux_key, Resource_group>(
THD *thd, const Resource_group::Aux_key &,
Cache_element<Resource_group> **);
template bool
Shared_dictionary_cache::get_uncached<Resource_group::Id_key, Resource_group>(
THD *thd, const Resource_group::Id_key &, enum_tx_isolation,
const Resource_group **) const;
template bool
Shared_dictionary_cache::get_uncached<Resource_group::Name_key, Resource_group>(
THD *thd, const Resource_group::Name_key &, enum_tx_isolation,
const Resource_group **) const;
template bool
Shared_dictionary_cache::get_uncached<Resource_group::Aux_key, Resource_group>(
THD *thd, const Resource_group::Aux_key &, enum_tx_isolation,
const Resource_group **) const;
template void Shared_dictionary_cache::put<Resource_group>(
const Resource_group *, Cache_element<Resource_group> **);
} // namespace cache
} // namespace dd
|