Skip to content

Commit b84cc2a

Browse files
committed
Merge pull request #1 from krowinski/develop
new code
2 parents 6cb2ae6 + b772d48 commit b84cc2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2993
-2202
lines changed

README.md

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,8 @@ Remember to change config for your user, host and password.
3535

3636
User should have replication privileges [ REPLICATION CLIENT, SELECT]
3737

38-
```php
39-
<?php
40-
error_reporting(E_ALL);
41-
date_default_timezone_set('UTC');
42-
ini_set('memory_limit', '8M');
43-
44-
include __DIR__ . '/../vendor/autoload.php';
45-
46-
use MySQLReplication\Service\BinLogStream;
47-
use MySQLReplication\Config\Config;
48-
49-
$binLogStream = new BinLogStream(
50-
new Config('root', '192.168.1.100', 3306, 'root')
51-
);
52-
while (1)
53-
{
54-
$result = $binLogStream->analysisBinLog();
55-
if (!is_null($result))
56-
{
57-
// all events got __toString() implementation
58-
echo $result;
59-
60-
// all events got JsonSerializable implementation
61-
//echo json_encode($result, JSON_PRETTY_PRINT);
62-
63-
//echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
64-
}
65-
}
38+
```sh
39+
php example/dump_events.php
6640
```
6741

6842
For this SQL sessions:

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"type": "library",
1111
"require": {
1212
"php": ">=5.4",
13-
"doctrine/dbal": "^2.5"
13+
"doctrine/dbal": "^2.5",
14+
"doctrine/collections": "^1.3"
1415
},
1516
"require-dev": {
1617
"phpunit/phpunit": "*"

example/benchmark.php

Lines changed: 75 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,96 @@
44
error_reporting(E_ALL);
55
ini_set('display_errors', 1);
66

7-
use MySQLReplication\Config\Config;
8-
use MySQLReplication\DataBase\DBHelper;
7+
use Doctrine\DBAL\DriverManager;
8+
use MySQLReplication\BinLogStream;
9+
use MySQLReplication\Config\ConfigService;
910
use MySQLReplication\Definitions\ConstEventType;
10-
use MySQLReplication\DTO\UpdateRowsDTO;
11-
use MySQLReplication\DTO\WriteRowsDTO;
12-
use MySQLReplication\Service\BinLogStream;
13-
14-
11+
use MySQLReplication\Event\DTO\UpdateRowsDTO;
1512

1613
/**
17-
* Class Base
14+
* Class Benchmark
1815
*/
1916
class Benchmark
2017
{
2118
/**
2219
* @var string
2320
*/
2421
private $database = 'mysqlreplication_test';
22+
2523
/**
26-
* @var \Doctrine\DBAL\Connection
27-
*/
28-
private $conn;
29-
/**
30-
* @var Config
24+
* Benchmark constructor.
3125
*/
32-
private $config;
33-
3426
public function __construct()
3527
{
36-
$this->config = new Config('root', '192.168.1.100', 3306, 'root');
37-
$this->conn = (new DBHelper($this->config))->getConnection();
28+
$conn = $this->getConnection();
29+
$conn->exec("DROP DATABASE IF EXISTS " . $this->database);
30+
$conn->exec("CREATE DATABASE " . $this->database);
31+
$conn->exec("USE " . $this->database);
32+
$conn->exec("CREATE TABLE test (i INT) ENGINE = MEMORY");
33+
$conn->exec("INSERT INTO test VALUES(1)");
34+
$conn->exec("CREATE TABLE test2 (i INT) ENGINE = MEMORY");
35+
$conn->exec("INSERT INTO test2 VALUES(1)");
36+
$conn->exec("RESET MASTER");
37+
38+
$this->binLogStream = new BinLogStream(
39+
(new ConfigService())->makeConfigFromArray([
40+
'user' => 'root',
41+
'host' => '192.168.1.100',
42+
'password' => 'root',
43+
'eventsOnly' => [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2],
44+
'slaveId' => 9999
45+
])
46+
);
47+
}
3848

39-
$this->conn->exec("DROP DATABASE IF EXISTS " . $this->database);
40-
$this->conn->exec("CREATE DATABASE " . $this->database);
41-
$this->conn->exec("USE " . $this->database);
42-
$this->conn->exec("CREATE TABLE test (i INT) ENGINE = MEMORY");
43-
$this->conn->exec("INSERT INTO test VALUES(1)");
44-
$this->conn->exec("CREATE TABLE test2 (i INT) ENGINE = MEMORY");
45-
$this->conn->exec("INSERT INTO test2 VALUES(1)");
46-
$this->conn->exec("RESET MASTER");
49+
/**
50+
* @return \Doctrine\DBAL\Connection
51+
* @throws \Doctrine\DBAL\DBALException
52+
*/
53+
private function getConnection()
54+
{
55+
return DriverManager::getConnection([
56+
'user' => 'root',
57+
'password' => 'root',
58+
'host' => '192.168.1.100',
59+
'port' => 3306,
60+
'driver' => 'pdo_mysql',
61+
'dbname' => $this->database
62+
]);
63+
}
4764

48-
$this->binLogStream = new BinLogStream(
49-
$this->config,
50-
'',
51-
'',
52-
'',
53-
'',
54-
[ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2]
55-
);
65+
/**
66+
*
67+
*/
68+
public function run()
69+
{
70+
$pid = pcntl_fork();
71+
if ($pid == -1)
72+
{
73+
die('could not fork');
74+
}
75+
else if ($pid)
76+
{
77+
$this->consume();
78+
pcntl_wait($status);
79+
}
80+
else
81+
{
82+
$this->produce();
83+
}
5684
}
5785

58-
public function consume()
86+
/**
87+
*
88+
*/
89+
private function consume()
5990
{
6091
$start = microtime(true);
6192
$i = 0;
6293

6394
while (1)
6495
{
65-
$result = $this->binLogStream->analysisBinLog();
96+
$result = $this->binLogStream->getBinLogEvent();
6697
if ($result instanceof UpdateRowsDTO)
6798
{
6899
$i += 1;
@@ -74,34 +105,22 @@ public function consume()
74105
}
75106
}
76107

77-
public function run()
78-
{
79-
$pid = pcntl_fork();
80-
if ($pid == -1) {
81-
die('could not fork');
82-
} else if ($pid) {
83-
$this->consume();
84-
pcntl_wait($status);
85-
} else {
86-
$this->produce();
87-
}
88-
89-
}
90-
91-
public function produce()
108+
/**
109+
* @throws \Doctrine\DBAL\DBALException
110+
*/
111+
private function produce()
92112
{
93-
$this->conn = (new DBHelper($this->config))->getConnection();
94-
$this->conn->exec("USE " . $this->database);
113+
$conn = $this->getConnection();
95114

96115
echo 'Start insert data' . PHP_EOL;
97116
while (1)
98117
{
99-
100-
$this->conn->exec("UPDATE test SET i = i + 1;");
101-
$this->conn->exec("UPDATE test2 SET i = i + 1;");
118+
$conn->exec("UPDATE test SET i = i + 1;");
119+
$conn->exec("UPDATE test2 SET i = i + 1;");
102120
}
103-
}
104121

122+
$conn->close();
123+
}
105124
}
106125

107126
(new Benchmark())->run();

example/dump_events.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55

66
include __DIR__ . '/../vendor/autoload.php';
77

8-
use MySQLReplication\Service\BinLogStream;
9-
use MySQLReplication\Config\Config;
8+
use MySQLReplication\BinLogStream;
9+
use MySQLReplication\Config\ConfigService;
1010

1111
$binLogStream = new BinLogStream(
12-
new Config('root', '192.168.1.100', 3306, 'root')
12+
(new ConfigService())->makeConfigFromArray([
13+
'user' => 'root',
14+
'host' => '192.168.1.100',
15+
'password' => 'root',
16+
//'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592',
17+
])
1318
);
1419
while (1)
1520
{
16-
$result = $binLogStream->analysisBinLog();
21+
$result = $binLogStream->getBinLogEvent();
1722
if (!is_null($result))
1823
{
1924
// all events got __toString() implementation

src/MySQLReplication/Pack/PackAuth.php renamed to src/MySQLReplication/BinLog/BinLogAuth.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
<?php
22

3-
namespace MySQLReplication\Pack;
3+
namespace MySQLReplication\BinLog;
44

5-
use MySQLReplication\Definitions\ConstAuth;
65
use MySQLReplication\Exception\BinLogException;
76

87
/**
98
* Class PackAuth
109
*/
11-
class PackAuth
10+
class BinLogAuth
1211
{
12+
/**
13+
* 2^24 - 1 16m
14+
* @var int
15+
*/
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];
29+
1330
/**
1431
* @param $flag
1532
* @param $user
1633
* @param $pass
1734
* @param $salt
18-
* @param string $db
1935
* @return string
2036
* @link http://dev.mysql.com/doc/internals/en/secure-password-authentication.html#packet-Authentication::Native41
2137
*/
22-
public static function initPack($flag, $user, $pass, $salt, $db = '')
38+
public function createAuthenticationPacket($flag, $user, $pass, $salt)
2339
{
2440
$data = pack('L', $flag);
25-
$data .= pack('L', ConstAuth::$PACK_MAX_LENGTH);
41+
$data .= pack('L', $this->packageMaxLength);
2642
$data .= chr(33);
2743
for ($i = 0; $i < 23; $i++)
2844
{
@@ -31,10 +47,6 @@ public static function initPack($flag, $user, $pass, $salt, $db = '')
3147
$result = sha1($pass, true) ^ sha1($salt . sha1(sha1($pass, true), true), true);
3248

3349
$data = $data . $user . chr(0) . chr(strlen($result)) . $result;
34-
if ($db)
35-
{
36-
$data .= $db . chr(0);
37-
}
3850
$str = pack('L', strlen($data));
3951
$s = $str[0] . $str[1] . $str[2];
4052
$data = $s . chr(1) . $data;
@@ -43,25 +55,26 @@ public static function initPack($flag, $user, $pass, $salt, $db = '')
4355
}
4456

4557
/**
46-
* @param $pack
58+
* @param string $packet
4759
* @return array
4860
* @throws BinLogException
4961
*/
50-
public static function success($pack)
62+
public function isWriteSuccessful($packet)
5163
{
52-
$head = ord($pack[0]);
53-
if (in_array($head, ConstAuth::$OK_PACK_HEAD))
64+
$head = ord($packet[0]);
65+
if (in_array($head, $this->packageOkHeader))
5466
{
5567
return ['status' => true, 'code' => 0, 'msg' => ''];
5668
}
5769
else
5870
{
59-
$error_code = unpack('v', $pack[1] . $pack[2])[1];
71+
$error_code = unpack('v', $packet[1] . $packet[2])[1];
6072
$error_msg = '';
61-
for ($i = 9; $i < strlen($pack); $i++)
73+
for ($i = 9; $i < strlen($packet); $i++)
6274
{
63-
$error_msg .= $pack[$i];
75+
$error_msg .= $packet[$i];
6476
}
77+
6578
throw new BinLogException($error_msg, $error_code);
6679
}
6780
}

0 commit comments

Comments
 (0)