forked from facebook/mysql-5.6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binlog.cc
15781 lines (13586 loc) · 546 KB
/
binlog.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) 2009, 2022, 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 also distributed 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 included with MySQL.
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/binlog.h"
#include "my_config.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <boost/algorithm/string.hpp>
#include <chrono>
#include <cinttypes>
#include <exception>
#include <sstream>
#include <string>
#include <utility>
#include "lex_string.h"
#include "map_helpers.h"
#include "my_alloc.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_systime.h"
#include "my_thread.h"
#include "sql/check_stack.h"
#include "sql/clone_handler.h"
#include "sql/failure_injection.h"
#include "sql_string.h"
#include "template_utils.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <algorithm>
#include <list>
#include <map>
#include <new>
#include <queue>
#include <sstream>
#include <vector>
#include "dur_prop.h"
#include "libbinlogevents/include/compression/base.h"
#include "libbinlogevents/include/compression/iterator.h"
#include "libbinlogevents/include/control_events.h"
#include "libbinlogevents/include/debug_vars.h"
#include "libbinlogevents/include/rows_event.h"
#include "libbinlogevents/include/statement_events.h"
#include "libbinlogevents/include/table_id.h"
#include "mf_wcomp.h" // wild_one, wild_many
#include "mutex_lock.h" // Mutex_lock
#include "my_base.h"
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_dir.h"
#include "my_sqlcommand.h"
#include "my_stacktrace.h" // my_safe_print_system_time
#include "my_thread_local.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_file.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/thread_type.h"
#include "mysqld_error.h"
#include "partition_info.h"
#include "prealloced_array.h"
#include "scope_guard.h"
#include "sql/binlog/global.h"
#include "sql/binlog/group_commit/bgc_ticket_manager.h" // Bgc_ticket_manager
#include "sql/binlog/recovery.h" // binlog::Binlog_recovery
#include "sql/binlog/tools/iterators.h"
#include "sql/binlog_ostream.h"
#include "sql/binlog_reader.h"
#include "sql/create_field.h"
#include "sql/current_thd.h"
#include "sql/debug_sync.h" // DEBUG_SYNC
#include "sql/derror.h" // ER_THD
#include "sql/discrete_interval.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item_func.h" // user_var_entry
#include "sql/key.h"
#include "sql/log.h"
#include "sql/log_event.h" // Rows_log_event
#include "sql/mysqld.h" // sync_binlog_period ...
#include "sql/mysqld_thd_manager.h" // Global_THD_manager
#include "sql/protocol.h"
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/raii/sentry.h" // raii::Sentry<>
#include "sql/replication.h"
#include "sql/rpl_binlog_sender.h"
#include "sql/rpl_filter.h"
#include "sql/rpl_gtid.h"
#include "sql/rpl_handler.h" // RUN_HOOK
#include "sql/rpl_mi.h" // Master_info
#include "sql/rpl_msr.h" // channel_map
#include "sql/rpl_record.h"
#include "sql/rpl_replica.h"
#include "sql/rpl_replica_commit_order_manager.h" // Commit_order_manager
#include "sql/rpl_rli.h" // Relay_log_info
#include "sql/rpl_rli_pdb.h" // Slave_worker
#include "sql/rpl_shardbeats.h" // Shardbeats_manager
#include "sql/rpl_transaction_ctx.h"
#include "sql/rpl_trx_boundary_parser.h" // Transaction_boundary_parser
#include "sql/rpl_utility.h"
#include "sql/sql_backup_lock.h" // is_instance_backup_locked
#include "sql/sql_base.h" // find_temporary_table
#include "sql/sql_bitmap.h"
#include "sql/sql_const.h"
#include "sql/sql_data_change.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_parse.h" // sqlcom_can_generate_row_events
#include "sql/sql_show.h" // append_identifier
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/transaction_info.h"
#include "sql/xa.h"
#include "sql/xa/sql_cmd_xa.h" // Sql_cmd_xa_*
#include "sql_partition.h"
#include "thr_lock.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
class Item;
using binary_log::checksum_crc32;
using std::list;
using std::max;
using std::min;
using std::string;
static bool enable_raft_plugin_save = false;
#define FLAGSTR(V, F) ((V) & (F) ? #F " " : "")
#define YESNO(X) ((X) ? "yes" : "no")
/**
@defgroup Binary_Log Binary Log
@{
*/
#define MY_OFF_T_UNDEF (~(my_off_t)0UL)
/*
Constants required for the limit unsafe warnings suppression
*/
// seconds after which the limit unsafe warnings suppression will be activated
#define LIMIT_UNSAFE_WARNING_ACTIVATION_TIMEOUT 50
// number of limit unsafe warnings after which the suppression will be activated
#define LIMIT_UNSAFE_WARNING_ACTIVATION_THRESHOLD_COUNT 50
static ulonglong limit_unsafe_suppression_start_time = 0;
static bool unsafe_warning_suppression_is_activated = false;
static int limit_unsafe_warning_count = 0;
static handlerton *binlog_hton;
bool opt_binlog_order_commits = true;
const char *log_bin_index = nullptr;
const char *log_bin_basename = nullptr;
// const char *opt_relaylog_index_name = nullptr;
const char *hlc_ts_lower_bound = "hlc_ts_lower_bound";
const char *hlc_ts_upper_bound = "hlc_ts_upper_bound";
const char *hlc_wait_timeout_ms = "hlc_wait_timeout_ms";
/* Size for IO_CACHE buffer for binlog & relay log */
ulong rpl_read_size;
bool rpl_semi_sync_source_enabled = false;
latency_histogram histogram_raft_trx_wait;
char *histogram_step_size_binlog_fsync = NULL;
int opt_histogram_step_size_binlog_group_commit = 1;
latency_histogram histogram_binlog_fsync;
counter_histogram histogram_binlog_group_commit;
latency_histogram histogram_binlog_group_commit_trx;
latency_histogram histogram_binlog_engine_commit_trx;
char *opt_histogram_binlog_commit_time_step_size = nullptr;
MYSQL_BIN_LOG mysql_bin_log(&sync_binlog_period);
Dump_log dump_log;
static int binlog_init(void *p);
static int binlog_start_trans_and_stmt(THD *thd, Log_event *start_event);
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_commit(handlerton *hton, THD *thd, bool all);
static int binlog_rollback(handlerton *hton, THD *thd, bool all);
/*
This function is used to prepare a transaction. For the binary log SE.
@param hton The pointer to the binlog SE plugin.
@param thd The THD session object holding the transaction to be prepared.
@param all Preparing a transaction (i.e. true) or a statement
(i.e. false).
@return 0 if the function is successfully executed, non-zero otherwise
*/
static int binlog_prepare(handlerton *hton, THD *thd, bool all);
/*
This function is used to mark an X/Open XA distributed transaction as
being prepared in the server transaction coordinator.
Is a no-op function, added to the handler API to workaround warnings that
are triggered for SEs participating in a transaction that requires this
callback but such callback is not available.
@param hton The pointer to the binlog SE plugin.
@param thd The THD session object holding the transaction to be updated.
@return 0 if the function is successfully executed, non-zero otherwise
*/
static int binlog_set_prepared_in_tc(handlerton *hton, THD *thd);
static void exec_binlog_error_action_abort(const char *err_string);
static void binlog_prepare_row_images(const THD *thd, TABLE *table,
bool is_update);
static bool is_loggable_xa_prepare(THD *thd);
static int check_instance_backup_locked();
static std::pair<std::string, uint> extract_file_index(
const std::string &file_name);
extern int ha_update_binlog_pos(const char *, my_off_t, Gtid *);
static ulong raft_new_trx_apply_log_err_window = 5000000 /* 5 sec */;
static Error_log_throttle raft_new_trx_apply_log_err_log_throttle(
raft_new_trx_apply_log_err_window, ERROR_LEVEL, 0, "Repl",
"Error log throttle: %10lu 'Trying to write new transaction into apply log'"
" error(s) suppressed");
/* Some static functions used by failure injection for binlog */
static void failure_inject_stall_binlog_rotate() {
std::string sleep_duration =
failure_injection.get_point_value(Failure_points::STALL_BINLOG_ROTATE);
static const std::regex UNSIGNED_INT_TYPE("[+]?[0-9]+");
unsigned long duration = 10; // Sleep for 10ms (default value)
if (std::regex_match(sleep_duration, UNSIGNED_INT_TYPE)) {
// Sleep for specified duration (in ms)
duration = std::stoul(sleep_duration);
}
std::this_thread::sleep_for(std::chrono::milliseconds(duration));
return;
}
/* END: Some static functions used by failure injection for binlog */
bool normalize_binlog_name(char *to, const char *from, bool is_relay_log) {
DBUG_TRACE;
bool error = false;
char buff[FN_REFLEN];
char *ptr = const_cast<char *>(from);
char *opt_name = is_relay_log ? opt_relay_logname : opt_bin_logname;
assert(from);
/* opt_name is not null and not empty and from is a relative path */
if (opt_name && opt_name[0] && from && !test_if_hard_path(from)) {
// take the path from opt_name
// take the filename from from
char log_dirpart[FN_REFLEN], log_dirname[FN_REFLEN];
size_t log_dirpart_len, log_dirname_len;
dirname_part(log_dirpart, opt_name, &log_dirpart_len);
dirname_part(log_dirname, from, &log_dirname_len);
/* log may be empty => relay-log or log-bin did not
hold paths, just filename pattern */
if (log_dirpart_len > 0) {
/* create the new path name */
if (fn_format(buff, from + log_dirname_len, log_dirpart, "",
MYF(MY_UNPACK_FILENAME | MY_SAFE_PATH)) == nullptr) {
error = true;
goto end;
}
ptr = buff;
}
}
assert(ptr);
if (ptr) {
size_t length = strlen(ptr);
// Strips the CR+LF at the end of log name and \0-terminates it.
if (length && ptr[length - 1] == '\n') {
ptr[length - 1] = 0;
length--;
if (length && ptr[length - 1] == '\r') {
ptr[length - 1] = 0;
length--;
}
}
if (!length) {
error = true;
goto end;
}
strmake(to, ptr, length);
}
end:
return error;
}
/**
@brief Checks whether purge conditions are met to be able to run purge
for binary log files.
This function checks whether the binary log is open, if the instance
is not locked for backup.
@param log The reference to the binary log.
@return std::pair<bool, int> the first element states whether there is a
purge condition violation. The second element states what is the associated
error code, if any.
*/
static std::pair<bool, int> check_purge_conditions(const MYSQL_BIN_LOG &log) {
// is the binary log open?
if (!log.is_open()) {
return std::make_pair(true, 0);
}
// is instance locked for backup ?
int error{0};
if ((error = check_instance_backup_locked()) != 0) {
return std::make_pair(true, error);
}
// go ahead, validations checked successfully
return std::make_pair(false, 0);
}
/**
@brief This function abstracts the calculation of the binary log files
retention lower bound. It is just a function that makes it easier
to handle the fact that there are two mutually exclusive variables
that control the purge period and one of them is deprecated.
NOTE: This function and part of the purge validation functions should
really move to a retention policy class that abstracts the
retention policy altogether and its controls. Perhaps we
can do that once expire_logs_days is removed and a refactoring
is done to also include retention based on storage space
occupied. Then we can uses the same retention abstraction
for binary and relay logs and possibly extend the options
to retain (binary) log files not only based on time, but
also on space used.
@return time_t the time after which log files are considered expired.
*/
static time_t calculate_auto_purge_lower_time_bound() {
if (DBUG_EVALUATE_IF("expire_logs_always", true, false)) return time(nullptr);
int64 expiration_time = 0;
int64 current_time = time(nullptr);
if (binlog_expire_logs_seconds > 0)
expiration_time = current_time - binlog_expire_logs_seconds;
else if (expire_logs_days > 0)
expiration_time =
current_time - expire_logs_days * static_cast<int64>(SECONDS_IN_24H);
// check for possible overflow conditions (4 bytes time_t)
if (expiration_time < std::numeric_limits<time_t>::min())
expiration_time = std::numeric_limits<time_t>::min();
// This function should only be called if binlog_expire_logs_seconds
// or expire_logs_days are greater than 0
assert(binlog_expire_logs_seconds > 0 || expire_logs_days > 0);
return static_cast<time_t>(expiration_time);
}
/**
@brief Checks if automatic purge conditions are met and therefore the
purge is allowed to be done. If not met returns true. Otherwise, false.
@return false if the check is successful. True otherwise.
*/
static bool check_auto_purge_conditions() {
// purge is disabled
if (!opt_binlog_expire_logs_auto_purge) return true;
// no retention window configured
if (binlog_expire_logs_seconds == 0 && expire_logs_days == 0) return true;
// go ahead, validations checked successfully
return false;
}
/**
Logical binlog file which wraps and hides the detail of lower layer storage
implementation. Binlog code just use this class to control real storage
*/
class MYSQL_BIN_LOG::Binlog_ofile : public Basic_ostream {
public:
~Binlog_ofile() override {
DBUG_TRACE;
close();
return;
}
/**
Opens the binlog file. It opens the lower layer storage.
@param[in] log_file_key The PSI_file_key for this stream
@param[in] binlog_name The file to be opened
@param[in] flags The flags used by IO_CACHE.
@param[in] existing True if opening the file, false if creating a new one.
@retval false Success
@retval true Error
*/
bool open(
#ifdef HAVE_PSI_INTERFACE
PSI_file_key log_file_key,
#endif
const char *binlog_name, myf flags, bool existing = false) {
DBUG_TRACE;
assert(m_pipeline_head == nullptr);
#ifndef NDEBUG
{
#ifndef HAVE_PSI_INTERFACE
PSI_file_key log_file_key = PSI_NOT_INSTRUMENTED;
#endif
MY_STAT info;
if (!mysql_file_stat(log_file_key, binlog_name, &info, MYF(0))) {
assert(existing == !(my_errno() == ENOENT));
set_my_errno(0);
}
}
#endif
std::unique_ptr<IO_CACHE_ostream> file_ostream(new IO_CACHE_ostream);
if (file_ostream->open(log_file_key, binlog_name, flags)) return true;
// Get the underlying IO_CACHE for the file stream
m_io_cache = file_ostream->get_io_cache();
m_pipeline_head = std::move(file_ostream);
/* Setup encryption for new files if needed */
if (!existing && rpl_encryption.is_enabled()) {
std::unique_ptr<Binlog_encryption_ostream> encrypted_ostream(
new Binlog_encryption_ostream());
if (encrypted_ostream->open(std::move(m_pipeline_head))) return true;
m_encrypted_header_size = encrypted_ostream->get_header_size();
m_pipeline_head = std::move(encrypted_ostream);
}
return false;
}
/**
Opens an existing binlog file. It opens the lower layer storage reusing the
existing file password if needed.
@param[in] log_file_key The PSI_file_key for this stream
@param[in] binlog_name The file to be opened
@param[in] flags The flags used by IO_CACHE.
@retval std::unique_ptr A Binlog_ofile object pointer.
@retval nullptr Error.
*/
static std::unique_ptr<Binlog_ofile> open_existing(
#ifdef HAVE_PSI_INTERFACE
PSI_file_key log_file_key,
#endif
const char *binlog_name, myf flags) {
DBUG_TRACE;
std::unique_ptr<Rpl_encryption_header> header;
unsigned char magic[BINLOG_MAGIC_SIZE];
/* Open a simple istream to read the magic from the file */
IO_CACHE_istream istream;
if (istream.open(key_file_binlog, key_file_binlog_cache, binlog_name,
MYF(MY_WME | MY_DONT_CHECK_FILESIZE), rpl_read_size))
return nullptr;
if (istream.read(magic, BINLOG_MAGIC_SIZE) != BINLOG_MAGIC_SIZE)
return nullptr;
assert(Rpl_encryption_header::ENCRYPTION_MAGIC_SIZE == BINLOG_MAGIC_SIZE);
/* Identify the file type by the magic to get the encryption header */
if (memcmp(magic, Rpl_encryption_header::ENCRYPTION_MAGIC,
BINLOG_MAGIC_SIZE) == 0) {
header = Rpl_encryption_header::get_header(&istream);
if (header == nullptr) return nullptr;
} else if (memcmp(magic, BINLOG_MAGIC, BINLOG_MAGIC_SIZE) != 0) {
return nullptr;
}
/* Open the binlog_ofile */
std::unique_ptr<Binlog_ofile> ret_ofile(new Binlog_ofile);
if (ret_ofile->open(
#ifdef HAVE_PSI_INTERFACE
log_file_key,
#endif
binlog_name, flags, true)) {
return nullptr;
}
if (header != nullptr) {
/* Add the encryption stream on top of IO_CACHE */
std::unique_ptr<Binlog_encryption_ostream> encrypted_ostream(
new Binlog_encryption_ostream);
ret_ofile->m_encrypted_header_size = header->get_header_size();
encrypted_ostream->open(std::move(ret_ofile->m_pipeline_head),
std::move(header));
ret_ofile->m_pipeline_head = std::move(encrypted_ostream);
ret_ofile->set_encrypted();
}
return ret_ofile;
}
void close() {
m_pipeline_head.reset(nullptr);
m_position = 0;
m_encrypted_header_size = 0;
}
/**
Writes data into storage and maintains binlog position.
@param[in] buffer the data will be written
@param[in] length the length of the data
@retval false Success
@retval true Error
*/
bool write(const unsigned char *buffer, my_off_t length) override {
assert(m_pipeline_head != nullptr);
if (m_pipeline_head->write(buffer, length)) return true;
m_position += length;
return false;
}
/**
Updates some bytes in the binlog file. If is only used for clearing
LOG_EVENT_BINLOG_IN_USE_F.
@param[in] buffer the data will be written
@param[in] length the length of the data
@param[in] offset the offset of the bytes will be updated
@retval false Success
@retval true Error
*/
bool update(const unsigned char *buffer, my_off_t length, my_off_t offset) {
assert(m_pipeline_head != nullptr);
return m_pipeline_head->seek(offset) ||
m_pipeline_head->write(buffer, length);
}
/**
Truncates some data at the end of the binlog file.
@param[in] offset where the binlog file will be truncated to.
@retval false Success
@retval true Error
*/
bool truncate(my_off_t offset) {
assert(m_pipeline_head != nullptr);
if (m_pipeline_head->truncate(offset)) return true;
m_position = offset;
return false;
}
bool flush() { return m_pipeline_head->flush(); }
bool sync() { return m_pipeline_head->sync(); }
bool flush_and_sync() { return flush() || sync(); }
my_off_t position() { return m_position; }
bool is_empty() { return position() == 0; }
bool is_open() { return m_pipeline_head != nullptr; }
/**
Returns the encrypted header size of the binary log file.
@retval 0 The file is not encrypted.
@retval >0 The encryption header size.
*/
int get_encrypted_header_size() { return m_encrypted_header_size; }
/**
Returns the real file size.
While position() returns the "file size" from the plain binary log events
stream point of view, this function considers the encryption header when it
exists.
@param writes_via_raft - the actual write to binlog happens via plugin,
so size accounting on mysql side is not dependable
@return The real file size considering the encryption header.
*/
my_off_t get_real_file_size() { return m_position + m_encrypted_header_size; }
/**
Get the pipeline head.
@retval Returns the pipeline head or nullptr.
*/
std::unique_ptr<Truncatable_ostream> get_pipeline_head() {
return std::move(m_pipeline_head);
}
/**
Check if the log file is encrypted.
@retval True if the log file is encrypted.
@retval False if the log file is not encrypted.
*/
bool is_encrypted() { return m_encrypted; }
/**
Set that the log file is encrypted.
*/
void set_encrypted() { m_encrypted = true; }
my_off_t get_my_b_tell() { return m_pipeline_head->get_my_b_tell(); }
/**
Return the underlying io_cache for this stream object
@retval A pointer to the underlying IO_CACHE
*/
IO_CACHE *get_io_cache() const { return m_io_cache; }
my_off_t *get_position_ptr() { return &m_position; }
/**
Seek to the specified offset in the stream. Also sets up the internal
state correctly.
@param[in] offset offset in the stream to seek to
@retval false Success
@retval true Error
*/
bool seek(my_off_t offset) {
if (m_pipeline_head->seek(offset)) return true; // error
m_position = 0;
if (m_encrypted && m_encrypted_header_size > 0 &&
m_encrypted_header_size <= (int)offset)
m_position = offset - m_encrypted_header_size;
else if (!m_encrypted)
m_position = offset;
return false; // success
}
private:
my_off_t m_position = 0;
int m_encrypted_header_size = 0;
std::unique_ptr<Truncatable_ostream> m_pipeline_head;
bool m_encrypted = false;
IO_CACHE *m_io_cache = nullptr;
};
/**
Helper class to switch to a new thread and then go back to the previous one,
when the object is destroyed using RAII.
This class is used to temporarily switch to another session (THD
structure). It will set up thread specific "globals" correctly
so that the POSIX thread looks exactly like the session attached to.
However, PSI_thread info is not touched as it is required to show
the actual physical view in PFS instrumentation i.e., it should
depict as the real thread doing the work instead of thread it switched
to.
On destruction, the original session (which is supplied to the
constructor) will be re-attached automatically. For example, with
this code, the value of @c current_thd will be the same before and
after execution of the code.
@code
{
for (int i = 0 ; i < count ; ++i)
{
// here we are attached to current_thd
// [...]
Thd_backup_and_restore switch_thd(current_thd, other_thd[i]);
// [...]
// here we are attached to other_thd[i]
// [...]
}
// here we are attached to current_thd
}
@endcode
@warning The class is not designed to be inherited from.
*/
class Thd_backup_and_restore {
public:
/**
Try to attach the POSIX thread to a session.
@param[in] backup_thd The thd to restore to when object is destructed.
@param[in] new_thd The thd to attach to.
*/
Thd_backup_and_restore(THD *backup_thd, THD *new_thd)
: m_backup_thd(backup_thd),
m_new_thd(new_thd),
m_new_thd_old_real_id(new_thd->real_id),
m_new_thd_old_thread_stack(new_thd->thread_stack) {
assert(m_backup_thd != nullptr && m_new_thd != nullptr);
// Reset the state of the current thd.
m_backup_thd->restore_globals();
m_new_thd->thread_stack = m_backup_thd->thread_stack;
m_new_thd->store_globals();
#ifdef HAVE_PSI_THREAD_INTERFACE
PSI_THREAD_CALL(set_mem_cnt_THD)(m_new_thd, &m_backup_cnt_thd);
#endif
}
/**
Restores to previous thd.
*/
~Thd_backup_and_restore() {
/*
Restore the global variables of the thd we previously attached to,
to its original state. In other words, detach the m_new_thd.
*/
m_new_thd->restore_globals();
m_new_thd->real_id = m_new_thd_old_real_id;
m_new_thd->thread_stack = m_new_thd_old_thread_stack;
// Reset the global variables to the original state.
m_backup_thd->store_globals();
#ifdef HAVE_PSI_THREAD_INTERFACE
PSI_THREAD_CALL(set_mem_cnt_THD)(m_backup_cnt_thd, &m_dummy_cnt_thd);
#endif
}
private:
THD *m_backup_thd;
THD *m_new_thd;
THD *m_backup_cnt_thd;
THD *m_dummy_cnt_thd;
my_thread_t m_new_thd_old_real_id;
const char *m_new_thd_old_thread_stack;
};
/**
Caches for non-transactional and transactional data before writing
it to the binary log.
@todo All the access functions for the flags suggest that the
encapsuling is not done correctly, so try to move any logic that
requires access to the flags into the cache.
*/
class binlog_cache_data {
public:
binlog_cache_data(bool trx_cache_arg, ulong *ptr_binlog_cache_use_arg,
ulong *ptr_binlog_cache_disk_use_arg)
: m_pending(nullptr),
ptr_binlog_cache_use(ptr_binlog_cache_use_arg),
ptr_binlog_cache_disk_use(ptr_binlog_cache_disk_use_arg) {
flags.transactional = trx_cache_arg;
}
bool open(my_off_t cache_size, my_off_t max_cache_size) {
return m_cache.open(cache_size, max_cache_size);
}
Binlog_cache_storage *get_cache() { return &m_cache; }
int finalize(THD *thd, Log_event *end_event);
int finalize(THD *thd, Log_event *end_event, XID_STATE *xs);
int flush(THD *thd, my_off_t *bytes, bool *wrote_xid);
int write_event(THD *thd, Log_event *event,
bool write_meta_data_event = false);
size_t get_event_counter() { return event_counter; }
size_t get_compressed_size() { return m_compressed_size; }
size_t get_decompressed_size() { return m_decompressed_size; }
binary_log::transaction::compression::type get_compression_type() {
return m_compression_type;
}
void set_compressed_size(size_t s) { m_compressed_size = s; }
void set_decompressed_size(size_t s) { m_decompressed_size = s; }
void set_compression_type(binary_log::transaction::compression::type t) {
m_compression_type = t;
}
virtual ~binlog_cache_data() {
assert(is_binlog_empty());
m_cache.close();
}
bool is_binlog_empty() const {
DBUG_PRINT("debug", ("%s_cache - pending: 0x%llx, bytes: %llu",
(flags.transactional ? "trx" : "stmt"),
(ulonglong)pending(), (ulonglong)m_cache.length()));
return pending() == nullptr && m_cache.is_empty();
}
bool is_finalized() const { return flags.finalized; }
bool is_transactional() const { return flags.transactional; }
Rows_log_event *pending() const { return m_pending; }
void set_pending(Rows_log_event *const pending) { m_pending = pending; }
void set_incident(void) { flags.incident = true; }
bool has_incident(void) const { return flags.incident; }
bool has_xid() const {
// There should only be an XID event if we are transactional
assert((flags.transactional && flags.with_xid) || !flags.with_xid);
return flags.with_xid;
}
bool is_trx_cache() const { return flags.transactional; }
my_off_t get_byte_position() const { return m_cache.length(); }
void cache_state_checkpoint(my_off_t pos_to_checkpoint) {
// We only need to store the cache state for pos > 0
if (pos_to_checkpoint) {
cache_state state;
state.with_rbr = flags.with_rbr;
state.with_sbr = flags.with_sbr;
state.with_start = flags.with_start;
state.with_end = flags.with_end;
state.with_content = flags.with_content;
state.event_counter = event_counter;
cache_state_map[pos_to_checkpoint] = state;
}
}
void cache_state_rollback(my_off_t pos_to_rollback) {
if (pos_to_rollback) {
std::map<my_off_t, cache_state>::iterator it;
it = cache_state_map.find(pos_to_rollback);
if (it != cache_state_map.end()) {
flags.with_rbr = it->second.with_rbr;
flags.with_sbr = it->second.with_sbr;
flags.with_start = it->second.with_start;
flags.with_end = it->second.with_end;
flags.with_content = it->second.with_content;
event_counter = it->second.event_counter;
} else
assert(it == cache_state_map.end());
}
// Rolling back to pos == 0 means cleaning up the cache.
else {
flags.with_rbr = false;
flags.with_sbr = false;
flags.with_start = false;
flags.with_end = false;
flags.with_content = false;
event_counter = 0;
}
}
/**
Reset the cache to unused state when the transaction is finished. It
drops all data in the cache and clears the flags of the transaction state.
*/
virtual void reset() {
compute_statistics();
remove_pending_event();
if (m_cache.reset()) {
LogErr(WARNING_LEVEL, ER_BINLOG_CANT_RESIZE_CACHE);
}
flags.incident = false;
flags.with_xid = false;
flags.immediate = false;
flags.finalized = false;
flags.with_sbr = false;
flags.with_rbr = false;
flags.with_start = false;
flags.with_end = false;
flags.with_content = false;
/*
The truncate function calls reinit_io_cache that calls my_b_flush_io_cache
which may increase disk_writes. This breaks the disk_writes use by the
binary log which aims to compute the ratio between in-memory cache usage
and disk cache usage. To avoid this undesirable behavior, we reset the
variable after truncating the cache.
*/
cache_state_map.clear();
event_counter = 0;
m_compressed_size = 0;
m_decompressed_size = 0;
m_compression_type = binary_log::transaction::compression::NONE;
assert(is_binlog_empty());
}
/**
Returns information about the cache content with respect to
the binlog_format of the events.
This will be used to set a flag on GTID_LOG_EVENT stating that the
transaction may have SBR statements or not, but the binlog dump
will show this flag as "rbr_only" when it is not set. That's why
an empty transaction should return true below, or else an empty
transaction would be assumed as "rbr_only" even not having RBR
events.
When dumping a binary log content using mysqlbinlog client program,
for any transaction assumed as "rbr_only" it will be printed a
statement changing the transaction isolation level to READ COMMITTED.
It doesn't make sense to have an empty transaction "requiring" this
isolation level change.
@return true The cache have SBR events or is empty.
@return false The cache contains a transaction with no SBR events.
*/
bool may_have_sbr_stmts() { return flags.with_sbr || !flags.with_rbr; }
/**
Check if the binlog cache contains an empty transaction, which has
two binlog events "BEGIN" and "COMMIT".
@return true The binlog cache contains an empty transaction.
@return false Otherwise.
*/
bool has_empty_transaction() {
/*
The empty transaction has two events in trx/stmt binlog cache
and no changes: one is a transaction start and other is a transaction
end (there should be no SBR changing content and no RBR events).
*/
if (flags.with_start && // Has transaction start statement
flags.with_end && // Has transaction end statement
!flags.with_content) // Has no other content than START/END
{
assert(event_counter == 2); // Two events in the cache only
assert(!flags.with_sbr); // No statements changing content
assert(!flags.with_rbr); // No rows changing content
assert(!flags.immediate); // Not a DDL
assert(!flags.with_xid); // Not a XID trx and not an atomic DDL Query
return true;
}
return false;
}
/**
Check if the binlog cache is empty or contains an empty transaction,
which has two binlog events "BEGIN" and "COMMIT".
@return true The binlog cache is empty or contains an empty transaction.
@return false Otherwise.
*/
bool is_empty_or_has_empty_transaction() {
return is_binlog_empty() || has_empty_transaction();
}
protected:
/*
This structure should have all cache variables/flags that should be restored
when a ROLLBACK TO SAVEPOINT statement be executed.
*/
struct cache_state {
bool with_sbr;
bool with_rbr;
bool with_start;
bool with_end;
bool with_content;
size_t event_counter;
};
/*
For every SAVEPOINT used, we will store a cache_state for the current