Debian Codegraph @

storage/innobase/include/srv0tmp.h

raw
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 #ifndef srv0tmp_h
29 #define srv0tmp_h
30 #include "srv0srv.h"
31 namespace ibt {
32
33 /** Purpose for using tablespace */
34 enum tbsp_purpose {
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 */
47 dberr_t open_or_create(bool create_new_db);
48
49 /** Session Temporary tablespace */
50 class Tablespace {
51 public:
52 Tablespace();
53
54 ~Tablespace();
55
56 /** Create the .IBT file with pattern "temp_*.ibt"
57 @return DB_SUCCESS on success */
58 dberr_t create();
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 */
81 void set_thread_id_and_purpose(my_thread_id thread_id,
82 enum tbsp_purpose purpose) {
83 ut_ad(m_thread_id == 0);
84 m_thread_id = thread_id;
85 m_purpose = purpose;
86 }
87
88 /** Reset the thread id while returning the tablespace to the pool */
89 void reset_thread_id_and_purpose() {
90 ut_ad(m_thread_id != 0);
91 m_thread_id = 0;
92 m_purpose = TBSP_NONE;
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 */
113 const space_id_t m_space_id;
114
115 /** Next available space_id for tablespace. These are
116 hardcoded space_ids at higher range */
117 static space_id_t m_last_used_space_id;
118
119 /** True only after .ibt file is created */
120 bool m_inited;
121
122 /** Tablespace belongs to this Session id */
123 my_thread_id m_thread_id;
124
125 /** Purpose for this tablespace */
126 enum tbsp_purpose m_purpose;
127 };
128
129 /** Pool of session temporary tablespaces. Each session gets at max two
130 tablespaces. For a session, we allocate one tablespace on the creation of
131 first intrinsic table and another on the creation of first user temporary
132 table (CREATE TEMPORARY TABLE t1). These tablespaces are private to session.
133 No other session can use them when a tablespace is in-use by the session.
134
135 Once a session disconnects, the tablespaces are truncated and released
136 to the pool. */
137 class Tablespace_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 */
146 ~Tablespace_pool();
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 */
192 void acquire() { mutex_enter(&m_mutex); }
193
194 /** Release the mutex */
195 void release() { mutex_exit(&m_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 */
210 bool m_pool_initialized;
211 /** Initial size of pool */
212 size_t m_init_size;
213 /** Vector of tablespaces that are unused */
214 Pool *m_free;
215 /** Vector of tablespaces that are being used */
216 Pool *m_active;
217 /** Mutex to protect concurrent operations on the pool */
218 ib_mutex_t m_mutex;
219 };
220
221 /** Pool of temporary tablespace */
222 extern class Tablespace_pool *tbsp_pool;
223
224 /** Server temp tablespaces directory, can be absolute path. */
225 extern char *srv_temp_dir;
226
227 /** Release a tablespace back to the pool. The tablespace
228 will be truncated before adding back to pool */
229 void free_tmp(Tablespace *ts);
230
231 /** Delete the pool manager. This should be called only on
232 shutdown */
233 void delete_pool_manager();
234
235 /** Close all files in the pool */
236 void close_files();
237
238 /** @return a session tablespace dedicated for replication
239 slave threads. Note this slave session tablespace could
240 be used from many slave worker threads */
241 Tablespace *get_rpl_slave_tblsp();
242 } // namespace ibt
243 #endif