python-mysql-replication icon indicating copy to clipboard operation
python-mysql-replication copied to clipboard

always get RotateEvent when didn't set `log_file`, `log_pos`, `skip_to_timestamp`

Open zhangjustin opened this issue 7 years ago • 1 comments
trafficstars

Here is my code



class ReadBinlog(object):
    def __init__(self):
        self.db_host = app.config['database']['host']
        self.db_port = app.config['database']['port']
        self.user = app.config['database']['user']
        self.passwd = app.config['database']['passwd']
        self.table_queue_mapping = app.config['table_queue_mapping']
        self.log_file = None
        self.log_position = None
        self.timestamp = None
        self.auto_position = None
        self.binlog_file = app.config['log_binlog']['path']
        self.server_id = app.config['database']['server_id']
    
    def pull_binglog(self):
        mysql_settings = {
            'host': self.db_host,
            'port': self.db_port,
            'user': self.user,
            'passwd': self.passwd
        }
        self.resume_stream = True


        only_events = (DeleteRowsEvent, UpdateRowsEvent, WriteRowsEvent, RotateEvent)
        print '=============================================='
        print self.log_file, self.log_position, self.timestamp
        print '=============================================='
        log_stream = BinLogStreamReader(
            connection_settings=mysql_settings,
            server_id=self.server_id,
            only_events=only_events,
            only_tables=self.table_queue_mapping.keys(),
            resume_stream=True,
            log_file=self.log_file,
            log_pos=self.log_position,
            skip_to_timestamp=self.timestamp,
        )
        while True:
            try:
                for binlog_event in log_stream:
                    app.log.info('new event coming')
                    if isinstance(binlog_event, RotateEvent):
                        postion = binlog_event.position
                        next_binlog = binlog_event.next_binlog
                        app.log.info('event timestamp {}'.format(binlog_event.timestamp))
                        app.log.info('next binlog file is: {}'.format(next_binlog))
                    else:
                        app.log.info('not rotateEvent')
                        self.analyze_event(binlog_event)
                        next_binlog = self.log_file
                        postion = self.log_position
                    log_timestamp = binlog_event.timestamp
                    log_content = ','.join([next_binlog, str(log_timestamp), str(postion)])
                    self.write_log_file(self.binlog_file, log_content)

here is my log

(.env) ➜  scripts git:(init) ✗ python main.py
2018-08-02 11:26:13,268 - main.py[line:113] - INFO: start to read mysql binlog!
==============================================
None None None False
==============================================
2018-08-02 11:26:15,136 - main.py[line:62] - INFO: new event coming
2018-08-02 11:26:15,145 - main.py[line:68] - INFO: event timestamp 0
2018-08-02 11:26:15,155 - main.py[line:69] - INFO: next binlog file is: mysql-bin-changelog.382574
2018-08-02 11:26:16,284 - main.py[line:62] - INFO: new event coming
2018-08-02 11:26:16,293 - main.py[line:68] - INFO: event timestamp 0
2018-08-02 11:26:16,301 - main.py[line:69] - INFO: next binlog file is: mysql-bin-changelog.382574
2018-08-02 11:26:17,397 - main.py[line:62] - INFO: new event coming
2018-08-02 11:26:17,406 - main.py[line:68] - INFO: event timestamp 0
2018-08-02 11:26:17,415 - main.py[line:69] - INFO: next binlog file is: mysql-bin-changelog.382574
2018-08-02 11:26:18,548 - main.py[line:62] - INFO: new event coming
2018-08-02 11:26:18,557 - main.py[line:68] - INFO: event timestamp 0
2018-08-02 11:26:18,567 - main.py[line:69] - INFO: next binlog file is: mysql-bin-changelog.382574
2018-08-02 11:26:19,677 - main.py[line:62] - INFO: new event coming
2018-08-02 11:26:19,689 - main.py[line:68] - INFO: event timestamp 0
2018-08-02 11:26:19,704 - main.py[line:69] - INFO: next binlog file is: mysql-bin-changelog.382574
2018-08-02 11:26:20,776 - main.py[line:62] - INFO: new event coming
2018-08-02 11:26:20,784 - main.py[line:68] - INFO: event timestamp 0
2018-08-02 11:26:20,793 - main.py[line:69] - INFO: next binlog file is: mysql-bin-changelog.382574
2018-08-02 11:26:21,920 - main.py[line:62] - INFO: new event coming

anything wrong with my code?

thank you

zhangjustin avatar Aug 02 '18 03:08 zhangjustin

@baloo , if you have free time, please have a look on this.

zhangjustin avatar Aug 02 '18 03:08 zhangjustin