From cd35e17892f035bfb551a492cfde1b7b17581bfe Mon Sep 17 00:00:00 2001 From: krowinski Date: Mon, 29 Jan 2024 21:40:16 +0100 Subject: [PATCH] bin position as string --- CHANGELOG.md | 12 +++++++----- src/MySQLReplication/BinLog/BinLogCurrent.php | 6 +++--- .../BinLog/BinLogSocketConnect.php | 2 +- src/MySQLReplication/Config/Config.php | 8 ++------ src/MySQLReplication/Config/ConfigBuilder.php | 4 ++-- src/MySQLReplication/Event/DTO/RotateDTO.php | 2 +- src/MySQLReplication/Event/Event.php | 2 +- src/MySQLReplication/Event/EventInfo.php | 4 ++-- src/MySQLReplication/Event/RotateEvent.php | 2 +- .../Repository/MasterStatusDTO.php | 4 ++-- .../BinaryDataReader/BinaryDataReaderTest.php | 15 +++++++-------- tests/Unit/Config/ConfigTest.php | 17 +++++++---------- 12 files changed, 36 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 901645a..e5cc6b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,15 @@ - Change: drop support for < 8.2 - Change: moved to enums, promoted properties - Added: logger for more socket info -- Added: slave_uuid support -- Change: config no longer static +- Added: slave_uuid support (#99) +- Change: EventInfo->id is now EventInfo->serverId (#83) +- Change: config no longer static (#94) - Chore: typos in README/code -- Chore: replace/remove old urls from code -- Chore: changed variables to underscore -- Added: support caching_sha2_password +- Chore: replace/remove old dead doc urls from code +- Chore: changed variables to underscore +- Added: support caching_sha2_password (#102) - Change: BinLogServerInfo static calls removed also added method getServerInfo to MySQLReplicationFactory +- Change: type of bin log position is now string as it can be bigger then php can hande 2^64-1 (#84) ## v7.0.1 (2021-03-09) diff --git a/src/MySQLReplication/BinLog/BinLogCurrent.php b/src/MySQLReplication/BinLog/BinLogCurrent.php index 8b2c658..7a59511 100644 --- a/src/MySQLReplication/BinLog/BinLogCurrent.php +++ b/src/MySQLReplication/BinLog/BinLogCurrent.php @@ -8,7 +8,7 @@ class BinLogCurrent implements JsonSerializable { - private int $binLogPosition; + private string $binLogPosition; private string $binFileName; @@ -16,12 +16,12 @@ class BinLogCurrent implements JsonSerializable private string $mariaDbGtid; - public function getBinLogPosition(): int + public function getBinLogPosition(): string { return $this->binLogPosition; } - public function setBinLogPosition(int $binLogPosition): void + public function setBinLogPosition(string $binLogPosition): void { $this->binLogPosition = $binLogPosition; } diff --git a/src/MySQLReplication/BinLog/BinLogSocketConnect.php b/src/MySQLReplication/BinLog/BinLogSocketConnect.php index 8661734..ccdbaba 100644 --- a/src/MySQLReplication/BinLog/BinLogSocketConnect.php +++ b/src/MySQLReplication/BinLog/BinLogSocketConnect.php @@ -296,7 +296,7 @@ private function setBinLogDump(): void $binFilePos = $this->config->binLogPosition; $binFileName = $this->config->binLogFileName; // if not set start from newest binlog - if ($binFilePos === 0 && $binFileName === '') { + if ($binFilePos === '' && $binFileName === '') { $masterStatusDTO = $this->repository->getMasterStatus(); $binFilePos = $masterStatusDTO->position; $binFileName = $masterStatusDTO->file; diff --git a/src/MySQLReplication/Config/Config.php b/src/MySQLReplication/Config/Config.php index ef1e676..e37e65f 100644 --- a/src/MySQLReplication/Config/Config.php +++ b/src/MySQLReplication/Config/Config.php @@ -18,7 +18,7 @@ public function __construct( public string $mariaDbGtid, public int $slaveId, public string $binLogFileName, - public int $binLogPosition, + public string $binLogPosition, public array $eventsOnly, public array $eventsIgnore, public array $tablesOnly, @@ -74,11 +74,7 @@ public function validate(): void ConfigException::SLAVE_ID_ERROR_CODE ); } - if (filter_var($this->binLogPosition, FILTER_VALIDATE_INT, [ - 'options' => [ - 'min_range' => 0, - ], - ]) === false) { + if (bccomp($this->binLogPosition, '0') === -1) { throw new ConfigException( ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE diff --git a/src/MySQLReplication/Config/ConfigBuilder.php b/src/MySQLReplication/Config/ConfigBuilder.php index 331bea1..e86e9d9 100644 --- a/src/MySQLReplication/Config/ConfigBuilder.php +++ b/src/MySQLReplication/Config/ConfigBuilder.php @@ -22,7 +22,7 @@ class ConfigBuilder private string $binLogFileName = ''; - private int $binLogPosition = 0; + private string $binLogPosition = ''; private array $eventsOnly = []; @@ -105,7 +105,7 @@ public function withBinLogFileName(string $binLogFileName): self return $this; } - public function withBinLogPosition(int $binLogPosition): self + public function withBinLogPosition(string $binLogPosition): self { $this->binLogPosition = $binLogPosition; diff --git a/src/MySQLReplication/Event/DTO/RotateDTO.php b/src/MySQLReplication/Event/DTO/RotateDTO.php index fecd16a..66ae072 100644 --- a/src/MySQLReplication/Event/DTO/RotateDTO.php +++ b/src/MySQLReplication/Event/DTO/RotateDTO.php @@ -13,7 +13,7 @@ class RotateDTO extends EventDTO public function __construct( EventInfo $eventInfo, - public readonly int $position, + public readonly string $position, public readonly string $nextBinlog ) { parent::__construct($eventInfo); diff --git a/src/MySQLReplication/Event/Event.php b/src/MySQLReplication/Event/Event.php index 125ee82..4af0caf 100644 --- a/src/MySQLReplication/Event/Event.php +++ b/src/MySQLReplication/Event/Event.php @@ -134,7 +134,7 @@ private function createEventInfo(BinaryDataReader $binaryDataReader): EventInfo $binaryDataReader->readUInt8(), $binaryDataReader->readInt32(), $binaryDataReader->readInt32(), - $binaryDataReader->readInt32(), + (string)$binaryDataReader->readInt32(), $binaryDataReader->readUInt16(), $this->binLogSocketConnect->getCheckSum(), $this->binLogSocketConnect->getBinLogCurrent() diff --git a/src/MySQLReplication/Event/EventInfo.php b/src/MySQLReplication/Event/EventInfo.php index 5fb39c5..b11a2a2 100644 --- a/src/MySQLReplication/Event/EventInfo.php +++ b/src/MySQLReplication/Event/EventInfo.php @@ -17,9 +17,9 @@ class EventInfo implements JsonSerializable public function __construct( public readonly int $timestamp, public readonly int $type, - public readonly int $id, + public readonly int $serverId, public readonly int $size, - public readonly int $pos, + public readonly string $pos, public readonly int $flag, public readonly bool $checkSum, public readonly BinLogCurrent $binLogCurrent diff --git a/src/MySQLReplication/Event/RotateEvent.php b/src/MySQLReplication/Event/RotateEvent.php index a0148e5..8beac31 100644 --- a/src/MySQLReplication/Event/RotateEvent.php +++ b/src/MySQLReplication/Event/RotateEvent.php @@ -13,7 +13,7 @@ class RotateEvent extends EventCommon { public function makeRotateEventDTO(): RotateDTO { - $binFilePos = (int)$this->binaryDataReader->readUInt64(); + $binFilePos = $this->binaryDataReader->readUInt64(); $binFileName = $this->binaryDataReader->read( $this->eventInfo->getSizeNoHeader() - $this->getSizeToRemoveByVersion() ); diff --git a/src/MySQLReplication/Repository/MasterStatusDTO.php b/src/MySQLReplication/Repository/MasterStatusDTO.php index 19d8671..1e20ecd 100644 --- a/src/MySQLReplication/Repository/MasterStatusDTO.php +++ b/src/MySQLReplication/Repository/MasterStatusDTO.php @@ -7,13 +7,13 @@ readonly class MasterStatusDTO { public function __construct( - public int $position, + public string $position, public string $file ) { } public static function makeFromArray(array $data): self { - return new self((int)$data['Position'], (string)$data['File']); + return new self((string)$data['Position'], (string)$data['File']); } } diff --git a/tests/Unit/BinaryDataReader/BinaryDataReaderTest.php b/tests/Unit/BinaryDataReader/BinaryDataReaderTest.php index f67c498..676d395 100644 --- a/tests/Unit/BinaryDataReader/BinaryDataReaderTest.php +++ b/tests/Unit/BinaryDataReader/BinaryDataReaderTest.php @@ -8,6 +8,7 @@ use MySQLReplication\BinaryDataReader\BinaryDataReader; use MySQLReplication\BinaryDataReader\BinaryDataReaderException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class BinaryDataReaderTest extends TestCase @@ -62,9 +63,7 @@ public function testShouldReadReadUInt64(): void ); } - /** - * @dataProvider dataProviderForUInt - */ + #[DataProvider('dataProviderForUInt')] public function testShouldReadUIntBySize(mixed $size, mixed $data, mixed $expected): void { self::assertSame($expected, $this->getBinaryRead($data)->readUIntBySize($size)); @@ -89,11 +88,11 @@ public static function dataProviderForBeInt(): array ]; } - /** - * @dataProvider dataProviderForBeInt - */ - public function testShouldReadIntBeBySize(int $size, string $data, int $expected): void - { + #[DataProvider('dataProviderForBeInt')] public function testShouldReadIntBeBySize( + int $size, + string $data, + int $expected + ): void { self::assertSame($expected, $this->getBinaryRead($data)->readIntBeBySize($size)); } diff --git a/tests/Unit/Config/ConfigTest.php b/tests/Unit/Config/ConfigTest.php index b522d72..9e137b1 100644 --- a/tests/Unit/Config/ConfigTest.php +++ b/tests/Unit/Config/ConfigTest.php @@ -9,6 +9,7 @@ use MySQLReplication\Config\Config; use MySQLReplication\Config\ConfigBuilder; use MySQLReplication\Config\ConfigException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ConfigTest extends TestCase @@ -24,7 +25,7 @@ public function shouldMakeConfig(): void 'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592', 'slaveId' => 1, 'binLogFileName' => 'binfile1.bin', - 'binLogPosition' => 999, + 'binLogPosition' => '999', 'eventsOnly' => [], 'eventsIgnore' => [], 'tablesOnly' => ['test_table'], @@ -133,11 +134,9 @@ public static function shouldCheckHeartbeatPeriodProvider(): array return [[0], [0.0], [0.001], [4294967], [2]]; } - /** - * @dataProvider shouldCheckHeartbeatPeriodProvider - */ - public function testShouldCheckHeartbeatPeriod(int|float $heartbeatPeriod): void - { + #[DataProvider('shouldCheckHeartbeatPeriodProvider')] public function testShouldCheckHeartbeatPeriod( + int|float $heartbeatPeriod + ): void { $config = (new ConfigBuilder())->withHeartbeatPeriod($heartbeatPeriod) ->build(); $config->validate(); @@ -154,7 +153,7 @@ public static function shouldValidateProvider(): array ['gtid', '-1', ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE], [ 'binLogPosition', - -1, + '-1', ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE, ], @@ -179,9 +178,7 @@ public static function shouldValidateProvider(): array ]; } - /** - * @dataProvider shouldValidateProvider - */ + #[DataProvider('shouldValidateProvider')] public function testShouldValidate( string $configKey, mixed $configValue,