forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweak_object_impl.cc
More file actions
259 lines (194 loc) · 7.82 KB
/
weak_object_impl.cc
File metadata and controls
259 lines (194 loc) · 7.82 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Copyright (c) 2014, 2025, 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/impl/types/weak_object_impl.h"
#include <memory>
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_loglevel.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysqld_error.h" // ER_*
#include "sql/dd/impl/object_key.h" // Needed for destructor
#include "sql/dd/impl/raw/raw_record.h" // Raw_record
#include "sql/dd/impl/raw/raw_table.h" // Raw_table
#include "sql/dd/impl/transaction_impl.h" // Open_dictionary_tables_ctx
#include "sql/dd/impl/types/entity_object_impl.h"
#include "sql/dd/string_type.h"
#include "sql/dd/types/object_table.h" // Object_table
#include "sql/debug_sync.h" // DEBUG_SYNC
#include "sql/log.h"
#include "sql/sql_class.h" // current_thd, THD
namespace dd {
///////////////////////////////////////////////////////////////////////////
/**
@brief
Store the DD object into DD table.
@param otx - DD transaction in use.
@return true - on failure and error is reported.
@return false - on success.
*/
template <bool use_pfs>
bool Weak_object_impl_<use_pfs>::store(Open_dictionary_tables_ctx *otx) {
DBUG_TRACE;
DBUG_EXECUTE_IF("fail_while_storing_dd_object", {
my_error(ER_LOCK_WAIT_TIMEOUT, MYF(0));
return true;
});
const Object_table &obj_table = this->object_table();
// Get main object table.
Raw_table *t = otx->get_table(obj_table.name());
assert(t);
// Insert or update record.
do {
/*
If we know that object has new primary key (e.g. to be generated
at insert time) we can skip looking up and updating old record.
This measure greatly reduces probability of InnoDB deadlocks between
concurrent DDL. Deadlocks occur because each of concurrent DDL
first looks up record with non-existing PK (e.g. INVALID_OBJECT_ID
or value greater than existing PK values for non-Entity objects) and
this acquires gap lock on supremum record and then tries to insert
row into this gap.
*/
if (this->has_new_primary_key()) break;
std::unique_ptr<Object_key> obj_key(this->create_primary_key());
if (!obj_key.get()) {
/* purecov: begin deadcode */
LogErr(ERROR_LEVEL, ER_DD_CANT_GET_OBJECT_KEY);
assert(false);
return true;
/* purecov: end */
}
std::unique_ptr<Raw_record> r;
if (t->prepare_record_for_update(*obj_key, r)) return true;
if (!r.get()) break;
// Existing record found -- do an UPDATE.
if (this->store_attributes(r.get())) {
my_error(ER_UPDATING_DD_TABLE, MYF(0), obj_table.name().c_str());
return true;
}
if (r->update()) return true;
return store_children(otx);
} while (false);
// No existing record exists -- do an INSERT.
std::unique_ptr<Raw_new_record> r(t->prepare_record_for_insert());
// Store attributes.
if (this->store_attributes(r.get())) {
my_error(ER_UPDATING_DD_TABLE, MYF(0), obj_table.name().c_str());
return true;
}
DEBUG_SYNC(current_thd, "before_insert_into_dd");
if (r->insert()) return true;
DBUG_EXECUTE_IF("weak_object_impl_store_fail_before_store_children", {
my_error(ER_UNKNOWN_ERROR, MYF(0));
return true;
});
this->set_primary_key_value(*r);
/*
It is necessary to destroy the Raw_new_record() object after
inserting the parent DD object and before creating children
DD object. The reason is that, the parent DD object may
insert a row in the same DD table (E.g., when storing parent
partition metadata in mysql.partitions), in which even the
child DD table would end-up inserting a row. (e.g., where
storing sub partition metadata in mysql.partitions)
Destroying Raw_new_record() enable accurate computation of
the auto increment values, for the child partition entry
being stored.
*/
r.reset();
if (store_children(otx)) return true;
/*
Mark object as having existing PK only after processing its children.
This allows non-entity children to rely on parent has_new_primary_key()
method to figure out if their primary key based on parent's one was not
used before.
*/
this->fix_has_new_primary_key();
return false;
}
///////////////////////////////////////////////////////////////////////////
/**
@brief
Drop the DD object from DD table.
@param otx - DD transaction in use.
@return true - on failure and error is reported.
@return false - on success.
*/
template <bool use_pfs>
bool Weak_object_impl_<use_pfs>::drop(Open_dictionary_tables_ctx *otx) const {
DBUG_TRACE;
DBUG_EXECUTE_IF("fail_while_dropping_dd_object", {
my_error(ER_LOCK_WAIT_TIMEOUT, MYF(0));
return true;
});
const Object_table &obj_table = this->object_table();
// Get main object table.
Raw_table *t = otx->get_table(obj_table.name());
assert(t);
// Find object to be dropped
std::unique_ptr<Object_key> obj_key(this->create_primary_key());
std::unique_ptr<Raw_record> r;
if (t->prepare_record_for_update(*obj_key, r)) return true;
if (!r.get()) {
/* purecov: begin deadcode */
LogErr(ERROR_LEVEL, ER_DD_CANT_CREATE_OBJECT_KEY);
assert(false);
return true;
/* purecov: end */
}
/**
Drop collections and then drop the object
We should drop collections first and then parent object
as we have referential constraints. Mostly the reverse
order of restore/store operation.
*/
if (this->drop_children(otx) || r->drop()) return true;
return false;
}
///////////////////////////////////////////////////////////////////////////
template <bool use_pfs>
bool Weak_object_impl_<use_pfs>::check_parent_consistency(
Entity_object_impl *parent, Object_id parent_id) const {
assert(parent);
assert(parent->id() == parent_id);
if (!parent) {
my_error(ER_INVALID_DD_OBJECT, MYF(0), this->object_table().name().c_str(),
"Invalid parent reference (NULL).");
return true;
}
if (parent->id() != parent_id) {
my_error(ER_INVALID_DD_OBJECT, MYF(0), this->object_table().name().c_str(),
"Invalid parent ID");
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////
} // namespace dd
template bool dd::Weak_object_impl_<true>::store(Open_dictionary_tables_ctx *);
template bool dd::Weak_object_impl_<false>::store(Open_dictionary_tables_ctx *);
template bool dd::Weak_object_impl_<true>::drop(
Open_dictionary_tables_ctx *) const;
template bool dd::Weak_object_impl_<false>::drop(
Open_dictionary_tables_ctx *) const;
template bool dd::Weak_object_impl_<true>::check_parent_consistency(
Entity_object_impl *, Object_id) const;
template bool dd::Weak_object_impl_<false>::check_parent_consistency(
Entity_object_impl *, Object_id) const;