forked from trdtnguyen/mysql-pmem
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmysqlbinlog.cc
3514 lines (3110 loc) · 115 KB
/
mysqlbinlog.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, 2017, 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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
TODO: print the catalog (some USE catalog.db ????).
Standalone program to read a MySQL binary log (or relay log).
Should be able to read any file of these categories, even with
--start-position.
An important fact: the Format_desc event of the log is at most the 3rd event
of the log; if it is the 3rd then there is this combination:
Format_desc_of_slave, Rotate_of_master, Format_desc_of_master.
*/
#define MYSQL_CLIENT
#undef MYSQL_SERVER
#include "client_priv.h"
#include "my_default.h"
#include <my_time.h>
#include <sslopt-vars.h>
/* That one is necessary for defines of OPTION_NO_FOREIGN_KEY_CHECKS etc */
#include "query_options.h"
#include <signal.h>
#include <my_dir.h>
#include "prealloced_array.h"
/*
error() is used in macro BINLOG_ERROR which is invoked in
rpl_gtid.h, hence the early forward declaration.
*/
static void error(const char *format, ...)
MY_ATTRIBUTE((format(printf, 1, 2)));
static void warning(const char *format, ...)
MY_ATTRIBUTE((format(printf, 1, 2)));
#include "rpl_gtid.h"
#include "log_event.h"
#include "log_event_old.h"
#include "sql_common.h"
#include "my_dir.h"
#include <welcome_copyright_notice.h> // ORACLE_WELCOME_COPYRIGHT_NOTICE
#include "sql_string.h"
#include "my_decimal.h"
#include "rpl_constants.h"
#include <algorithm>
#include <utility>
#include <map>
using std::min;
using std::max;
/*
Map containing the names of databases to be rewritten,
to a different one.
*/
static
std::map<std::string, std::string> map_mysqlbinlog_rewrite_db;
/**
The function represents Log_event delete wrapper
to reset possibly active temp_buf member.
It's to be invoked in context where the member is
not bound with dynamically allocated memory and therefore can
be reset as simple as with plain assignment to NULL.
@param ev a pointer to Log_event instance
*/
inline void reset_temp_buf_and_delete(Log_event *ev)
{
ev->temp_buf= NULL;
delete ev;
}
static bool
rewrite_db(char **buf, ulong *buf_size,
uint offset_db, uint offset_len)
{
char* ptr= *buf;
char* old_db= ptr + offset_db;
uint old_db_len= (uint) ptr[offset_len];
std::map<std::string, std::string>::iterator new_db_it=
map_mysqlbinlog_rewrite_db.find(std::string(old_db, old_db_len));
if (new_db_it == map_mysqlbinlog_rewrite_db.end())
return false;
const char *new_db=new_db_it->second.c_str();
DBUG_ASSERT(new_db && new_db != old_db);
size_t new_db_len= strlen(new_db);
// Reallocate buffer if needed.
if (new_db_len > old_db_len)
{
char *new_buf= (char *) my_realloc(PSI_NOT_INSTRUMENTED, *buf,
*buf_size + new_db_len - old_db_len, MYF(0));
if (!new_buf)
return true;
*buf= new_buf;
}
// Move the tail of buffer to the correct place.
if (new_db_len != old_db_len)
memmove(*buf + offset_db + new_db_len,
*buf + offset_db + old_db_len,
*buf_size - (offset_db + old_db_len));
// Write new_db and new_db_len.
strncpy((*buf) + offset_db, new_db, new_db_len);
(*buf)[offset_len]= (char) new_db_len;
// Update event length in header.
int4store((*buf) + EVENT_LEN_OFFSET, (*buf_size) - old_db_len + new_db_len);
// finally update the event len argument
*buf_size= (*buf_size) - old_db_len + new_db_len;
return false;
}
/**
Replace the database by another database in the buffer of a
Table_map_log_event.
The TABLE_MAP event buffer structure :
Before Rewriting :
+-------------+-----------+----------+------+----------------+
|common_header|post_header|old_db_len|old_db|event data... |
+-------------+-----------+----------+------+----------------+
After Rewriting :
+-------------+-----------+----------+------+----------------+
|common_header|post_header|new_db_len|new_db|event data... |
+-------------+-----------+----------+------+----------------+
In case the new database name is longer than the old database
length, it will reallocate the buffer.
@param[in,out] buf Pointer to event buffer to be processed
@param[in,out] event_len Length of the event
@param[in] fde The Format_description_log_event
@retval false Success
@retval true Out of memory
*/
bool
Table_map_log_event::rewrite_db_in_buffer(char **buf, ulong *event_len,
const Format_description_log_event *fde)
{
uint headers_len= fde->common_header_len +
fde->post_header_len[binary_log::TABLE_MAP_EVENT - 1];
return rewrite_db(buf, event_len, headers_len+1, headers_len);
}
/**
Replace the database by another database in the buffer of a
Query_log_event.
The QUERY_EVENT buffer structure:
Before Rewriting :
+-------------+-----------+-----------+------+------+
|common_header|post_header|status_vars|old_db|... |
+-------------+-----------+-----------+------+------+
After Rewriting :
+-------------+-----------+-----------+------+------+
|common_header|post_header|status_vars|new_db|... |
+-------------+-----------+-----------+------+------+
The db_len is inside the post header, more specifically:
+---------+---------+------+--------+--------+------+
|thread_id|exec_time|db_len|err_code|status_vars_len|
+---------+---------+------+--------+--------+------+
Thence we need to change the post header and the payload,
which is the one carrying the database name.
In case the new database name is longer than the old database
length, it will reallocate the buffer.
@param[in,out] buf Pointer to event buffer to be processed
@param[in,out] event_len Length of the event
@param[in] fde The Format_description_log_event
@retval false Success
@retval true Out of memory
*/
bool
Query_log_event::rewrite_db_in_buffer(char **buf, ulong *event_len,
const Format_description_log_event *fde)
{
uint8 common_header_len= fde->common_header_len;
uint8 query_header_len= fde->post_header_len[binary_log::QUERY_EVENT-1];
char* ptr= *buf;
uint sv_len= 0;
DBUG_EXECUTE_IF("simulate_corrupt_event_len", *event_len=0;);
/* Error if the event content is too small */
if (*event_len < (common_header_len + query_header_len))
return true;
/* Check if there are status variables in the event */
if ((query_header_len - QUERY_HEADER_MINIMAL_LEN) > 0)
{
sv_len= uint2korr(ptr + common_header_len + Q_STATUS_VARS_LEN_OFFSET);
}
/* now we have a pointer to the position where the database is. */
uint offset_len= common_header_len + Q_DB_LEN_OFFSET;
uint offset_db= common_header_len + query_header_len + sv_len;
if ((uint)((*buf)[EVENT_TYPE_OFFSET]) == binary_log::EXECUTE_LOAD_QUERY_EVENT)
offset_db+= Binary_log_event::EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN;
return rewrite_db(buf, event_len, offset_db, offset_len);
}
static
bool rewrite_db_filter(char **buf, ulong *event_len,
const Format_description_log_event *fde)
{
if (map_mysqlbinlog_rewrite_db.empty())
return false;
uint event_type= (uint)((*buf)[EVENT_TYPE_OFFSET]);
switch(event_type)
{
case binary_log::TABLE_MAP_EVENT:
return Table_map_log_event::rewrite_db_in_buffer(buf, event_len, fde);
case binary_log::QUERY_EVENT:
case binary_log::EXECUTE_LOAD_QUERY_EVENT:
return Query_log_event::rewrite_db_in_buffer(buf, event_len, fde);
default:
break;
}
return false;
}
/*
The character set used should be equal to the one used in mysqld.cc for
server rewrite-db
*/
#define mysqld_charset &my_charset_latin1
#define CLIENT_CAPABILITIES (CLIENT_LONG_PASSWORD | CLIENT_LONG_FLAG | CLIENT_LOCAL_FILES)
char server_version[SERVER_VERSION_LENGTH];
ulong filter_server_id = 0;
/*
This strucure is used to store the event and the log postion of the events
which is later used to print the event details from correct log postions.
The Log_event *event is used to store the pointer to the current event and
the event_pos is used to store the current event log postion.
*/
struct buff_event_info
{
Log_event *event;
my_off_t event_pos;
};
/*
One statement can result in a sequence of several events: Intvar_log_events,
User_var_log_events, and Rand_log_events, followed by one
Query_log_event. If statements are filtered out, the filter has to be
checked for the Query_log_event. So we have to buffer the Intvar,
User_var, and Rand events and their corresponding log postions until we see
the Query_log_event. This dynamic array buff_ev is used to buffer a structure
which stores such an event and the corresponding log position.
*/
typedef Prealloced_array<buff_event_info, 16, true> Buff_ev;
Buff_ev *buff_ev(PSI_NOT_INSTRUMENTED);
// needed by net_serv.c
ulong bytes_sent = 0L, bytes_received = 0L;
ulong mysqld_net_retry_count = 10L;
ulong open_files_limit;
ulong opt_binlog_rows_event_max_size;
uint test_flags = 0;
static uint opt_protocol= 0;
static FILE *result_file;
#ifndef DBUG_OFF
static const char* default_dbug_option = "d:t:o,/tmp/mysqlbinlog.trace";
#endif
static const char *load_default_groups[]= { "mysqlbinlog","client",0 };
static my_bool one_database=0, disable_log_bin= 0;
static my_bool opt_hexdump= 0;
const char *base64_output_mode_names[]=
{"NEVER", "AUTO", "UNSPEC", "DECODE-ROWS", NullS};
TYPELIB base64_output_mode_typelib=
{ array_elements(base64_output_mode_names) - 1, "",
base64_output_mode_names, NULL };
static enum_base64_output_mode opt_base64_output_mode= BASE64_OUTPUT_UNSPEC;
static char *opt_base64_output_mode_str= 0;
static my_bool opt_remote_alias= 0;
const char *remote_proto_names[]=
{"BINLOG-DUMP-NON-GTIDS", "BINLOG-DUMP-GTIDS", NullS};
TYPELIB remote_proto_typelib=
{ array_elements(remote_proto_names) - 1, "",
remote_proto_names, NULL };
static enum enum_remote_proto {
BINLOG_DUMP_NON_GTID= 0,
BINLOG_DUMP_GTID= 1,
BINLOG_LOCAL= 2
} opt_remote_proto= BINLOG_LOCAL;
static char *opt_remote_proto_str= 0;
static char *database= 0;
static char *output_file= 0;
static char *rewrite= 0;
static my_bool force_opt= 0, short_form= 0, idempotent_mode= 0;
static my_bool debug_info_flag, debug_check_flag;
static my_bool force_if_open_opt= 1, raw_mode= 0;
static my_bool to_last_remote_log= 0, stop_never= 0;
static my_bool opt_verify_binlog_checksum= 1;
static ulonglong offset = 0;
static int64 stop_never_slave_server_id= -1;
static int64 connection_server_id= -1;
static char* host = 0;
static int port= 0;
static uint my_end_arg;
static const char* sock= 0;
static char *opt_plugin_dir= 0, *opt_default_auth= 0;
static my_bool opt_secure_auth= TRUE;
#if defined (_WIN32) && !defined (EMBEDDED_LIBRARY)
static char *shared_memory_base_name= 0;
#endif
static char* user = 0;
static char* pass = 0;
static char *opt_bind_addr = NULL;
static char *charset= 0;
static uint verbose= 0;
static ulonglong start_position, stop_position;
#define start_position_mot ((my_off_t)start_position)
#define stop_position_mot ((my_off_t)stop_position)
static char *start_datetime_str, *stop_datetime_str;
static my_time_t start_datetime= 0, stop_datetime= MY_TIME_T_MAX;
static ulonglong rec_count= 0;
static MYSQL* mysql = NULL;
static char* dirname_for_local_load= 0;
static uint opt_server_id_bits = 0;
static ulong opt_server_id_mask = 0;
Sid_map *global_sid_map= NULL;
Checkable_rwlock *global_sid_lock= NULL;
Gtid_set *gtid_set_included= NULL;
Gtid_set *gtid_set_excluded= NULL;
/**
Pointer to the Format_description_log_event of the currently active binlog.
This will be changed each time a new Format_description_log_event is
found in the binlog. It is finally destroyed at program termination.
*/
static Format_description_log_event* glob_description_event= NULL;
/**
Exit status for functions in this file.
*/
enum Exit_status {
/** No error occurred and execution should continue. */
OK_CONTINUE= 0,
/** An error occurred and execution should stop. */
ERROR_STOP,
/** No error occurred but execution should stop. */
OK_STOP
};
/*
Options that will be used to filter out events.
*/
static char *opt_include_gtids_str= NULL,
*opt_exclude_gtids_str= NULL;
static my_bool opt_skip_gtids= 0;
static bool filter_based_on_gtids= false;
/* It is set to true when BEGIN is found, and false when the transaction ends. */
static bool in_transaction= false;
/* It is set to true when GTID is found, and false when the transaction ends. */
static bool seen_gtid= false;
static Exit_status dump_local_log_entries(PRINT_EVENT_INFO *print_event_info,
const char* logname);
static Exit_status dump_remote_log_entries(PRINT_EVENT_INFO *print_event_info,
const char* logname);
static Exit_status dump_single_log(PRINT_EVENT_INFO *print_event_info,
const char* logname);
static Exit_status dump_multiple_logs(int argc, char **argv);
static Exit_status safe_connect();
struct buff_event_info buff_event;
class Load_log_processor
{
char target_dir_name[FN_REFLEN];
size_t target_dir_name_len;
/*
When we see first event corresponding to some LOAD DATA statement in
binlog, we create temporary file to store data to be loaded.
We add name of this file to file_names set using its file_id as index.
If we have Create_file event (i.e. we have binary log in pre-5.0.3
format) we also store save event object to be able which is needed to
emit LOAD DATA statement when we will meet Exec_load_data event.
If we have Begin_load_query event we simply store 0 in
File_name_record::event field.
*/
struct File_name_record
{
char *fname;
Create_file_log_event *event;
};
typedef std::map<uint, File_name_record> File_names;
File_names file_names;
/**
Looks for a non-existing filename by adding a numerical suffix to
the given base name, creates the generated file, and returns the
filename by modifying the filename argument.
@param[in,out] filename Base filename
@param[in,out] file_name_end Pointer to last character of
filename. The numerical suffix will be written to this position.
Note that there must be a least five bytes of allocated memory
after file_name_end.
@retval -1 Error (can't find new filename).
@retval >=0 Found file.
*/
File create_unique_file(char *filename, char *file_name_end)
{
File res;
/* If we have to try more than 1000 times, something is seriously wrong */
for (uint version= 0; version<1000; version++)
{
sprintf(file_name_end,"-%x",version);
if ((res= my_create(filename,0,
O_CREAT|O_EXCL|O_BINARY|O_WRONLY,MYF(0)))!=-1)
return res;
}
return -1;
}
public:
Load_log_processor() : file_names()
{}
~Load_log_processor() {}
void init_by_dir_name(const char *dir)
{
target_dir_name_len= (convert_dirname(target_dir_name, dir, NullS) -
target_dir_name);
}
void init_by_cur_dir()
{
if (my_getwd(target_dir_name,sizeof(target_dir_name),MYF(MY_WME)))
exit(1);
target_dir_name_len= strlen(target_dir_name);
}
void destroy()
{
File_names::iterator iter= file_names.begin();
File_names::iterator end= file_names.end();
for (; iter != end; ++iter)
{
File_name_record *ptr= &iter->second;
if (ptr->fname)
{
my_free(ptr->fname);
delete ptr->event;
memset(ptr, 0, sizeof(File_name_record));
}
}
file_names.clear();
}
/**
Obtain Create_file event for LOAD DATA statement by its file_id
and remove it from this Load_log_processor's list of events.
Checks whether we have already seen a Create_file_log_event with
the given file_id. If yes, returns a pointer to the event and
removes the event from array describing active temporary files.
From this moment, the caller is responsible for freeing the memory
occupied by the event.
@param[in] file_id File id identifying LOAD DATA statement.
@return Pointer to Create_file_log_event, or NULL if we have not
seen any Create_file_log_event with this file_id.
*/
Create_file_log_event *grab_event(uint file_id)
{
File_name_record *ptr;
Create_file_log_event *res;
File_names::iterator it= file_names.find(file_id);
if (it == file_names.end())
return NULL;
ptr= &((*it).second);
if ((res= ptr->event))
memset(ptr, 0, sizeof(File_name_record));
return res;
}
/**
Obtain file name of temporary file for LOAD DATA statement by its
file_id and remove it from this Load_log_processor's list of events.
@param[in] file_id Identifier for the LOAD DATA statement.
Checks whether we have already seen Begin_load_query event for
this file_id. If yes, returns the file name of the corresponding
temporary file and removes the filename from the array of active
temporary files. From this moment, the caller is responsible for
freeing the memory occupied by this name.
@return String with the name of the temporary file, or NULL if we
have not seen any Begin_load_query_event with this file_id.
*/
char *grab_fname(uint file_id)
{
File_name_record *ptr;
char *res= NULL;
File_names::iterator it= file_names.find(file_id);
if (it == file_names.end())
return NULL;
ptr= &((*it).second);
if (!ptr->event)
{
res= ptr->fname;
memset(ptr, 0, sizeof(File_name_record));
}
return res;
}
Exit_status process(Create_file_log_event *ce);
Exit_status process(Begin_load_query_log_event *ce);
Exit_status process(Append_block_log_event *ae);
File prepare_new_file_for_old_format(Load_log_event *le, char *filename);
Exit_status load_old_format_file(NET* net, const char *server_fname,
uint server_fname_len, File file);
Exit_status process_first_event(const char *bname, size_t blen,
const uchar *block,
size_t block_len, uint file_id,
Create_file_log_event *ce);
};
/**
Creates and opens a new temporary file in the directory specified by previous call to init_by_dir_name() or init_by_cur_dir().
@param[in] le The basename of the created file will start with the
basename of the file pointed to by this Load_log_event.
@param[out] filename Buffer to save the filename in.
@return File handle >= 0 on success, -1 on error.
*/
File Load_log_processor::prepare_new_file_for_old_format(Load_log_event *le,
char *filename)
{
size_t len;
char *tail;
File file;
fn_format(filename, le->fname, target_dir_name, "", MY_REPLACE_DIR);
len= strlen(filename);
tail= filename + len;
if ((file= create_unique_file(filename,tail)) < 0)
{
error("Could not construct local filename %s.",filename);
return -1;
}
le->set_fname_outside_temp_buf(filename,len+(uint) strlen(tail));
return file;
}
/**
Reads a file from a server and saves it locally.
@param[in,out] net The server to read from.
@param[in] server_fname The name of the file that the server should
read.
@param[in] server_fname_len The length of server_fname.
@param[in,out] file The file to write to.
@retval ERROR_STOP An error occurred - the program should terminate.
@retval OK_CONTINUE No error, the program should continue.
*/
Exit_status Load_log_processor::load_old_format_file(NET* net,
const char*server_fname,
uint server_fname_len,
File file)
{
uchar buf[FN_REFLEN+1];
buf[0] = 0;
memcpy(buf + 1, server_fname, server_fname_len + 1);
if (my_net_write(net, buf, server_fname_len +2) || net_flush(net))
{
error("Failed requesting the remote dump of %s.", server_fname);
return ERROR_STOP;
}
for (;;)
{
ulong packet_len = my_net_read(net);
if (packet_len == 0)
{
if (my_net_write(net, (uchar*) "", 0) || net_flush(net))
{
error("Failed sending the ack packet.");
return ERROR_STOP;
}
/*
we just need to send something, as the server will read but
not examine the packet - this is because mysql_load() sends
an OK when it is done
*/
break;
}
else if (packet_len == packet_error)
{
error("Failed reading a packet during the dump of %s.", server_fname);
return ERROR_STOP;
}
if (packet_len > UINT_MAX)
{
error("Illegal length of packet read from net.");
return ERROR_STOP;
}
if (my_write(file, (uchar*) net->read_pos,
(uint) packet_len, MYF(MY_WME|MY_NABP)))
return ERROR_STOP;
}
return OK_CONTINUE;
}
/**
Process the first event in the sequence of events representing a
LOAD DATA statement.
Creates a temporary file to be used in LOAD DATA and writes first
block of data to it. Registers its file name (and optional
Create_file event) in the array of active temporary files.
@param bname Base name for temporary file to be created.
@param blen Base name length.
@param block First block of data to be loaded.
@param block_len First block length.
@param file_id Identifies the LOAD DATA statement.
@param ce Pointer to Create_file event object if we are processing
this type of event.
@retval ERROR_STOP An error occurred - the program should terminate.
@retval OK_CONTINUE No error, the program should continue.
*/
Exit_status Load_log_processor::process_first_event(const char *bname,
size_t blen,
const uchar *block,
size_t block_len,
uint file_id,
Create_file_log_event *ce)
{
size_t full_len= target_dir_name_len + blen + 9 + 9 + 1;
Exit_status retval= OK_CONTINUE;
char *fname, *ptr;
File file;
File_name_record rec;
DBUG_ENTER("Load_log_processor::process_first_event");
if (!(fname= (char*) my_malloc(PSI_NOT_INSTRUMENTED,
full_len,MYF(MY_WME))))
{
error("Out of memory.");
delete ce;
DBUG_RETURN(ERROR_STOP);
}
memcpy(fname, target_dir_name, target_dir_name_len);
ptr= fname + target_dir_name_len;
memcpy(ptr,bname,blen);
ptr+= blen;
ptr+= sprintf(ptr, "-%x", file_id);
if ((file= create_unique_file(fname,ptr)) < 0)
{
error("Could not construct local filename %s%s.",
target_dir_name,bname);
my_free(fname);
delete ce;
DBUG_RETURN(ERROR_STOP);
}
rec.fname= fname;
rec.event= ce;
/*
fname is freed in process_event()
after Execute_load_query_log_event or Execute_load_log_event
will have been processed, otherwise in Load_log_processor::destroy()
*/
file_names[file_id]= rec;
if (ce)
ce->set_fname_outside_temp_buf(fname, (uint) strlen(fname));
if (my_write(file, (uchar*)block, block_len, MYF(MY_WME|MY_NABP)))
{
error("Failed writing to file.");
retval= ERROR_STOP;
}
if (my_close(file, MYF(MY_WME)))
{
error("Failed closing file.");
retval= ERROR_STOP;
}
DBUG_RETURN(retval);
}
/**
Process the given Create_file_log_event.
@see Load_log_processor::process_first_event(const char*,uint,const char*,uint,uint,Create_file_log_event*)
@param ce Create_file_log_event to process.
@retval ERROR_STOP An error occurred - the program should terminate.
@retval OK_CONTINUE No error, the program should continue.
*/
Exit_status Load_log_processor::process(Create_file_log_event *ce)
{
const char *bname= ce->fname + dirname_length(ce->fname);
size_t blen= ce->fname_len - (bname-ce->fname);
return process_first_event(bname, blen, ce->block, ce->block_len,
ce->file_id, ce);
}
/**
Process the given Begin_load_query_log_event.
@see Load_log_processor::process_first_event(const char*,uint,const char*,uint,uint,Create_file_log_event*)
@param ce Begin_load_query_log_event to process.
@retval ERROR_STOP An error occurred - the program should terminate.
@retval OK_CONTINUE No error, the program should continue.
*/
Exit_status Load_log_processor::process(Begin_load_query_log_event *blqe)
{
return process_first_event("SQL_LOAD_MB", 11, blqe->block, blqe->block_len,
blqe->file_id, 0);
}
/**
Process the given Append_block_log_event.
Appends the chunk of the file contents specified by the event to the
file created by a previous Begin_load_query_log_event or
Create_file_log_event.
If the file_id for the event does not correspond to any file
previously registered through a Begin_load_query_log_event or
Create_file_log_event, this member function will print a warning and
return OK_CONTINUE. It is safe to return OK_CONTINUE, because no
query will be written for this event. We should not print an error
and fail, since the missing file_id could be because a (valid)
--start-position has been specified after the Begin/Create event but
before this Append event.
@param ae Append_block_log_event to process.
@retval ERROR_STOP An error occurred - the program should terminate.
@retval OK_CONTINUE No error, the program should continue.
*/
Exit_status Load_log_processor::process(Append_block_log_event *ae)
{
DBUG_ENTER("Load_log_processor::process");
File_names::iterator it= file_names.find(ae->file_id);
const char *fname= ((it != file_names.end()) ?
(*it).second.fname : NULL);
if (fname)
{
File file;
Exit_status retval= OK_CONTINUE;
if (((file= my_open(fname,
O_APPEND|O_BINARY|O_WRONLY,MYF(MY_WME))) < 0))
{
error("Failed opening file %s", fname);
DBUG_RETURN(ERROR_STOP);
}
if (my_write(file,(uchar*)ae->block,ae->block_len,MYF(MY_WME|MY_NABP)))
{
error("Failed writing to file %s", fname);
retval= ERROR_STOP;
}
if (my_close(file,MYF(MY_WME)))
{
error("Failed closing file %s", fname);
retval= ERROR_STOP;
}
DBUG_RETURN(retval);
}
/*
There is no Create_file event (a bad binlog or a big
--start-position). Assuming it's a big --start-position, we just do
nothing and print a warning.
*/
warning("Ignoring Append_block as there is no "
"Create_file event for file_id: %u", ae->file_id);
DBUG_RETURN(OK_CONTINUE);
}
static Load_log_processor load_processor;
/**
Replace windows-style backslashes by forward slashes so it can be
consumed by the mysql client, which requires Unix path.
@todo This is only useful under windows, so may be ifdef'ed out on
other systems. /Sven
@todo If a Create_file_log_event contains a filename with a
backslash (valid under unix), then we have problems under windows.
/Sven
@param[in,out] fname Filename to modify. The filename is modified
in-place.
*/
static void convert_path_to_forward_slashes(char *fname)
{
while (*fname)
{
if (*fname == '\\')
*fname= '/';
fname++;
}
}
/**
Indicates whether the given database should be filtered out,
according to the --database=X option.
@param log_dbname Name of database.
@return nonzero if the database with the given name should be
filtered out, 0 otherwise.
*/
static bool shall_skip_database(const char *log_dbname)
{
return one_database &&
(log_dbname != NULL) &&
strcmp(log_dbname, database);
}
/**
Checks whether the given event should be filtered out,
according to the include-gtids, exclude-gtids and
skip-gtids options.
@param ev Pointer to the event to be checked.
@return true if the event should be filtered out,
false, otherwise.
*/
static bool shall_skip_gtids(Log_event* ev)
{
bool filtered= false;
switch (ev->get_type_code())
{
case binary_log::GTID_LOG_EVENT:
case binary_log::ANONYMOUS_GTID_LOG_EVENT:
{
Gtid_log_event *gtid= (Gtid_log_event *) ev;
if (opt_include_gtids_str != NULL)
{
filtered= filtered ||
!gtid_set_included->contains_gtid(gtid->get_sidno(true),
gtid->get_gno());
}
if (opt_exclude_gtids_str != NULL)
{
filtered= filtered ||
gtid_set_excluded->contains_gtid(gtid->get_sidno(true),
gtid->get_gno());
}
filter_based_on_gtids= filtered;
filtered= filtered || opt_skip_gtids;
}
break;
/* Skip previous gtids if --skip-gtids is set. */
case binary_log::PREVIOUS_GTIDS_LOG_EVENT:
filtered= opt_skip_gtids;
break;
/*
Transaction boundaries reset the global filtering flag.
Since in the relay log a transaction can span multiple
log files, we do not reset filter_based_on_gtids flag when
processing control events (they can appear in the middle
of a transaction). But then, if:
FILE1: ... GTID BEGIN QUERY QUERY COMMIT ROTATE
FILE2: FD BEGIN QUERY QUERY COMMIT
Events on the second file would not be outputted, even
though they should.
*/
case binary_log::XID_EVENT:
filtered= filter_based_on_gtids;
filter_based_on_gtids= false;
break;
case binary_log::QUERY_EVENT:
filtered= filter_based_on_gtids;
if (((Query_log_event *)ev)->ends_group())
filter_based_on_gtids= false;
break;
/*
Never skip STOP, FD, ROTATE, IGNORABLE or INCIDENT events.
SLAVE_EVENT and START_EVENT_V3 are there for completion.
Although in the binlog transactions do not span multiple
log files, in the relay-log, that can happen. As such,
we need to explicitly state that we do not filter these
events, because there is a chance that they appear in the
middle of a filtered transaction, e.g.:
FILE1: ... GTID BEGIN QUERY QUERY ROTATE
FILE2: FD QUERY QUERY COMMIT GTID BEGIN ...
In this case, ROTATE and FD events should be processed and
outputted.
*/
case binary_log::START_EVENT_V3: /* for completion */
case binary_log::SLAVE_EVENT: /* for completion */
case binary_log::STOP_EVENT:
case binary_log::FORMAT_DESCRIPTION_EVENT:
case binary_log::ROTATE_EVENT:
case binary_log::IGNORABLE_LOG_EVENT:
case binary_log::INCIDENT_EVENT:
filtered= false;
break;