Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/MySQLReplication/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion src/MySQLReplication/Event/RowEvent/RowEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
64 changes: 64 additions & 0 deletions tests/Integration/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/Event/EventConsumeHeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace MySQLReplication\Tests\Unit\Event;

use MySQLReplication\BinLog\BinLogCurrent;
use MySQLReplication\BinLog\BinLogServerInfo;
use MySQLReplication\BinLog\BinLogSocketConnect;
use MySQLReplication\Config\Config;
use MySQLReplication\Event\Event;
use MySQLReplication\Event\RowEvent\RowEventFactory;
use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* The event header's timestamp, server id, size and next position are unsigned 32-bit fields.
*/
class EventConsumeHeaderTest extends TestCase
{
public function testNextPositionPastTwoGibibytesAdvancesTheBinLogPosition(): void
{
$current = new BinLogCurrent();
$current->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());
}
}
Loading