Skip to content

Commit 0016ed4

Browse files
committed
tests
1 parent 8bae5bb commit 0016ed4

29 files changed

+604
-194
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ composer.phar
66
composer.lock
77
.php_cs.cache
88
.DS_Store
9-
Thumbs.db
9+
Thumbs.db
10+
/example/profiler.php

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"require": {
1212
"php": ">=5.4",
1313
"doctrine/dbal": "^2.5",
14-
"doctrine/collections": "^1.3"
14+
"doctrine/collections": "^1.3",
15+
"ext-sockets": "^0.0.0"
1516
},
1617
"require-dev": {
1718
"phpunit/phpunit": "*"

example/benchmark.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
include __DIR__ . '/../vendor/autoload.php';
66

77
use Doctrine\DBAL\DriverManager;
8-
use MySQLReplication\BinLogStream;
8+
use MySQLReplication\MySQLReplicationFactory;
99
use MySQLReplication\Config\ConfigService;
1010
use MySQLReplication\Definitions\ConstEventType;
1111
use MySQLReplication\Event\DTO\UpdateRowsDTO;
@@ -35,10 +35,10 @@ public function __construct()
3535
$conn->exec("INSERT INTO test2 VALUES(1)");
3636
$conn->exec("RESET MASTER");
3737

38-
$this->binLogStream = new BinLogStream(
38+
$this->binLogStream = new MySQLReplicationFactory(
3939
(new ConfigService())->makeConfigFromArray([
4040
'user' => 'root',
41-
'host' => '192.168.1.100',
41+
'ip' => '192.168.1.100',
4242
'password' => 'root',
4343
'eventsOnly' => [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2],
4444
'slaveId' => 9999
@@ -96,7 +96,7 @@ private function consume()
9696
$result = $this->binLogStream->getBinLogEvent();
9797
if ($result instanceof UpdateRowsDTO)
9898
{
99-
$i += 1;
99+
++$i;
100100
if (0 === ($i % 1000))
101101
{
102102
echo ((int)($i / (microtime(true) - $start)) . ' event by seconds (' . $i . ' total)') . PHP_EOL;
@@ -120,7 +120,8 @@ private function produce()
120120
}
121121

122122
$conn->close();
123+
die;
123124
}
124125
}
125126

126-
(new Benchmark())->run();
127+
(new Benchmark())->run();

example/dump_events.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
date_default_timezone_set('UTC');
55
include __DIR__ . '/../vendor/autoload.php';
66

7-
use MySQLReplication\BinLogStream;
7+
use MySQLReplication\MySQLReplicationFactory;
88
use MySQLReplication\Config\ConfigService;
99

10-
$binLogStream = new BinLogStream(
10+
$binLogStream = new MySQLReplicationFactory(
1111
(new ConfigService())->makeConfigFromArray([
1212
'user' => 'root',
13-
'host' => '192.168.1.100',
13+
'ip' => '192.168.1.100',
1414
'password' => 'root',
1515
//'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592',
1616
])
@@ -28,4 +28,4 @@
2828

2929
echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
3030
}
31-
}
31+
}

src/MySQLReplication/BinLog/BinLogAuth.php

+3-42
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace MySQLReplication\BinLog;
44

5-
use MySQLReplication\Exception\BinLogException;
6-
75
/**
86
* Class PackAuth
97
*/
@@ -13,19 +11,7 @@ class BinLogAuth
1311
* 2^24 - 1 16m
1412
* @var int
1513
*/
16-
public $packageMaxLength = 16777215;
17-
18-
/**
19-
* http://dev.mysql.com/doc/internals/en/auth-phase-fast-path.html 00 FE
20-
* @var array
21-
*/
22-
public $packageOkHeader = [0, 254];
23-
24-
/**
25-
* FF
26-
* @var array
27-
*/
28-
public $packageErrorHeader = [255];
14+
private $binaryDataMaxLength = 16777215;
2915

3016
/**
3117
* @param $flag
@@ -35,10 +21,10 @@ class BinLogAuth
3521
* @return string
3622
* @link http://dev.mysql.com/doc/internals/en/secure-password-authentication.html#packet-Authentication::Native41
3723
*/
38-
public function createAuthenticationPacket($flag, $user, $pass, $salt)
24+
public function createAuthenticationBinary($flag, $user, $pass, $salt)
3925
{
4026
$data = pack('L', $flag);
41-
$data .= pack('L', $this->packageMaxLength);
27+
$data .= pack('L', $this->binaryDataMaxLength);
4228
$data .= chr(33);
4329
for ($i = 0; $i < 23; $i++)
4430
{
@@ -53,29 +39,4 @@ public function createAuthenticationPacket($flag, $user, $pass, $salt)
5339

5440
return $data;
5541
}
56-
57-
/**
58-
* @param string $packet
59-
* @return array
60-
* @throws BinLogException
61-
*/
62-
public function isWriteSuccessful($packet)
63-
{
64-
$head = ord($packet[0]);
65-
if (in_array($head, $this->packageOkHeader))
66-
{
67-
return ['status' => true, 'code' => 0, 'msg' => ''];
68-
}
69-
else
70-
{
71-
$error_code = unpack('v', $packet[1] . $packet[2])[1];
72-
$error_msg = '';
73-
for ($i = 9; $i < strlen($packet); $i++)
74-
{
75-
$error_msg .= $packet[$i];
76-
}
77-
78-
throw new BinLogException($error_msg, $error_code);
79-
}
80-
}
8142
}

src/MySQLReplication/BinLog/BinLogConnect.php

+65-41
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
namespace MySQLReplication\BinLog;
33

4+
use MySQLReplication\BinLog\Exception\BinLogException;
45
use MySQLReplication\Config\Config;
56
use MySQLReplication\Repository\MySQLRepository;
67
use MySQLReplication\Definitions\ConstCapabilityFlags;
78
use MySQLReplication\Definitions\ConstCommand;
8-
use MySQLReplication\Exception\BinLogException;
99
use MySQLReplication\Gtid\GtidCollection;
1010

1111
/**
@@ -24,7 +24,7 @@ class BinLogConnect
2424
/**
2525
* @var MySQLRepository
2626
*/
27-
private $DBHelper;
27+
private $mySQLRepository;
2828
/**
2929
* @var Config
3030
*/
@@ -37,25 +37,33 @@ class BinLogConnect
3737
* @var GtidCollection
3838
*/
3939
private $gtidCollection;
40+
/**
41+
* http://dev.mysql.com/doc/internals/en/auth-phase-fast-path.html 00 FE
42+
* @var array
43+
*/
44+
private $packageOkHeader = [0, 254];
4045

4146
/**
4247
* @param Config $config
43-
* @param MySQLRepository $DBHelper
48+
* @param MySQLRepository $mySQLRepository
4449
* @param BinLogAuth $packAuth
4550
* @param GtidCollection $gtidCollection
4651
*/
4752
public function __construct(
4853
Config $config,
49-
MySQLRepository $DBHelper,
54+
MySQLRepository $mySQLRepository,
5055
BinLogAuth $packAuth,
5156
GtidCollection $gtidCollection
5257
) {
53-
$this->DBHelper = $DBHelper;
58+
$this->mySQLRepository = $mySQLRepository;
5459
$this->config = $config;
5560
$this->packAuth = $packAuth;
5661
$this->gtidCollection = $gtidCollection;
5762
}
5863

64+
/**
65+
*
66+
*/
5967
public function __destruct()
6068
{
6169
if (true === $this->isConnected())
@@ -81,20 +89,25 @@ public function getCheckSum()
8189
return $this->checkSum;
8290
}
8391

92+
8493
/**
8594
* @throws BinLogException
86-
* @return self
8795
*/
8896
public function connectToStream()
8997
{
98+
if (false === filter_var($this->config->getIp(), FILTER_VALIDATE_IP))
99+
{
100+
throw new BinLogException('Given parameter "' . $this->config->getIp() . '" is not a valid IP');
101+
}
102+
90103
if (false === ($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)))
91104
{
92105
throw new BinLogException('Unable to create a socket:' . socket_strerror(socket_last_error()), socket_last_error());
93106
}
94107
socket_set_block($this->socket);
95108
socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
96109

97-
if (false === socket_connect($this->socket, $this->config->getHost(), $this->config->getPort()))
110+
if (false === socket_connect($this->socket, $this->config->getIp(), $this->config->getPort()))
98111
{
99112
throw new BinLogException(socket_strerror(socket_last_error()), socket_last_error());
100113
}
@@ -104,6 +117,9 @@ public function connectToStream()
104117
$this->getBinlogStream();
105118
}
106119

120+
/**
121+
*
122+
*/
107123
private function serverInfo()
108124
{
109125
BinLogServerInfo::parsePackage($this->getPacket(false));
@@ -126,11 +142,37 @@ public function getPacket($checkForOkByte = true)
126142
$result = $this->readFromSocket($dataLength);
127143
if (true === $checkForOkByte)
128144
{
129-
$this->packAuth->isWriteSuccessful($result);
145+
$this->isWriteSuccessful($result);
130146
}
131147
return $result;
132148
}
133149

150+
151+
/**
152+
* @param string $packet
153+
* @return array
154+
* @throws BinLogException
155+
*/
156+
public function isWriteSuccessful($packet)
157+
{
158+
$head = ord($packet[0]);
159+
if (in_array($head, $this->packageOkHeader))
160+
{
161+
return ['status' => true, 'code' => 0, 'msg' => ''];
162+
}
163+
else
164+
{
165+
$error_code = unpack('v', $packet[1] . $packet[2])[1];
166+
$error_msg = '';
167+
for ($i = 9; $i < strlen($packet); $i++)
168+
{
169+
$error_msg .= $packet[$i];
170+
}
171+
172+
throw new BinLogException($error_msg, $error_code);
173+
}
174+
}
175+
134176
/**
135177
* @param $length
136178
* @return string
@@ -143,43 +185,25 @@ private function readFromSocket($length)
143185
throw new BinLogException('read 5 bytes from mysql server has gone away');
144186
}
145187

146-
try
147-
{
148-
$bytes_read = 0;
149-
$body = '';
150-
while ($bytes_read < $length)
151-
{
152-
$resp = socket_read($this->socket, $length - $bytes_read);
153-
if ($resp === false)
154-
{
155-
throw new BinLogException(socket_strerror(socket_last_error()), socket_last_error());
156-
}
157-
158-
// server kill connection or server gone away
159-
if (strlen($resp) === 0)
160-
{
161-
throw new BinLogException('read less ' . ($length - strlen($body)));
162-
}
163-
$body .= $resp;
164-
$bytes_read += strlen($resp);
165-
}
166-
if (strlen($body) < $length)
167-
{
168-
throw new BinLogException('read less ' . ($length - strlen($body)));
169-
}
170-
return $body;
171-
} catch (\Exception $e)
188+
if ($length === socket_recv($this->socket, $buf, $length, MSG_WAITALL))
172189
{
173-
throw new BinLogException(var_export($e, true));
190+
return $buf;
174191
}
192+
193+
throw new BinLogException(socket_strerror(socket_last_error()), socket_last_error());
175194
}
176195

177196
/**
178197
* @throws BinLogException
179198
*/
180199
private function auth()
181200
{
182-
$data = $this->packAuth->createAuthenticationPacket(ConstCapabilityFlags::getCapabilities(), $this->config->getUser(), $this->config->getPassword(), BinLogServerInfo::getSalt());
201+
$data = $this->packAuth->createAuthenticationBinary(
202+
ConstCapabilityFlags::getCapabilities(),
203+
$this->config->getUser(),
204+
$this->config->getPassword(),
205+
BinLogServerInfo::getSalt()
206+
);
183207

184208
$this->writeToSocket($data);
185209
$this->getPacket();
@@ -202,7 +226,7 @@ private function writeToSocket($data)
202226
*/
203227
private function getBinlogStream()
204228
{
205-
$this->checkSum = $this->DBHelper->isCheckSum();
229+
$this->checkSum = $this->mySQLRepository->isCheckSum();
206230
if (true === $this->checkSum)
207231
{
208232
$this->execute('SET @master_binlog_checksum=@@global.binlog_checksum');
@@ -215,7 +239,7 @@ private function getBinlogStream()
215239

216240
if ('' === $binFilePos || '' === $binFileName)
217241
{
218-
$master = $this->DBHelper->getMasterStatus();
242+
$master = $this->mySQLRepository->getMasterStatus();
219243
$binFilePos = $master['Position'];
220244
$binFileName = $master['File'];
221245
}
@@ -228,7 +252,7 @@ private function getBinlogStream()
228252
}
229253
else
230254
{
231-
$prelude = pack('l', 26 + $this->gtidCollection->getEncodedPacketLength()) . chr(ConstCommand::COM_BINLOG_DUMP_GTID);
255+
$prelude = pack('l', 26 + $this->gtidCollection->getEncodedLength()) . chr(ConstCommand::COM_BINLOG_DUMP_GTID);
232256
$prelude .= pack('S', 0);
233257
$prelude .= pack('I', $this->config->getSlaveId());
234258
$prelude .= pack('I', 3);
@@ -237,8 +261,8 @@ private function getBinlogStream()
237261
$prelude .= chr(0);
238262
$prelude .= pack('Q', 4);
239263

240-
$prelude .= pack('I', $this->gtidCollection->getEncodedPacketLength());
241-
$prelude .= $this->gtidCollection->getEncodedPacket();
264+
$prelude .= pack('I', $this->gtidCollection->getEncodedLength());
265+
$prelude .= $this->gtidCollection->getEncoded();
242266
}
243267

244268
$this->writeToSocket($prelude);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace MySQLReplication\BinLog\Exception;
4+
5+
use MySQLReplication\Exception\MySQLReplicationException;
6+
7+
class BinLogException extends MySQLReplicationException
8+
{
9+
}

0 commit comments

Comments
 (0)