1
1
# -*- coding: utf-8 -*-
2
2
3
- import pymysql
4
3
import struct
5
4
from distutils .version import LooseVersion
6
5
6
+ import pymysql
7
7
from pymysql .constants .COMMAND import COM_BINLOG_DUMP , COM_REGISTER_SLAVE
8
8
from pymysql .cursors import DictCursor
9
9
10
- from .packet import BinLogPacketWrapper
11
10
from .constants .BINLOG import TABLE_MAP_EVENT , ROTATE_EVENT , FORMAT_DESCRIPTION_EVENT
12
- from .gtid import GtidSet
13
11
from .event import (
14
12
QueryEvent , RotateEvent , FormatDescriptionEvent ,
15
13
XidEvent , GtidEvent , StopEvent , XAPrepareEvent ,
16
14
BeginLoadQueryEvent , ExecuteLoadQueryEvent ,
17
15
HeartbeatLogEvent , NotImplementedEvent , MariadbGtidEvent ,
18
- MariadbAnnotateRowsEvent , RandEvent , MariadbStartEncryptionEvent )
16
+ MariadbAnnotateRowsEvent , RandEvent , MariadbStartEncryptionEvent , RowsQueryLogEvent )
19
17
from .exceptions import BinLogNotEnabled
18
+ from .gtid import GtidSet
19
+ from .packet import BinLogPacketWrapper
20
20
from .row_event import (
21
21
UpdateRowsEvent , WriteRowsEvent , DeleteRowsEvent , TableMapEvent )
22
22
33
33
34
34
35
35
class ReportSlave (object ):
36
-
37
36
"""Represent the values that you may report when connecting as a slave
38
37
to a master. SHOW SLAVE HOSTS related"""
39
38
@@ -68,7 +67,7 @@ def __init__(self, value):
68
67
self .hostname = value
69
68
70
69
def __repr__ (self ):
71
- return '<ReportSlave hostname=%s username=%s password=%s port=%d>' % \
70
+ return '<ReportSlave hostname=%s username=%s password=%s port=%d>' % \
72
71
(self .hostname , self .username , self .password , self .port )
73
72
74
73
def encoded (self , server_id , master_id = 0 ):
@@ -123,7 +122,6 @@ def encoded(self, server_id, master_id=0):
123
122
124
123
125
124
class BinLogStreamReader (object ):
126
-
127
125
"""Connect to replication stream and read event
128
126
"""
129
127
report_slave = None
@@ -317,7 +315,7 @@ def __connect_to_stream(self):
317
315
4294967 ))
318
316
# If heartbeat is too low, the connection will disconnect before,
319
317
# this is also the behavior in mysql
320
- heartbeat = float (min (net_timeout / 2. , self .slave_heartbeat ))
318
+ heartbeat = float (min (net_timeout / 2. , self .slave_heartbeat ))
321
319
if heartbeat > 4294967 :
322
320
heartbeat = 4294967
323
321
@@ -353,7 +351,7 @@ def __connect_to_stream(self):
353
351
cur .close ()
354
352
355
353
prelude = struct .pack ('<i' , len (self .log_file ) + 11 ) \
356
- + bytes (bytearray ([COM_BINLOG_DUMP ]))
354
+ + bytes (bytearray ([COM_BINLOG_DUMP ]))
357
355
358
356
if self .__resume_stream :
359
357
prelude += struct .pack ('<I' , self .log_pos )
@@ -370,7 +368,7 @@ def __connect_to_stream(self):
370
368
prelude += self .log_file .encode ()
371
369
else :
372
370
if self .is_mariadb :
373
- prelude = self .__set_mariadb_settings ()
371
+ prelude = self .__set_mariadb_settings ()
374
372
else :
375
373
# Format for mysql packet master_auto_position
376
374
#
@@ -417,8 +415,8 @@ def __connect_to_stream(self):
417
415
8 + # binlog_pos_info_size
418
416
4 ) # encoded_data_size
419
417
420
- prelude = b'' + struct .pack ('<i' , header_size + encoded_data_size )\
421
- + bytes (bytearray ([COM_BINLOG_DUMP_GTID ]))
418
+ prelude = b'' + struct .pack ('<i' , header_size + encoded_data_size ) \
419
+ + bytes (bytearray ([COM_BINLOG_DUMP_GTID ]))
422
420
423
421
flags = 0
424
422
if not self .__blocking :
@@ -455,7 +453,7 @@ def __connect_to_stream(self):
455
453
def __set_mariadb_settings (self ):
456
454
# https://mariadb.com/kb/en/5-slave-registration/
457
455
cur = self ._stream_connection .cursor ()
458
- if self .auto_position != None :
456
+ if self .auto_position != None :
459
457
cur .execute ("SET @slave_connect_state='%s'" % self .auto_position )
460
458
cur .execute ("SET @slave_gtid_strict_mode=1" )
461
459
cur .execute ("SET @slave_gtid_ignore_duplicates=0" )
@@ -466,7 +464,7 @@ def __set_mariadb_settings(self):
466
464
4 + # binlog pos
467
465
2 + # binlog flags
468
466
4 + # slave server_id,
469
- 4 # requested binlog file name , set it to empty
467
+ 4 # requested binlog file name , set it to empty
470
468
)
471
469
472
470
prelude = struct .pack ('<i' , header_size ) + bytes (bytearray ([COM_BINLOG_DUMP ]))
@@ -478,11 +476,11 @@ def __set_mariadb_settings(self):
478
476
479
477
# Enable annotate rows event
480
478
if self .__annotate_rows_event :
481
- flags |= 0x02 # BINLOG_SEND_ANNOTATE_ROWS_EVENT
479
+ flags |= 0x02 # BINLOG_SEND_ANNOTATE_ROWS_EVENT
482
480
483
481
if not self .__blocking :
484
482
flags |= 0x01 # BINLOG_DUMP_NON_BLOCK
485
-
483
+
486
484
# binlog flags
487
485
prelude += struct .pack ('<H' , flags )
488
486
@@ -622,10 +620,11 @@ def _allowed_event_list(self, only_events, ignored_events,
622
620
HeartbeatLogEvent ,
623
621
NotImplementedEvent ,
624
622
MariadbGtidEvent ,
623
+ RowsQueryLogEvent ,
625
624
MariadbAnnotateRowsEvent ,
626
625
RandEvent ,
627
- MariadbStartEncryptionEvent
628
- ))
626
+ MariadbStartEncryptionEvent ,
627
+ ))
629
628
if ignored_events is not None :
630
629
for e in ignored_events :
631
630
events .remove (e )
0 commit comments