Skip to content

Commit 9a9a903

Browse files
syeeunsjulien-duponchelle
authored andcommitted
Add MariadbBinLogCheckPointEvent
Co-authored-by:: yangbaechu <[email protected]> Co-authored-by:: jaehyeonpy <[email protected]> Co-authored-by:: ebang <[email protected]>
1 parent 47ae923 commit 9a9a903

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

Diff for: examples/mariadb_gtid/read_event.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pymysql
22

33
from pymysqlreplication import BinLogStreamReader, gtid
4-
from pymysqlreplication.event import GtidEvent, RotateEvent, MariadbGtidEvent, QueryEvent,MariadbAnnotateRowsEvent
4+
from pymysqlreplication.event import GtidEvent, RotateEvent, MariadbGtidEvent, QueryEvent,MariadbAnnotateRowsEvent, MariadbBinLogCheckPointEvent
55
from pymysqlreplication.row_event import WriteRowsEvent, UpdateRowsEvent, DeleteRowsEvent
66

77
MARIADB_SETTINGS = {
@@ -62,6 +62,7 @@ def query_server_id(self):
6262
blocking=False,
6363
only_events=[
6464
MariadbGtidEvent,
65+
MariadbBinLogCheckPointEvent,
6566
RotateEvent,
6667
WriteRowsEvent,
6768
UpdateRowsEvent,

Diff for: pymysqlreplication/binlogstream.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
BeginLoadQueryEvent, ExecuteLoadQueryEvent,
1515
HeartbeatLogEvent, NotImplementedEvent, MariadbGtidEvent,
1616
MariadbAnnotateRowsEvent, RandEvent, MariadbStartEncryptionEvent, RowsQueryLogEvent,
17-
MariadbGtidListEvent)
17+
MariadbGtidListEvent, MariadbBinLogCheckPointEvent)
1818
from .exceptions import BinLogNotEnabled
1919
from .gtid import GtidSet
2020
from .packet import BinLogPacketWrapper
@@ -625,7 +625,8 @@ def _allowed_event_list(self, only_events, ignored_events,
625625
MariadbAnnotateRowsEvent,
626626
RandEvent,
627627
MariadbStartEncryptionEvent,
628-
MariadbGtidListEvent
628+
MariadbGtidListEvent,
629+
MariadbBinLogCheckPointEvent
629630
))
630631
if ignored_events is not None:
631632
for e in ignored_events:

Diff for: pymysqlreplication/event.py

+19
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ def _dump(self):
110110
print("Flags:", self.flags)
111111
print('GTID:', self.gtid)
112112

113+
class MariadbBinLogCheckPointEvent(BinLogEvent):
114+
"""
115+
Represents a checkpoint in a binlog event in MariaDB.
116+
117+
More details are available in the MariaDB Knowledge Base:
118+
https://mariadb.com/kb/en/binlog_checkpoint_event/
119+
120+
:ivar filename_length: int - The length of the filename.
121+
:ivar filename: str - The name of the file saved at the checkpoint.
122+
"""
123+
124+
def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs):
125+
super(MariadbBinLogCheckPointEvent, self).__init__(from_packet, event_size, table_map, ctl_connection,
126+
**kwargs)
127+
filename_length = self.packet.read_uint32()
128+
self.filename = self.packet.read(filename_length).decode()
129+
130+
def _dump(self):
131+
print('Filename:', self.filename)
113132

114133
class MariadbAnnotateRowsEvent(BinLogEvent):
115134
"""

Diff for: pymysqlreplication/packet.py

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class BinLogPacketWrapper(object):
8888
# MariaDB GTID
8989
constants.MARIADB_ANNOTATE_ROWS_EVENT: event.MariadbAnnotateRowsEvent,
9090
constants.MARIADB_BINLOG_CHECKPOINT_EVENT: event.NotImplementedEvent,
91+
constants.MARIADB_BINLOG_CHECKPOINT_EVENT: event.MariadbBinLogCheckPointEvent,
9192
constants.MARIADB_GTID_EVENT: event.MariadbGtidEvent,
9293
constants.MARIADB_GTID_GTID_LIST_EVENT: event.MariadbGtidListEvent,
9394
constants.MARIADB_START_ENCRYPTION_EVENT: event.MariadbStartEncryptionEvent

Diff for: pymysqlreplication/tests/base.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,9 @@ def setUp(self):
144144
self.connect_conn_control(db)
145145
self.stream = None
146146
self.resetBinLog()
147-
147+
148+
def bin_log_basename(self):
149+
cursor = self.execute('SELECT @@log_bin_basename')
150+
bin_log_basename = cursor.fetchone()[0]
151+
bin_log_basename = bin_log_basename.split("/")[-1]
152+
return bin_log_basename

Diff for: pymysqlreplication/tests/test_basic.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def ignoredEvents(self):
2727
return [GtidEvent]
2828

2929
def test_allowed_event_list(self):
30-
self.assertEqual(len(self.stream._allowed_event_list(None, None, False)), 21)
31-
self.assertEqual(len(self.stream._allowed_event_list(None, None, True)), 20)
32-
self.assertEqual(len(self.stream._allowed_event_list(None, [RotateEvent], False)), 20)
30+
self.assertEqual(len(self.stream._allowed_event_list(None, None, False)), 22)
31+
self.assertEqual(len(self.stream._allowed_event_list(None, None, True)), 21)
32+
self.assertEqual(len(self.stream._allowed_event_list(None, [RotateEvent], False)), 21)
3333
self.assertEqual(len(self.stream._allowed_event_list([RotateEvent], None, False)), 1)
3434

3535
def test_read_query_event(self):
@@ -1009,6 +1009,32 @@ def test_parsing(self):
10091009
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-:1")
10101010
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac::1")
10111011

1012+
class TestMariadbBinlogStreamReader(base.PyMySQLReplicationMariaDbTestCase):
1013+
def test_binlog_checkpoint_event(self):
1014+
self.stream.close()
1015+
self.stream = BinLogStreamReader(
1016+
self.database,
1017+
server_id=1023,
1018+
blocking=False,
1019+
is_mariadb=True
1020+
)
1021+
1022+
query = "DROP TABLE IF EXISTS test"
1023+
self.execute(query)
1024+
1025+
query = "CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT, data VARCHAR (50) NOT NULL, PRIMARY KEY (id))"
1026+
self.execute(query)
1027+
self.stream.close()
1028+
1029+
event = self.stream.fetchone()
1030+
self.assertIsInstance(event, RotateEvent)
1031+
1032+
event = self.stream.fetchone()
1033+
self.assertIsInstance(event,FormatDescriptionEvent)
1034+
1035+
event = self.stream.fetchone()
1036+
self.assertIsInstance(event, MariadbBinLogCheckPointEvent)
1037+
self.assertEqual(event.filename, self.bin_log_basename()+".000001")
10121038

10131039
class TestMariadbBinlogStreamReader(base.PyMySQLReplicationMariaDbTestCase):
10141040

0 commit comments

Comments
 (0)