-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathbootstrapper.cc
1981 lines (1730 loc) · 78 KB
/
bootstrapper.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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/bootstrap/bootstrapper.h"
#include <stddef.h>
#include <sys/types.h>
#include <memory>
#include <new>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "my_dbug.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/my_loglevel.h"
#include "mysql/strings/m_ctype.h"
#include "mysql_version.h" // MYSQL_VERSION_ID
#include "mysqld_error.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/dd/cache/dictionary_client.h" // dd::cache::Dictionary_client
#include "sql/dd/dd.h" // dd::create_object
#include "sql/dd/impl/bootstrap/bootstrap_ctx.h" // DD_bootstrap_ctx
#include "sql/dd/impl/cache/shared_dictionary_cache.h" // Shared_dictionary_cache
#include "sql/dd/impl/cache/storage_adapter.h" // Storage_adapter
#include "sql/dd/impl/dictionary_impl.h" // dd::Dictionary_impl
#include "sql/dd/impl/raw/object_keys.h"
#include "sql/dd/impl/sdi.h" // dd::sdi::store
#include "sql/dd/impl/tables/character_sets.h" // dd::tables::Character_sets
#include "sql/dd/impl/tables/collations.h" // dd::tables::Collations
#include "sql/dd/impl/tables/dd_properties.h" // dd::tables::DD_properties
#include "sql/dd/impl/tables/tables.h" // dd::tables::Tables
#include "sql/dd/impl/types/schema_impl.h" // dd::Schema_impl
#include "sql/dd/impl/types/table_impl.h" // dd::Table_impl
#include "sql/dd/impl/types/tablespace_impl.h" // dd::Table_impl
#include "sql/dd/impl/upgrade/dd.h" // dd::upgrade::upgrade_tables
#include "sql/dd/impl/upgrade/server.h" // dd::upgrade::do_server_upgrade_checks
#include "sql/dd/impl/utils.h" // dd::execute_query
#include "sql/dd/info_schema/metadata.h" // IS_DD_VERSION
#include "sql/dd/object_id.h"
#include "sql/dd/types/abstract_table.h"
#include "sql/dd/types/object_table.h" // dd::Object_table
#include "sql/dd/types/object_table_definition.h" // dd::Object_table_definition
#include "sql/dd/types/table.h"
#include "sql/dd/types/tablespace.h"
#include "sql/dd/types/tablespace_file.h" // dd::Tablespace_file
#include "sql/dd/upgrade/server.h" // UPGRADE_NONE
#include "sql/handler.h" // dict_init_mode_t
#include "sql/mdl.h"
#include "sql/mysqld.h"
#include "sql/sd_notify.h" // sysd::notify
#include "sql/thd_raii.h"
#include "storage/perfschema/pfs_dd_version.h" // PFS_DD_VERSION
using namespace dd;
///////////////////////////////////////////////////////////////////////////
namespace {
// Initialize recovery in the DDSE.
bool DDSE_dict_recover(THD *thd, dict_recovery_mode_t dict_recovery_mode,
uint version) {
if (!opt_initialize)
sysd::notify("STATUS=InnoDB crash recovery in progress\n");
handlerton *ddse = ha_resolve_by_legacy_type(thd, DB_TYPE_INNODB);
if (ddse->dict_recover == nullptr) return true;
bool error = ddse->dict_recover(dict_recovery_mode, version);
if (!opt_initialize)
sysd::notify("STATUS=InnoDB crash recovery ",
error ? "unsuccessful" : "successful", "\n");
/*
Commit when tablespaces have been initialized, since in that
case, tablespace meta data is added.
*/
if (dict_recovery_mode == DICT_RECOVERY_INITIALIZE_TABLESPACES)
return dd::end_transaction(thd, error);
return error;
}
/*
Update the System_tables registry with meta data from 'dd_properties'.
Iterate over the tables in the DD_properties. If this is minor downgrade,
add new tables that were added in the newer version to the System_tables
registry. If this is not minor downgrade, assert that all tables in the
DD_properties indeed have a corresponding entry in the System_tables
registry.
*/
bool update_system_tables(THD *thd) {
std::unique_ptr<dd::Properties> system_tables_props;
bool exists = false;
if (dd::tables::DD_properties::instance().get(
thd, "SYSTEM_TABLES", &system_tables_props, &exists) ||
!exists) {
my_error(ER_DD_INIT_FAILED, MYF(0));
return true;
}
/*
We would normally use a range based loop here, but the developerstudio
compiler on Solaris does not handle this when the base collection has
pure virtual begin() and end() functions.
*/
for (Properties::const_iterator it = system_tables_props->begin();
it != system_tables_props->end(); ++it) {
// Check if this is a CORE, INERT, SECOND or DDSE table.
if (!dd::get_dictionary()->is_dd_table_name(MYSQL_SCHEMA_NAME.str,
it->first)) {
if (bootstrap::DD_bootstrap_ctx::instance().is_minor_downgrade()) {
/*
Add tables as type CORE regardless of the actual type, which
is irrelevant in this case.
*/
System_tables::instance()->add(MYSQL_SCHEMA_NAME.str, it->first,
System_tables::Types::CORE, nullptr);
} else {
my_error(ER_DD_METADATA_NOT_FOUND, MYF(0), it->first.c_str());
return true;
}
} else {
/*
The table is a known DD table. Then, we get its definition
and add it to the Object_table instance. The definition might
not exist if the table was added after the version that we
are upgrading from.
*/
String_type tbl_prop_str;
if (!system_tables_props->exists(it->first) ||
system_tables_props->get(it->first, &tbl_prop_str))
continue;
const Object_table *table_def = System_tables::instance()->find_table(
MYSQL_SCHEMA_NAME.str, it->first);
assert(table_def);
std::unique_ptr<dd::Properties> tbl_props(
Properties::parse_properties(tbl_prop_str));
String_type def;
if (tbl_props->get(dd::tables::DD_properties::dd_key(
dd::tables::DD_properties::DD_property::DEF),
&def)) {
my_error(ER_DD_METADATA_NOT_FOUND, MYF(0), it->first.c_str());
return true;
}
std::unique_ptr<Properties> table_def_properties(
Properties::parse_properties(def));
table_def->set_actual_table_definition(*table_def_properties);
}
}
return false;
}
// Create a DD table using the target table definition.
bool create_target_table(THD *thd, const Object_table *object_table) {
assert(object_table != nullptr);
/*
The target table definition may not be present if the table
is abandoned. That's ok, not an error.
*/
if (object_table->is_abandoned()) return false;
String_type target_ddl_statement("");
const Object_table_definition *target_table_def =
object_table->target_table_definition();
assert(target_table_def != nullptr);
target_ddl_statement = target_table_def->get_ddl();
assert(!target_ddl_statement.empty());
return dd::execute_query(thd, target_ddl_statement);
}
// Create a DD table using the actual table definition.
/* purecov: begin inspected */
bool create_actual_table(THD *thd, const Object_table *object_table) {
/*
For minor downgrade, tables might have been added in the upgraded
server that we do not have any Object_table instance for. In that
case, we just skip them.
*/
if (object_table == nullptr) {
assert(bootstrap::DD_bootstrap_ctx::instance().is_minor_downgrade());
return false;
}
String_type actual_ddl_statement("");
const Object_table_definition *actual_table_def =
object_table->actual_table_definition();
/*
The actual definition may not be present. This will happen during
upgrade if the new DD version adds a new DD table which was not
present in the DD we are upgrading from. This is OK, not an error.
*/
if (actual_table_def == nullptr) return false;
actual_ddl_statement = actual_table_def->get_ddl();
assert(!actual_ddl_statement.empty());
return dd::execute_query(thd, actual_ddl_statement);
}
/* purecov: end */
/*
Acquire exclusive meta data locks for the DD schema, tablespace and
table names.
*/
bool acquire_exclusive_mdl(THD *thd) {
// All MDL requests.
MDL_request_list mdl_requests;
// Prepare MDL request for the schema name.
MDL_request schema_request;
MDL_REQUEST_INIT(&schema_request, MDL_key::SCHEMA, MYSQL_SCHEMA_NAME.str, "",
MDL_EXCLUSIVE, MDL_TRANSACTION);
mdl_requests.push_front(&schema_request);
// Prepare MDL request for the tablespace name.
MDL_request tablespace_request;
MDL_REQUEST_INIT(&tablespace_request, MDL_key::TABLESPACE, "",
MYSQL_TABLESPACE_NAME.str, MDL_EXCLUSIVE, MDL_TRANSACTION);
mdl_requests.push_front(&tablespace_request);
// Prepare MDL requests for all tables names.
for (System_tables::Const_iterator it = System_tables::instance()->begin();
it != System_tables::instance()->end(); ++it) {
// Skip extraneous tables during minor downgrade.
if ((*it)->entity() == nullptr) continue;
MDL_request *table_request = new (thd->mem_root) MDL_request;
if (table_request == nullptr) return true;
MDL_REQUEST_INIT(table_request, MDL_key::TABLE, MYSQL_SCHEMA_NAME.str,
(*it)->entity()->name().c_str(), MDL_EXCLUSIVE,
MDL_TRANSACTION);
mdl_requests.push_front(table_request);
}
// Finally, acquire all the MDL locks.
return (thd->mdl_context.acquire_locks(&mdl_requests,
thd->variables.lock_wait_timeout));
}
/*
Acquire the DD schema, tablespace and table objects. Clone the objects,
reset ID, store persistently, and update the storage adapter.
*/
bool flush_meta_data(THD *thd) {
// Acquire exclusive meta data locks for the relevant DD objects.
if (acquire_exclusive_mdl(thd)) return true;
{
/*
Use a scoped auto releaser to make sure the cached objects are released
before the shared cache is reset.
*/
dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
/*
First, we acquire the DD schema and tablespace and keep them in
local variables. We also clone them, the clones will be used for
updating the ids. We also acquire all the DD table objects to make
sure the shared cache is populated, and we keep the original objects
as well as clones in a vector. The auto releaser will make sure the
objects are not evicted. This must be ensured since we need to make
sure the ids stay consistent across all objects in the shared cache.
*/
const Schema *dd_schema = nullptr;
const Tablespace *dd_tspace = nullptr;
std::vector<const Table_impl *> dd_tables; // Owned by the shared cache.
std::vector<std::unique_ptr<Table_impl>> dd_table_clones;
if (thd->dd_client()->acquire(dd::String_type(MYSQL_SCHEMA_NAME.str),
&dd_schema) ||
thd->dd_client()->acquire(dd::String_type(MYSQL_TABLESPACE_NAME.str),
&dd_tspace))
return dd::end_transaction(thd, true);
std::unique_ptr<Schema_impl> dd_schema_clone(
dynamic_cast<Schema_impl *>(dd_schema->clone()));
std::unique_ptr<Tablespace_impl> dd_tspace_clone(
dynamic_cast<Tablespace_impl *>(dd_tspace->clone()));
for (System_tables::Const_iterator it = System_tables::instance()->begin();
it != System_tables::instance()->end(); ++it) {
/*
We add nullptr to the dd_tables vector for abandoned
tables and system tables to have the same number of objects
in the System_tables list, the dd_tables vector and the
dd_table_clones vector.
*/
const dd::Table *dd_table = nullptr;
if ((*it)->property() != System_tables::Types::SYSTEM &&
thd->dd_client()->acquire(MYSQL_SCHEMA_NAME.str,
(*it)->entity()->name(), &dd_table))
return dd::end_transaction(thd, true);
dd_tables.push_back(dynamic_cast<const Table_impl *>(dd_table));
/*
If this is an abandoned table, we can't clone it. Thus, we
push back a nullptr to make sure we have the same number of
elements in the dd_table_clones as in the System_tables.
*/
std::unique_ptr<Table_impl> dd_table_clone;
if (dd_table != nullptr) {
dd_table_clones.push_back(std::unique_ptr<Table_impl>(
dynamic_cast<Table_impl *>(dd_table->clone())));
} else {
dd_table_clones.push_back(nullptr);
}
}
/*
We have now populated the shared cache with the core objects, and kept
clones of all DD objects. The scoped auto releaser makes sure we will
not evict the objects from the shared cache until the auto releaser
exits scope. Thus, within the scope of the auto releaser, we can modify
the contents of the core registry in the storage adapter without risking
that this will interfere with the contents of the shared cache, because
the DD transactions will acquire the core objects from the shared cache.
*/
/*
First, we modify and store the DD schema without changing the cached
copy. We cannot use acquire_for_modification() here, because that
would make the DD sub-transactions (e.g. when calling store()) see a
partially modified set of core objects, where e.g. the mysql
schema object has got its new, real id (from the auto-inc column
in the dd.schemata table), whereas the core DD table objects still
refer to the id that was allocated when creating the scaffolding.
So we first store all the objects persistently, and make sure that
the on-disk data will have correct and consistent ids. When all objects
are stored, we update the contents of the core registry in the
storage adapter to reflect the persisted data. Finally, the shared
cache is reset so that on next acquisition, the DD objects will be
fetched from the core registry in the storage adapter.
*/
/*
We must set the ID to INVALID to make the object get a fresh ID from
the auto inc ID column.
*/
dd_schema_clone->set_id(INVALID_OBJECT_ID);
dd_tspace_clone->set_id(INVALID_OBJECT_ID);
if (dd::cache::Storage_adapter::instance()->store(
thd, static_cast<Schema *>(dd_schema_clone.get())) ||
dd::cache::Storage_adapter::instance()->store(
thd, static_cast<Tablespace *>(dd_tspace_clone.get())))
return dd::end_transaction(thd, true);
/*
Now, the DD schema and DD tablespace are stored persistently. We will
not update the core registry until after we have stored all DD tables.
At that point, we can update all the core registry objects in one go
and avoid using a partially update core registry for e.g. object
acquisition.
*/
std::vector<std::unique_ptr<Table_impl>>::iterator clone_it =
dd_table_clones.begin();
for (System_tables::Const_iterator it = System_tables::instance()->begin();
it != System_tables::instance()->end() &&
clone_it != dd_table_clones.end();
++it, ++clone_it) {
// Skip abandoned tables and system tables.
if ((*clone_it) == nullptr ||
(*it)->property() == System_tables::Types::SYSTEM)
continue;
assert((*it)->entity()->name() == (*clone_it)->name());
// We must set the ID to INVALID to let the object get an auto inc ID.
(*clone_it)->set_id(INVALID_OBJECT_ID);
/*
Change the schema and tablespace id to match the ids of the
persisted objects. Note that this means the persisted DD table
objects will have consistent IDs, but the IDs in the objects in
the core registry will not be updated yet.
*/
(*clone_it)->set_schema_id(dd_schema_clone->id());
(*clone_it)->set_tablespace_id(dd_tspace_clone->id());
if (dd::cache::Storage_adapter::instance()->store(
thd, static_cast<Table *>((*clone_it).get())))
return dd::end_transaction(thd, true);
}
/*
Update and store the predefined tablespace objects. The DD tablespace
has already been stored above, so we iterate only over the tablespaces
of type PREDEFINED_DDSE.
*/
for (System_tablespaces::Const_iterator it =
System_tablespaces::instance()->begin(
System_tablespaces::Types::PREDEFINED_DDSE);
it != System_tablespaces::instance()->end();
it = System_tablespaces::instance()->next(
it, System_tablespaces::Types::PREDEFINED_DDSE)) {
const dd::Tablespace *tspace = nullptr;
if (thd->dd_client()->acquire((*it)->key().second, &tspace))
return dd::end_transaction(thd, true);
std::unique_ptr<Tablespace_impl> tspace_clone(
dynamic_cast<Tablespace_impl *>(tspace->clone()));
// We must set the ID to INVALID to enable storing the object.
tspace_clone->set_id(INVALID_OBJECT_ID);
if (dd::cache::Storage_adapter::instance()->store(
thd, static_cast<Tablespace *>(tspace_clone.get())))
return dd::end_transaction(thd, true);
/*
Only the DD tablespace is needed to handle cache misses, so we can
just drop the predefined tablespaces from the core registry now that
it has been persisted.
*/
dd::cache::Storage_adapter::instance()->core_drop(thd, tspace);
}
/*
Now, the DD schema and tablespace as well as the DD tables have been
persisted. The last thing we do before resetting the shared cache is
to update the contents of the core registry to match the persisted
objects. First, we update the core registry with the persisted DD
schema and tablespace.
*/
dd::cache::Storage_adapter::instance()->core_drop(thd, dd_schema);
dd::cache::Storage_adapter::instance()->core_store(
thd, static_cast<Schema *>(dd_schema_clone.get()));
dd::cache::Storage_adapter::instance()->core_drop(thd, dd_tspace);
dd::cache::Storage_adapter::instance()->core_store(
thd, static_cast<Tablespace *>(dd_tspace_clone.get()));
// Make sure the IDs after storing are as expected.
assert(dd_schema_clone->id() == 1);
assert(dd_tspace_clone->id() == 1);
/*
Finally, we update the core registry of the DD tables. This must be
done in two loops to avoid issues related to overlapping ID sequences.
*/
std::vector<const Table_impl *>::const_iterator table_it =
dd_tables.begin();
for (System_tables::Const_iterator it = System_tables::instance()->begin();
it != System_tables::instance()->end() && table_it != dd_tables.end();
++it, ++table_it) {
// Skip abandoned tables and system tables.
if ((*table_it) == nullptr ||
(*it)->property() == System_tables::Types::SYSTEM)
continue;
assert((*it)->entity()->name() == (*table_it)->name());
dd::cache::Storage_adapter::instance()->core_drop(
thd, static_cast<const Table *>(*table_it));
}
clone_it = dd_table_clones.begin();
for (System_tables::Const_iterator it = System_tables::instance()->begin();
it != System_tables::instance()->end() &&
clone_it != dd_table_clones.end();
++it, ++clone_it) {
// Skip abandoned tables and system tables.
if ((*clone_it) == nullptr ||
(*it)->property() == System_tables::Types::SYSTEM)
continue;
if ((*it)->property() == System_tables::Types::CORE) {
assert((*it)->entity()->name() == (*clone_it)->name());
dd::cache::Storage_adapter::instance()->core_store(
thd, static_cast<Table *>((*clone_it).get()));
}
}
}
/*
Now, the auto releaser has released the objects, and we can go ahead and
reset the shared cache.
*/
dd::cache::Shared_dictionary_cache::instance()->reset(true);
if (dd::end_transaction(thd, false)) {
return true;
}
/*
Use a scoped auto releaser to make sure the objects cached for SDI
writing, FK parent information reload, and DD property storage are
released.
*/
dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
// Acquire the DD tablespace and write SDI
const Tablespace *dd_tspace = nullptr;
if (thd->dd_client()->acquire(dd::String_type(MYSQL_TABLESPACE_NAME.str),
&dd_tspace) ||
dd::sdi::store(thd, dd_tspace)) {
return dd::end_transaction(thd, true);
}
// Acquire the DD schema and write SDI
const Schema *dd_schema = nullptr;
if (thd->dd_client()->acquire(dd::String_type(MYSQL_SCHEMA_NAME.str),
&dd_schema) ||
dd::sdi::store(thd, dd_schema)) {
return dd::end_transaction(thd, true);
}
/*
Acquire the DD table objects and write SDI for them. Also sync from
the DD tables in order to get the FK parent information reloaded.
*/
for (System_tables::Const_iterator it = System_tables::instance()->begin();
it != System_tables::instance()->end(); ++it) {
// Skip system tables.
if ((*it)->property() == System_tables::Types::SYSTEM) continue;
const dd::Table *dd_table = nullptr;
if (thd->dd_client()->acquire(MYSQL_SCHEMA_NAME.str,
(*it)->entity()->name(), &dd_table)) {
return dd::end_transaction(thd, true);
}
// Skip abandoned tables.
if (dd_table == nullptr) continue;
/*
Make sure the registry of the core DD objects is updated with an
object read from the DD tables, with updated FK parent information.
Store the object to make sure SDI is written.
*/
Abstract_table::Name_key table_key;
Abstract_table::update_name_key(&table_key, dd_schema->id(),
dd_table->name());
const dd::Abstract_table *persisted_dd_table = nullptr;
if (dd::cache::Storage_adapter::instance()->get(
thd, table_key, ISO_READ_COMMITTED, true, &persisted_dd_table) ||
persisted_dd_table == nullptr ||
dd::sdi::store(thd, dynamic_cast<const Table *>(persisted_dd_table))) {
if (persisted_dd_table != nullptr) delete persisted_dd_table;
return dd::end_transaction(thd, true);
}
if ((*it)->property() == System_tables::Types::CORE) {
dd::cache::Storage_adapter::instance()->core_drop(thd, dd_table);
dd::cache::Storage_adapter::instance()->core_store(
thd, dynamic_cast<Table *>(
const_cast<Abstract_table *>(persisted_dd_table)));
}
if (persisted_dd_table != nullptr) delete persisted_dd_table;
}
bootstrap::DD_bootstrap_ctx::instance().set_stage(bootstrap::Stage::SYNCED);
return dd::end_transaction(thd, false);
}
// Insert additional data into the DD tables.
bool populate_tables(THD *thd) {
// Iterate over DD tables, populate tables.
for (System_tables::Const_iterator it = System_tables::instance()->begin();
it != System_tables::instance()->end(); ++it) {
// Skip system tables.
if ((*it)->property() == System_tables::Types::SYSTEM) continue;
// Retrieve list of SQL statements to execute.
const Object_table_definition *table_def =
(*it)->entity()->target_table_definition();
// Skip abandoned tables.
if (table_def == nullptr) continue;
bool error = false;
std::vector<dd::String_type> stmt = table_def->get_dml();
for (std::vector<dd::String_type>::iterator stmt_it = stmt.begin();
stmt_it != stmt.end() && !error; ++stmt_it)
error = dd::execute_query(thd, *stmt_it);
// Commit the statement based population.
if (dd::end_transaction(thd, error)) return true;
// If no error, call the low level table population method, and commit it.
error = (*it)->entity()->populate(thd);
if (dd::end_transaction(thd, error)) return true;
}
bootstrap::DD_bootstrap_ctx::instance().set_stage(
bootstrap::Stage::POPULATED);
return false;
}
// Re-populate character sets and collations upon normal restart.
bool repopulate_charsets_and_collations(THD *thd) {
/*
We must check if the DDSE is started in a way that makes the DD
read only. For now, we only support InnoDB as SE for the DD. The call
to retrieve the handlerton for the DDSE should be replaced by a more
generic mechanism.
*/
handlerton *ddse = ha_resolve_by_legacy_type(thd, DB_TYPE_INNODB);
if (ddse->is_dict_readonly && ddse->is_dict_readonly()) {
LogErr(WARNING_LEVEL, ER_DD_NO_WRITES_NO_REPOPULATION, "InnoDB", " ");
return false;
}
/*
Otherwise, turn off FK checks, re-populate and commit.
The FK checks must be turned off since the collations and
character sets reference each other.
*/
bool error = dd::execute_query(thd, "SET FOREIGN_KEY_CHECKS= 0") ||
tables::Collations::instance().populate(thd) ||
tables::Character_sets::instance().populate(thd);
/*
We must commit the re-population before executing a new query, which
expects the transaction to be empty, and finally, turn FK checks back on.
*/
error |= dd::end_transaction(thd, error);
error |= dd::execute_query(thd, "SET FOREIGN_KEY_CHECKS= 1");
bootstrap::DD_bootstrap_ctx::instance().set_stage(
bootstrap::Stage::POPULATED);
return error;
}
/*
Verify that the storage adapter contains the core DD objects and
nothing else.
*/
bool verify_contents(THD *thd) {
// Verify that the DD schema is present, and that its id == 1.
Schema::Name_key schema_key;
Schema::update_name_key(&schema_key, MYSQL_SCHEMA_NAME.str);
Object_id dd_schema_id =
cache::Storage_adapter::instance()->core_get_id<Schema>(schema_key);
assert(dd_schema_id == MYSQL_SCHEMA_DD_ID);
if (dd_schema_id == INVALID_OBJECT_ID) {
LogErr(ERROR_LEVEL, ER_DD_SCHEMA_NOT_FOUND, MYSQL_SCHEMA_NAME.str);
return dd::end_transaction(thd, true);
}
assert(cache::Storage_adapter::instance()->core_size<Schema>() == 1);
// Verify that the core DD tables are present.
#ifndef NDEBUG
size_t n_core_tables = 0;
#endif
for (System_tables::Const_iterator it =
System_tables::instance()->begin(System_tables::Types::CORE);
it != System_tables::instance()->end();
it = System_tables::instance()->next(it, System_tables::Types::CORE)) {
// Skip extraneous tables for minor downgrade.
if ((*it)->entity() == nullptr) continue;
#ifndef NDEBUG
n_core_tables++;
#endif
Table::Name_key table_key;
Table::update_name_key(&table_key, dd_schema_id, (*it)->entity()->name());
Object_id dd_table_id =
cache::Storage_adapter::instance()->core_get_id<Table>(table_key);
assert(dd_table_id != INVALID_OBJECT_ID);
if (dd_table_id == INVALID_OBJECT_ID) {
LogErr(ERROR_LEVEL, ER_DD_TABLE_NOT_FOUND,
(*it)->entity()->name().c_str());
return dd::end_transaction(thd, true);
}
}
assert(cache::Storage_adapter::instance()->core_size<Abstract_table>() ==
n_core_tables);
// Verify that the dictionary tablespace is present and that its id == 1.
Tablespace::Name_key tspace_key;
Tablespace::update_name_key(&tspace_key, MYSQL_TABLESPACE_NAME.str);
Object_id dd_tspace_id =
cache::Storage_adapter::instance()->core_get_id<Tablespace>(tspace_key);
assert(dd_tspace_id == MYSQL_TABLESPACE_DD_ID);
if (dd_tspace_id == INVALID_OBJECT_ID) {
LogErr(ERROR_LEVEL, ER_DD_TABLESPACE_NOT_FOUND, MYSQL_TABLESPACE_NAME.str);
return dd::end_transaction(thd, true);
}
assert(cache::Storage_adapter::instance()->core_size<Tablespace>() == 1);
return dd::end_transaction(thd, false);
}
} // namespace
///////////////////////////////////////////////////////////////////////////
namespace dd {
namespace bootstrap {
/*
Do the necessary DD-related initialization in the DDSE, and get the
predefined tables and tablespaces.
*/
bool DDSE_dict_init(THD *thd, dict_init_mode_t dict_init_mode, uint version) {
handlerton *ddse = ha_resolve_by_legacy_type(thd, DB_TYPE_INNODB);
/*
The lists with element wrappers are mem root allocated. The wrapped
instances are allocated dynamically in the DDSE. These instances will be
owned by the System_tables registry by the end of this function.
*/
List<const Object_table> ddse_tables;
List<const Plugin_tablespace> ddse_tablespaces;
sysd::notify("STATUS=InnoDB initialization in progress\n");
bool innodb_init_failed =
(ddse->ddse_dict_init == nullptr ||
ddse->ddse_dict_init(dict_init_mode, version, &ddse_tables,
&ddse_tablespaces));
sysd::notify("STATUS=InnoDB initialization ",
innodb_init_failed ? "unsuccessful" : "successful", "\n");
if (innodb_init_failed) return true;
/*
Iterate over the table definitions and add them to the System_tables
registry. The Object_table instances will later be used to execute
CREATE TABLE statements to actually create the tables.
If Object_table::is_hidden(), then we add the tables as type DDSE_PRIVATE
(not available neither for DDL nor DML), otherwise, we add them as type
DDSE_PROTECTED (available for DML, not for DDL).
*/
List_iterator<const Object_table> table_it(ddse_tables);
const Object_table *ddse_table = nullptr;
while ((ddse_table = table_it++)) {
System_tables::Types table_type = System_tables::Types::DDSE_PROTECTED;
if (ddse_table->is_hidden()) {
table_type = System_tables::Types::DDSE_PRIVATE;
}
System_tables::instance()->add(MYSQL_SCHEMA_NAME.str, ddse_table->name(),
table_type, ddse_table);
}
/*
Get the server version number from the DD tablespace header and verify
that we are allowed to upgrade from that version. The error handling is done
after adding the ddse tables into the system registry to avoid memory leaks.
*/
if (!opt_initialize) {
uint server_version = 0;
if (ddse->dict_get_server_version == nullptr ||
ddse->dict_get_server_version(&server_version)) {
LogErr(ERROR_LEVEL, ER_CANNOT_GET_SERVER_VERSION_FROM_TABLESPACE_HEADER);
return true;
}
if (server_version != MYSQL_VERSION_ID) {
if (opt_upgrade_mode == UPGRADE_NONE) {
LogErr(ERROR_LEVEL, ER_SERVER_UPGRADE_OFF);
return true;
}
if (!DD_bootstrap_ctx::instance().supported_server_version(
server_version)) {
if (server_version > MYSQL_VERSION_ID &&
!DD_bootstrap_ctx::instance().is_server_patch_downgrade(
server_version))
LogErr(ERROR_LEVEL, ER_INVALID_SERVER_DOWNGRADE_NOT_PATCH,
server_version, MYSQL_VERSION_ID);
else
LogErr(ERROR_LEVEL, ER_SERVER_UPGRADE_VERSION_NOT_SUPPORTED,
server_version);
return true;
}
}
}
/*
At this point, the Systen_tables registry contains the INERT DD tables,
and the DDSE tables. Before we continue, we must add the remaining
DD tables.
*/
System_tables::instance()->add_remaining_dd_tables();
/*
Iterate over the tablespace definitions, add the names and the
tablespace meta data to the System_tablespaces registry. The
meta data will be used later to create dd::Tablespace objects.
The Plugin_tablespace instances are owned by the DDSE.
*/
List_iterator<const Plugin_tablespace> tablespace_it(ddse_tablespaces);
const Plugin_tablespace *tablespace = nullptr;
while ((tablespace = tablespace_it++)) {
// Add the name and the object instance to the registry with the
// appropriate property.
if (my_strcasecmp(system_charset_info, MYSQL_TABLESPACE_NAME.str,
tablespace->get_name()) == 0)
System_tablespaces::instance()->add(
tablespace->get_name(), System_tablespaces::Types::DD, tablespace);
else
System_tablespaces::instance()->add(
tablespace->get_name(), System_tablespaces::Types::PREDEFINED_DDSE,
tablespace);
}
return false;
}
// Initialize the data dictionary.
bool initialize_dictionary(THD *thd, Dictionary_impl *d) {
store_predefined_tablespace_metadata(thd);
if (create_dd_schema(thd) || initialize_dd_properties(thd) ||
create_tables(thd, nullptr))
return true;
if (DDSE_dict_recover(thd, DICT_RECOVERY_INITIALIZE_SERVER,
d->get_target_dd_version()) ||
flush_meta_data(thd) ||
DDSE_dict_recover(thd, DICT_RECOVERY_INITIALIZE_TABLESPACES,
d->get_target_dd_version()) ||
populate_tables(thd) ||
update_properties(thd, nullptr, nullptr,
String_type(MYSQL_SCHEMA_NAME.str)) ||
verify_contents(thd) || update_versions(thd))
return true;
DBUG_EXECUTE_IF(
"schema_read_only",
if (dd::execute_query(thd, "CREATE SCHEMA schema_read_only") ||
dd::execute_query(thd, "ALTER SCHEMA schema_read_only READ ONLY=1") ||
dd::execute_query(thd, "CREATE TABLE schema_read_only.t(i INT)") ||
dd::execute_query(thd, "DROP SCHEMA schema_read_only"))
assert(false););
bootstrap::DD_bootstrap_ctx::instance().set_stage(bootstrap::Stage::FINISHED);
return false;
}
// First time server start and initialization of the data dictionary.
bool initialize(THD *thd) {
bootstrap::DD_bootstrap_ctx::instance().set_stage(bootstrap::Stage::STARTED);
/*
Set tx_read_only to false to allow installing DD tables even
if the server is started with --transaction-read-only=true.
*/
thd->variables.transaction_read_only = false;
thd->tx_read_only = false;
Disable_autocommit_guard autocommit_guard(thd);
Dictionary_impl *d = dd::Dictionary_impl::instance();
assert(d);
cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
/*
Each step in the install process below is committed independently,
either implicitly (for e.g. "CREATE TABLE") or explicitly (for the
operations in the "populate()" methods). Thus, there is no need to
commit explicitly here.
*/
if (DDSE_dict_init(thd, DICT_INIT_CREATE_FILES, d->get_target_dd_version()) ||
initialize_dictionary(thd, d))
return true;
assert(d->get_target_dd_version() == d->get_actual_dd_version(thd));
LogErr(INFORMATION_LEVEL, ER_DD_VERSION_INSTALLED,
d->get_target_dd_version());
return false;
}
// Initialize dictionary in case of server restart.
bool restart_dictionary(THD *thd) {
Disable_autocommit_guard autocommit_guard(thd);
Dictionary_impl *d = dd::Dictionary_impl::instance();
assert(d);
cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
if (bootstrap::DDSE_dict_init(thd, DICT_INIT_CHECK_FILES,
d->get_target_dd_version())) {
LogErr(ERROR_LEVEL, ER_DD_SE_INIT_FAILED);
return true;
}
// RAII to handle error messages.
dd::upgrade::Bootstrap_error_handler bootstrap_error_handler;
// RAII to handle error in execution of CREATE TABLE.
Key_length_error_handler key_error_handler;
/*
Ignore ER_TOO_LONG_KEY for dictionary tables during restart.
Do not print the error in error log as we are creating only the
cached objects and not physical tables.
TODO: Workaround due to bug#20629014. Remove when the bug is fixed.
*/
thd->push_internal_handler(&key_error_handler);
bootstrap_error_handler.set_log_error(false);
bootstrap::DD_bootstrap_ctx::instance().set_stage(bootstrap::Stage::STARTED);
/*
Set tx_read_only to false to allow installing DD tables even
if the server is started with --transaction-read-only=true.
*/
thd->variables.transaction_read_only = false;
thd->tx_read_only = false;
// Set explicit_defaults_for_timestamp variable for dictionary creation
thd->variables.explicit_defaults_for_timestamp = true;
store_predefined_tablespace_metadata(thd);
if (create_dd_schema(thd) || initialize_dd_properties(thd) ||
create_tables(thd, nullptr) || sync_meta_data(thd) ||
DDSE_dict_recover(thd, DICT_RECOVERY_RESTART_SERVER,
d->get_actual_dd_version(thd)) ||
upgrade::do_server_upgrade_checks(thd) || upgrade::upgrade_tables(thd) ||
repopulate_charsets_and_collations(thd) || verify_contents(thd) ||
update_versions(thd)) {
bootstrap_error_handler.set_log_error(true);
thd->pop_internal_handler();
return true;
}
DBUG_EXECUTE_IF(
"schema_read_only",
if (dd::execute_query(thd, "CREATE SCHEMA schema_read_only") ||
dd::execute_query(thd, "ALTER SCHEMA schema_read_only READ ONLY=1") ||
dd::execute_query(thd, "CREATE TABLE schema_read_only.t(i INT)") ||
dd::execute_query(thd, "DROP SCHEMA schema_read_only") ||
dd::execute_query(thd, "CREATE TABLE IF NOT EXISTS S.restart(i INT)"))
assert(false););
bootstrap::DD_bootstrap_ctx::instance().set_stage(bootstrap::Stage::FINISHED);
LogErr(INFORMATION_LEVEL, ER_DD_VERSION_FOUND, d->get_actual_dd_version(thd));
bootstrap_error_handler.set_log_error(true);
thd->pop_internal_handler();
return false;
}
// Initialize dictionary in case of server restart.
void recover_innodb_upon_upgrade(THD *thd) {
Dictionary_impl *d = dd::Dictionary_impl::instance();
store_predefined_tablespace_metadata(thd);
// RAII to handle error in execution of CREATE TABLE.
Key_length_error_handler key_error_handler;
/*
Ignore ER_TOO_LONG_KEY for dictionary tables during restart.
Do not print the error in error log as we are creating only the
cached objects and not physical tables.
TODO: Workaround due to bug#20629014. Remove when the bug is fixed.
*/
thd->push_internal_handler(&key_error_handler);
if (create_dd_schema(thd) || initialize_dd_properties(thd) ||
create_tables(thd, nullptr) ||
DDSE_dict_recover(thd, DICT_RECOVERY_RESTART_SERVER,
d->get_actual_dd_version(thd))) {
// Error is not be handled in this case as we are on cleanup code path.
LogErr(WARNING_LEVEL, ER_DD_INIT_UPGRADE_FAILED);
}
thd->pop_internal_handler();
return;
}
bool setup_dd_objects_and_collations(THD *thd) {
// Continue with server startup.
bootstrap::DD_bootstrap_ctx::instance().set_stage(
bootstrap::Stage::CREATED_TABLES);
/*