1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MySQLReplication\Event; |
4
|
|
|
|
5
|
|
|
use MySQLReplication\BinaryDataReader\BinaryDataReaderException; |
6
|
|
|
use MySQLReplication\BinaryDataReader\BinaryDataReaderFactory; |
7
|
|
|
use MySQLReplication\BinLog\BinLogException; |
8
|
|
|
use MySQLReplication\BinLog\BinLogSocketConnect; |
9
|
|
|
use MySQLReplication\Config\Config; |
10
|
|
|
use MySQLReplication\Config\ConfigException; |
11
|
|
|
use MySQLReplication\Definitions\ConstEventsNames; |
12
|
|
|
use MySQLReplication\Definitions\ConstEventType; |
13
|
|
|
use MySQLReplication\Event\DTO\FormatDescriptionEventDTO; |
14
|
|
|
use MySQLReplication\Event\DTO\HeartbeatDTO; |
15
|
|
|
use MySQLReplication\Event\RowEvent\RowEventFactory; |
16
|
|
|
use MySQLReplication\Exception\MySQLReplicationException; |
17
|
|
|
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderException; |
18
|
|
|
use Psr\SimpleCache\CacheInterface; |
19
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Event |
24
|
|
|
* @package MySQLReplication\Event |
25
|
|
|
*/ |
26
|
|
|
class Event |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var BinLogSocketConnect |
30
|
|
|
*/ |
31
|
|
|
private $socketConnect; |
32
|
|
|
/** |
33
|
|
|
* @var BinaryDataReaderFactory |
34
|
|
|
*/ |
35
|
|
|
private $packageService; |
36
|
|
|
/** |
37
|
|
|
* @var RowEventFactory |
38
|
|
|
*/ |
39
|
|
|
private $rowEventService; |
40
|
|
|
/** |
41
|
|
|
* @var EventDispatcher |
42
|
|
|
*/ |
43
|
|
|
private $eventDispatcher; |
44
|
|
|
/** |
45
|
|
|
* @var CacheInterface |
46
|
|
|
*/ |
47
|
|
|
private $cache; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* BinLogPack constructor. |
51
|
|
|
* @param BinLogSocketConnect $socketConnect |
52
|
|
|
* @param BinaryDataReaderFactory $packageService |
53
|
|
|
* @param RowEventFactory $rowEventService |
54
|
|
|
* @param EventDispatcher $eventDispatcher |
55
|
|
|
* @param CacheInterface $cache |
56
|
|
|
*/ |
57
|
54 |
|
public function __construct( |
58
|
|
|
BinLogSocketConnect $socketConnect, |
59
|
|
|
BinaryDataReaderFactory $packageService, |
60
|
|
|
RowEventFactory $rowEventService, |
61
|
|
|
EventDispatcher $eventDispatcher, |
62
|
|
|
CacheInterface $cache |
63
|
|
|
) { |
64
|
54 |
|
$this->socketConnect = $socketConnect; |
65
|
54 |
|
$this->packageService = $packageService; |
66
|
54 |
|
$this->rowEventService = $rowEventService; |
67
|
54 |
|
$this->eventDispatcher = $eventDispatcher; |
68
|
54 |
|
$this->cache = $cache; |
69
|
54 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws BinaryDataReaderException |
73
|
|
|
* @throws BinLogException |
74
|
|
|
* @throws ConfigException |
75
|
|
|
* @throws EventException |
76
|
|
|
* @throws MySQLReplicationException |
77
|
|
|
* @throws JsonBinaryDecoderException |
78
|
|
|
* @throws InvalidArgumentException |
79
|
|
|
* @throws \MySQLReplication\Socket\SocketException |
80
|
|
|
*/ |
81
|
54 |
|
public function consume() |
82
|
|
|
{ |
83
|
54 |
|
$binaryDataReader = $this->packageService->makePackageFromBinaryData($this->socketConnect->getResponse()); |
84
|
|
|
|
85
|
|
|
// "ok" value on first byte continue |
86
|
54 |
|
$binaryDataReader->advance(1); |
87
|
|
|
|
88
|
|
|
// decode all events data |
89
|
54 |
|
$eventInfo = new EventInfo( |
90
|
54 |
|
$binaryDataReader->readInt32(), |
91
|
54 |
|
$binaryDataReader->readUInt8(), |
92
|
54 |
|
$binaryDataReader->readInt32(), |
93
|
54 |
|
$binaryDataReader->readInt32(), |
94
|
54 |
|
$binaryDataReader->readInt32(), |
95
|
54 |
|
$binaryDataReader->readUInt16(), |
96
|
54 |
|
$this->socketConnect->getCheckSum(), |
97
|
54 |
|
$this->socketConnect->getBinLogCurrent() |
98
|
54 |
|
); |
99
|
|
|
|
100
|
54 |
|
if (ConstEventType::TABLE_MAP_EVENT === $eventInfo->getType()) { |
|
|
|
|
101
|
52 |
|
$event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeTableMapDTO(); |
102
|
52 |
|
if (null !== $event && Config::checkEvent($eventInfo->getType())) { |
103
|
50 |
|
$this->eventDispatcher->dispatch(ConstEventsNames::TABLE_MAP, $event); |
104
|
50 |
|
} |
105
|
|
|
|
106
|
52 |
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
54 |
|
if (!Config::checkEvent($eventInfo->getType())) { |
|
|
|
|
110
|
3 |
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
54 |
|
if (in_array($eventInfo->getType(), [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2], true)) { |
114
|
1 |
|
$event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeUpdateRowsDTO(); |
115
|
1 |
|
if ($event !== null) { |
116
|
1 |
|
$this->eventDispatcher->dispatch(ConstEventsNames::UPDATE, $event); |
117
|
1 |
|
} |
118
|
54 |
|
} elseif (in_array($eventInfo->getType(), [ConstEventType::WRITE_ROWS_EVENT_V1, ConstEventType::WRITE_ROWS_EVENT_V2], true)) { |
119
|
52 |
|
$event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeWriteRowsDTO(); |
120
|
52 |
|
if ($event !== null) { |
121
|
52 |
|
$this->eventDispatcher->dispatch(ConstEventsNames::WRITE, $event); |
122
|
52 |
|
} |
123
|
54 |
|
} elseif (in_array($eventInfo->getType(), [ConstEventType::DELETE_ROWS_EVENT_V1, ConstEventType::DELETE_ROWS_EVENT_V2], true)) { |
124
|
1 |
|
$event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeDeleteRowsDTO(); |
125
|
1 |
|
if ($event !== null) { |
126
|
1 |
|
$this->eventDispatcher->dispatch(ConstEventsNames::DELETE, $event); |
127
|
1 |
|
} |
128
|
54 |
|
} elseif (ConstEventType::XID_EVENT === $eventInfo->getType()) { |
|
|
|
|
129
|
3 |
|
$this->eventDispatcher->dispatch(ConstEventsNames::XID, (new XidEvent($eventInfo, $binaryDataReader))->makeXidDTO()); |
130
|
54 |
|
} elseif (ConstEventType::ROTATE_EVENT === $eventInfo->getType()) { |
|
|
|
|
131
|
|
|
$this->cache->clear(); |
132
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::ROTATE, (new RotateEvent($eventInfo, $binaryDataReader))->makeRotateEventDTO()); |
133
|
54 |
|
} elseif (ConstEventType::GTID_LOG_EVENT === $eventInfo->getType()) { |
|
|
|
|
134
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::GTID, (new GtidEvent($eventInfo, $binaryDataReader))->makeGTIDLogDTO()); |
135
|
54 |
|
} elseif (ConstEventType::QUERY_EVENT === $eventInfo->getType()) { |
|
|
|
|
136
|
54 |
|
$this->eventDispatcher->dispatch(ConstEventsNames::QUERY, (new QueryEvent($eventInfo, $binaryDataReader))->makeQueryDTO() |
137
|
54 |
|
); |
138
|
54 |
|
} elseif (ConstEventType::MARIA_GTID_EVENT === $eventInfo->getType()) { |
|
|
|
|
139
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::MARIADB_GTID, (new MariaDbGtidEvent($eventInfo, $binaryDataReader))->makeMariaDbGTIDLogDTO()); |
140
|
54 |
|
} elseif (ConstEventType::FORMAT_DESCRIPTION_EVENT === $eventInfo->getType()) { |
|
|
|
|
141
|
54 |
|
$this->eventDispatcher->dispatch(ConstEventsNames::FORMAT_DESCRIPTION, new FormatDescriptionEventDTO($eventInfo)); |
142
|
54 |
|
} elseif (ConstEventType::HEARTBEAT_LOG_EVENT === $eventInfo->getType()) { |
|
|
|
|
143
|
|
|
$this->eventDispatcher->dispatch(ConstEventsNames::HEARTBEAT, new HeartbeatDTO($eventInfo)); |
144
|
|
|
} |
145
|
54 |
|
} |
146
|
|
|
} |
147
|
|
|
|