/
log.cc
12467 lines (10729 loc) · 376 KB
/
log.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, 2018, Oracle and/or its affiliates.
Copyright (c) 2009, 2022, 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
@brief
logging of commands
@todo
Abort logging when we get an error in reading or writing log files
*/
#include "mariadb.h" /* NO_EMBEDDED_ACCESS_CHECKS */
#include "sql_priv.h"
#include "log.h"
#include "sql_base.h" // open_log_table
#include "sql_repl.h"
#include "sql_delete.h" // mysql_truncate
#include "sql_parse.h" // command_name
#include "sql_time.h" // calc_time_from_sec, my_time_compare
#include "tztime.h" // my_tz_OFFSET0, struct Time_zone
#include "log_event.h" // Query_log_event
#include "rpl_filter.h"
#include "rpl_rli.h"
#include "sql_audit.h"
#include "mysqld.h"
#include "ddl_log.h"
#include <my_dir.h>
#include <m_ctype.h> // For test_if_number
#include <set_var.h> // for Sys_last_gtid_ptr
#include <ilist.h>
#ifdef _WIN32
#include "message.h"
#endif
#include "sql_plugin.h"
#include "debug_sync.h"
#include "sql_show.h"
#include "my_pthread.h"
#include "semisync_master.h"
#include "sp_rcontext.h"
#include "sp_head.h"
#include "sql_table.h"
#include "wsrep_mysqld.h"
#ifdef WITH_WSREP
#include "wsrep_trans_observer.h"
#include "wsrep_status.h"
#endif /* WITH_WSREP */
#ifdef HAVE_REPLICATION
#include "semisync_master.h"
#include "semisync_slave.h"
#include <utility> // pair
#endif
/* max size of the log message */
#define MAX_LOG_BUFFER_SIZE 1024
#define MAX_TIME_SIZE 32
#define MY_OFF_T_UNDEF (~(my_off_t)0UL)
/* Truncate cache log files bigger than this */
#define CACHE_FILE_TRUNC_SIZE 65536
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
handlerton *binlog_hton;
LOGGER logger;
const char *log_bin_index= 0;
const char *log_bin_basename= 0;
MYSQL_BIN_LOG mysql_bin_log(&sync_binlog_period);
static bool test_if_number(const char *str,
ulong *res, bool allow_wildcards);
static int binlog_init(void *p);
static int binlog_close_connection(handlerton *hton, THD *thd);
static int binlog_savepoint_set(handlerton *hton, THD *thd, void *sv);
static int binlog_savepoint_rollback(handlerton *hton, THD *thd, void *sv);
static bool binlog_savepoint_rollback_can_release_mdl(handlerton *hton,
THD *thd);
static int binlog_rollback(handlerton *hton, THD *thd, bool all);
static int binlog_prepare(handlerton *hton, THD *thd, bool all);
static int binlog_start_consistent_snapshot(handlerton *hton, THD *thd);
static int binlog_flush_cache(THD *thd, binlog_cache_mngr *cache_mngr,
Log_event *end_ev, bool all, bool using_stmt,
bool using_trx, bool is_ro_1pc);
static int binlog_online_alter_end_trans(THD *thd, bool all, bool commit);
static const LEX_CSTRING write_error_msg=
{ STRING_WITH_LEN("error writing to the binary log") };
static my_bool opt_optimize_thread_scheduling= TRUE;
ulong binlog_checksum_options;
#ifndef DBUG_OFF
ulong opt_binlog_dbug_fsync_sleep= 0;
#endif
mysql_mutex_t LOCK_prepare_ordered;
mysql_cond_t COND_prepare_ordered;
mysql_mutex_t LOCK_after_binlog_sync;
mysql_mutex_t LOCK_commit_ordered;
static ulonglong binlog_status_var_num_commits;
static ulonglong binlog_status_var_num_group_commits;
static ulonglong binlog_status_group_commit_trigger_count;
static ulonglong binlog_status_group_commit_trigger_lock_wait;
static ulonglong binlog_status_group_commit_trigger_timeout;
static char binlog_snapshot_file[FN_REFLEN];
static ulonglong binlog_snapshot_position;
static const char *fatal_log_error=
"Could not use %s for logging (error %d). "
"Turning logging off for the whole duration of the MariaDB server process. "
"To turn it on again: fix the cause, shutdown the MariaDB server and "
"restart it.";
static SHOW_VAR binlog_status_vars_detail[]=
{
{"commits",
(char *)&binlog_status_var_num_commits, SHOW_LONGLONG},
{"group_commits",
(char *)&binlog_status_var_num_group_commits, SHOW_LONGLONG},
{"group_commit_trigger_count",
(char *)&binlog_status_group_commit_trigger_count, SHOW_LONGLONG},
{"group_commit_trigger_lock_wait",
(char *)&binlog_status_group_commit_trigger_lock_wait, SHOW_LONGLONG},
{"group_commit_trigger_timeout",
(char *)&binlog_status_group_commit_trigger_timeout, SHOW_LONGLONG},
{"snapshot_file",
(char *)&binlog_snapshot_file, SHOW_CHAR},
{"snapshot_position",
(char *)&binlog_snapshot_position, SHOW_LONGLONG},
{NullS, NullS, SHOW_LONG}
};
/*
Variables for the binlog background thread.
Protected by the MYSQL_BIN_LOG::LOCK_binlog_background_thread mutex.
*/
static bool binlog_background_thread_started= false;
static bool binlog_background_thread_stop= false;
static MYSQL_BIN_LOG::xid_count_per_binlog *
binlog_background_thread_queue= NULL;
static bool start_binlog_background_thread();
static rpl_binlog_state rpl_global_gtid_binlog_state;
void setup_log_handling()
{
rpl_global_gtid_binlog_state.init();
}
/**
purge logs, master and slave sides both, related error code
converter.
Called from @c purge_error_message(), @c MYSQL_BIN_LOG::reset_logs()
@param res an internal to purging routines error code
@return the user level error code ER_*
*/
uint purge_log_get_error_code(int res)
{
uint errcode= 0;
switch (res) {
case 0: break;
case LOG_INFO_EOF: errcode= ER_UNKNOWN_TARGET_BINLOG; break;
case LOG_INFO_IO: errcode= ER_IO_ERR_LOG_INDEX_READ; break;
case LOG_INFO_INVALID:errcode= ER_BINLOG_PURGE_PROHIBITED; break;
case LOG_INFO_SEEK: errcode= ER_FSEEK_FAIL; break;
case LOG_INFO_MEM: errcode= ER_OUT_OF_RESOURCES; break;
case LOG_INFO_FATAL: errcode= ER_BINLOG_PURGE_FATAL_ERR; break;
case LOG_INFO_IN_USE: errcode= ER_LOG_IN_USE; break;
case LOG_INFO_EMFILE: errcode= ER_BINLOG_PURGE_EMFILE; break;
default: errcode= ER_LOG_PURGE_UNKNOWN_ERR; break;
}
return errcode;
}
/**
Silence all errors and warnings reported when performing a write
to a log table.
Errors and warnings are not reported to the client or SQL exception
handlers, so that the presence of logging does not interfere and affect
the logic of an application.
*/
class Silence_log_table_errors : public Internal_error_handler
{
char m_message[MYSQL_ERRMSG_SIZE];
public:
Silence_log_table_errors()
{
m_message[0]= '\0';
}
virtual ~Silence_log_table_errors() = default;
virtual bool handle_condition(THD *thd,
uint sql_errno,
const char* sql_state,
Sql_condition::enum_warning_level *level,
const char* msg,
Sql_condition ** cond_hdl);
const char *message() const { return m_message; }
};
bool
Silence_log_table_errors::handle_condition(THD *,
uint,
const char*,
Sql_condition::enum_warning_level*,
const char* msg,
Sql_condition ** cond_hdl)
{
*cond_hdl= NULL;
strmake_buf(m_message, msg);
return TRUE;
}
sql_print_message_func sql_print_message_handlers[3] =
{
sql_print_information,
sql_print_warning,
sql_print_error
};
/**
Create the name of the log file
@param[OUT] out a pointer to a new allocated name will go there
@param[IN] log_ext The extension for the file (e.g .log)
@param[IN] once whether to use malloc_once or a normal malloc.
*/
void make_default_log_name(char **out, const char* log_ext, bool once)
{
char buff[FN_REFLEN+10];
fn_format(buff, opt_log_basename, "", log_ext, MYF(MY_REPLACE_EXT));
if (once)
*out= my_once_strdup(buff, MYF(MY_WME));
else
{
my_free(*out);
*out= my_strdup(PSI_INSTRUMENT_ME, buff, MYF(MY_WME));
}
}
/*
Helper classes to store non-transactional and transactional data
before copying it to the binary log.
*/
class binlog_cache_data
{
public:
binlog_cache_data(): before_stmt_pos(MY_OFF_T_UNDEF), m_pending(0), status(0),
incident(FALSE), saved_max_binlog_cache_size(0), ptr_binlog_cache_use(0),
ptr_binlog_cache_disk_use(0)
{ }
~binlog_cache_data()
{
DBUG_ASSERT(empty());
close_cached_file(&cache_log);
}
/*
Return 1 if there is no relevant entries in the cache
This is:
- Cache is empty
- There are row or critical (DDL?) events in the cache
The status test is needed to avoid writing entries with only
a table map entry, which would crash in do_apply_event() on the slave
as it assumes that there is always a row entry after a table map.
*/
bool empty() const
{
return (pending() == NULL &&
(my_b_write_tell(&cache_log) == 0 ||
((status & (LOGGED_ROW_EVENT | LOGGED_CRITICAL)) == 0)));
}
Rows_log_event *pending() const
{
return m_pending;
}
void set_pending(Rows_log_event *const pending_arg)
{
m_pending= pending_arg;
}
void set_incident(void)
{
incident= TRUE;
}
bool has_incident(void)
{
return(incident);
}
void reset()
{
bool cache_was_empty= empty();
bool truncate_file= (cache_log.file != -1 &&
my_b_write_tell(&cache_log) > CACHE_FILE_TRUNC_SIZE);
truncate(0,1); // Forget what's in cache
if (!cache_was_empty)
compute_statistics();
if (truncate_file)
my_chsize(cache_log.file, 0, 0, MYF(MY_WME));
status= 0;
incident= FALSE;
before_stmt_pos= MY_OFF_T_UNDEF;
DBUG_ASSERT(empty());
}
my_off_t get_byte_position() const
{
return my_b_tell(&cache_log);
}
my_off_t get_prev_position()
{
return(before_stmt_pos);
}
void set_prev_position(my_off_t pos)
{
before_stmt_pos= pos;
}
void restore_prev_position()
{
truncate(before_stmt_pos);
}
void restore_savepoint(my_off_t pos)
{
truncate(pos);
if (pos < before_stmt_pos)
before_stmt_pos= MY_OFF_T_UNDEF;
}
void set_binlog_cache_info(my_off_t param_max_binlog_cache_size,
ulong *param_ptr_binlog_cache_use,
ulong *param_ptr_binlog_cache_disk_use)
{
/*
The assertions guarantee that the set_binlog_cache_info is
called just once and information passed as parameters are
never zero.
This is done while calling the constructor binlog_cache_mngr.
We cannot set information in the constructor binlog_cache_data
because the space for binlog_cache_mngr is allocated through
a placement new.
In the future, we can refactor this and change it to avoid
the set_binlog_info.
*/
DBUG_ASSERT(saved_max_binlog_cache_size == 0);
DBUG_ASSERT(param_max_binlog_cache_size != 0);
DBUG_ASSERT(ptr_binlog_cache_use == 0);
DBUG_ASSERT(param_ptr_binlog_cache_use != 0);
DBUG_ASSERT(ptr_binlog_cache_disk_use == 0);
DBUG_ASSERT(param_ptr_binlog_cache_disk_use != 0);
saved_max_binlog_cache_size= param_max_binlog_cache_size;
ptr_binlog_cache_use= param_ptr_binlog_cache_use;
ptr_binlog_cache_disk_use= param_ptr_binlog_cache_disk_use;
cache_log.end_of_file= saved_max_binlog_cache_size;
}
void add_status(enum_logged_status status_arg)
{
status|= status_arg;
}
/*
Cache to store data before copying it to the binary log.
*/
IO_CACHE cache_log;
protected:
/*
Binlog position before the start of the current statement.
*/
my_off_t before_stmt_pos;
private:
/*
Pending binrows event. This event is the event where the rows are currently
written.
*/
Rows_log_event *m_pending;
/*
Bit flags for what has been writing to cache. Used to
discard logs without any data changes.
see enum_logged_status;
*/
uint32 status;
/*
This indicates that some events did not get into the cache and most likely
it is corrupted.
*/
bool incident;
/**
This function computes binlog cache and disk usage.
*/
void compute_statistics()
{
statistic_increment(*ptr_binlog_cache_use, &LOCK_status);
if (cache_log.disk_writes != 0)
{
#ifdef REAL_STATISTICS
statistic_add(*ptr_binlog_cache_disk_use,
cache_log.disk_writes, &LOCK_status);
#else
statistic_increment(*ptr_binlog_cache_disk_use, &LOCK_status);
#endif
cache_log.disk_writes= 0;
}
}
/*
Stores the values of maximum size of the cache allowed when this cache
is configured. This corresponds to either
. max_binlog_cache_size or max_binlog_stmt_cache_size.
*/
my_off_t saved_max_binlog_cache_size;
/*
Stores a pointer to the status variable that keeps track of the in-memory
cache usage. This corresponds to either
. binlog_cache_use or binlog_stmt_cache_use.
*/
ulong *ptr_binlog_cache_use;
/*
Stores a pointer to the status variable that keeps track of the disk
cache usage. This corresponds to either
. binlog_cache_disk_use or binlog_stmt_cache_disk_use.
*/
ulong *ptr_binlog_cache_disk_use;
/*
It truncates the cache to a certain position. This includes deleting the
pending event.
*/
void truncate(my_off_t pos, bool reset_cache=0)
{
DBUG_PRINT("info", ("truncating to position %lu", (ulong) pos));
cache_log.error=0;
if (pending())
{
delete pending();
set_pending(0);
}
reinit_io_cache(&cache_log, WRITE_CACHE, pos, 0, reset_cache);
cache_log.end_of_file= saved_max_binlog_cache_size;
}
binlog_cache_data& operator=(const binlog_cache_data& info);
binlog_cache_data(const binlog_cache_data& info);
};
class online_alter_cache_data: public Sql_alloc, public ilist_node<>,
public binlog_cache_data
{
public:
void store_prev_position()
{
before_stmt_pos= my_b_write_tell(&cache_log);
}
handlerton *hton;
Cache_flip_event_log *sink_log;
SAVEPOINT *sv_list;
};
void Log_event_writer::add_status(enum_logged_status status)
{
if (likely(cache_data))
cache_data->add_status(status);
}
void Log_event_writer::set_incident()
{
cache_data->set_incident();
}
class binlog_cache_mngr {
public:
binlog_cache_mngr(my_off_t param_max_binlog_stmt_cache_size,
my_off_t param_max_binlog_cache_size,
ulong *param_ptr_binlog_stmt_cache_use,
ulong *param_ptr_binlog_stmt_cache_disk_use,
ulong *param_ptr_binlog_cache_use,
ulong *param_ptr_binlog_cache_disk_use)
: last_commit_pos_offset(0), using_xa(FALSE), xa_xid(0)
{
stmt_cache.set_binlog_cache_info(param_max_binlog_stmt_cache_size,
param_ptr_binlog_stmt_cache_use,
param_ptr_binlog_stmt_cache_disk_use);
trx_cache.set_binlog_cache_info(param_max_binlog_cache_size,
param_ptr_binlog_cache_use,
param_ptr_binlog_cache_disk_use);
last_commit_pos_file[0]= 0;
}
void reset(bool do_stmt, bool do_trx)
{
if (do_stmt)
stmt_cache.reset();
if (do_trx)
{
trx_cache.reset();
using_xa= FALSE;
last_commit_pos_file[0]= 0;
last_commit_pos_offset= 0;
}
}
binlog_cache_data* get_binlog_cache_data(bool is_transactional)
{
return (is_transactional ? &trx_cache : &stmt_cache);
}
IO_CACHE* get_binlog_cache_log(bool is_transactional)
{
return (is_transactional ? &trx_cache.cache_log : &stmt_cache.cache_log);
}
binlog_cache_data stmt_cache;
binlog_cache_data trx_cache;
/*
Binlog position for current transaction.
For START TRANSACTION WITH CONSISTENT SNAPSHOT, this is the binlog
position corresponding to the snapshot taken. During (and after) commit,
this is set to the binlog position corresponding to just after the
commit (so storage engines can store it in their transaction log).
*/
char last_commit_pos_file[FN_REFLEN];
my_off_t last_commit_pos_offset;
/*
Flag set true if this transaction is committed with log_xid() as part of
XA, false if not.
*/
bool using_xa;
my_xid xa_xid;
bool need_unlog;
/*
Id of binlog that transaction was written to; only needed if need_unlog is
true.
*/
ulong binlog_id;
/* Set if we get an error during commit that must be returned from unlog(). */
bool delayed_error;
//Will be reset when gtid is written into binlog
uchar gtid_flags3;
decltype (rpl_gtid::seq_no) sa_seq_no;
private:
binlog_cache_mngr& operator=(const binlog_cache_mngr& info);
binlog_cache_mngr(const binlog_cache_mngr& info);
};
/**
The function handles the first phase of two-phase binlogged ALTER.
On master binlogs START ALTER when that is configured to do so.
On slave START ALTER gets binlogged and its gtid committed into gtid slave pos
table.
@param thd Thread handle.
@param start_alter_id Start Alter identifier or zero.
@param[out]
partial_alter Is set to true when Start Alter phase is completed.
@param if_exists True indicates the binary logging of the query
should be done with "if exists" option.
@return false on success, true on failure
@return @c partial_alter set to @c true when START ALTER phase
has been completed
*/
bool write_bin_log_start_alter(THD *thd, bool& partial_alter,
uint64 start_alter_id, bool if_exists)
{
#if defined(HAVE_REPLICATION)
if (thd->variables.option_bits & OPTION_BIN_TMP_LOG_OFF)
return false;
if (start_alter_id)
{
if (thd->rgi_slave->get_finish_event_group_called())
return false; // can get here through retrying
DBUG_EXECUTE_IF("at_write_start_alter", {
debug_sync_set_action(thd,
STRING_WITH_LEN("now wait_for alter_cont"));
});
Master_info *mi= thd->rgi_slave->rli->mi;
start_alter_info *info= thd->rgi_slave->sa_info;
bool is_shutdown= false;
info->sa_seq_no= start_alter_id;
info->domain_id= thd->variables.gtid_domain_id;
mysql_mutex_lock(&mi->start_alter_list_lock);
// possible stop-slave's marking of the whole alter state list is checked
is_shutdown= mi->is_shutdown;
mi->start_alter_list.push_back(info, &mi->mem_root);
mysql_mutex_unlock(&mi->start_alter_list_lock);
info->state= start_alter_state::REGISTERED;
thd->rgi_slave->commit_orderer.wait_for_prior_commit(thd);
thd->rgi_slave->start_alter_ev->update_pos(thd->rgi_slave);
if (mysql_bin_log.is_open())
{
Write_log_with_flags wlwf (thd, Gtid_log_event::FL_START_ALTER_E1);
if (write_bin_log(thd, true, thd->query(), thd->query_length()))
{
DBUG_ASSERT(thd->is_error());
return true;
}
}
thd->rgi_slave->mark_start_commit();
thd->wakeup_subsequent_commits(0);
thd->rgi_slave->finish_start_alter_event_group();
if (is_shutdown)
{
/* SA exists abruptly and will notify any CA|RA waiter. */
mysql_mutex_lock(&mi->start_alter_lock);
/*
If there is (or will be) unlikely any CA it will execute
the whole query before to stop itself.
*/
info->direct_commit_alter= true;
info->state= start_alter_state::ROLLBACK_ALTER;
mysql_mutex_unlock(&mi->start_alter_lock);
return true;
}
return false;
}
#endif
#ifndef WITH_WSREP
rpl_group_info *rgi= thd->rgi_slave ? thd->rgi_slave : thd->rgi_fake;
#else
rpl_group_info *rgi= thd->slave_thread ? thd->rgi_slave :
WSREP(thd) ? (thd->wsrep_rgi ? thd->wsrep_rgi : thd->rgi_fake) :
thd->rgi_fake;
#endif
if (!rgi && thd->variables.binlog_alter_two_phase)
{
/* slave applier can handle here only regular ALTER */
DBUG_ASSERT(!rgi || !(rgi->gtid_ev_flags_extra &
(Gtid_log_event::FL_START_ALTER_E1 |
Gtid_log_event::FL_COMMIT_ALTER_E1 |
Gtid_log_event::FL_ROLLBACK_ALTER_E1)));
/*
After logging binlog state stays flagged with SA flags3 an seq_no.
The state is not reset after write_bin_log() is done which is
deferred for the second logging phase.
*/
thd->set_binlog_flags_for_alter(Gtid_log_event::FL_START_ALTER_E1);
if(write_bin_log_with_if_exists(thd, false, false, if_exists, false))
{
DBUG_ASSERT(thd->is_error());
thd->set_binlog_flags_for_alter(0);
return true;
}
partial_alter= true;
}
else if (rgi && rgi->direct_commit_alter)
{
DBUG_ASSERT(rgi->gtid_ev_flags_extra &
Gtid_log_event::FL_COMMIT_ALTER_E1);
partial_alter= true;
}
return false;
}
bool LOGGER::is_log_table_enabled(uint log_table_type)
{
switch (log_table_type) {
case QUERY_LOG_SLOW:
return (table_log_handler != NULL) && global_system_variables.sql_log_slow
&& (log_output_options & LOG_TABLE);
case QUERY_LOG_GENERAL:
return (table_log_handler != NULL) && opt_log
&& (log_output_options & LOG_TABLE);
default:
DBUG_ASSERT(0);
return FALSE; /* make compiler happy */
}
}
/**
Check if a given table is opened log table
@param table Table to check
@param check_if_opened Only fail if it's a log table in use
@param error_msg String to put in error message if not ok.
No error message if 0
@return 0 ok
@return # Type of log file
*/
int check_if_log_table(const TABLE_LIST *table,
bool check_if_opened,
const char *error_msg)
{
int result= 0;
if (table->db.length == 5 &&
!my_strcasecmp(table_alias_charset, table->db.str, "mysql"))
{
const char *table_name= table->table_name.str;
if (table->table_name.length == 11 &&
!my_strcasecmp(table_alias_charset, table_name, "general_log"))
{
result= QUERY_LOG_GENERAL;
goto end;
}
if (table->table_name.length == 8 &&
!my_strcasecmp(table_alias_charset, table_name, "slow_log"))
{
result= QUERY_LOG_SLOW;
goto end;
}
}
return 0;
end:
if (!check_if_opened || logger.is_log_table_enabled(result))
{
if (error_msg)
my_error(ER_BAD_LOG_STATEMENT, MYF(0), error_msg);
return result;
}
return 0;
}
Log_to_csv_event_handler::Log_to_csv_event_handler() = default;
Log_to_csv_event_handler::~Log_to_csv_event_handler() = default;
void Log_to_csv_event_handler::cleanup()
{
logger.is_log_tables_initialized= FALSE;
}
/* log event handlers */
/**
Log command to the general log table
Log given command to the general log table.
@param event_time command start timestamp
@param user_host the pointer to the string with user@host info
@param user_host_len length of the user_host string. this is computed
once and passed to all general log event handlers
@param thread_id Id of the thread, issued a query
@param command_type the type of the command being logged
@param command_type_len the length of the string above
@param sql_text the very text of the query being executed
@param sql_text_len the length of sql_text string
@return This function attempts to never call my_error(). This is
necessary, because general logging happens already after a statement
status has been sent to the client, so the client can not see the
error anyway. Besides, the error is not related to the statement
being executed and is internal, and thus should be handled
internally (@todo: how?).
If a write to the table has failed, the function attempts to
write to a short error message to the file. The failure is also
indicated in the return value.
@retval FALSE OK
@retval TRUE error occurred
*/
bool Log_to_csv_event_handler::
log_general(THD *thd, my_hrtime_t event_time, const char *user_host, size_t user_host_len, my_thread_id thread_id_arg,
const char *command_type, size_t command_type_len,
const char *sql_text, size_t sql_text_len,
CHARSET_INFO *client_cs)
{
TABLE_LIST table_list;
TABLE *table;
bool result= TRUE;
bool need_close= FALSE;
bool need_pop= FALSE;
bool need_rnd_end= FALSE;
uint field_index;
Silence_log_table_errors error_handler;
Open_tables_backup open_tables_backup;
THD::used_t save_time_zone_used= thd->used & THD::TIME_ZONE_USED;
DBUG_ENTER("log_general");
/*
CSV uses TIME_to_timestamp() internally if table needs to be repaired
which will set TIME_ZONE_USED
*/
table_list.init_one_table(&MYSQL_SCHEMA_NAME, &GENERAL_LOG_NAME, 0,
TL_WRITE_CONCURRENT_INSERT);
/*
1) open_log_table generates an error of the
table can not be opened or is corrupted.
2) "INSERT INTO general_log" can generate warning sometimes.
Suppress these warnings and errors, they can't be dealt with
properly anyway.
QQ: this problem needs to be studied in more detail.
Comment this 2 lines and run "cast.test" to see what's happening.
*/
thd->push_internal_handler(& error_handler);
need_pop= TRUE;
if (!(table= open_log_table(thd, &table_list, &open_tables_backup)))
goto err;
need_close= TRUE;
if (table->file->extra(HA_EXTRA_MARK_AS_LOG_TABLE) ||
table->file->ha_rnd_init_with_error(0))
goto err;
need_rnd_end= TRUE;
/* Honor next number columns if present */
table->next_number_field= table->found_next_number_field;
/*
NOTE: we do not call restore_record() here, as all fields are
filled by the Logger (=> no need to load default ones).
*/
/*
We do not set a value for table->field[0], as it will use
default value (which is CURRENT_TIMESTAMP).
*/
/* check that all columns exist */
if (table->s->fields < 6)
goto err;
DBUG_ASSERT(table->field[0]->type() == MYSQL_TYPE_TIMESTAMP);
table->field[0]->store_timestamp(
hrtime_to_my_time(event_time), hrtime_sec_part(event_time));
/* do a write */
if (table->field[1]->store(user_host, user_host_len, client_cs) ||
table->field[2]->store((longlong) thread_id_arg, TRUE) ||
table->field[3]->store((longlong) global_system_variables.server_id,
TRUE) ||
table->field[4]->store(command_type, command_type_len, client_cs))
goto err;
/*
A positive return value in store() means truncation.
Still logging a message in the log in this case.
*/
table->field[5]->flags|= FIELDFLAG_HEX_ESCAPE;
if (table->field[5]->store(sql_text, sql_text_len, client_cs) < 0)
goto err;
/* mark all fields as not null */
table->field[1]->set_notnull();
table->field[2]->set_notnull();
table->field[3]->set_notnull();
table->field[4]->set_notnull();
table->field[5]->set_notnull();
/* Set any extra columns to their default values */
for (field_index= 6 ; field_index < table->s->fields ; field_index++)
{
table->field[field_index]->set_default();
}
if (table->file->ha_write_row(table->record[0]))
goto err;
result= FALSE;
err:
if (result && !thd->killed)
sql_print_error("Failed to write to mysql.general_log: %s",
error_handler.message());
if (need_rnd_end)
{
table->file->ha_rnd_end();
table->file->ha_release_auto_increment();
}
if (need_pop)
thd->pop_internal_handler();
if (need_close)
close_log_table(thd, &open_tables_backup);
thd->used= (thd->used & ~THD::TIME_ZONE_USED) | save_time_zone_used;
DBUG_RETURN(result);
}
/*
Log a query to the slow log table
SYNOPSIS
log_slow()
thd THD of the query
current_time current timestamp
user_host the pointer to the string with user@host info
user_host_len length of the user_host string. this is computed once
and passed to all general log event handlers
query_time Amount of time the query took to execute (in microseconds)
lock_time Amount of time the query was locked (in microseconds)
is_command The flag, which determines, whether the sql_text is a
query or an administrator command (these are treated
differently by the old logging routines)
sql_text the very text of the query or administrator command
processed
sql_text_len the length of sql_text string
DESCRIPTION
Log a query to the slow log table
RETURN
FALSE - OK
TRUE - error occurred
*/
bool Log_to_csv_event_handler::
log_slow(THD *thd, my_hrtime_t current_time,
const char *user_host, size_t user_host_len,
ulonglong query_utime, ulonglong lock_utime, bool is_command,
const char *sql_text, size_t sql_text_len)
{
TABLE_LIST table_list;
TABLE *table;
bool result= TRUE;
bool need_close= FALSE;
bool need_rnd_end= FALSE;