2
2
3
3
import pymysql
4
4
import struct
5
+ import time
5
6
from distutils .version import LooseVersion
6
7
7
8
from pymysql .constants .COMMAND import COM_BINLOG_DUMP , COM_REGISTER_SLAVE
30
31
# 2006 MySQL server has gone away
31
32
MYSQL_EXPECTED_ERROR_CODES = [2013 , 2006 ]
32
33
34
+ __PYMYSQL_VERSION_LT_06 = pymysql .__version__ < LooseVersion ("0.6" )
33
35
34
36
class ReportSlave (object ):
35
37
@@ -208,11 +210,6 @@ def __init__(self, connection_settings, server_id,
208
210
each time the client is disconnected and then auto-reconnected
209
211
to the mysql server (OperationalError 2006/2013) if resume_stream
210
212
is False. so it's suggested to set resume_stream to True.
211
-
212
- an additional RotateEvent and FormatDescriptionEvent will be
213
- fetched each time the client is disconnected and then auto-
214
- reconnected to the server. (no matter resume_stream is True
215
- or False)
216
213
"""
217
214
218
215
self .__connection_settings = connection_settings
@@ -263,7 +260,7 @@ def __init__(self, connection_settings, server_id,
263
260
self .pymysql_wrapper = pymysql .connect
264
261
265
262
def close (self ):
266
- if getattr ( self , '_stream_connection' , None ) and self . _stream_connection . open :
263
+ if self . __connected_stream :
267
264
self ._stream_connection .close ()
268
265
if getattr (self , '_ctl_connection' , None ):
269
266
# break reference cycle between stream reader and underlying
@@ -304,7 +301,7 @@ def _register_slave(self):
304
301
305
302
packet = self .report_slave .encoded (self .__server_id )
306
303
307
- if pymysql . __version__ < LooseVersion ( "0.6" ) :
304
+ if __PYMYSQL_VERSION_LT_06 :
308
305
self ._stream_connection .wfile .write (packet )
309
306
self ._stream_connection .wfile .flush ()
310
307
self ._stream_connection .read_packet ()
@@ -334,7 +331,7 @@ def __connect_to_stream(self, force_reconnect=False):
334
331
# server_id (4) -- server id of this slave
335
332
# log_file (string.EOF) -- filename of the binlog on the master
336
333
self ._stream_connection = self .pymysql_wrapper (** self .__connection_settings )
337
- if pymysql . __version__ < LooseVersion ( "0.6" ) :
334
+ if __PYMYSQL_VERSION_LT_06 :
338
335
self ._stream_connection ._read_packet = self ._stream_connection .read_packet
339
336
340
337
self .__use_checksum = self .__checksum_enabled ()
@@ -467,7 +464,7 @@ def __connect_to_stream(self, force_reconnect=False):
467
464
# encoded_data
468
465
prelude += gtid_set .encoded ()
469
466
470
- if pymysql . __version__ < LooseVersion ( "0.6" ) :
467
+ if __PYMYSQL_VERSION_LT_06 :
471
468
self ._stream_connection .wfile .write (prelude )
472
469
self ._stream_connection .wfile .flush ()
473
470
else :
@@ -493,7 +490,10 @@ def __fetchone(self):
493
490
except pymysql .OperationalError as error :
494
491
code , message = error .args
495
492
if code in MYSQL_EXPECTED_ERROR_CODES :
493
+ time .sleep (5 )
496
494
self .__connect_to_stream (force_reconnect = True )
495
+ # skip the first 2 events (RotateEvent and FormatDescriptionEvent)
496
+ _ = self .__fetchone (), self .__fetchone ()
497
497
continue
498
498
raise
499
499
0 commit comments