MySQL  8.0.21
Source Code Documentation
srv0tmp.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2018, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 
25 *****************************************************************************/
26 
27 #ifndef srv0tmp_h
28 #define srv0tmp_h
29 #include "srv0srv.h"
30 namespace 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 */
46 dberr_t open_or_create(bool create_new_db);
47 
48 /** Sesssion Temporary tablespace */
49 class Tablespace {
50  public:
51  Tablespace();
52 
53  ~Tablespace();
54 
55  /** Create the .IBT file with pattern "temp_*.ibt"
56  @return DB_SUCCESS on success */
57  dberr_t create();
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 */
119  bool m_inited;
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
129 tablespaces. For a session, we allocate one tablespace on the creation of
130 first intrinsic table and another on the creation of first user temporary
131 table (CREATE TEMPORARY TABLE t1). These tablespaces are private to session.
132 No other session can use them when a tablespace is in-use by the session.
133 
134 Once a session disconnects, the tablespaces are truncated and released
135 to 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 */
145  ~Tablespace_pool();
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 */
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  private:
180  /** Acquire the mutex. It is used for all
181  operations on the pool */
182  void acquire() { mutex_enter(&m_mutex); }
183 
184  /** Release the mutex */
185  void release() { mutex_exit(&m_mutex); }
186 
187  /** Expand the pool to the requested size
188  @param[in] size Number of tablespaces to be created
189  @return DB_SUCCESS on success, else DB_ERROR on error */
190  dberr_t expand(size_t size);
191 
192  /** Delete old session temporary tablespaces found
193  on startup. This can happen if server is killed and
194  started again
195  @param[in] create_new_db true if we are bootstrapping */
196  void delete_old_pool(bool create_new_db);
197 
198  private:
199  /** True after the pool has been initialized */
201  /** Initial size of pool */
202  size_t m_init_size;
203  /** Vector of tablespaces that are unused */
205  /** Vector of tablespaces that are being used */
207  /** Mutex to protect concurrent operations on the pool */
208  ib_mutex_t m_mutex;
209 };
210 
211 /** Pool of temporary tablespace */
212 extern class Tablespace_pool *tbsp_pool;
213 
214 /** Server temp tablespaces directory, can be absolute path. */
215 extern char *srv_temp_dir;
216 
217 /** Release a tablespace back to the pool. The tablespace
218 will be truncated before adding back to pool */
219 void free_tmp(Tablespace *ts);
220 
221 /** Delete the pool manager. This should be called only on
222 shutdown */
223 void delete_pool_manager();
224 
225 /** Close all files in the pool */
226 void close_files();
227 
228 /** @return a session tablespace dedicated for replication
229 slave threads. Note this slave session tablespace could
230 be used from many slave worker threads */
232 } // namespace ibt
233 #endif
Pool * m_active
Vector of tablespaces that are being used.
Definition: srv0tmp.h:206
ib_mutex_t m_mutex
Mutex to protect concurrent operations on the pool.
Definition: srv0tmp.h:208
bool close() const
Close the .ibt file.
Definition: srv0tmp.cc:129
bool operator==(const Tablespace &other)
comparator for two tablespace objects
Definition: srv0tmp.h:69
char * srv_temp_dir
Server temp tablespaces directory, can be absolute path.
Definition: srv0tmp.cc:64
bool truncate()
Truncate the tablespace.
Definition: srv0tmp.cc:138
uint32_t file_id() const
The id used for name on disk temp_1.ibt, temp_2.ibt, etc.
Definition: srv0tmp.cc:155
enum tbsp_purpose m_purpose
Purpose for this tablespace.
Definition: srv0tmp.h:125
std::string file_name() const
Definition: srv0tmp.cc:159
void close_files()
Close all files in the pool.
Definition: srv0tmp.cc:386
void acquire()
Acquire the mutex.
Definition: srv0tmp.h:182
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:443
static space_id_t m_last_used_space_id
Next available space_id for tablespace.
Definition: srv0tmp.h:116
space_id_t space_id() const
Definition: srv0tmp.h:74
bool initialize(THD *thd)
Initialize the dictionary while starting the server for the first time.
Definition: bootstrapper.cc:869
The server main program.
const space_id_t m_space_id
space_id of the current tablespace
Definition: srv0tmp.h:112
enum tbsp_purpose purpose() const
Definition: srv0tmp.h:98
Tablespace is used by the slave node in a replication setup.
Definition: srv0tmp.h:40
Tablespace is not being used for any temporary table.
Definition: srv0tmp.h:34
Tablespace()
Sesssion Temporary tablespace.
Definition: srv0tmp.cc:67
void for_each(const Shards< COUNT > &shards, Function &&f) noexcept
Iterate over the shards.
Definition: ut0counter.h:322
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:188
dberr_t
Definition: db0err.h:38
void delete_pool_manager()
Delete the pool manager.
Definition: srv0tmp.cc:384
Sesssion Temporary tablespace.
Definition: srv0tmp.h:49
tbsp_purpose
Purpose for using tablespace.
Definition: srv0tmp.h:33
bool m_inited
True only after .ibt file is created.
Definition: srv0tmp.h:119
Allocate the memory for the object in blocks.
Definition: ut0pool.h:46
class Tablespace_pool * tbsp_pool
Pool of temporary tablespace.
Definition: srv0tmp.cc:61
my_thread_id thread_id() const
Definition: srv0tmp.h:95
uint32 space_id_t
Tablespace identifier.
Definition: api0api.h:59
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:65
~Tablespace()
Definition: srv0tmp.cc:73
uint32 my_thread_id
Definition: my_thread_local.h:33
#define mutex_enter(M)
Definition: ut0mutex.h:115
size_t m_init_size
Initial size of pool.
Definition: srv0tmp.h:202
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
Pool of session temporary tablespaces.
Definition: srv0tmp.h:136
my_thread_id m_thread_id
Tablespace belongs to this Session id.
Definition: srv0tmp.h:122
std::string path() const
Definition: srv0tmp.cc:165
#define mutex_exit(M)
Definition: ut0mutex.h:122
Definition: srv0tmp.h:30
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
void reset_thread_id_and_purpose()
Reset the thread id while returning the tablespace to the pool.
Definition: srv0tmp.h:88
Pool * m_free
Vector of tablespaces that are unused.
Definition: srv0tmp.h:204
Tablespace * get_rpl_slave_tblsp()
Definition: srv0tmp.cc:392
void free_tmp(Tablespace *ts)
Release a tablespace back to the pool.
Definition: srv0tmp.cc:379
dberr_t open_or_create(bool create_new_db)
Create the session temporary tablespaces on startup.
Definition: srv0tmp.cc:363
dberr_t create()
Create the .IBT file with pattern "temp_*.ibt".
Definition: srv0tmp.cc:93
void release()
Release the mutex.
Definition: srv0tmp.h:185
Tablespace is used for user temporary tables.
Definition: srv0tmp.h:36
Tablespace is used for intrinsic tables.
Definition: srv0tmp.h:38
bool m_pool_initialized
True after the pool has been initialized.
Definition: srv0tmp.h:200