From fcf514fc24f7f645fe797ed2acb55721d8738530 Mon Sep 17 00:00:00 2001 From: cyppe Date: Sat, 14 Nov 2026 14:41:08 +0100 Subject: [PATCH 1/3] Read the event header's 32-bit fields unsigned The timestamp, server id, event size and next-event position are unsigned 32-bit. Read signed, a next position past 2 GiB became negative, EventInfo ignored it, and BinLogCurrent stopped advancing inside a binlog file that a large transaction grew past 2 GiB. --- src/MySQLReplication/Event/Event.php | 11 +++-- tests/Unit/Event/EventConsumeHeaderTest.php | 54 +++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tests/Unit/Event/EventConsumeHeaderTest.php diff --git a/src/MySQLReplication/Event/Event.php b/src/MySQLReplication/Event/Event.php index bf9ea6a9..e10c66bc 100644 --- a/src/MySQLReplication/Event/Event.php +++ b/src/MySQLReplication/Event/Event.php @@ -152,12 +152,15 @@ private function makeEvent(BinaryDataReader $binaryDataReader): ?EventDTO */ private function createEventInfo(BinaryDataReader $binaryDataReader): EventInfo { + // timestamp, server id, event size and next-event position are all unsigned 32-bit + // fields; reading them signed turned positions past 2 GiB negative, which EventInfo then + // ignored, so the resume position silently stopped advancing return new EventInfo( - $binaryDataReader->readInt32(), + $binaryDataReader->readUInt32(), $binaryDataReader->readUInt8(), - $binaryDataReader->readInt32(), - $binaryDataReader->readInt32(), - (string)$binaryDataReader->readInt32(), + $binaryDataReader->readUInt32(), + $binaryDataReader->readUInt32(), + (string)$binaryDataReader->readUInt32(), $binaryDataReader->readUInt16(), $this->binLogSocketConnect->getCheckSum(), $this->binLogSocketConnect->getBinLogCurrent() diff --git a/tests/Unit/Event/EventConsumeHeaderTest.php b/tests/Unit/Event/EventConsumeHeaderTest.php new file mode 100644 index 00000000..20d2c616 --- /dev/null +++ b/tests/Unit/Event/EventConsumeHeaderTest.php @@ -0,0 +1,54 @@ +setBinLogPosition('2147483000'); + + $socket = $this->createStub(BinLogSocketConnect::class); + $socket->method('getResponse')->willReturn( + chr(0) + . pack('V', 0xF0000000) // timestamp + . chr(0) // type UNKNOWN_EVENT, not subscribed + . pack('V', 0xFFFFFFFE) // server id + . pack('V', 19) // event size + . pack('V', 0x90000000) // next position, past 2 GiB + . pack('v', 0) // flags + ); + $socket->method('getBinLogCurrent')->willReturn($current); + $socket->method('getCheckSum')->willReturn(false); + + $config = $this->createStub(Config::class); + $config->method('checkEvent')->willReturn(false); + + (new Event( + $socket, + $this->createStub(RowEventFactory::class), + $this->createStub(EventDispatcherInterface::class), + $this->createStub(CacheInterface::class), + $config, + $this->createStub(BinLogServerInfo::class) + ))->consume(); + + self::assertSame((string)0x90000000, $current->getBinLogPosition()); + } +} From 92db81cf092cff23331ea076ed208a01491eeba9 Mon Sep 17 00:00:00 2001 From: cyppe Date: Sat, 14 Nov 2026 14:41:08 +0100 Subject: [PATCH 2/3] Skip the whole v2 rows-event extra data block The extra data length counts its own two bytes (rows_event.cpp reads it and subtracts 2), but findTableMap() skipped length / 8 bytes. For the usual length 2 that skipped nothing, which was right by chance. MySQL 8.0.16+ writes the partition id into every rows event of a partitioned table (typecode plus one or two 2-byte ids, length 5 or 7), which was left unread: a two-row INSERT into a HASH-partitioned table decoded as garbage rows. Skip length - 2. --- .../Event/RowEvent/RowEvent.php | 7 ++- tests/Integration/BasicTest.php | 61 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/MySQLReplication/Event/RowEvent/RowEvent.php b/src/MySQLReplication/Event/RowEvent/RowEvent.php index 0bd701dd..0ffac804 100644 --- a/src/MySQLReplication/Event/RowEvent/RowEvent.php +++ b/src/MySQLReplication/Event/RowEvent/RowEvent.php @@ -417,7 +417,12 @@ protected function findTableMap(): ?TableMap ], true )) { - $this->binaryDataReader->read((int)($this->binaryDataReader->readUInt16() / 8)); + // v2 extra data: a 2-byte length that counts itself, then extra-row-info such as the + // partition id MySQL 8.0.16+ writes for partitioned tables (and NDB info). Skip it whole. + $extraDataLength = $this->binaryDataReader->readUInt16(); + if ($extraDataLength > 2) { + $this->binaryDataReader->advance($extraDataLength - 2); + } } $this->binaryDataReader->readCodedBinary(); diff --git a/tests/Integration/BasicTest.php b/tests/Integration/BasicTest.php index 278635fc..c1037234 100644 --- a/tests/Integration/BasicTest.php +++ b/tests/Integration/BasicTest.php @@ -66,6 +66,67 @@ public function testShouldGetUpdateEvent(): void self::assertEquals('World', $event->values[0]['after']['data']); } + public function testShouldDecodeRowEventsOfAPartitionedTable(): void + { + if ($this->mySQLReplicationFactory?->getServerInfo()->isMariaDb()) { + self::markTestSkipped('MariaDB writes no partition info into rows events'); + } + + // MySQL 8.0.16+ writes the partition id into the extra-row-info of every v2 rows event of a + // partitioned table, so the extra data block is longer than its own 2-byte length field. + $event = $this->createAndInsertValue( + 'CREATE TABLE test (id INT NOT NULL, data VARCHAR(50) NOT NULL, PRIMARY KEY (id)) PARTITION BY HASH(id) PARTITIONS 4', + 'INSERT INTO test VALUES (1, \'Hello\'), (2, \'There\')' + ); + self::assertInstanceOf(WriteRowsDTO::class, $event); + self::assertSame([[ + 'id' => 1, + 'data' => 'Hello', + ]], $event->values); + // the second row lands in another partition, so MySQL writes it as its own rows event + $event = $this->getEvent(); + self::assertInstanceOf(WriteRowsDTO::class, $event); + self::assertSame([[ + 'id' => 2, + 'data' => 'There', + ]], $event->values); + + // moving the row to another partition writes both the source and the target partition id + $this->connection->executeStatement('UPDATE test SET data = \'World\', id = 3 WHERE id = 1'); + + self::assertInstanceOf(XidDTO::class, $this->getEvent()); + self::assertInstanceOf(QueryDTO::class, $this->getEvent()); + self::assertInstanceOf(TableMapDTO::class, $this->getEvent()); + + /** @var UpdateRowsDTO $event */ + $event = $this->getEvent(); + self::assertInstanceOf(UpdateRowsDTO::class, $event); + self::assertSame([[ + 'before' => [ + 'id' => 1, + 'data' => 'Hello', + ], + 'after' => [ + 'id' => 3, + 'data' => 'World', + ], + ]], $event->values); + + $this->connection->executeStatement('DELETE FROM test WHERE id = 2'); + + self::assertInstanceOf(XidDTO::class, $this->getEvent()); + self::assertInstanceOf(QueryDTO::class, $this->getEvent()); + self::assertInstanceOf(TableMapDTO::class, $this->getEvent()); + + /** @var DeleteRowsDTO $event */ + $event = $this->getEvent(); + self::assertInstanceOf(DeleteRowsDTO::class, $event); + self::assertSame([[ + 'id' => 2, + 'data' => 'There', + ]], $event->values); + } + public function testShouldGetWriteEventDropTable(): void { // exercises the information_schema fallback's "table no longer exists" guard, so force it From e0faaebbf1bc839c10b185323a45668b13d446d4 Mon Sep 17 00:00:00 2001 From: cyppe Date: Sat, 14 Nov 2026 14:50:05 +0100 Subject: [PATCH 3/3] Run the partition test only on MySQL 8.0.16+ --- tests/Integration/BasicTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/Integration/BasicTest.php b/tests/Integration/BasicTest.php index c1037234..8299e0a7 100644 --- a/tests/Integration/BasicTest.php +++ b/tests/Integration/BasicTest.php @@ -68,8 +68,11 @@ public function testShouldGetUpdateEvent(): void public function testShouldDecodeRowEventsOfAPartitionedTable(): void { - if ($this->mySQLReplicationFactory?->getServerInfo()->isMariaDb()) { - self::markTestSkipped('MariaDB writes no partition info into rows events'); + $serverInfo = $this->mySQLReplicationFactory?->getServerInfo(); + if ($serverInfo === null || $serverInfo->isMariaDb() + || preg_match('/^\d+\.\d+\.\d+/', $serverInfo->serverVersion, $version) !== 1 + || version_compare($version[0], '8.0.16', '<')) { + self::markTestSkipped('Needs MySQL 8.0.16+, which writes the partition id into rows events'); } // MySQL 8.0.16+ writes the partition id into the extra-row-info of every v2 rows event of a