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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/*****************************************************************************
Copyright (c) 2018, 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 "srv0tmp.h"
#include <algorithm>
#include "dict0dict.h"
#include "ib0mutex.h"
#include "srv0srv.h"
#include "srv0start.h"
namespace ibt {
/** The initial size of temporary tablespace pool */
const uint32_t INIT_SIZE = 10;
/** The number of tablespaces added to the pool every time the pool is expanded
*/
const uint32_t POOL_EXPAND_SIZE = 10;
/** Thread id for the replication thread */
const uint32_t SLAVE_THREAD_ID = UINT32_MAX;
/** Directory to store the session temporary tablespaces.
Used when user doesn't provide a temporary tablespace dir */
static const char DIR_NAME[] = "#innodb_temp";
/** Filename prefix to identify the session temporary tablespaces.
They are of pattern temp_*.ibt */
static const char PREFIX_NAME[] = "temp_";
/** Directory name where session temporary tablespaces are stored.
This location is decided after consulting srv_temp_dir */
static std::string temp_tbsp_dir;
/** Tablespace to be used by the replication thread */
static Tablespace *rpl_slave_tblsp = nullptr;
Tablespace_pool *tbsp_pool = nullptr;
/* Directory to store session temporary tablespaces, provided by user */
char *srv_temp_dir = nullptr;
/** Session Temporary tablespace */
Tablespace::Tablespace()
: m_space_id(++m_last_used_space_id), m_inited(), m_thread_id() {
ut_ad(m_space_id <= dict_sys_t::s_max_temp_space_id);
m_purpose = TBSP_NONE;
}
Tablespace::~Tablespace() {
if (!m_inited) {
return;
}
close();
ut_ad(srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS);
bool file_pre_exists = false;
bool success = os_file_delete_if_exists(innodb_temp_file_key, path().c_str(),
&file_pre_exists);
if (file_pre_exists && !success) {
ib::error(ER_IB_FAILED_TO_DELETE_TABLESPACE_FILE)
<< "Failed to delete file " << path();
os_file_get_last_error(true);
ut_d(ut_error);
}
}
dberr_t Tablespace::create() {
ut_ad(m_space_id > dict_sys_t::s_min_temp_space_id);
/* Create the filespace flags */
uint32_t fsp_flags =
fsp_flags_init(univ_page_size, /* page sizes and a flag if compressed */
false, /* needed only for compressed tables */
false, /* has DATA_DIR */
true, /* is shared */
true); /* is temporary */
dberr_t err = fil_ibt_create(m_space_id, file_name().c_str(), path().c_str(),
fsp_flags, FIL_IBT_FILE_INITIAL_SIZE);
if (err != DB_SUCCESS) {
return (err);
}
m_inited = true;
mtr_t mtr;
mtr_start(&mtr);
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO);
bool ret = fsp_header_init(m_space_id, FIL_IBT_FILE_INITIAL_SIZE, &mtr);
mtr_commit(&mtr);
if (!ret) {
return (DB_ERROR);
}
return (DB_SUCCESS);
}
bool Tablespace::close() const {
if (!m_inited) {
return (false);
}
fil_space_close(m_space_id);
return (true);
}
bool Tablespace::truncate() {
if (!m_inited) {
return false;
}
if (!fil_truncate_tablespace(m_space_id, FIL_IBT_FILE_INITIAL_SIZE)) {
return false;
}
mtr_t mtr;
mtr_start(&mtr);
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO);
fsp_header_init(m_space_id, FIL_IBT_FILE_INITIAL_SIZE, &mtr);
mtr_commit(&mtr);
return true;
}
uint32_t Tablespace::file_id() const {
return (m_space_id - dict_sys_t::s_min_temp_space_id);
}
std::string Tablespace::file_name() const {
std::string str(PREFIX_NAME);
str += std::to_string(file_id());
return (str);
}
std::string Tablespace::path() const {
std::string str(temp_tbsp_dir);
str.append(file_name());
str.append(DOT_IBT);
return (str);
}
/** Space_ids for Session temporary tablespace. The available range is
from dict_sys_t::s_min_temp_space_id to dict_sys_t::s_max_temp_space_id.
Total 400K space_ids are reserved for session temporary tablespaces. */
space_id_t Tablespace::m_last_used_space_id = dict_sys_t::s_min_temp_space_id;
Tablespace_pool::Tablespace_pool(size_t init_size)
: m_pool_initialized(),
m_init_size(init_size),
m_free(nullptr),
m_active(nullptr) {
mutex_create(LATCH_ID_TEMP_POOL_MANAGER, &m_mutex);
}
Tablespace_pool::~Tablespace_pool() {
mutex_destroy(&m_mutex);
for (Tablespace *ts : *m_active) {
ut::delete_(ts);
}
for (Tablespace *ts : *m_free) {
ut::delete_(ts);
}
ut::delete_(m_active);
ut::delete_(m_free);
}
Tablespace *Tablespace_pool::get(my_thread_id id, enum tbsp_purpose purpose) {
DBUG_EXECUTE_IF("ibt_pool_exhausted", return (nullptr););
Tablespace *ts = nullptr;
acquire();
if (m_free->size() == 0) {
/* Free pool is empty. Add more tablespaces by expanding it */
dberr_t err = expand(POOL_EXPAND_SIZE);
if (err != DB_SUCCESS) {
/* Failure to expand the pool means that there is no disk space
available to create .IBT files */
release();
ib::error(ER_IB_UNABLE_TO_EXPAND_TEMPORARY_TABLESPACE_POOL)
<< "Unable to expand the temporary tablespace pool";
return (nullptr);
}
}
ts = m_free->back();
m_free->pop_back();
m_active->push_back(ts);
ts->set_thread_id_and_purpose(id, purpose);
release();
return (ts);
}
void Tablespace_pool::free_ts(Tablespace *ts) {
space_id_t space_id = ts->space_id();
fil_space_t *space = fil_space_get(space_id);
ut_ad(space != nullptr);
if (space->size != FIL_IBT_FILE_INITIAL_SIZE) {
ts->truncate();
}
acquire();
Pool::iterator it = std::find(m_active->begin(), m_active->end(), ts);
if (it != m_active->end()) {
m_active->erase(it);
} else {
ut_d(ut_error);
}
m_free->push_back(ts);
release();
}
dberr_t Tablespace_pool::initialize(bool create_new_db) {
if (m_pool_initialized) {
return (DB_SUCCESS);
}
ut_ad(m_active == nullptr && m_free == nullptr);
m_active = ut::new_withkey<Pool>(UT_NEW_THIS_FILE_PSI_KEY);
if (m_active == nullptr) {
return (DB_OUT_OF_MEMORY);
}
m_free = ut::new_withkey<Pool>(UT_NEW_THIS_FILE_PSI_KEY);
if (m_free == nullptr) {
return (DB_OUT_OF_MEMORY);
}
delete_old_pool(create_new_db);
dberr_t err = expand(m_init_size);
if (err != DB_SUCCESS) {
return (err);
}
m_pool_initialized = true;
return (DB_SUCCESS);
}
dberr_t Tablespace_pool::expand(size_t size) {
ut_ad(!m_pool_initialized || mutex_own(&m_mutex));
for (size_t i = 0; i < size; i++) {
Tablespace *ts = ut::new_withkey<Tablespace>(UT_NEW_THIS_FILE_PSI_KEY);
if (ts == nullptr) {
return DB_OUT_OF_MEMORY;
}
dberr_t err = ts->create();
if (err == DB_SUCCESS) {
m_free->push_back(ts);
} else {
ut::delete_(ts);
return err;
}
}
return DB_SUCCESS;
}
void Tablespace_pool::delete_old_pool(bool create_new_db) {
if (create_new_db) {
return;
}
ib::info(ER_IB_MSG_SCANNING_TEMP_TABLESPACE_DIR)
<< "Scanning temp tablespace dir:'" << temp_tbsp_dir << "'";
os_file_type_t type;
bool exists = false;
os_file_status(temp_tbsp_dir.c_str(), &exists, &type);
if (!exists) {
return;
} else {
ut_ad(type == OS_FILE_TYPE_DIR);
}
/* Walk the sub-tree of dir. */
Dir_Walker::walk(temp_tbsp_dir.c_str(), false, [&](const std::string &path) {
/* If it is a file and the suffix matches ".ibt", then delete it */
if (!Dir_Walker::is_directory(path) && path.size() >= 4 &&
(path.compare(path.length() - 4, 4, DOT_IBT) == 0)) {
os_file_delete_if_exists(innodb_temp_file_key, path.c_str(), nullptr);
}
});
}
/** Create the directory holding the temporary pool tablespaces.
@return DB_SUCCESS in case of success and error if unable to create
the directory */
static dberr_t create_temp_dir() {
temp_tbsp_dir = srv_temp_dir;
/* Append PATH separator */
if ((temp_tbsp_dir.length() != 0) &&
(*temp_tbsp_dir.rbegin() != OS_PATH_SEPARATOR)) {
temp_tbsp_dir += OS_PATH_SEPARATOR;
}
Fil_path temp_dir_path(srv_temp_dir);
if (temp_dir_path.is_same_as(MySQL_datadir_path())) {
/* User didn't pass explicit temp tablespace dir,
create directory for temp tablespaces under this */
temp_tbsp_dir.append(DIR_NAME);
bool ret = os_file_create_directory(temp_tbsp_dir.c_str(), false);
if (!ret) {
ib::error(ER_IB_TMP_TABLESPACE_CANNOT_CREATE_DIRECTORY)
<< "Cannot create directory: " << temp_tbsp_dir.c_str();
return (DB_CANNOT_OPEN_FILE);
}
temp_tbsp_dir += OS_PATH_SEPARATOR;
srv_temp_dir = const_cast<char *>(temp_tbsp_dir.c_str());
} else {
/* Explicit directory passed by user. Assume it exists as the parameter
innodb_temp_tablespaces_dir has already been validated in
innodb_init_params, returns appropriate error if the directory could not be
found */
return (DB_SUCCESS);
}
return (DB_SUCCESS);
}
dberr_t open_or_create(bool create_new_db) {
dberr_t err = create_temp_dir();
if (err != DB_SUCCESS) {
return (err);
}
tbsp_pool =
ut::new_withkey<Tablespace_pool>(UT_NEW_THIS_FILE_PSI_KEY, INIT_SIZE);
if (tbsp_pool == nullptr) {
return (DB_OUT_OF_MEMORY);
}
err = tbsp_pool->initialize(create_new_db);
return (err);
}
void free_tmp(Tablespace *ts) {
ts->reset_thread_id_and_purpose();
tbsp_pool->free_ts(ts);
}
void delete_pool_manager() { ut::delete_(tbsp_pool); }
void close_files() {
auto close = [&](const ibt::Tablespace *ts) { ts->close(); };
ibt::tbsp_pool->iterate_tbsp(close);
}
Tablespace *get_rpl_slave_tblsp() {
if (rpl_slave_tblsp == nullptr) {
rpl_slave_tblsp = tbsp_pool->get(SLAVE_THREAD_ID, TBSP_SLAVE);
}
return (rpl_slave_tblsp);
}
} // namespace ibt
|