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/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..8299e0a7 100644 --- a/tests/Integration/BasicTest.php +++ b/tests/Integration/BasicTest.php @@ -66,6 +66,70 @@ public function testShouldGetUpdateEvent(): void self::assertEquals('World', $event->values[0]['after']['data']); } + public function testShouldDecodeRowEventsOfAPartitionedTable(): void + { + $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 + // 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 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()); + } +}