This repository was archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathSyncProcess.php
More file actions
205 lines (163 loc) · 6.09 KB
/
Copy pathSyncProcess.php
File metadata and controls
205 lines (163 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
declare(strict_types=1);
namespace Kafka\Producer;
use Kafka\Broker;
use Kafka\Exception;
use Kafka\LoggerTrait;
use Kafka\ProducerConfig;
use Kafka\Protocol\Protocol;
use Psr\Log\LoggerAwareTrait;
use function array_keys;
use function count;
use function explode;
use function json_encode;
use function shuffle;
use function substr;
use function trim;
class SyncProcess
{
use LoggerAwareTrait;
use LoggerTrait;
/** @var RecordValidator */
private $recordValidator;
public function __construct(?RecordValidator $recordValidator = null)
{
$this->recordValidator = $recordValidator ?? new RecordValidator();
$config = $this->getConfig();
\Kafka\Protocol::init($config->getBrokerVersion(), $this->logger);
$broker = $this->getBroker();
$broker->setConfig($config);
$this->syncMeta();
}
/**
* @param mixed[][] $recordSet
*
* @return mixed[]
*
* @throws \Kafka\Exception
*/
public function send(array $recordSet): array
{
$broker = $this->getBroker();
$config = $this->getConfig();
$requiredAck = $config->getRequiredAck();
$timeout = $config->getTimeout();
$compression = $config->getCompression();
// get send message
// data struct
// topic:
// partId:
// key:
// value:
if (empty($recordSet)) {
return [];
}
$sendData = $this->convertRecordSet($recordSet);
$result = [];
foreach ($sendData as $brokerId => $topicList) {
$connect = $broker->getDataConnect((string) $brokerId, Broker::SOCKET_MODE_SYNC);
if ($connect === null) {
return [];
}
$params = [
'required_ack' => $requiredAck,
'timeout' => $timeout,
'data' => $topicList,
'compression' => $compression,
];
$this->debug('Send message start, params:' . json_encode($params));
$requestData = \Kafka\Protocol::encode(\Kafka\Protocol::PRODUCE_REQUEST, $params);
$connect->write($requestData);
if ($requiredAck !== 0) { // If it is 0 the server will not send any response
$dataLen = Protocol::unpack(Protocol::BIT_B32, $connect->read(4));
$recordSet = $connect->read($dataLen);
$correlationId = Protocol::unpack(Protocol::BIT_B32, substr($recordSet, 0, 4));
$ret = \Kafka\Protocol::decode(\Kafka\Protocol::PRODUCE_REQUEST, substr($recordSet, 4));
$result[] = $ret;
}
}
return $result;
}
public function syncMeta(): void
{
$this->debug('Start sync metadata request');
$brokerList = ProducerConfig::getInstance()->getMetadataBrokerList();
$brokerHost = [];
foreach (explode(',', $brokerList) as $key => $val) {
if (trim($val)) {
$brokerHost[] = $val;
}
}
if (count($brokerHost) === 0) {
throw new Exception('No valid broker configured');
}
shuffle($brokerHost);
$broker = $this->getBroker();
foreach ($brokerHost as $host) {
$socket = $broker->getMetaConnect($host, Broker::SOCKET_MODE_SYNC);
if ($socket === null) {
continue;
}
$params = [];
$this->debug('Start sync metadata request params:' . json_encode($params));
$requestData = \Kafka\Protocol::encode(\Kafka\Protocol::METADATA_REQUEST, $params);
$socket->write($requestData);
$dataLen = Protocol::unpack(Protocol::BIT_B32, $socket->read(4));
$data = $socket->read($dataLen);
$correlationId = Protocol::unpack(Protocol::BIT_B32, substr($data, 0, 4));
$result = \Kafka\Protocol::decode(\Kafka\Protocol::METADATA_REQUEST, substr($data, 4));
if (! isset($result['brokers'], $result['topics'])) {
throw new Exception('Get metadata is fail, brokers or topics is null.');
}
$broker = $this->getBroker();
$broker->setData($result['topics'], $result['brokers']);
return;
}
throw Exception\ConnectionException::fromBrokerList($brokerList);
}
/**
* @param string[][] $recordSet
*
* @return mixed[]
*/
protected function convertRecordSet(array $recordSet): array
{
$sendData = [];
$broker = $this->getBroker();
$topics = $broker->getTopics();
foreach ($recordSet as $record) {
$this->recordValidator->validate($record, $topics);
$topicMeta = $topics[$record['topic']];
$partNums = array_keys($topicMeta);
shuffle($partNums);
$partId = isset($record['partId'], $topicMeta[$record['partId']]) ? $record['partId'] : $partNums[0];
$brokerId = $topicMeta[$partId];
$topicData = [];
if (isset($sendData[$brokerId][$record['topic']])) {
$topicData = $sendData[$brokerId][$record['topic']];
}
$partition = [];
if (isset($topicData['partitions'][$partId])) {
$partition = $topicData['partitions'][$partId];
}
$partition['partition_id'] = $partId;
if (trim($record['key'] ?? '') !== '') {
$partition['messages'][] = ['value' => $record['value'], 'key' => $record['key']];
} else {
$partition['messages'][] = $record['value'];
}
$topicData['partitions'][$partId] = $partition;
$topicData['topic_name'] = $record['topic'];
$sendData[$brokerId][$record['topic']] = $topicData;
}
return $sendData;
}
private function getBroker(): Broker
{
return Broker::getInstance();
}
private function getConfig(): ProducerConfig
{
return ProducerConfig::getInstance();
}
}