| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
#include "srv0tmp.h" |
| 29 |
#include <algorithm> |
| 30 |
#include "dict0dict.h" |
| 31 |
#include "ib0mutex.h" |
| 32 |
#include "srv0srv.h" |
| 33 |
#include "srv0start.h" |
| 34 |
|
| 35 |
namespace ibt { |
| 36 |
|
| 37 |
/** The initial size of temporary tablespace pool */ |
| 38 |
const uint32_t INIT_SIZE = 10; |
| 39 |
|
| 40 |
/** The number of tablespaces added to the pool every time the pool is expanded |
| 41 |
*/ |
| 42 |
const uint32_t POOL_EXPAND_SIZE = 10; |
| 43 |
|
| 44 |
/** Thread id for the replication thread */ |
| 45 |
const uint32_t SLAVE_THREAD_ID = UINT32_MAX; |
| 46 |
|
| 47 |
/** Directory to store the session temporary tablespaces. |
| 48 |
Used when user doesn't provide a temporary tablespace dir */ |
| 49 |
static const char DIR_NAME[] = "#innodb_temp"; |
| 50 |
|
| 51 |
/** Filename prefix to identify the session temporary tablespaces. |
| 52 |
They are of pattern temp_*.ibt */ |
| 53 |
static const char PREFIX_NAME[] = "temp_"; |
| 54 |
|
| 55 |
/** Directory name where session temporary tablespaces are stored. |
| 56 |
This location is decided after consulting srv_temp_dir */ |
| 57 |
static std::string temp_tbsp_dir; |
| 58 |
|
| 59 |
/** Tablespace to be used by the replication thread */ |
| 60 |
static Tablespace *rpl_slave_tblsp = nullptr; |
| 61 |
|
| 62 |
Tablespace_pool *tbsp_pool = nullptr; |
| 63 |
|
| 64 |
/* Directory to store session temporary tablespaces, provided by user */ |
| 65 |
char *srv_temp_dir = nullptr; |
| 66 |
|
| 67 |
/** Session Temporary tablespace */ |
| 68 |
Tablespace::Tablespace() |
| 69 |
: m_space_id(++m_last_used_space_id), m_inited(), m_thread_id() { |
| 70 |
ut_ad(m_space_id <= dict_sys_t::s_max_temp_space_id); |
| 71 |
m_purpose = TBSP_NONE; |
| 72 |
} |
| 73 |
|
| 74 |
Tablespace::~Tablespace() { |
| 75 |
if (!m_inited) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
close(); |
| 80 |
|
| 81 |
ut_ad(srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS); |
| 82 |
bool file_pre_exists = false; |
| 83 |
bool success = os_file_delete_if_exists(innodb_temp_file_key, path().c_str(), |
| 84 |
&file_pre_exists); |
| 85 |
|
| 86 |
if (file_pre_exists && !success) { |
| 87 |
ib::error(ER_IB_FAILED_TO_DELETE_TABLESPACE_FILE) |
| 88 |
<< "Failed to delete file " << path(); |
| 89 |
os_file_get_last_error(true); |
| 90 |
ut_d(ut_error); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
dberr_t Tablespace::create() { |
| 95 |
ut_ad(m_space_id > dict_sys_t::s_min_temp_space_id); |
| 96 |
|
| 97 |
/* Create the filespace flags */ |
| 98 |
uint32_t fsp_flags = |
| 99 |
fsp_flags_init(univ_page_size, /* page sizes and a flag if compressed */ |
| 100 |
false, /* needed only for compressed tables */ |
| 101 |
false, /* has DATA_DIR */ |
| 102 |
true, /* is shared */ |
| 103 |
true); /* is temporary */ |
| 104 |
|
| 105 |
dberr_t err = fil_ibt_create(m_space_id, file_name().c_str(), path().c_str(), |
| 106 |
fsp_flags, FIL_IBT_FILE_INITIAL_SIZE); |
| 107 |
|
| 108 |
if (err != DB_SUCCESS) { |
| 109 |
return (err); |
| 110 |
} |
| 111 |
|
| 112 |
m_inited = true; |
| 113 |
|
| 114 |
mtr_t mtr; |
| 115 |
mtr_start(&mtr); |
| 116 |
|
| 117 |
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO); |
| 118 |
|
| 119 |
bool ret = fsp_header_init(m_space_id, FIL_IBT_FILE_INITIAL_SIZE, &mtr); |
| 120 |
mtr_commit(&mtr); |
| 121 |
|
| 122 |
if (!ret) { |
| 123 |
return (DB_ERROR); |
| 124 |
} |
| 125 |
return (DB_SUCCESS); |
| 126 |
} |
| 127 |
|
| 128 |
bool Tablespace::close() const { |
| 129 |
if (!m_inited) { |
| 130 |
return (false); |
| 131 |
} |
| 132 |
|
| 133 |
fil_space_close(m_space_id); |
| 134 |
return (true); |
| 135 |
} |
| 136 |
|
| 137 |
bool Tablespace::truncate() { |
| 138 |
if (!m_inited) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
if (!fil_truncate_tablespace(m_space_id, FIL_IBT_FILE_INITIAL_SIZE)) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
mtr_t mtr; |
| 147 |
|
| 148 |
mtr_start(&mtr); |
| 149 |
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO); |
| 150 |
fsp_header_init(m_space_id, FIL_IBT_FILE_INITIAL_SIZE, &mtr); |
| 151 |
mtr_commit(&mtr); |
| 152 |
|
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
uint32_t Tablespace::file_id() const { |
| 157 |
return (m_space_id - dict_sys_t::s_min_temp_space_id); |
| 158 |
} |
| 159 |
|
| 160 |
std::string Tablespace::file_name() const { |
| 161 |
std::string str(PREFIX_NAME); |
| 162 |
str += std::to_string(file_id()); |
| 163 |
return (str); |
| 164 |
} |
| 165 |
|
| 166 |
std::string Tablespace::path() const { |
| 167 |
std::string str(temp_tbsp_dir); |
| 168 |
str.append(file_name()); |
| 169 |
str.append(DOT_IBT); |
| 170 |
return (str); |
| 171 |
} |
| 172 |
|
| 173 |
/** Space_ids for Session temporary tablespace. The available range is |
| 174 |
from dict_sys_t::s_min_temp_space_id to dict_sys_t::s_max_temp_space_id. |
| 175 |
Total 400K space_ids are reserved for session temporary tablespaces. */ |
| 176 |
space_id_t Tablespace::m_last_used_space_id = dict_sys_t::s_min_temp_space_id; |
| 177 |
|
| 178 |
Tablespace_pool::Tablespace_pool(size_t init_size) |
| 179 |
: m_pool_initialized(), |
| 180 |
m_init_size(init_size), |
| 181 |
m_free(nullptr), |
| 182 |
m_active(nullptr) { |
| 183 |
mutex_create(LATCH_ID_TEMP_POOL_MANAGER, &m_mutex); |
| 184 |
} |
| 185 |
|
| 186 |
Tablespace_pool::~Tablespace_pool() { |
| 187 |
mutex_destroy(&m_mutex); |
| 188 |
for (Tablespace *ts : *m_active) { |
| 189 |
ut::delete_(ts); |
| 190 |
} |
| 191 |
|
| 192 |
for (Tablespace *ts : *m_free) { |
| 193 |
ut::delete_(ts); |
| 194 |
} |
| 195 |
ut::delete_(m_active); |
| 196 |
ut::delete_(m_free); |
| 197 |
} |
| 198 |
|
| 199 |
Tablespace *Tablespace_pool::get(my_thread_id id, enum tbsp_purpose purpose) { |
| 200 |
DBUG_EXECUTE_IF("ibt_pool_exhausted", return (nullptr);); |
| 201 |
|
| 202 |
Tablespace *ts = nullptr; |
| 203 |
acquire(); |
| 204 |
if (m_free->size() == 0) { |
| 205 |
/* Free pool is empty. Add more tablespaces by expanding it */ |
| 206 |
dberr_t err = expand(POOL_EXPAND_SIZE); |
| 207 |
if (err != DB_SUCCESS) { |
| 208 |
/* Failure to expand the pool means that there is no disk space |
| 209 |
available to create .IBT files */ |
| 210 |
release(); |
| 211 |
ib::error(ER_IB_UNABLE_TO_EXPAND_TEMPORARY_TABLESPACE_POOL) |
| 212 |
<< "Unable to expand the temporary tablespace pool"; |
| 213 |
return (nullptr); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
ts = m_free->back(); |
| 218 |
m_free->pop_back(); |
| 219 |
m_active->push_back(ts); |
| 220 |
ts->set_thread_id_and_purpose(id, purpose); |
| 221 |
|
| 222 |
release(); |
| 223 |
return (ts); |
| 224 |
} |
| 225 |
|
| 226 |
void Tablespace_pool::free_ts(Tablespace *ts) { |
| 227 |
space_id_t space_id = ts->space_id(); |
| 228 |
fil_space_t *space = fil_space_get(space_id); |
| 229 |
ut_ad(space != nullptr); |
| 230 |
|
| 231 |
if (space->size != FIL_IBT_FILE_INITIAL_SIZE) { |
| 232 |
ts->truncate(); |
| 233 |
} |
| 234 |
|
| 235 |
acquire(); |
| 236 |
|
| 237 |
Pool::iterator it = std::find(m_active->begin(), m_active->end(), ts); |
| 238 |
if (it != m_active->end()) { |
| 239 |
m_active->erase(it); |
| 240 |
} else { |
| 241 |
ut_d(ut_error); |
| 242 |
} |
| 243 |
|
| 244 |
m_free->push_back(ts); |
| 245 |
|
| 246 |
release(); |
| 247 |
} |
| 248 |
|
| 249 |
dberr_t Tablespace_pool::initialize(bool create_new_db) { |
| 250 |
if (m_pool_initialized) { |
| 251 |
return (DB_SUCCESS); |
| 252 |
} |
| 253 |
|
| 254 |
ut_ad(m_active == nullptr && m_free == nullptr); |
| 255 |
|
| 256 |
m_active = ut::new_withkey<Pool>(UT_NEW_THIS_FILE_PSI_KEY); |
| 257 |
if (m_active == nullptr) { |
| 258 |
return (DB_OUT_OF_MEMORY); |
| 259 |
} |
| 260 |
|
| 261 |
m_free = ut::new_withkey<Pool>(UT_NEW_THIS_FILE_PSI_KEY); |
| 262 |
if (m_free == nullptr) { |
| 263 |
return (DB_OUT_OF_MEMORY); |
| 264 |
} |
| 265 |
|
| 266 |
delete_old_pool(create_new_db); |
| 267 |
dberr_t err = expand(m_init_size); |
| 268 |
if (err != DB_SUCCESS) { |
| 269 |
return (err); |
| 270 |
} |
| 271 |
|
| 272 |
m_pool_initialized = true; |
| 273 |
return (DB_SUCCESS); |
| 274 |
} |
| 275 |
|
| 276 |
dberr_t Tablespace_pool::expand(size_t size) { |
| 277 |
ut_ad(!m_pool_initialized || mutex_own(&m_mutex)); |
| 278 |
for (size_t i = 0; i < size; i++) { |
| 279 |
Tablespace *ts = ut::new_withkey<Tablespace>(UT_NEW_THIS_FILE_PSI_KEY); |
| 280 |
|
| 281 |
if (ts == nullptr) { |
| 282 |
return DB_OUT_OF_MEMORY; |
| 283 |
} |
| 284 |
|
| 285 |
dberr_t err = ts->create(); |
| 286 |
|
| 287 |
if (err == DB_SUCCESS) { |
| 288 |
m_free->push_back(ts); |
| 289 |
} else { |
| 290 |
ut::delete_(ts); |
| 291 |
return err; |
| 292 |
} |
| 293 |
} |
| 294 |
return DB_SUCCESS; |
| 295 |
} |
| 296 |
|
| 297 |
void Tablespace_pool::delete_old_pool(bool create_new_db) { |
| 298 |
if (create_new_db) { |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
ib::info(ER_IB_MSG_SCANNING_TEMP_TABLESPACE_DIR) |
| 303 |
<< "Scanning temp tablespace dir:'" << temp_tbsp_dir << "'"; |
| 304 |
|
| 305 |
os_file_type_t type; |
| 306 |
bool exists = false; |
| 307 |
|
| 308 |
os_file_status(temp_tbsp_dir.c_str(), &exists, &type); |
| 309 |
|
| 310 |
if (!exists) { |
| 311 |
return; |
| 312 |
} else { |
| 313 |
ut_ad(type == OS_FILE_TYPE_DIR); |
| 314 |
} |
| 315 |
|
| 316 |
/* Walk the sub-tree of dir. */ |
| 317 |
Dir_Walker::walk(temp_tbsp_dir.c_str(), false, [&](const std::string &path) { |
| 318 |
/* If it is a file and the suffix matches ".ibt", then delete it */ |
| 319 |
|
| 320 |
if (!Dir_Walker::is_directory(path) && path.size() >= 4 && |
| 321 |
(path.compare(path.length() - 4, 4, DOT_IBT) == 0)) { |
| 322 |
os_file_delete_if_exists(innodb_temp_file_key, path.c_str(), nullptr); |
| 323 |
} |
| 324 |
}); |
| 325 |
} |
| 326 |
|
| 327 |
/** Create the directory holding the temporary pool tablespaces. |
| 328 |
@return DB_SUCCESS in case of success and error if unable to create |
| 329 |
the directory */ |
| 330 |
static dberr_t create_temp_dir() { |
| 331 |
temp_tbsp_dir = srv_temp_dir; |
| 332 |
|
| 333 |
/* Append PATH separator */ |
| 334 |
if ((temp_tbsp_dir.length() != 0) && |
| 335 |
(*temp_tbsp_dir.rbegin() != OS_PATH_SEPARATOR)) { |
| 336 |
temp_tbsp_dir += OS_PATH_SEPARATOR; |
| 337 |
} |
| 338 |
|
| 339 |
Fil_path temp_dir_path(srv_temp_dir); |
| 340 |
|
| 341 |
if (temp_dir_path.is_same_as(MySQL_datadir_path())) { |
| 342 |
/* User didn't pass explicit temp tablespace dir, |
| 343 |
create directory for temp tablespaces under this */ |
| 344 |
temp_tbsp_dir.append(DIR_NAME); |
| 345 |
bool ret = os_file_create_directory(temp_tbsp_dir.c_str(), false); |
| 346 |
if (!ret) { |
| 347 |
ib::error(ER_IB_TMP_TABLESPACE_CANNOT_CREATE_DIRECTORY) |
| 348 |
<< "Cannot create directory: " << temp_tbsp_dir.c_str(); |
| 349 |
return (DB_CANNOT_OPEN_FILE); |
| 350 |
} |
| 351 |
temp_tbsp_dir += OS_PATH_SEPARATOR; |
| 352 |
srv_temp_dir = const_cast<char *>(temp_tbsp_dir.c_str()); |
| 353 |
} else { |
| 354 |
/* Explicit directory passed by user. Assume it exists as the parameter |
| 355 |
innodb_temp_tablespaces_dir has already been validated in |
| 356 |
innodb_init_params, returns appropriate error if the directory could not be |
| 357 |
found */ |
| 358 |
return (DB_SUCCESS); |
| 359 |
} |
| 360 |
|
| 361 |
return (DB_SUCCESS); |
| 362 |
} |
| 363 |
|
| 364 |
dberr_t open_or_create(bool create_new_db) { |
| 365 |
dberr_t err = create_temp_dir(); |
| 366 |
if (err != DB_SUCCESS) { |
| 367 |
return (err); |
| 368 |
} |
| 369 |
|
| 370 |
tbsp_pool = |
| 371 |
ut::new_withkey<Tablespace_pool>(UT_NEW_THIS_FILE_PSI_KEY, INIT_SIZE); |
| 372 |
if (tbsp_pool == nullptr) { |
| 373 |
return (DB_OUT_OF_MEMORY); |
| 374 |
} |
| 375 |
|
| 376 |
err = tbsp_pool->initialize(create_new_db); |
| 377 |
|
| 378 |
return (err); |
| 379 |
} |
| 380 |
|
| 381 |
void free_tmp(Tablespace *ts) { |
| 382 |
ts->reset_thread_id_and_purpose(); |
| 383 |
tbsp_pool->free_ts(ts); |
| 384 |
} |
| 385 |
|
| 386 |
void delete_pool_manager() { ut::delete_(tbsp_pool); } |
| 387 |
|
| 388 |
void close_files() { |
| 389 |
auto close = [&](const ibt::Tablespace *ts) { ts->close(); }; |
| 390 |
|
| 391 |
ibt::tbsp_pool->iterate_tbsp(close); |
| 392 |
} |
| 393 |
|
| 394 |
Tablespace *get_rpl_slave_tblsp() { |
| 395 |
if (rpl_slave_tblsp == nullptr) { |
| 396 |
rpl_slave_tblsp = tbsp_pool->get(SLAVE_THREAD_ID, TBSP_SLAVE); |
| 397 |
} |
| 398 |
return (rpl_slave_tblsp); |
| 399 |
} |
| 400 |
|
| 401 |
} // namespace ibt |