-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbinlog.cc
9103 lines (7887 loc) · 281 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, 2015, Oracle and/or its affiliates. All rights reserved.
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,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
#include "my_global.h"
#include "log.h"
#include "binlog.h"
#include "log_event.h"
#include "rpl_filter.h"
#include "rpl_rli.h"
#include "sql_plugin.h"
#include "rpl_handler.h"
#include "rpl_info_factory.h"
#include "rpl_utility.h"
#include "debug_sync.h"
#include "global_threads.h"
#include "sql_show.h"
#include "sql_parse.h"
#include "rpl_mi.h"
#include <list>
#include <string>
#include <my_stacktrace.h>
using std::max;
using std::min;
using std::string;
using std::list;
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
/**
@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
#define MAX_SESSION_ATTACH_TRIES 10
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= 0;
const char *log_bin_basename= 0;
MYSQL_BIN_LOG mysql_bin_log(&sync_binlog_period);
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);
static int binlog_prepare(handlerton *hton, THD *thd, bool all);
/**
Helper class to hold a mutex for the duration of the
block.
Eliminates the need for explicit unlocking of mutexes on, e.g.,
error returns. On passing a null pointer, the sentry will not do
anything.
*/
class Mutex_sentry
{
public:
Mutex_sentry(mysql_mutex_t *mutex)
: m_mutex(mutex)
{
if (m_mutex)
mysql_mutex_lock(mutex);
}
~Mutex_sentry()
{
if (m_mutex)
mysql_mutex_unlock(m_mutex);
#ifndef DBUG_OFF
m_mutex= 0;
#endif
}
private:
mysql_mutex_t *m_mutex;
// It's not allowed to copy this object in any way
Mutex_sentry(Mutex_sentry const&);
void operator=(Mutex_sentry const&);
};
/**
Print system time.
*/
static void print_system_time()
{
#ifdef __WIN__
SYSTEMTIME utc_time;
GetSystemTime(&utc_time);
const long hrs= utc_time.wHour;
const long mins= utc_time.wMinute;
const long secs= utc_time.wSecond;
#else
/* Using time() instead of my_time() to avoid looping */
const time_t curr_time= time(NULL);
/* Calculate time of day */
const long tmins = curr_time / 60;
const long thrs = tmins / 60;
const long hrs = thrs % 24;
const long mins = tmins % 60;
const long secs = curr_time % 60;
#endif
char hrs_buf[3]= "00";
char mins_buf[3]= "00";
char secs_buf[3]= "00";
int base= 10;
my_safe_itoa(base, hrs, &hrs_buf[2]);
my_safe_itoa(base, mins, &mins_buf[2]);
my_safe_itoa(base, secs, &secs_buf[2]);
my_safe_printf_stderr("---------- %s:%s:%s UTC - ",
hrs_buf, mins_buf, secs_buf);
}
/**
Helper class to perform a thread excursion.
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 physial 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
{
Thread_excursion excursion(current_thd);
for (int i = 0 ; i < count ; ++i)
excursion.attach_to(other_thd[i]);
}
@endcode
@warning The class is not designed to be inherited from.
*/
class Thread_excursion
{
public:
Thread_excursion(THD *thd)
: m_original_thd(thd)
{
}
~Thread_excursion() {
#ifndef EMBEDDED_LIBRARY
if (unlikely(setup_thread_globals(m_original_thd)))
DBUG_ASSERT(0); // Out of memory?!
#endif
}
/**
Try to attach the POSIX thread to a session.
- This function attaches the POSIX thread to a session
in MAX_SESSION_ATTACH_TRIES tries when encountering
'out of memory' error, and terminates the server after
failed in MAX_SESSION_ATTACH_TRIES tries.
@param[in] thd The thd of a session
*/
void try_to_attach_to(THD *thd)
{
int i= 0;
/*
Attach the POSIX thread to a session in MAX_SESSION_ATTACH_TRIES
tries when encountering 'out of memory' error.
*/
while (i < MAX_SESSION_ATTACH_TRIES)
{
/*
Currently attach_to(...) returns ER_OUTOFMEMORY or 0. So
we continue to attach the POSIX thread when encountering
the ER_OUTOFMEMORY error. Please take care other error
returned from attach_to(...) in future.
*/
if (!attach_to(thd))
{
if (i > 0)
sql_print_warning("Server overcomes the temporary 'out of memory' "
"in '%d' tries while attaching to session thread "
"during the group commit phase.\n", i + 1);
break;
}
i++;
}
/*
Terminate the server after failed to attach the POSIX thread
to a session in MAX_SESSION_ATTACH_TRIES tries.
*/
if (MAX_SESSION_ATTACH_TRIES == i)
{
print_system_time();
my_safe_printf_stderr("%s", "[Fatal] Out of memory while attaching to "
"session thread during the group commit phase. "
"Data consistency between master and slave can "
"be guaranteed after server restarts.\n");
_exit(EXIT_FAILURE);
}
}
private:
/**
Attach the POSIX thread to a session.
*/
int attach_to(THD *thd)
{
#ifndef EMBEDDED_LIBRARY
if (DBUG_EVALUATE_IF("simulate_session_attach_error", 1, 0)
|| unlikely(setup_thread_globals(thd)))
{
/*
Indirectly uses pthread_setspecific, which can only return
ENOMEM or EINVAL. Since store_globals are using correct keys,
the only alternative is out of memory.
*/
return ER_OUTOFMEMORY;
}
#endif /* EMBEDDED_LIBRARY */
return 0;
}
int setup_thread_globals(THD *thd) const {
int error= 0;
THD *original_thd= my_pthread_getspecific(THD*, THR_THD);
MEM_ROOT* original_mem_root= my_pthread_getspecific(MEM_ROOT*, THR_MALLOC);
if ((error= my_pthread_setspecific_ptr(THR_THD, thd)))
goto exit0;
if ((error= my_pthread_setspecific_ptr(THR_MALLOC, &thd->mem_root)))
goto exit1;
if ((error= set_mysys_var(thd->mysys_var)))
goto exit2;
goto exit0;
exit2:
error= my_pthread_setspecific_ptr(THR_MALLOC, original_mem_root);
exit1:
error= my_pthread_setspecific_ptr(THR_THD, original_thd);
exit0:
return error;
}
THD *m_original_thd;
};
/**
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,
my_off_t max_binlog_cache_size_arg,
ulong *ptr_binlog_cache_use_arg,
ulong *ptr_binlog_cache_disk_use_arg)
: m_pending(0), saved_max_binlog_cache_size(max_binlog_cache_size_arg),
ptr_binlog_cache_use(ptr_binlog_cache_use_arg),
ptr_binlog_cache_disk_use(ptr_binlog_cache_disk_use_arg)
{
reset();
flags.transactional= trx_cache_arg;
cache_log.end_of_file= saved_max_binlog_cache_size;
}
int finalize(THD *thd, Log_event *end_event);
int flush(THD *thd, my_off_t *bytes, bool *wrote_xid);
int write_event(THD *thd, Log_event *event);
virtual ~binlog_cache_data()
{
DBUG_ASSERT(is_binlog_empty());
close_cached_file(&cache_log);
}
bool is_binlog_empty() const
{
my_off_t pos= my_b_tell(&cache_log);
DBUG_PRINT("debug", ("%s_cache - pending: 0x%llx, bytes: %llu",
(flags.transactional ? "trx" : "stmt"),
(ulonglong) pending(), (ulonglong) pos));
return pending() == NULL && pos == 0;
}
bool is_group_cache_empty() const
{
return group_cache.is_empty();
}
#ifndef DBUG_OFF
bool dbug_is_finalized() const {
return flags.finalized;
}
#endif
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
DBUG_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 my_b_tell(&cache_log);
}
virtual void reset()
{
compute_statistics();
truncate(0);
/*
If IOCACHE has a file associated, change its size to 0.
It is safer to do it here, since we are certain that one
asked the cache to go to position 0 with truncate.
*/
if(cache_log.file != -1)
{
int error= 0;
if((error= my_chsize(cache_log.file, 0, 0, MYF(MY_WME))))
sql_print_warning("Unable to resize binlog IOCACHE auxilary file");
DBUG_EXECUTE_IF("show_io_cache_size",
{
ulong file_size= my_seek(cache_log.file,
0L,MY_SEEK_END,MYF(MY_WME+MY_FAE));
sql_print_error("New size:%ld", file_size);
});
}
flags.incident= false;
flags.with_xid= false;
flags.immediate= false;
flags.finalized= 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_log.disk_writes= 0;
group_cache.clear();
DBUG_ASSERT(is_binlog_empty());
}
/*
Sets the write position to point at the position given. If the
cache has swapped to a file, it reinitializes it, so that the
proper data is added to the IO_CACHE buffer. Otherwise, it just
does a my_b_seek.
my_b_seek will not work if the cache has swapped, that's why
we do this workaround.
@param[IN] pos the new write position.
@param[IN] use_reinit if the position should be reset resorting
to reset_io_cache (which may issue a flush_io_cache
inside)
@return The previous write position.
*/
my_off_t reset_write_pos(my_off_t pos, bool use_reinit)
{
DBUG_ENTER("reset_write_pos");
DBUG_ASSERT(cache_log.type == WRITE_CACHE);
my_off_t oldpos= get_byte_position();
if (use_reinit)
reinit_io_cache(&cache_log, WRITE_CACHE, pos, 0, 0);
else
my_b_seek(&cache_log, pos);
DBUG_RETURN(oldpos);
}
/*
Cache to store data before copying it to the binary log.
*/
IO_CACHE cache_log;
/**
The group cache for this cache.
*/
Group_cache group_cache;
protected:
/*
It truncates the cache to a certain position. This includes deleting the
pending event.
*/
void truncate(my_off_t pos)
{
DBUG_PRINT("info", ("truncating to position %lu", (ulong) pos));
remove_pending_event();
reinit_io_cache(&cache_log, WRITE_CACHE, pos, 0, 0);
cache_log.end_of_file= saved_max_binlog_cache_size;
}
/**
Flush pending event to the cache buffer.
*/
int flush_pending_event(THD *thd) {
if (m_pending)
{
m_pending->set_flags(Rows_log_event::STMT_END_F);
if (int error= write_event(thd, m_pending))
return error;
thd->clear_binlog_table_maps();
}
return 0;
}
/**
Remove the pending event.
*/
int remove_pending_event() {
delete m_pending;
m_pending= NULL;
return 0;
}
struct Flags {
/*
Defines if this is either a trx-cache or stmt-cache, respectively, a
transactional or non-transactional cache.
*/
bool transactional:1;
/*
This indicates that some events did not get into the cache and most likely
it is corrupted.
*/
bool incident:1;
/*
This indicates that the cache should be written without BEGIN/END.
*/
bool immediate:1;
/*
This flag indicates that the buffer was finalized and has to be
flushed to disk.
*/
bool finalized:1;
/*
This indicates that the cache contain an XID event.
*/
bool with_xid:1;
} flags;
private:
/*
Pending binrows event. This event is the event where the rows are currently
written.
*/
Rows_log_event *m_pending;
/**
This function computes binlog cache and disk usage.
*/
void compute_statistics()
{
if (!is_binlog_empty())
{
statistic_increment(*ptr_binlog_cache_use, &LOCK_status);
if (cache_log.disk_writes != 0)
statistic_increment(*ptr_binlog_cache_disk_use, &LOCK_status);
}
}
/*
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;
binlog_cache_data& operator=(const binlog_cache_data& info);
binlog_cache_data(const binlog_cache_data& info);
};
class binlog_stmt_cache_data
: public binlog_cache_data
{
public:
binlog_stmt_cache_data(bool trx_cache_arg,
my_off_t max_binlog_cache_size_arg,
ulong *ptr_binlog_cache_use_arg,
ulong *ptr_binlog_cache_disk_use_arg)
: binlog_cache_data(trx_cache_arg,
max_binlog_cache_size_arg,
ptr_binlog_cache_use_arg,
ptr_binlog_cache_disk_use_arg)
{
}
using binlog_cache_data::finalize;
int finalize(THD *thd);
};
int
binlog_stmt_cache_data::finalize(THD *thd)
{
if (flags.immediate)
{
if (int error= finalize(thd, NULL))
return error;
}
else
{
Query_log_event
end_evt(thd, STRING_WITH_LEN("COMMIT"), false, false, true, 0, true);
if (int error= finalize(thd, &end_evt))
return error;
}
return 0;
}
class binlog_trx_cache_data : public binlog_cache_data
{
public:
binlog_trx_cache_data(bool trx_cache_arg,
my_off_t max_binlog_cache_size_arg,
ulong *ptr_binlog_cache_use_arg,
ulong *ptr_binlog_cache_disk_use_arg)
: binlog_cache_data(trx_cache_arg,
max_binlog_cache_size_arg,
ptr_binlog_cache_use_arg,
ptr_binlog_cache_disk_use_arg),
m_cannot_rollback(FALSE), before_stmt_pos(MY_OFF_T_UNDEF)
{ }
void reset()
{
DBUG_ENTER("reset");
DBUG_PRINT("enter", ("before_stmt_pos: %llu", (ulonglong) before_stmt_pos));
m_cannot_rollback= FALSE;
before_stmt_pos= MY_OFF_T_UNDEF;
binlog_cache_data::reset();
DBUG_PRINT("return", ("before_stmt_pos: %llu", (ulonglong) before_stmt_pos));
DBUG_VOID_RETURN;
}
bool cannot_rollback() const
{
return m_cannot_rollback;
}
void set_cannot_rollback()
{
m_cannot_rollback= TRUE;
}
my_off_t get_prev_position() const
{
return before_stmt_pos;
}
void set_prev_position(my_off_t pos)
{
DBUG_ENTER("set_prev_position");
DBUG_PRINT("enter", ("before_stmt_pos: %llu", (ulonglong) before_stmt_pos));
before_stmt_pos= pos;
DBUG_PRINT("return", ("before_stmt_pos: %llu", (ulonglong) before_stmt_pos));
DBUG_VOID_RETURN;
}
void restore_prev_position()
{
DBUG_ENTER("restore_prev_position");
DBUG_PRINT("enter", ("before_stmt_pos: %llu", (ulonglong) before_stmt_pos));
binlog_cache_data::truncate(before_stmt_pos);
before_stmt_pos= MY_OFF_T_UNDEF;
DBUG_PRINT("return", ("before_stmt_pos: %llu", (ulonglong) before_stmt_pos));
DBUG_VOID_RETURN;
}
void restore_savepoint(my_off_t pos)
{
DBUG_ENTER("restore_savepoint");
DBUG_PRINT("enter", ("before_stmt_pos: %llu", (ulonglong) before_stmt_pos));
binlog_cache_data::truncate(pos);
if (pos <= before_stmt_pos)
before_stmt_pos= MY_OFF_T_UNDEF;
DBUG_PRINT("return", ("before_stmt_pos: %llu", (ulonglong) before_stmt_pos));
DBUG_VOID_RETURN;
}
using binlog_cache_data::truncate;
int truncate(THD *thd, bool all);
private:
/*
It will be set TRUE if any statement which cannot be rolled back safely
is put in trx_cache.
*/
bool m_cannot_rollback;
/*
Binlog position before the start of the current statement.
*/
my_off_t before_stmt_pos;
binlog_trx_cache_data& operator=(const binlog_trx_cache_data& info);
binlog_trx_cache_data(const binlog_trx_cache_data& info);
};
class binlog_cache_mngr {
public:
binlog_cache_mngr(my_off_t max_binlog_stmt_cache_size_arg,
ulong *ptr_binlog_stmt_cache_use_arg,
ulong *ptr_binlog_stmt_cache_disk_use_arg,
my_off_t max_binlog_cache_size_arg,
ulong *ptr_binlog_cache_use_arg,
ulong *ptr_binlog_cache_disk_use_arg)
: stmt_cache(FALSE, max_binlog_stmt_cache_size_arg,
ptr_binlog_stmt_cache_use_arg,
ptr_binlog_stmt_cache_disk_use_arg),
trx_cache(TRUE, max_binlog_cache_size_arg,
ptr_binlog_cache_use_arg,
ptr_binlog_cache_disk_use_arg)
{ }
binlog_cache_data* get_binlog_cache_data(bool is_transactional)
{
if (is_transactional)
return &trx_cache;
else
return &stmt_cache;
}
IO_CACHE* get_binlog_cache_log(bool is_transactional)
{
return (is_transactional ? &trx_cache.cache_log : &stmt_cache.cache_log);
}
/**
Convenience method to check if both caches are empty.
*/
bool is_binlog_empty() const {
return stmt_cache.is_binlog_empty() && trx_cache.is_binlog_empty();
}
#ifndef DBUG_OFF
bool dbug_any_finalized() const {
return stmt_cache.dbug_is_finalized() || trx_cache.dbug_is_finalized();
}
#endif
/*
Convenience method to flush both caches to the binary log.
@param bytes_written Pointer to variable that will be set to the
number of bytes written for the flush.
@param wrote_xid Pointer to variable that will be set to @c
true if any XID event was written to the
binary log. Otherwise, the variable will not
be touched.
@return Error code on error, zero if no error.
*/
int flush(THD *thd, my_off_t *bytes_written, bool *wrote_xid)
{
my_off_t stmt_bytes= 0;
my_off_t trx_bytes= 0;
DBUG_ASSERT(stmt_cache.has_xid() == 0);
if (int error= stmt_cache.flush(thd, &stmt_bytes, wrote_xid))
return error;
if (int error= trx_cache.flush(thd, &trx_bytes, wrote_xid))
return error;
*bytes_written= stmt_bytes + trx_bytes;
return 0;
}
binlog_stmt_cache_data stmt_cache;
binlog_trx_cache_data trx_cache;
private:
binlog_cache_mngr& operator=(const binlog_cache_mngr& info);
binlog_cache_mngr(const binlog_cache_mngr& info);
};
static binlog_cache_mngr *thd_get_cache_mngr(const THD *thd)
{
/*
If opt_bin_log is not set, binlog_hton->slot == -1 and hence
thd_get_ha_data(thd, hton) segfaults.
*/
DBUG_ASSERT(opt_bin_log);
return (binlog_cache_mngr *)thd_get_ha_data(thd, binlog_hton);
}
/**
Checks if the BINLOG_CACHE_SIZE's value is greater than MAX_BINLOG_CACHE_SIZE.
If this happens, the BINLOG_CACHE_SIZE is set to MAX_BINLOG_CACHE_SIZE.
*/
void check_binlog_cache_size(THD *thd)
{
if (binlog_cache_size > max_binlog_cache_size)
{
if (thd)
{
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
ER_BINLOG_CACHE_SIZE_GREATER_THAN_MAX,
ER(ER_BINLOG_CACHE_SIZE_GREATER_THAN_MAX),
(ulong) binlog_cache_size,
(ulong) max_binlog_cache_size);
}
else
{
sql_print_warning(ER_DEFAULT(ER_BINLOG_CACHE_SIZE_GREATER_THAN_MAX),
(ulong) binlog_cache_size,
(ulong) max_binlog_cache_size);
}
binlog_cache_size= max_binlog_cache_size;
}
}
/**
Checks if the BINLOG_STMT_CACHE_SIZE's value is greater than MAX_BINLOG_STMT_CACHE_SIZE.
If this happens, the BINLOG_STMT_CACHE_SIZE is set to MAX_BINLOG_STMT_CACHE_SIZE.
*/
void check_binlog_stmt_cache_size(THD *thd)
{
if (binlog_stmt_cache_size > max_binlog_stmt_cache_size)
{
if (thd)
{
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
ER_BINLOG_STMT_CACHE_SIZE_GREATER_THAN_MAX,
ER(ER_BINLOG_STMT_CACHE_SIZE_GREATER_THAN_MAX),
(ulong) binlog_stmt_cache_size,
(ulong) max_binlog_stmt_cache_size);
}
else
{
sql_print_warning(ER_DEFAULT(ER_BINLOG_STMT_CACHE_SIZE_GREATER_THAN_MAX),
(ulong) binlog_stmt_cache_size,
(ulong) max_binlog_stmt_cache_size);
}
binlog_stmt_cache_size= max_binlog_stmt_cache_size;
}
}
/**
Check whether binlog_hton has valid slot and enabled
*/
bool binlog_enabled()
{
return(binlog_hton && binlog_hton->slot != HA_SLOT_UNDEF);
}
/*
Save position of binary log transaction cache.
SYNPOSIS
binlog_trans_log_savepos()
thd The thread to take the binlog data from
pos Pointer to variable where the position will be stored
DESCRIPTION
Save the current position in the binary log transaction cache into
the variable pointed to by 'pos'
*/
static void
binlog_trans_log_savepos(THD *thd, my_off_t *pos)
{
DBUG_ENTER("binlog_trans_log_savepos");
DBUG_ASSERT(pos != NULL);
binlog_cache_mngr *const cache_mngr= thd_get_cache_mngr(thd);
DBUG_ASSERT(mysql_bin_log.is_open());
*pos= cache_mngr->trx_cache.get_byte_position();
DBUG_PRINT("return", ("position: %lu", (ulong) *pos));
DBUG_VOID_RETURN;
}
/*
this function is mostly a placeholder.
conceptually, binlog initialization (now mostly done in MYSQL_BIN_LOG::open)
should be moved here.
*/
static int binlog_init(void *p)
{
binlog_hton= (handlerton *)p;
binlog_hton->state=opt_bin_log ? SHOW_OPTION_YES : SHOW_OPTION_NO;
binlog_hton->db_type=DB_TYPE_BINLOG;
binlog_hton->savepoint_offset= sizeof(my_off_t);
binlog_hton->close_connection= binlog_close_connection;
binlog_hton->savepoint_set= binlog_savepoint_set;
binlog_hton->savepoint_rollback= binlog_savepoint_rollback;
binlog_hton->savepoint_rollback_can_release_mdl=
binlog_savepoint_rollback_can_release_mdl;
binlog_hton->commit= binlog_commit;
binlog_hton->rollback= binlog_rollback;
binlog_hton->prepare= binlog_prepare;
binlog_hton->flags= HTON_NOT_USER_SELECTABLE | HTON_HIDDEN;
return 0;
}
static int binlog_close_connection(handlerton *hton, THD *thd)
{
DBUG_ENTER("binlog_close_connection");
binlog_cache_mngr *const cache_mngr= thd_get_cache_mngr(thd);
DBUG_ASSERT(cache_mngr->is_binlog_empty());
DBUG_ASSERT(cache_mngr->trx_cache.is_group_cache_empty() &&
cache_mngr->stmt_cache.is_group_cache_empty());
DBUG_PRINT("debug", ("Set ha_data slot %d to 0x%llx", binlog_hton->slot, (ulonglong) NULL));
thd_set_ha_data(thd, binlog_hton, NULL);
cache_mngr->~binlog_cache_mngr();
my_free(cache_mngr);
DBUG_RETURN(0);
}
int binlog_cache_data::write_event(THD *thd, Log_event *ev)
{
DBUG_ENTER("binlog_cache_data::write_event");
if (gtid_mode > 0)
{
Group_cache::enum_add_group_status status=
group_cache.add_logged_group(thd, get_byte_position());
if (status == Group_cache::ERROR)
DBUG_RETURN(1);
else if (status == Group_cache::APPEND_NEW_GROUP)
{
Gtid_log_event gtid_ev(thd, is_trx_cache());
if (gtid_ev.write(&cache_log) != 0)
DBUG_RETURN(1);
}
}
if (ev != NULL)
{
DBUG_EXECUTE_IF("simulate_disk_full_at_flush_pending",
{DBUG_SET("+d,simulate_file_write_error");});
if (ev->write(&cache_log) != 0)
{
DBUG_EXECUTE_IF("simulate_disk_full_at_flush_pending",
{
DBUG_SET("-d,simulate_file_write_error");
DBUG_SET("-d,simulate_disk_full_at_flush_pending");
/*
after +d,simulate_file_write_error the local cache
is in unsane state. Since -d,simulate_file_write_error
revokes the first simulation do_write_cache()
can't be run without facing an assert.
So it's blocked with the following 2nd simulation:
*/
DBUG_SET("+d,simulate_do_write_cache_failure");
});
DBUG_RETURN(1);
}
if (ev->get_type_code() == XID_EVENT)
flags.with_xid= true;
if (ev->is_using_immediate_logging())
flags.immediate= true;
}
DBUG_RETURN(0);
}
/**
Checks if the given GTID exists in the Group_cache. If not, add it
as an empty group.
@todo Move this function into the cache class?
@param thd THD object that owns the Group_cache
@param cache_data binlog_cache_data object for the cache
@param gtid GTID to check
*/
static int write_one_empty_group_to_cache(THD *thd,
binlog_cache_data *cache_data,
Gtid gtid)
{
DBUG_ENTER("write_one_empty_group_to_cache");
Group_cache *group_cache= &cache_data->group_cache;
if (group_cache->contains_gtid(gtid))
DBUG_RETURN(0);
/*
Apparently this code is not being called. We need to
investigate if this is a bug or this code is not
necessary. /Alfranio
Empty groups are currently being handled in the function
gtid_empty_group_log_and_cleanup().
*/
DBUG_ASSERT(0); /*NOTREACHED*/
#ifdef NON_ERROR_GTID
IO_CACHE *cache= &cache_data->cache_log;
Group_cache::enum_add_group_status status= group_cache->add_empty_group(gtid);
if (status == Group_cache::ERROR)
DBUG_RETURN(1);
DBUG_ASSERT(status == Group_cache::APPEND_NEW_GROUP);
Gtid_specification spec= { GTID_GROUP, gtid };
Gtid_log_event gtid_ev(thd, cache_data->is_trx_cache(), &spec);
if (gtid_ev.write(cache) != 0)
DBUG_RETURN(1);