-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathdd_tablespace.cc
235 lines (195 loc) · 7.99 KB
/
dd_tablespace.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/dd/dd_tablespace.h"
#include <stddef.h>
#include <string.h>
#include <memory>
#include "lex_string.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_sys.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/dd/cache/dictionary_client.h" // dd::cache::Dictionary_client
#include "sql/dd/dd.h" // dd::create_object
#include "sql/dd/dictionary.h" // dd::Dictionary::is_dd_table...
#include "sql/dd/impl/dictionary_impl.h" // dd::dd_tablespace_id()
#include "sql/dd/impl/system_registry.h" // dd::System_tablespaces
#include "sql/dd/object_id.h"
#include "sql/dd/properties.h" // dd::Properties
#include "sql/dd/string_type.h"
#include "sql/dd/types/index.h" // dd::Index
#include "sql/dd/types/partition.h" // dd::Partition
#include "sql/dd/types/partition_index.h" // dd::Partition_index
#include "sql/dd/types/table.h" // dd::Table
#include "sql/dd/types/tablespace.h" // dd::Tablespace
#include "sql/dd/types/tablespace_file.h" // dd::Tablespace_file
#include "sql/key.h"
#include "sql/sql_class.h" // THD
#include "sql/sql_servers.h"
#include "sql/sql_table.h" // validate_comment_length
#include "sql/table.h"
namespace {
template <typename T>
bool get_and_store_tablespace_name(THD *thd, const T *obj,
Tablespace_hash_set *tablespace_set) {
dd::String_type tablespace_name;
if (get_tablespace_name(thd, obj, &tablespace_name)) {
return true;
}
if (!tablespace_name.empty()) {
tablespace_set->insert(tablespace_name.c_str());
}
return false;
}
} // anonymous namespace
namespace dd {
bool fill_table_and_parts_tablespace_names(
THD *thd, const char *db_name, const char *table_name,
Tablespace_hash_set *tablespace_set) {
// Get hold of the dd::Table object.
dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
const dd::Table *table_obj = nullptr;
if (thd->dd_client()->acquire(db_name, table_name, &table_obj)) {
// Error is reported by the dictionary subsystem.
return true;
}
if (table_obj == nullptr) {
/*
A non-existing table is a perfectly valid scenario, e.g. for
statements using the 'IF EXISTS' clause. Thus, we cannot throw
an error in this situation, we just continue returning succuss.
*/
return false;
}
// Add the tablespace name used by dd::Table.
if (get_and_store_tablespace_name(thd, table_obj, tablespace_set)) {
return true;
}
/*
Add tablespaces used by partition/subpartition definitions.
Note that dd::Table::partitions() gives use both partitions
and sub-partitions.
*/
if (table_obj->partition_type() != dd::Table::PT_NONE) {
// Iterate through tablespace names used by partitions/indexes.
for (const dd::Partition *part_obj : table_obj->partitions()) {
if (get_and_store_tablespace_name(thd, part_obj, tablespace_set))
return true;
for (const dd::Partition_index *part_idx_obj : part_obj->indexes())
if (get_and_store_tablespace_name(thd, part_idx_obj, tablespace_set))
return true;
// Iterate through tablespace names used by subpartition/indexes.
for (const dd::Partition *sub_part_obj : part_obj->subpartitions()) {
if (get_and_store_tablespace_name(thd, sub_part_obj, tablespace_set))
return true;
for (const dd::Partition_index *subpart_idx_obj :
sub_part_obj->indexes())
if (get_and_store_tablespace_name(thd, subpart_idx_obj,
tablespace_set))
return true;
}
}
}
// Add tablespaces used by indexes.
for (const dd::Index *idx_obj : table_obj->indexes())
if (get_and_store_tablespace_name(thd, idx_obj, tablespace_set))
return true;
// TODO WL#7156: Add tablespaces used by individual columnns.
return false;
}
/**
Read tablespace name of a tablespace_id from Table or similar object.
@param thd Thread invoking this call.
@param obj Table/Partition/Index/.. object whose
tablespace name is being read.
@param [out] name Tablespace name.
@return true - On failure.
@return false - On success.
*/
template <typename T>
bool get_tablespace_name(THD *thd, const T *obj, String_type *name) {
//
// Read Tablespace
//
assert(name->empty());
if (obj->tablespace_id() == Dictionary_impl::dd_tablespace_id()) {
// If this is the DD tablespace id, then we use its name.
*name = MYSQL_TABLESPACE_NAME.str;
} else if (obj->tablespace_id() != dd::INVALID_OBJECT_ID) {
/*
We get here, when we have a table in a tablespace
which is 'innodb_system' or a user defined tablespace.
We cannot take MDL lock as we don't know the tablespace name.
Without a MDL lock we cannot acquire a object placing it in DD
cache. So we are acquiring the object uncached.
Note that in theory the fact that we are opening a table in
some tablespace means that this tablespace can't be dropped
or created concurrently, so in theory we hold implicit IS
lock on tablespace (similarly to how it happens for schemas).
*/
dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
std::unique_ptr<dd::Tablespace> tablespace;
if (thd->dd_client()->acquire_uncached(obj->tablespace_id(), &tablespace)) {
// acquire() always fails with a error being reported.
return true;
}
// Report error if tablespace not found.
if (!tablespace) {
my_error(ER_INVALID_DD_OBJECT_ID, MYF(0), obj->tablespace_id());
return true;
}
*name = tablespace->name();
} else {
/*
If user has specified special tablespace name like
'innodb_file_per_table' then we read it from tablespace options.
*/
if (obj->options().exists("tablespace"))
(void)obj->options().get("tablespace", name);
}
return false;
}
template <typename T>
bool get_tablespace_name(THD *thd, const T *obj, const char **tablespace_name,
MEM_ROOT *mem_root) {
String_type name;
if (get_tablespace_name(thd, obj, &name)) return true;
*tablespace_name = nullptr;
if (!name.empty() && !(*tablespace_name = strmake_root(mem_root, name.c_str(),
name.length()))) {
return true;
}
return false;
}
// The explicit instantiation of the template members below
// is not handled well by doxygen, so we enclose this in a
// cond/endcon block.
/**
@cond
*/
template bool get_tablespace_name<dd::Partition>(THD *, dd::Partition const *,
char const **, MEM_ROOT *);
template bool get_tablespace_name<dd::Table>(THD *, dd::Table const *,
char const **, MEM_ROOT *);
/**
@endcond
*/
} // namespace dd