Skip to content

Commit d5fe687

Browse files
author
taehun.kim
committed
Add properties in gtid event
Motivation: GtidEvent is aleady implemented but there are missing 6 properties. If we respect these properties, user can use GtidEvent with more convenient. Motivation: - Fix(event.py): Add two properties. - Add(test_basic.py): Add test for GtidEvent. Result: for now on, user can use read_immediate_commit_timestamp and read_original_commit_timestamp properties.
1 parent 6850c0d commit d5fe687

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Diff for: pymysqlreplication/event.py

+23
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ def __init__(self, from_packet, event_size, table_map, ctl_connection, **kwargs)
108108
self.last_committed = struct.unpack("<Q", self.packet.read(8))[0]
109109
self.sequence_number = struct.unpack("<Q", self.packet.read(8))[0]
110110

111+
format_string = '<Q'
112+
read_immediate_commit_timestamp = self.packet.read(7)
113+
self.immediate_commit_timestamp = read_immediate_commit_timestamp.ljust(struct.calcsize(format_string), b'\x00')
114+
self.immediate_commit_timestamp = struct.unpack(format_string, self.immediate_commit_timestamp)[0]
115+
self.immediate_commit_timestamp = self.immediate_commit_timestamp & (
116+
(1 << (8 * struct.calcsize(format_string))) - 1)
117+
self.immediate_commit_timestamp = struct.pack(format_string, self.immediate_commit_timestamp)
118+
119+
read_original_commit_timestamp = self.packet.read(7)
120+
self.original_commit_timestamp = read_original_commit_timestamp.ljust(struct.calcsize(format_string), b'\x00')
121+
self.original_commit_timestamp = struct.unpack(format_string, self.original_commit_timestamp)[0]
122+
self.original_commit_timestamp = self.original_commit_timestamp & (
123+
(1 << (8 * struct.calcsize(format_string))) - 1)
124+
self.original_commit_timestamp = struct.pack(format_string, self.original_commit_timestamp)
125+
126+
# todo(hun): We should implement the properties below,
127+
# but due to the lack of documentation on the variable-length property,
128+
# we've left it as future work.
129+
# - transaction_length, unknown Variable Length
130+
# - immediate_server_version, 4 bytes
131+
# - original_server_version, 4 bytes
132+
# - Commit group ticket, 8 bytes, This property is only introduced in the CPP implementation.
133+
111134
@property
112135
def gtid(self):
113136
"""

Diff for: pymysqlreplication/tests/test_basic.py

+31
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,37 @@ def test_rows_query_log_event(self):
14721472
event = self.stream.fetchone()
14731473
self.assertIsInstance(event, RowsQueryLogEvent)
14741474

1475+
class TestGtidEvent(base.PyMySQLReplicationTestCase):
1476+
def setUp(self):
1477+
super(TestGtidEvent, self).setUp()
1478+
self.execute("SET SESSION binlog_rows_query_log_events=1")
1479+
1480+
def tearDown(self):
1481+
self.execute("SET SESSION binlog_rows_query_log_events=0")
1482+
super(TestGtidEvent, self).tearDown()
1483+
1484+
def test_gtid_event(self):
1485+
self.stream.close()
1486+
self.stream = BinLogStreamReader(
1487+
self.database,
1488+
server_id=1024,
1489+
only_events=[GtidEvent],
1490+
)
1491+
self.execute(
1492+
"CREATE TABLE IF NOT EXISTS test (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))"
1493+
)
1494+
gtid_event = self.stream.fetchone()
1495+
1496+
self.assertIsInstance(gtid_event, GtidEvent)
1497+
self.assertIsInstance(gtid_event.event_type, int)
1498+
self.assertIsInstance(gtid_event.sid, bytes)
1499+
self.assertIsInstance(gtid_event.gno, int)
1500+
self.assertIsInstance(gtid_event.lt_type, int)
1501+
self.assertIsInstance(gtid_event.last_committed, int)
1502+
self.assertIsInstance(gtid_event.sequence_number, int)
1503+
self.assertEqual(gtid_event.sequence_number, 1)
1504+
self.assertIsInstance(gtid_event.immediate_commit_timestamp, bytes)
1505+
self.assertIsInstance(gtid_event.original_commit_timestamp, bytes)
14751506

14761507
class TestLatin1(base.PyMySQLReplicationTestCase):
14771508
def setUp(self):

0 commit comments

Comments
 (0)