Skip to content

Commit c446135

Browse files
authored
Merge pull request #4 from Moln/feature/retry-connect
[Feature]retry connection.
2 parents 031f57e + 844de37 commit c446135

14 files changed

+158
-46
lines changed

.gitattributes

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.coveralls.yml export-ignore
2+
/.gitattributes export-ignore
3+
/.github/ export-ignore
4+
/.gitignore export-ignore
5+
/.travis.yml export-ignore
6+
/.travis/ export-ignore
7+
/phpunit.xml export-ignore
8+
/tests/ export-ignore

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
"ext-sockets": "*",
1919
"doctrine/collections": "^1.3",
2020
"doctrine/dbal": "^3.0",
21+
"psr/log": "^1.0 | ^2.0 | ^3.0",
2122
"psr/simple-cache": "^1.0 | ^3.0",
2223
"symfony/event-dispatcher": "^3.1|^4.0|^5.0|^6.0"
2324
},
2425
"require-dev": {
26+
"monolog/monolog": "^2.8",
2527
"phpunit/phpunit": "^9.0"
2628
},
2729
"license": "MIT",
@@ -44,5 +46,8 @@
4446
"minimum-stability": "stable",
4547
"config": {
4648
"sort-packages": true
49+
},
50+
"conflict": {
51+
"krowinski/php-mysql-replication": "*"
4752
}
4853
}

example/resuming.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
date_default_timezone_set('UTC');
88
include __DIR__ . '/../vendor/autoload.php';
99

10+
use Monolog\Handler\StreamHandler;
11+
use Monolog\Logger;
1012
use MySQLReplication\BinLog\BinLogCurrent;
1113
use MySQLReplication\Config\ConfigBuilder;
1214
use MySQLReplication\Event\DTO\EventDTO;
1315
use MySQLReplication\Event\EventSubscribers;
1416
use MySQLReplication\MySQLReplicationFactory;
1517

18+
$logger = new Logger("app", [new StreamHandler(STDOUT)]);
19+
BinLogBootstrap::clear();
1620
/**
1721
* Your db configuration @see ConfigBuilder for more options
1822
*/
@@ -22,7 +26,10 @@
2226
->withHost('127.0.0.1')
2327
->withPort(3306)
2428
->withPassword('root')
25-
->build()
29+
->withRetry(3)
30+
->build(),
31+
null, null, null, null,
32+
$logger
2633
);
2734

2835
/**
@@ -66,11 +73,15 @@ class BinLogBootstrap
6673
private static function getFileAndPath(): string
6774
{
6875
if (null === self::$fileAndPath) {
69-
self::$fileAndPath = sys_get_temp_dir() . '/bin-log-replicator-last-position';
76+
self::$fileAndPath = '/tmp/bin-log-replicator-last-position';
7077
}
7178
return self::$fileAndPath;
7279
}
7380

81+
public static function clear() {
82+
file_exists(self::getFileAndPath()) && unlink(self::getFileAndPath());
83+
}
84+
7485
/**
7586
* @param BinLogCurrent $binLogCurrent
7687
*/

phpunit.xml

+15-23
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="tests/bootstrap.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false">
11-
<testsuites>
12-
<testsuite name="Integration Suite">
13-
<directory>./tests/Integration</directory>
14-
</testsuite>
15-
<testsuite name="Unit Suite">
16-
<directory>./tests/Unit</directory>
17-
</testsuite>
18-
</testsuites>
19-
<filter>
20-
<whitelist>
21-
<directory suffix=".php">src/</directory>
22-
</whitelist>
23-
</filter>
24-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Integration Suite">
10+
<directory>./tests/Integration</directory>
11+
</testsuite>
12+
<testsuite name="Unit Suite">
13+
<directory>./tests/Unit</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

src/MySQLReplication/BinLog/BinLogCurrent.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ class BinLogCurrent implements JsonSerializable
1010
/**
1111
* @var int
1212
*/
13-
private $binLogPosition;
13+
private $binLogPosition = 0;
1414
/**
1515
* @var string
1616
*/
17-
private $binFileName;
17+
private $binFileName = '';
1818
/**
1919
* @var string
2020
*/

src/MySQLReplication/BinLog/BinLogServerInfo.php

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static function parsePackage(string $data, string $version): BinLogServer
1717
{
1818
$i = 0;
1919
$length = strlen($data);
20+
$serverInfo = [];
2021
$serverInfo['protocol_version'] = ord($data[$i]);
2122
++$i;
2223

src/MySQLReplication/BinLog/BinLogSocketConnect.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,16 @@ public function __construct(
5252
$this->socket = $socket;
5353
$this->binLogCurrent = new BinLogCurrent();
5454

55-
$this->socket->connectToStream($config->getHost(), $config->getPort());
55+
}
56+
57+
public function isConnected(): bool
58+
{
59+
return $this->socket->isConnected();
60+
}
61+
62+
public function connect(): void
63+
{
64+
$this->socket->connectToStream($this->config->getHost(), $this->config->getPort());
5665
$this->binLogServerInfo = BinLogServerInfo::parsePackage(
5766
$this->getResponse(false),
5867
$this->repository->getVersion()
@@ -275,8 +284,9 @@ private function setBinLogDumpGtid(): void
275284
*/
276285
private function setBinLogDump(): void
277286
{
278-
$binFilePos = $this->config->getBinLogPosition();
279-
$binFileName = $this->config->getBinLogFileName();
287+
$binLogCurrent = $this->binLogCurrent;
288+
$binFilePos = $binLogCurrent->getBinLogPosition() ?: $this->config->getBinLogPosition();
289+
$binFileName = $binLogCurrent->getBinFileName() ?: $this->config->getBinLogFileName();
280290
if (0 === $binFilePos && '' === $binFileName) {
281291
$masterStatusDTO = $this->repository->getMasterStatus();
282292
$binFilePos = $masterStatusDTO->getPosition();
@@ -292,8 +302,8 @@ private function setBinLogDump(): void
292302
$this->socket->writeToSocket($data);
293303
$this->getResponse();
294304

295-
$this->binLogCurrent->setBinLogPosition($binFilePos);
296-
$this->binLogCurrent->setBinFileName($binFileName);
305+
$binLogCurrent->setBinLogPosition($binFilePos);
306+
$binLogCurrent->setBinFileName($binFileName);
297307
}
298308

299309
public function getBinLogCurrent(): BinLogCurrent

src/MySQLReplication/Config/Config.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Config implements JsonSerializable
2424
private $tableCacheSize;
2525
private $custom;
2626
private $heartbeatPeriod;
27+
private $retry;
2728

2829
public function __construct(
2930
string $user,
@@ -42,7 +43,8 @@ public function __construct(
4243
array $databasesOnly,
4344
int $tableCacheSize,
4445
array $custom,
45-
float $heartbeatPeriod
46+
float $heartbeatPeriod,
47+
int $retry = 0
4648
) {
4749
$this->user = $user;
4850
$this->host = $host;
@@ -61,6 +63,7 @@ public function __construct(
6163
$this->tableCacheSize = $tableCacheSize;
6264
$this->custom = $custom;
6365
$this->heartbeatPeriod = $heartbeatPeriod;
66+
$this->retry = $retry;
6467
}
6568

6669
/**
@@ -220,6 +223,11 @@ public function getHeartbeatPeriod(): float
220223
return $this->heartbeatPeriod;
221224
}
222225

226+
public function getRetry(): int
227+
{
228+
return $this->retry;
229+
}
230+
223231
public function jsonSerialize()
224232
{
225233
return get_object_vars($this);

src/MySQLReplication/Config/ConfigBuilder.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ConfigBuilder
2222
private $tableCacheSize = 128;
2323
private $custom = [];
2424
private $heartbeatPeriod = 0.0;
25+
private $retry = 0;
2526

2627
public function withUser(string $user): self
2728
{
@@ -147,6 +148,13 @@ public function withHeartbeatPeriod(float $heartbeatPeriod): self
147148
return $this;
148149
}
149150

151+
public function withRetry(int $retry): self
152+
{
153+
$this->retry = $retry;
154+
155+
return $this;
156+
}
157+
150158
public function build(): Config
151159
{
152160
return new Config(
@@ -166,7 +174,8 @@ public function build(): Config
166174
$this->databasesOnly,
167175
$this->tableCacheSize,
168176
$this->custom,
169-
$this->heartbeatPeriod
177+
$this->heartbeatPeriod,
178+
$this->retry
170179
);
171180
}
172181
}

src/MySQLReplication/Event/Event.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use MySQLReplication\BinaryDataReader\BinaryDataReader;
77
use MySQLReplication\BinaryDataReader\BinaryDataReaderException;
88
use MySQLReplication\BinLog\BinLogException;
9-
use MySQLReplication\BinLog\BinLogServerInfo;
109
use MySQLReplication\BinLog\BinLogSocketConnect;
1110
use MySQLReplication\Config\Config;
1211
use MySQLReplication\Definitions\ConstEventType;
@@ -35,7 +34,6 @@ class Event
3534
* @var Config
3635
*/
3736
private $config;
38-
private $binLogServerInfo;
3937

4038
public function __construct(
4139
Config $config,
@@ -45,13 +43,17 @@ public function __construct(
4543
CacheInterface $cache
4644
) {
4745
$this->config = $config;
48-
$this->binLogServerInfo = $binLogSocketConnect->getBinLogServerInfo();
4946
$this->binLogSocketConnect = $binLogSocketConnect;
5047
$this->rowEventFactory = $rowEventFactory;
5148
$this->eventDispatcher = $eventDispatcher;
5249
$this->cache = $cache;
5350
}
5451

52+
public function connect(): void
53+
{
54+
$this->binLogSocketConnect->connect();
55+
}
56+
5557
/**
5658
* @throws BinaryDataReaderException
5759
* @throws BinLogException
@@ -62,6 +64,7 @@ public function __construct(
6264
*/
6365
public function consume(): void
6466
{
67+
$binLogServerInfo = $this->binLogSocketConnect->getBinLogServerInfo();
6568
$binaryDataReader = new BinaryDataReader($this->binLogSocketConnect->getResponse());
6669

6770
// check EOF_Packet -> https://dev.mysql.com/doc/internals/en/packet-EOF_Packet.html
@@ -80,7 +83,7 @@ public function consume(): void
8083
$eventDTO = $this->rowEventFactory->makeRowEvent($binaryDataReader, $eventInfo)->makeTableMapDTO();
8184
} else if (ConstEventType::ROTATE_EVENT === $eventInfo->getType()) {
8285
$this->cache->clear();
83-
$eventDTO = (new RotateEvent($this->binLogServerInfo, $eventInfo, $binaryDataReader))->makeRotateEventDTO();
86+
$eventDTO = (new RotateEvent($binLogServerInfo, $eventInfo, $binaryDataReader))->makeRotateEventDTO();
8487
} else if (ConstEventType::GTID_LOG_EVENT === $eventInfo->getType()) {
8588
$eventDTO = (new GtidEvent($eventInfo, $binaryDataReader))->makeGTIDLogDTO();
8689
} else if (ConstEventType::HEARTBEAT_LOG_EVENT === $eventInfo->getType()) {
@@ -127,7 +130,8 @@ private function createEventInfo(BinaryDataReader $binaryDataReader): EventInfo
127130

128131
private function filterDummyMariaDbEvents(QueryDTO $queryDTO): ?QueryDTO
129132
{
130-
if ($this->binLogServerInfo->isMariaDb() && false !== strpos($queryDTO->getQuery(), self::MARIADB_DUMMY_QUERY)) {
133+
if ($this->binLogSocketConnect->getBinLogServerInfo()->isMariaDb() &&
134+
false !== strpos($queryDTO->getQuery(), self::MARIADB_DUMMY_QUERY)) {
131135
return null;
132136
}
133137

0 commit comments

Comments
 (0)