/
handler.cc
8973 lines (7716 loc) · 263 KB
/
handler.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) 2000, 2016, Oracle and/or its affiliates.
Copyright (c) 2009, 2023, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 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 Street, Fifth Floor, Boston, MA 02110-1335 USA */
/** @file handler.cc
@brief
Handler-calling-functions
*/
#include "mariadb.h"
#include <inttypes.h>
#include "sql_priv.h"
#include "unireg.h"
#include "rpl_rli.h"
#include "sql_cache.h" // query_cache, query_cache_*
#include "sql_connect.h" // global_table_stats
#include "key.h" // key_copy, key_unpack, key_cmp_if_same, key_cmp
#include "sql_table.h" // build_table_filename
#include "sql_parse.h" // check_stack_overrun
#include "sql_base.h" // TDC_element
#include "discover.h" // extension_based_table_discovery, etc
#include "log_event.h" // *_rows_log_event
#include "create_options.h"
#include <myisampack.h>
#include "transaction.h"
#include "myisam.h"
#include "probes_mysql.h"
#include <mysql/psi/mysql_table.h>
#include <pfs_transaction_provider.h>
#include <mysql/psi/mysql_transaction.h>
#include "debug_sync.h" // DEBUG_SYNC
#include "debug.h" // debug_decrement_counter
#include "sql_audit.h"
#include "ha_sequence.h"
#include "rowid_filter.h"
#include "mysys_err.h"
#include "optimizer_defaults.h"
#ifdef WITH_PARTITION_STORAGE_ENGINE
#include "ha_partition.h"
#endif
#ifdef WITH_ARIA_STORAGE_ENGINE
#include "../storage/maria/ha_maria.h"
#endif
#include "semisync_master.h"
#include "wsrep_mysqld.h"
#ifdef WITH_WSREP
#include "wsrep_binlog.h"
#include "wsrep_xid.h"
#include "wsrep_thd.h"
#include "wsrep_trans_observer.h" /* wsrep transaction hooks */
#include "wsrep_var.h" /* wsrep_hton_check() */
#endif /* WITH_WSREP */
/**
@def MYSQL_TABLE_LOCK_WAIT
Instrumentation helper for table io_waits.
@param OP the table operation to be performed
@param FLAGS per table operation flags.
@param PAYLOAD the code to instrument.
@sa MYSQL_END_TABLE_WAIT.
*/
#ifdef HAVE_PSI_TABLE_INTERFACE
#define MYSQL_TABLE_LOCK_WAIT(OP, FLAGS, PAYLOAD) \
{ \
if (m_psi != NULL) \
{ \
PSI_table_locker *locker; \
PSI_table_locker_state state; \
locker= PSI_TABLE_CALL(start_table_lock_wait) \
(& state, m_psi, OP, FLAGS, \
__FILE__, __LINE__); \
PAYLOAD \
if (locker != NULL) \
PSI_TABLE_CALL(end_table_lock_wait)(locker); \
} \
else \
{ \
PAYLOAD \
} \
}
#else
#define MYSQL_TABLE_LOCK_WAIT(OP, FLAGS, PAYLOAD) \
PAYLOAD
#endif
/*
While we have legacy_db_type, we have this array to
check for dups and to find handlerton from legacy_db_type.
Remove when legacy_db_type is finally gone
*/
st_plugin_int *hton2plugin[MAX_HA];
static handlerton *installed_htons[128];
#define BITMAP_STACKBUF_SIZE (128/8)
KEY_CREATE_INFO default_key_create_info=
{ HA_KEY_ALG_UNDEF, 0, 0, {NullS, 0}, {NullS, 0}, true, false };
/* number of entries in handlertons[] */
ulong total_ha= 0;
/* number of storage engines (from handlertons[]) that support 2pc */
ulong total_ha_2pc= 0;
#ifdef DBUG_ASSERT_EXISTS
/*
Number of non-mandatory 2pc handlertons whose initialization failed
to estimate total_ha_2pc value under supposition of the failures
have not occcured.
*/
ulong failed_ha_2pc= 0;
#endif
/* size of savepoint storage area (see ha_init) */
ulong savepoint_alloc_size= 0;
static const LEX_CSTRING sys_table_aliases[]=
{
{ STRING_WITH_LEN("INNOBASE") }, { STRING_WITH_LEN("INNODB") },
{ STRING_WITH_LEN("HEAP") }, { STRING_WITH_LEN("MEMORY") },
{ STRING_WITH_LEN("MERGE") }, { STRING_WITH_LEN("MRG_MYISAM") },
{ STRING_WITH_LEN("Maria") }, { STRING_WITH_LEN("Aria") },
{NullS, 0}
};
const LEX_CSTRING ha_row_type[]=
{
{ STRING_WITH_LEN("") },
{ STRING_WITH_LEN("FIXED") },
{ STRING_WITH_LEN("DYNAMIC") },
{ STRING_WITH_LEN("COMPRESSED") },
{ STRING_WITH_LEN("REDUNDANT") },
{ STRING_WITH_LEN("COMPACT") },
{ STRING_WITH_LEN("PAGE") }
};
const char *tx_isolation_names[]=
{ "READ-UNCOMMITTED", "READ-COMMITTED", "REPEATABLE-READ", "SERIALIZABLE",
NullS};
TYPELIB tx_isolation_typelib= {array_elements(tx_isolation_names)-1,"",
tx_isolation_names, NULL};
static TYPELIB known_extensions= {0,"known_exts", NULL, NULL};
uint known_extensions_id= 0;
class Table_exists_error_handler : public Internal_error_handler
{
public:
Table_exists_error_handler()
: m_handled_errors(0), m_unhandled_errors(0)
{}
bool handle_condition(THD *thd,
uint sql_errno,
const char* sqlstate,
Sql_condition::enum_warning_level *level,
const char* msg,
Sql_condition ** cond_hdl)
{
*cond_hdl= NULL;
if (non_existing_table_error(sql_errno))
{
m_handled_errors++;
return TRUE;
}
if (*level == Sql_condition::WARN_LEVEL_ERROR)
m_unhandled_errors++;
return FALSE;
}
bool safely_trapped_errors()
{
return ((m_handled_errors > 0) && (m_unhandled_errors == 0));
}
private:
int m_handled_errors;
int m_unhandled_errors;
};
static int commit_one_phase_2(THD *thd, bool all, THD_TRANS *trans,
bool is_real_trans);
static plugin_ref ha_default_plugin(THD *thd)
{
if (thd->variables.table_plugin)
return thd->variables.table_plugin;
return my_plugin_lock(thd, global_system_variables.table_plugin);
}
static plugin_ref ha_default_tmp_plugin(THD *thd)
{
if (thd->variables.tmp_table_plugin)
return thd->variables.tmp_table_plugin;
if (global_system_variables.tmp_table_plugin)
return my_plugin_lock(thd, global_system_variables.tmp_table_plugin);
return ha_default_plugin(thd);
}
/** @brief
Return the default storage engine handlerton for thread
SYNOPSIS
ha_default_handlerton(thd)
thd current thread
RETURN
pointer to handlerton
*/
handlerton *ha_default_handlerton(THD *thd)
{
plugin_ref plugin= ha_default_plugin(thd);
DBUG_ASSERT(plugin);
handlerton *hton= plugin_hton(plugin);
DBUG_ASSERT(hton);
return hton;
}
handlerton *ha_default_tmp_handlerton(THD *thd)
{
plugin_ref plugin= ha_default_tmp_plugin(thd);
DBUG_ASSERT(plugin);
handlerton *hton= plugin_hton(plugin);
DBUG_ASSERT(hton);
return hton;
}
/** @brief
Return the storage engine handlerton for the supplied name
SYNOPSIS
ha_resolve_by_name(thd, name)
thd current thread
name name of storage engine
RETURN
pointer to storage engine plugin handle
*/
plugin_ref ha_resolve_by_name(THD *thd, const LEX_CSTRING *name,
bool tmp_table)
{
const LEX_CSTRING *table_alias;
plugin_ref plugin;
redo:
if (thd && !my_charset_latin1.strnncoll(
(const uchar *)name->str, name->length,
(const uchar *)STRING_WITH_LEN("DEFAULT"), 0))
return tmp_table ? ha_default_tmp_plugin(thd) : ha_default_plugin(thd);
if ((plugin= my_plugin_lock_by_name(thd, name, MYSQL_STORAGE_ENGINE_PLUGIN)))
{
handlerton *hton= plugin_hton(plugin);
if (hton && !(hton->flags & HTON_NOT_USER_SELECTABLE))
return plugin;
/*
unlocking plugin immediately after locking is relatively low cost.
*/
plugin_unlock(thd, plugin);
}
/*
We check for the historical aliases.
*/
for (table_alias= sys_table_aliases; table_alias->str; table_alias+= 2)
{
if (!my_charset_latin1.strnncoll(
(const uchar *)name->str, name->length,
(const uchar *)table_alias->str, table_alias->length))
{
name= table_alias + 1;
goto redo;
}
}
return NULL;
}
bool
Storage_engine_name::resolve_storage_engine_with_error(THD *thd,
handlerton **ha,
bool tmp_table)
{
if (plugin_ref plugin= ha_resolve_by_name(thd, &m_storage_engine_name,
tmp_table))
{
*ha= plugin_hton(plugin);
return false;
}
*ha= NULL;
if (thd->variables.sql_mode & MODE_NO_ENGINE_SUBSTITUTION)
{
my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), m_storage_engine_name.str);
return true;
}
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
ER_UNKNOWN_STORAGE_ENGINE,
ER_THD(thd, ER_UNKNOWN_STORAGE_ENGINE),
m_storage_engine_name.str);
return false;
}
plugin_ref ha_lock_engine(THD *thd, const handlerton *hton)
{
if (hton)
{
st_plugin_int *plugin= hton2plugin[hton->slot];
return my_plugin_lock(thd, plugin_int_to_ref(plugin));
}
return NULL;
}
handlerton *ha_resolve_by_legacy_type(THD *thd, enum legacy_db_type db_type)
{
plugin_ref plugin;
switch (db_type) {
case DB_TYPE_DEFAULT:
return ha_default_handlerton(thd);
default:
if (db_type > DB_TYPE_UNKNOWN && db_type < DB_TYPE_DEFAULT &&
(plugin= ha_lock_engine(thd, installed_htons[db_type])))
return plugin_hton(plugin);
/* fall through */
case DB_TYPE_UNKNOWN:
return NULL;
}
}
/**
Use other database handler if databasehandler is not compiled in.
*/
handlerton *ha_checktype(THD *thd, handlerton *hton, bool no_substitute)
{
if (ha_storage_engine_is_enabled(hton))
return hton;
if (no_substitute)
return NULL;
#ifdef WITH_WSREP
(void)wsrep_after_rollback(thd, false);
#endif /* WITH_WSREP */
return ha_default_handlerton(thd);
} /* ha_checktype */
handler *get_new_handler(TABLE_SHARE *share, MEM_ROOT *alloc,
handlerton *db_type)
{
handler *file;
DBUG_ENTER("get_new_handler");
DBUG_PRINT("enter", ("alloc: %p", alloc));
if (ha_storage_engine_is_enabled(db_type))
{
if ((file= db_type->create(db_type, share, alloc)))
file->init();
DBUG_RETURN(file);
}
/*
Try the default table type
Here the call to current_thd() is ok as we call this function a lot of
times but we enter this branch very seldom.
*/
file= get_new_handler(share, alloc, ha_default_handlerton(current_thd));
DBUG_RETURN(file);
}
#ifdef WITH_PARTITION_STORAGE_ENGINE
handler *get_ha_partition(partition_info *part_info)
{
ha_partition *partition;
DBUG_ENTER("get_ha_partition");
if ((partition= new ha_partition(partition_hton, part_info)))
{
if (partition->initialize_partition(current_thd->mem_root))
{
delete partition;
partition= 0;
}
else
partition->init();
}
else
{
my_error(ER_OUTOFMEMORY, MYF(ME_FATAL),
static_cast<int>(sizeof(ha_partition)));
}
DBUG_RETURN(((handler*) partition));
}
#endif
static const char **handler_errmsgs;
C_MODE_START
static const char **get_handler_errmsgs(int nr)
{
return handler_errmsgs;
}
C_MODE_END
/**
Register handler error messages for use with my_error().
@retval
0 OK
@retval
!=0 Error
*/
int ha_init_errors(void)
{
#define SETMSG(nr, msg) handler_errmsgs[(nr) - HA_ERR_FIRST]= (msg)
/* Allocate a pointer array for the error message strings. */
/* Zerofill it to avoid uninitialized gaps. */
if (! (handler_errmsgs= (const char**) my_malloc(key_memory_handler_errmsgs,
HA_ERR_ERRORS * sizeof(char*),
MYF(MY_WME | MY_ZEROFILL))))
return 1;
/* Set the dedicated error messages. */
SETMSG(HA_ERR_KEY_NOT_FOUND, ER_DEFAULT(ER_KEY_NOT_FOUND));
SETMSG(HA_ERR_FOUND_DUPP_KEY, ER_DEFAULT(ER_DUP_KEY));
SETMSG(HA_ERR_RECORD_CHANGED, "Update which is recoverable");
SETMSG(HA_ERR_WRONG_INDEX, "Wrong index given to function");
SETMSG(HA_ERR_CRASHED, ER_DEFAULT(ER_NOT_KEYFILE));
SETMSG(HA_ERR_WRONG_IN_RECORD, ER_DEFAULT(ER_CRASHED_ON_USAGE));
SETMSG(HA_ERR_OUT_OF_MEM, "Table handler out of memory");
SETMSG(HA_ERR_NOT_A_TABLE, "Incorrect file format '%.64s'");
SETMSG(HA_ERR_WRONG_COMMAND, "Command not supported");
SETMSG(HA_ERR_OLD_FILE, ER_DEFAULT(ER_OLD_KEYFILE));
SETMSG(HA_ERR_NO_ACTIVE_RECORD, "No record read in update");
SETMSG(HA_ERR_RECORD_DELETED, "Intern record deleted");
SETMSG(HA_ERR_RECORD_FILE_FULL, ER_DEFAULT(ER_RECORD_FILE_FULL));
SETMSG(HA_ERR_INDEX_FILE_FULL, "No more room in index file '%.64s'");
SETMSG(HA_ERR_END_OF_FILE, "End in next/prev/first/last");
SETMSG(HA_ERR_UNSUPPORTED, ER_DEFAULT(ER_ILLEGAL_HA));
SETMSG(HA_ERR_TO_BIG_ROW, "Too big row");
SETMSG(HA_WRONG_CREATE_OPTION, "Wrong create option");
SETMSG(HA_ERR_FOUND_DUPP_UNIQUE, ER_DEFAULT(ER_DUP_UNIQUE));
SETMSG(HA_ERR_UNKNOWN_CHARSET, "Can't open charset");
SETMSG(HA_ERR_WRONG_MRG_TABLE_DEF, ER_DEFAULT(ER_WRONG_MRG_TABLE));
SETMSG(HA_ERR_CRASHED_ON_REPAIR, ER_DEFAULT(ER_CRASHED_ON_REPAIR));
SETMSG(HA_ERR_CRASHED_ON_USAGE, ER_DEFAULT(ER_CRASHED_ON_USAGE));
SETMSG(HA_ERR_LOCK_WAIT_TIMEOUT, ER_DEFAULT(ER_LOCK_WAIT_TIMEOUT));
SETMSG(HA_ERR_LOCK_TABLE_FULL, ER_DEFAULT(ER_LOCK_TABLE_FULL));
SETMSG(HA_ERR_READ_ONLY_TRANSACTION, ER_DEFAULT(ER_READ_ONLY_TRANSACTION));
SETMSG(HA_ERR_LOCK_DEADLOCK, ER_DEFAULT(ER_LOCK_DEADLOCK));
SETMSG(HA_ERR_CANNOT_ADD_FOREIGN, ER_DEFAULT(ER_CANNOT_ADD_FOREIGN));
SETMSG(HA_ERR_NO_REFERENCED_ROW, ER_DEFAULT(ER_NO_REFERENCED_ROW_2));
SETMSG(HA_ERR_ROW_IS_REFERENCED, ER_DEFAULT(ER_ROW_IS_REFERENCED_2));
SETMSG(HA_ERR_NO_SAVEPOINT, "No savepoint with that name");
SETMSG(HA_ERR_NON_UNIQUE_BLOCK_SIZE, "Non unique key block size");
SETMSG(HA_ERR_NO_SUCH_TABLE, "No such table: '%.64s'");
SETMSG(HA_ERR_TABLE_EXIST, ER_DEFAULT(ER_TABLE_EXISTS_ERROR));
SETMSG(HA_ERR_NO_CONNECTION, "Could not connect to storage engine");
SETMSG(HA_ERR_TABLE_DEF_CHANGED, ER_DEFAULT(ER_TABLE_DEF_CHANGED));
SETMSG(HA_ERR_FOREIGN_DUPLICATE_KEY, "FK constraint would lead to duplicate key");
SETMSG(HA_ERR_TABLE_NEEDS_UPGRADE, ER_DEFAULT(ER_TABLE_NEEDS_UPGRADE));
SETMSG(HA_ERR_TABLE_READONLY, ER_DEFAULT(ER_OPEN_AS_READONLY));
SETMSG(HA_ERR_AUTOINC_READ_FAILED, ER_DEFAULT(ER_AUTOINC_READ_FAILED));
SETMSG(HA_ERR_AUTOINC_ERANGE, ER_DEFAULT(ER_WARN_DATA_OUT_OF_RANGE));
SETMSG(HA_ERR_TOO_MANY_CONCURRENT_TRXS, ER_DEFAULT(ER_TOO_MANY_CONCURRENT_TRXS));
SETMSG(HA_ERR_INDEX_COL_TOO_LONG, ER_DEFAULT(ER_INDEX_COLUMN_TOO_LONG));
SETMSG(HA_ERR_INDEX_CORRUPT, ER_DEFAULT(ER_INDEX_CORRUPT));
SETMSG(HA_FTS_INVALID_DOCID, "Invalid InnoDB FTS Doc ID");
SETMSG(HA_ERR_DISK_FULL, ER_DEFAULT(ER_DISK_FULL));
SETMSG(HA_ERR_FTS_TOO_MANY_WORDS_IN_PHRASE, "Too many words in a FTS phrase or proximity search");
SETMSG(HA_ERR_FK_DEPTH_EXCEEDED, "Foreign key cascade delete/update exceeds");
SETMSG(HA_ERR_TABLESPACE_MISSING, ER_DEFAULT(ER_TABLESPACE_MISSING));
/* Register the error messages for use with my_error(). */
return my_error_register(get_handler_errmsgs, HA_ERR_FIRST, HA_ERR_LAST);
}
/**
Unregister handler error messages.
@retval
0 OK
@retval
!=0 Error
*/
static int ha_finish_errors(void)
{
/* Allocate a pointer array for the error message strings. */
my_error_unregister(HA_ERR_FIRST, HA_ERR_LAST);
my_free(handler_errmsgs);
handler_errmsgs= 0;
return 0;
}
static Atomic_counter<int32> need_full_discover_for_existence(0);
static Atomic_counter<int32> engines_with_discover_file_names(0);
static Atomic_counter<int32> engines_with_discover(0);
static int full_discover_for_existence(handlerton *, const char *, const char *)
{ return 0; }
static int ext_based_existence(handlerton *, const char *, const char *)
{ return 0; }
static int hton_ext_based_table_discovery(handlerton *hton, LEX_CSTRING *db,
MY_DIR *dir, handlerton::discovered_list *result)
{
/*
tablefile_extensions[0] is the metadata file, see
the comment above tablefile_extensions declaration
*/
return extension_based_table_discovery(dir, hton->tablefile_extensions[0],
result);
}
static void update_discovery_counters(handlerton *hton, int val)
{
if (hton->discover_table_existence == full_discover_for_existence)
need_full_discover_for_existence+= val;
if (hton->discover_table_names && hton->tablefile_extensions[0])
engines_with_discover_file_names+= val;
if (hton->discover_table)
engines_with_discover+= val;
}
int ha_drop_table(THD *thd, handlerton *hton, const char *path)
{
if (ha_check_if_updates_are_ignored(thd, hton, "DROP"))
return 0; // Simulate dropped
return hton->drop_table(hton, path);
}
static int hton_drop_table(handlerton *hton, const char *path)
{
char tmp_path[FN_REFLEN];
handler *file= get_new_handler(nullptr, current_thd->mem_root, hton);
if (!file)
{
/*
If file is not defined it means that the engine can't create a
handler if share is not set or we got an out of memory error
*/
return my_errno == ENOMEM ? ENOMEM : ENOENT;
}
path= get_canonical_filename(file, path, tmp_path);
int error= file->delete_table(path);
delete file;
return error;
}
int ha_finalize_handlerton(st_plugin_int *plugin)
{
int deinit_status= 0;
handlerton *hton= (handlerton *)plugin->data;
DBUG_ENTER("ha_finalize_handlerton");
/* hton can be NULL here, if ha_initialize_handlerton() failed. */
if (!hton)
goto end;
if (installed_htons[hton->db_type] == hton)
installed_htons[hton->db_type]= NULL;
if (hton->panic)
hton->panic(hton, HA_PANIC_CLOSE);
if (plugin->plugin->deinit)
deinit_status= plugin->plugin->deinit(NULL);
free_sysvar_table_options(hton);
update_discovery_counters(hton, -1);
/*
In case a plugin is uninstalled and re-installed later, it should
reuse an array slot. Otherwise the number of uninstall/install
cycles would be limited.
*/
if (hton->slot != HA_SLOT_UNDEF)
{
/* Make sure we are not unpluging another plugin */
DBUG_ASSERT(hton2plugin[hton->slot] == plugin);
DBUG_ASSERT(hton->slot < MAX_HA);
hton2plugin[hton->slot]= NULL;
}
my_free(hton);
end:
DBUG_RETURN(deinit_status);
}
/*
Get a pointer to the global engine optimizer costs (like
innodb.disk_read_cost) and store the pointer in the handlerton.
This is called once when a handlerton is created.
We also update the not set global costs with the default costs
to allow information_schema to print the real used values.
*/
static bool update_optimizer_costs(handlerton *hton)
{
OPTIMIZER_COSTS costs= default_optimizer_costs;
LEX_CSTRING *name= hton_name(hton);
if (hton->update_optimizer_costs)
hton->update_optimizer_costs(&costs);
mysql_mutex_lock(&LOCK_optimizer_costs);
hton->optimizer_costs= get_or_create_optimizer_costs(name->str,
name->length);
if (!hton->optimizer_costs)
{
mysql_mutex_unlock(&LOCK_optimizer_costs);
return 1; // OOM
}
/* Update not set values from current default costs */
for (uint i=0 ; i < sizeof(OPTIMIZER_COSTS)/sizeof(double) ; i++)
{
double *var= ((double*) hton->optimizer_costs)+i;
if (*var == OPTIMIZER_COST_UNDEF)
*var= ((double*) &costs)[i];
}
mysql_mutex_unlock(&LOCK_optimizer_costs);
return 0;
}
const char *hton_no_exts[]= { 0 };
int ha_initialize_handlerton(st_plugin_int *plugin)
{
handlerton *hton;
DBUG_ENTER("ha_initialize_handlerton");
DBUG_PRINT("plugin", ("initialize plugin: '%s'", plugin->name.str));
hton= (handlerton *)my_malloc(key_memory_handlerton, sizeof(handlerton),
MYF(MY_WME | MY_ZEROFILL));
if (hton == NULL)
{
sql_print_error("Unable to allocate memory for plugin '%s' handlerton.",
plugin->name.str);
goto err_no_hton_memory;
}
hton->tablefile_extensions= hton_no_exts;
hton->discover_table_names= hton_ext_based_table_discovery;
hton->drop_table= hton_drop_table;
hton->slot= HA_SLOT_UNDEF;
/* Historical Requirement */
plugin->data= hton; // shortcut for the future
if (plugin->plugin->init && plugin->plugin->init(hton))
{
sql_print_error("Plugin '%s' init function returned error.",
plugin->name.str);
goto err;
}
// hton_ext_based_table_discovery() works only when discovery
// is supported and the engine if file-based.
if (hton->discover_table_names == hton_ext_based_table_discovery &&
(!hton->discover_table || !hton->tablefile_extensions[0]))
hton->discover_table_names= NULL;
// default discover_table_existence implementation
if (!hton->discover_table_existence && hton->discover_table)
{
if (hton->tablefile_extensions[0])
hton->discover_table_existence= ext_based_existence;
else
hton->discover_table_existence= full_discover_for_existence;
}
uint tmp;
ulong fslot;
DBUG_EXECUTE_IF("unstable_db_type", {
static int i= (int) DB_TYPE_FIRST_DYNAMIC;
hton->db_type= (enum legacy_db_type)++i;
});
/* now check the db_type for conflict */
if (hton->db_type <= DB_TYPE_UNKNOWN ||
hton->db_type >= DB_TYPE_DEFAULT ||
installed_htons[hton->db_type])
{
int idx= (int) DB_TYPE_FIRST_DYNAMIC;
while (idx < (int) DB_TYPE_DEFAULT && installed_htons[idx])
idx++;
if (idx == (int) DB_TYPE_DEFAULT)
{
sql_print_warning("Too many storage engines!");
goto err_deinit;
}
if (hton->db_type != DB_TYPE_UNKNOWN)
sql_print_warning("Storage engine '%s' has conflicting typecode. "
"Assigning value %d.", plugin->plugin->name, idx);
hton->db_type= (enum legacy_db_type) idx;
}
/*
In case a plugin is uninstalled and re-installed later, it should
reuse an array slot. Otherwise the number of uninstall/install
cycles would be limited. So look for a free slot.
*/
DBUG_PRINT("plugin", ("total_ha: %lu", total_ha));
for (fslot= 0; fslot < total_ha; fslot++)
{
if (!hton2plugin[fslot])
break;
}
if (fslot < total_ha)
hton->slot= fslot;
else
{
if (total_ha >= MAX_HA)
{
sql_print_error("Too many plugins loaded. Limit is %lu. "
"Failed on '%s'", (ulong) MAX_HA, plugin->name.str);
goto err_deinit;
}
hton->slot= total_ha++;
}
installed_htons[hton->db_type]= hton;
tmp= hton->savepoint_offset;
hton->savepoint_offset= savepoint_alloc_size;
savepoint_alloc_size+= tmp;
hton2plugin[hton->slot]=plugin;
if (!(hton->flags & HTON_HIDDEN) && update_optimizer_costs(hton))
goto err_deinit;
if (hton->prepare)
{
total_ha_2pc++;
if (tc_log && tc_log != get_tc_log_implementation())
{
total_ha_2pc--;
hton->prepare= 0;
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_UNKNOWN_ERROR,
"Cannot enable tc-log at run-time. "
"XA features of %s are disabled",
plugin->name.str);
}
}
/*
This is entirely for legacy. We will create a new "disk based" hton and a
"memory" hton which will be configurable longterm. We should be able to
remove partition.
*/
switch (hton->db_type) {
case DB_TYPE_HEAP:
heap_hton= hton;
break;
case DB_TYPE_MYISAM:
myisam_hton= hton;
break;
case DB_TYPE_PARTITION_DB:
partition_hton= hton;
break;
case DB_TYPE_SEQUENCE:
sql_sequence_hton= hton;
break;
default:
break;
};
resolve_sysvar_table_options(hton);
update_discovery_counters(hton, 1);
DBUG_RETURN(0);
err_deinit:
/*
Let plugin do its inner deinitialization as plugin->init()
was successfully called before.
*/
if (plugin->plugin->deinit)
(void) plugin->plugin->deinit(NULL);
err:
#ifdef DBUG_ASSERT_EXISTS
if (hton->prepare)
failed_ha_2pc++;
#endif
my_free(hton);
err_no_hton_memory:
plugin->data= NULL;
DBUG_RETURN(1);
}
int ha_init()
{
int error= 0;
DBUG_ENTER("ha_init");
DBUG_ASSERT(total_ha < MAX_HA);
/*
Check if there is a transaction-capable storage engine besides the
binary log (which is considered a transaction-capable storage engine in
counting total_ha)
*/
opt_using_transactions= total_ha > (ulong) opt_bin_log;
savepoint_alloc_size+= sizeof(SAVEPOINT);
DBUG_RETURN(error);
}
int ha_end()
{
int error= 0;
DBUG_ENTER("ha_end");
/*
This should be eventually based on the graceful shutdown flag.
So if flag is equal to HA_PANIC_CLOSE, the deallocate
the errors.
*/
if (unlikely(ha_finish_errors()))
error= 1;
DBUG_RETURN(error);
}
static my_bool dropdb_handlerton(THD *unused1, plugin_ref plugin,
void *path)
{
handlerton *hton= plugin_hton(plugin);
if (hton->drop_database)
hton->drop_database(hton, (char *)path);
return FALSE;
}
void ha_drop_database(const char* path)
{
plugin_foreach(NULL, dropdb_handlerton, MYSQL_STORAGE_ENGINE_PLUGIN,
(char*) path);
}
static my_bool checkpoint_state_handlerton(THD *unused1, plugin_ref plugin,
void *disable)
{
handlerton *hton= plugin_hton(plugin);
if (hton->checkpoint_state)
hton->checkpoint_state(hton, (int) *(bool*) disable);
return FALSE;
}
void ha_checkpoint_state(bool disable)
{
plugin_foreach(NULL, checkpoint_state_handlerton, MYSQL_STORAGE_ENGINE_PLUGIN, &disable);
}
struct st_commit_checkpoint_request {
void *cookie;
void (*pre_hook)(void *);
};
static my_bool commit_checkpoint_request_handlerton(THD *unused1, plugin_ref plugin,
void *data)
{
st_commit_checkpoint_request *st= (st_commit_checkpoint_request *)data;
handlerton *hton= plugin_hton(plugin);
if (hton->commit_checkpoint_request)
{
void *cookie= st->cookie;
if (st->pre_hook)
(*st->pre_hook)(cookie);
(*hton->commit_checkpoint_request)(cookie);
}
return FALSE;
}
/*
Invoke commit_checkpoint_request() in all storage engines that implement it.
If pre_hook is non-NULL, the hook will be called prior to each invocation.
*/
void
ha_commit_checkpoint_request(void *cookie, void (*pre_hook)(void *))
{
st_commit_checkpoint_request st;
st.cookie= cookie;
st.pre_hook= pre_hook;
plugin_foreach(NULL, commit_checkpoint_request_handlerton,
MYSQL_STORAGE_ENGINE_PLUGIN, &st);
}
/**
@note
don't bother to rollback here, it's done already
there's no need to rollback here as all transactions must
be rolled back already
*/
void ha_close_connection(THD* thd)
{
for (auto i= 0; i < MAX_HA; i++)
{
if (plugin_ref plugin= thd->ha_data[i].lock)
{
thd->ha_data[i].lock= NULL;
handlerton *hton= plugin_hton(plugin);
if (hton->close_connection)
hton->close_connection(hton, thd);
thd_set_ha_data(thd, hton, 0);
plugin_unlock(NULL, plugin);
}
DBUG_ASSERT(!thd->ha_data[i].ha_ptr);
}
}
static my_bool kill_handlerton(THD *thd, plugin_ref plugin,
void *level)
{
handlerton *hton= plugin_hton(plugin);
mysql_mutex_assert_owner(&thd->LOCK_thd_data);
if (hton->kill_query && thd_get_ha_data(thd, hton))
hton->kill_query(hton, thd, *(enum thd_kill_levels *) level);
return FALSE;
}
void ha_kill_query(THD* thd, enum thd_kill_levels level)
{
DBUG_ENTER("ha_kill_query");
plugin_foreach(thd, kill_handlerton, MYSQL_STORAGE_ENGINE_PLUGIN, &level);
DBUG_VOID_RETURN;
}
static my_bool plugin_disable_internal_writes(THD *, plugin_ref plugin,
void *disable)
{
if (void(*diw)(bool)= plugin_hton(plugin)->disable_internal_writes)
diw(*static_cast<bool*>(disable));
return FALSE;
}
void ha_disable_internal_writes(bool disable)
{
plugin_foreach(NULL, plugin_disable_internal_writes,
MYSQL_STORAGE_ENGINE_PLUGIN, &disable);
}
static my_bool signal_ddl_recovery_done(THD *, plugin_ref plugin, void *)
{
handlerton *hton= plugin_hton(plugin);
if (hton->signal_ddl_recovery_done)
(hton->signal_ddl_recovery_done)(hton);
return 0;
}
void ha_signal_ddl_recovery_done()