MySQL 8.0.40
Source Code Documentation
srv0tmp.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2018, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28#ifndef srv0tmp_h
29#define srv0tmp_h
30#include "srv0srv.h"
31namespace ibt {
32
33/** Purpose for using tablespace */
35 TBSP_NONE = 0, /*!< Tablespace is not being used for any
36 temporary table */
37 TBSP_USER, /*!< Tablespace is used for user temporary
38 tables */
39 TBSP_INTRINSIC, /*!< Tablespace is used for intrinsic
40 tables */
41 TBSP_SLAVE /*!< Tablespace is used by the slave node
42 in a replication setup */
43};
44/** Create the session temporary tablespaces on startup
45@param[in] create_new_db true if bootstrapping
46@return DB_SUCCESS on success, else DB_ERROR on failure */
47dberr_t open_or_create(bool create_new_db);
48
49/** Session Temporary tablespace */
51 public:
52 Tablespace();
53
55
56 /** Create the .IBT file with pattern "temp_*.ibt"
57 @return DB_SUCCESS on success */
59
60 /** Close the .ibt file. Doesn't delete the tablespace
61 @return true on success, false on failure */
62 bool close() const;
63
64 /** Truncate the tablespace
65 @return true on success, false on failure */
66 bool truncate();
67
68 /** comparator for two tablespace objects
69 @return true if space_ids are same, else false */
70 bool operator==(const Tablespace &other) {
71 return (this->m_space_id == other.m_space_id);
72 }
73
74 /** @return Return the space_id of the tablespace */
75 space_id_t space_id() const { return (m_space_id); }
76
77 /** Set the thread id of the thread and the purpose of using the tablespace
78 @param[in] thread_id thread id of the thread requesting the
79 tablespace
80 @param[in] purpose purpose for requesting the tablespace */
82 enum tbsp_purpose purpose) {
83 ut_ad(m_thread_id == 0);
86 }
87
88 /** Reset the thread id while returning the tablespace to the pool */
90 ut_ad(m_thread_id != 0);
91 m_thread_id = 0;
93 }
94
95 /** @return thread id of the thread using the tablespace */
96 my_thread_id thread_id() const { return (m_thread_id); }
97
98 /** @return purpose for which the tablespace is being used */
99 enum tbsp_purpose purpose() const { return (m_purpose); }
100
101 /** @return complete path including filename */
102 std::string path() const;
103
104 private:
105 /** The id used for name on disk temp_1.ibt, temp_2.ibt, etc
106 @return the offset based on s_min_temp_space_id. The minimum offset is 1 */
107 uint32_t file_id() const;
108
109 /** @return the file_name only excluding the path */
110 std::string file_name() const;
111
112 /** space_id of the current tablespace */
114
115 /** Next available space_id for tablespace. These are
116 hardcoded space_ids at higher range */
118
119 /** True only after .ibt file is created */
121
122 /** Tablespace belongs to this Session id */
124
125 /** Purpose for this tablespace */
127};
128
129/** Pool of session temporary tablespaces. Each session gets at max two
130tablespaces. For a session, we allocate one tablespace on the creation of
131first intrinsic table and another on the creation of first user temporary
132table (CREATE TEMPORARY TABLE t1). These tablespaces are private to session.
133No other session can use them when a tablespace is in-use by the session.
134
135Once a session disconnects, the tablespaces are truncated and released
136to the pool. */
138 public:
139 using Pool = std::list<Tablespace *, ut::allocator<Tablespace *>>;
140
141 /** Tablespace_pool constructor
142 @param[in] init_size Initial size of the tablespace pool */
143 Tablespace_pool(size_t init_size);
144
145 /** Destructor */
147
148 /** Return a session temporary tablespace. If
149 pool is exhausted, expand and return one
150 @param[in] id session id
151 @param[in] purpose purpose of using the tablespace
152 @return Handle to the tablespace */
153 Tablespace *get(my_thread_id id, enum tbsp_purpose purpose);
154
155 /** Truncate and release the tablespace back to the pool
156 @param[in] ts tablespace that need to be released back to the pool */
157 void free_ts(Tablespace *ts);
158
159 /** Initialize the pool on startup. Also delete
160 old tablespaces if found
161 @param[in] create_new_db true if the database is being created,
162 false otherwise
163 @return DB_SUCCESS on success, else DB_ERROR on failure */
164 dberr_t initialize(bool create_new_db);
165
166 /** Iterate through the list of tablespaces and perform specified operation
167 on the tablespace on every iteration.
168 @param[in] f Function pointer for the function to be
169 executed on every iteration */
170 template <typename F>
171 void iterate_tbsp(F &&f) {
172 acquire();
173
174 std::for_each(begin(*m_active), end(*m_active), f);
175 std::for_each(begin(*m_free), end(*m_free), f);
176
177 release();
178 }
179
180 /** Gets current pool size.
181 @return Number of tablespaces in the pool, both active and free ones. */
182 size_t get_size() {
183 acquire();
184 size_t current_size = m_active->size() + m_free->size();
185 release();
186 return current_size;
187 }
188
189 private:
190 /** Acquire the mutex. It is used for all
191 operations on the pool */
193
194 /** Release the mutex */
196
197 /** Expand the pool to the requested size
198 @param[in] size Number of tablespaces to be created
199 @return DB_SUCCESS on success, else DB_ERROR on error */
200 dberr_t expand(size_t size);
201
202 /** Delete old session temporary tablespaces found
203 on startup. This can happen if server is killed and
204 started again
205 @param[in] create_new_db true if we are bootstrapping */
206 void delete_old_pool(bool create_new_db);
207
208 private:
209 /** True after the pool has been initialized */
211 /** Initial size of pool */
213 /** Vector of tablespaces that are unused */
215 /** Vector of tablespaces that are being used */
217 /** Mutex to protect concurrent operations on the pool */
218 ib_mutex_t m_mutex;
219};
220
221/** Pool of temporary tablespace */
222extern class Tablespace_pool *tbsp_pool;
223
224/** Server temp tablespaces directory, can be absolute path. */
225extern char *srv_temp_dir;
226
227/** Release a tablespace back to the pool. The tablespace
228will be truncated before adding back to pool */
229void free_tmp(Tablespace *ts);
230
231/** Delete the pool manager. This should be called only on
232shutdown */
234
235/** Close all files in the pool */
236void close_files();
237
238/** @return a session tablespace dedicated for replication
239slave threads. Note this slave session tablespace could
240be used from many slave worker threads */
242} // namespace ibt
243#endif
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:52
Pool of session temporary tablespaces.
Definition: srv0tmp.h:137
void acquire()
Acquire the mutex.
Definition: srv0tmp.h:192
~Tablespace_pool()
Destructor.
Definition: srv0tmp.cc:186
bool m_pool_initialized
True after the pool has been initialized.
Definition: srv0tmp.h:210
void delete_old_pool(bool create_new_db)
Delete old session temporary tablespaces found on startup.
Definition: srv0tmp.cc:297
void iterate_tbsp(F &&f)
Iterate through the list of tablespaces and perform specified operation on the tablespace on every it...
Definition: srv0tmp.h:171
size_t m_init_size
Initial size of pool.
Definition: srv0tmp.h:212
void release()
Release the mutex.
Definition: srv0tmp.h:195
dberr_t initialize(bool create_new_db)
Initialize the pool on startup.
Definition: srv0tmp.cc:249
dberr_t expand(size_t size)
Expand the pool to the requested size.
Definition: srv0tmp.cc:276
Tablespace_pool(size_t init_size)
Tablespace_pool constructor.
Definition: srv0tmp.cc:178
size_t get_size()
Gets current pool size.
Definition: srv0tmp.h:182
Tablespace * get(my_thread_id id, enum tbsp_purpose purpose)
Return a session temporary tablespace.
Definition: srv0tmp.cc:199
Pool * m_free
Vector of tablespaces that are unused.
Definition: srv0tmp.h:214
void free_ts(Tablespace *ts)
Truncate and release the tablespace back to the pool.
Definition: srv0tmp.cc:226
ib_mutex_t m_mutex
Mutex to protect concurrent operations on the pool.
Definition: srv0tmp.h:218
Pool * m_active
Vector of tablespaces that are being used.
Definition: srv0tmp.h:216
Session Temporary tablespace.
Definition: srv0tmp.h:50
const space_id_t m_space_id
space_id of the current tablespace
Definition: srv0tmp.h:113
std::string file_name() const
Definition: srv0tmp.cc:160
bool close() const
Close the .ibt file.
Definition: srv0tmp.cc:128
std::string path() const
Definition: srv0tmp.cc:166
bool m_inited
True only after .ibt file is created.
Definition: srv0tmp.h:120
Tablespace()
Session Temporary tablespace.
Definition: srv0tmp.cc:68
bool truncate()
Truncate the tablespace.
Definition: srv0tmp.cc:137
my_thread_id m_thread_id
Tablespace belongs to this Session id
Definition: srv0tmp.h:123
static space_id_t m_last_used_space_id
Next available space_id for tablespace.
Definition: srv0tmp.h:117
enum tbsp_purpose purpose() const
Definition: srv0tmp.h:99
void reset_thread_id_and_purpose()
Reset the thread id while returning the tablespace to the pool.
Definition: srv0tmp.h:89
uint32_t file_id() const
The id used for name on disk temp_1.ibt, temp_2.ibt, etc.
Definition: srv0tmp.cc:156
void set_thread_id_and_purpose(my_thread_id thread_id, enum tbsp_purpose purpose)
Set the thread id of the thread and the purpose of using the tablespace.
Definition: srv0tmp.h:81
space_id_t space_id() const
Definition: srv0tmp.h:75
~Tablespace()
Definition: srv0tmp.cc:74
enum tbsp_purpose m_purpose
Purpose for this tablespace.
Definition: srv0tmp.h:126
dberr_t create()
Create the .IBT file with pattern "temp_*.ibt".
Definition: srv0tmp.cc:94
bool operator==(const Tablespace &other)
comparator for two tablespace objects
Definition: srv0tmp.h:70
my_thread_id thread_id() const
Definition: srv0tmp.h:96
dberr_t
Definition: db0err.h:39
uint32 my_thread_id
Definition: my_thread_local.h:34
void for_each(const Shards< COUNT > &shards, Function &&f) noexcept
Iterate over the shards.
Definition: ut0counter.h:323
Definition: srv0tmp.h:31
char * srv_temp_dir
Server temp tablespaces directory, can be absolute path.
Definition: srv0tmp.cc:65
dberr_t open_or_create(bool create_new_db)
Create the session temporary tablespaces on startup.
Definition: srv0tmp.cc:364
class Tablespace_pool * tbsp_pool
Pool of temporary tablespace.
Definition: srv0tmp.cc:62
void free_tmp(Tablespace *ts)
Release a tablespace back to the pool.
Definition: srv0tmp.cc:381
void close_files()
Close all files in the pool.
Definition: srv0tmp.cc:388
void delete_pool_manager()
Delete the pool manager.
Definition: srv0tmp.cc:386
tbsp_purpose
Purpose for using tablespace.
Definition: srv0tmp.h:34
@ TBSP_NONE
Tablespace is not being used for any temporary table.
Definition: srv0tmp.h:35
@ TBSP_INTRINSIC
Tablespace is used for intrinsic tables.
Definition: srv0tmp.h:39
@ TBSP_SLAVE
Tablespace is used by the slave node in a replication setup.
Definition: srv0tmp.h:41
@ TBSP_USER
Tablespace is used for user temporary tables.
Definition: srv0tmp.h:37
Tablespace * get_rpl_slave_tblsp()
Definition: srv0tmp.cc:394
The server main program.
Allocate the memory for the object in blocks.
Definition: ut0pool.h:47
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:69
#define mutex_exit(M)
Definition: ut0mutex.h:123
#define mutex_enter(M)
Definition: ut0mutex.h:117