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