-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessageView.php
More file actions
435 lines (398 loc) · 12.4 KB
/
Copy pathMessageView.php
File metadata and controls
435 lines (398 loc) · 12.4 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Apache\Rocketmq;
use Apache\Rocketmq\V2\Message;
use Apache\Rocketmq\V2\Endpoints;
use Apache\Rocketmq\V2\Encoding;
/**
* MessageView - Rich wrapper for consumed messages.
*
* Wraps the raw protobuf Message received from the broker with additional
* delivery metadata (receipt handle, endpoints, delivery attempt count,
* born timestamp, born host) that is needed for ack/nack/retry operations.
*
* On construction, verifies body integrity (CRC32) and decompresses GZIP if needed.
* Marks the message as corrupted if verification or decompression fails.
*/
class MessageView implements MessageViewInterface
{
private Message $message;
private ?string $receiptHandle;
private ?Endpoints $endpoints;
private int $deliveryAttempt;
private string $bodyStr = '';
private bool $corrupted = false;
private int $bornTimestamp = 0;
private string $bornHost = '';
private int $decodeTimestamp = 0;
/**
* Construct a MessageView, extracting metadata and processing body.
*
* @param Message $message The protobuf message from broker
* @param string|null $receiptHandle Receipt handle for ack/nack
* @param Endpoints|null $endpoints Broker endpoints
* @param int $deliveryAttempt Number of delivery attempts (starts at 1)
*/
public function __construct(Message $message, ?string $receiptHandle = null, ?Endpoints $endpoints = null, int $deliveryAttempt = 1)
{
$this->message = $message;
if ($receiptHandle === null) {
$sysProps = $message->getSystemProperties();
$receiptHandle = $sysProps?->getReceiptHandle() ?: null;
}
$this->receiptHandle = $receiptHandle;
$this->endpoints = $endpoints;
$this->deliveryAttempt = max(1, $deliveryAttempt);
$this->decodeTimestamp = time();
// Extract born timestamp and host from system properties
$sysProps = $message->getSystemProperties();
if ($sysProps) {
$bornTs = $sysProps->getBornTimestamp();
if ($bornTs) {
$this->bornTimestamp = $bornTs->getSeconds() ?? 0;
}
$this->bornHost = $sysProps->getBornHost() ?? '';
// Verify body integrity and decompress
$this->bodyStr = $this->processBody($message, $sysProps);
}
if ($this->bodyStr === null) {
$body = $message->getBody();
$this->bodyStr = is_string($body) ? $body : (string)$body;
}
}
/**
* Get the topic resource.
* @return object The topic resource
*/
public function getTopicResource(): object
{
return $this->message->getTopic();
}
/**
* Process body: verify integrity and decompress if needed.
*
* @param Message $message The protobuf message
* @param object|null $sysProps System properties from the message
* @return string|null The processed body string, or null if processing was skipped
*/
private function processBody(Message $message, $sysProps): ?string
{
$rawBody = $message->getBody();
$body = is_string($rawBody) ? $rawBody : (string)$rawBody;
if ($body === '') {
return '';
}
// Step 1: Verify body integrity via digest.
// The digest covers the encoded (raw) body bytes, so it must be checked
// before any decompression.
if ($sysProps !== null) {
$bodyDigest = $sysProps->getBodyDigest();
if ($bodyDigest !== null && $bodyDigest !== '') {
// getBodyDigest returns a Digest object, extract type and checksum
$digestType = is_object($bodyDigest) ? $bodyDigest->getType() : null;
$digestChecksum = is_object($bodyDigest) ? $bodyDigest->getChecksum() : (string)$bodyDigest;
if ($digestChecksum !== '' && $digestChecksum !== null) {
$this->verifyBodyDigest($body, $digestChecksum, $digestType);
}
}
}
// Step 2: Decompress if encoding is GZIP
$encoding = Encoding::IDENTITY;
if ($sysProps !== null) {
$encoding = $sysProps->getBodyEncoding();
}
if ($encoding === Encoding::GZIP) {
$decompressed = @gzdecode($body);
if ($decompressed === false) {
$this->corrupted = true;
Logger::getInstance('MessageView')->warning("Failed to decompress GZIP body for messageId=" . $this->getMessageId());
return null;
}
$body = $decompressed;
}
return $body;
}
/**
* Set the receipt handle for ack/nack operations.
*
* @param string|null $receiptHandle The receipt handle
* @return void
*/
public function setReceiptHandle(?string $receiptHandle): void
{
$this->receiptHandle = $receiptHandle;
}
/**
* Verify body integrity using the given digest type and checksum.
*
* @param string $body The raw (encoded) message body bytes to verify
* @param string $checksum The expected checksum
* @param int|null $digestType The digest type (CRC32, MD5, SHA1)
* @return void
*/
private function verifyBodyDigest(string $body, string $checksum, $digestType): void
{
$computed = '';
if ($digestType === \Apache\Rocketmq\V2\DigestType::CRC32) {
$computed = Utilities::crc32CheckSum($body);
} elseif ($digestType === \Apache\Rocketmq\V2\DigestType::MD5) {
$computed = strtoupper(md5($body));
} elseif ($digestType === \Apache\Rocketmq\V2\DigestType::SHA1) {
$computed = strtoupper(sha1($body));
} else {
// Unknown digest type, skip verification
return;
}
if ($computed !== $checksum) {
$this->corrupted = true;
Logger::getInstance('MessageView')->warning(
"Body digest mismatch for messageId=" . $this->getMessageId() .
", expected=" . $checksum . ", actual=" . $computed
);
}
}
/**
* Get the underlying protobuf Message.
*
* @return Message
*/
public function getMessage(): Message
{
return $this->message;
}
/**
* Get the topic name.
*
* @return string
*/
public function getTopic(): string
{
if ($this->message->hasTopic()) {
return $this->message->getTopic()->getName();
}
return '';
}
/**
* Get the message body as string.
*
* @return string
*/
public function getBody(): string
{
return $this->bodyStr ?? '';
}
/**
* Get the message ID from system properties.
*
* @return string
*/
public function getMessageId(): string
{
$sysProps = $this->message->getSystemProperties();
return $sysProps?->getMessageId() ?? '';
}
/**
* Get the tag from system properties.
*
* @return string|null
*/
public function getTag(): ?string
{
$sysProps = $this->message->getSystemProperties();
if ($sysProps !== null && $sysProps->hasTag()) {
return $sysProps->getTag();
}
return null;
}
/**
* Get the message keys from system properties.
*
* @return array
*/
public function getKeys(): array
{
$sysProps = $this->message->getSystemProperties();
if ($sysProps !== null) {
$keys = $sysProps->getKeys();
if ($keys === null) {
return [];
}
if ($keys instanceof \Traversable) {
return iterator_to_array($keys);
}
return (array)$keys;
}
return [];
}
/**
* Get the message group from system properties.
*
* @return string|null
*/
public function getMessageGroup(): ?string
{
$sysProps = $this->message->getSystemProperties();
if ($sysProps !== null && $sysProps->hasMessageGroup()) {
return $sysProps->getMessageGroup();
}
return null;
}
/**
* Get the receipt handle for ack/nack operations.
*
* @return string|null
*/
public function getReceiptHandle(): ?string
{
return $this->receiptHandle;
}
/**
* Get the broker endpoints that delivered this message.
*
* @return Endpoints|null
*/
public function getEndpoints(): ?Endpoints
{
return $this->endpoints;
}
/**
* Get the number of delivery attempts for this message.
* Starts at 1 for the first delivery.
*
* @return int
*/
public function getDeliveryAttempt(): int
{
return $this->deliveryAttempt;
}
/**
* Get user properties.
*
* @return array
*/
public function getProperties(): array
{
$props = $this->message->getUserProperties();
if ($props === null) {
return [];
}
if ($props instanceof \Traversable) {
return iterator_to_array($props);
}
return (array)$props;
}
/**
* Get a specific user property by key.
*
* @param string $key The property key
* @return string|null The property value, or null if not found
*/
public function getProperty(string $key): ?string
{
$props = $this->getProperties();
return isset($props[$key]) ? $props[$key] : null;
}
/**
* Get the system properties from the underlying protobuf message.
*
* @return object|null The system properties object, or null if not set
*/
public function getSystemProperties(): ?object
{
return $this->message->getSystemProperties();
}
/**
* Check if this is a FIFO message.
*
* @return bool
*/
public function isFifo(): bool
{
return $this->getMessageGroup() !== null;
}
/**
* Whether the message body integrity check or decompression failed.
* Corrupted messages should not be processed.
*
* @return bool
*/
public function isCorrupted(): bool
{
return $this->corrupted;
}
/**
* Get the timestamp when the producer created this message.
*
* @return int Unix timestamp (seconds)
*/
public function getBornTimestamp(): int
{
return $this->bornTimestamp;
}
/**
* Get the host that produced this message.
*
* @return string
*/
public function getBornHost(): string
{
return $this->bornHost;
}
/**
* Get the timestamp when this message was decoded locally.
*
* @return int Unix timestamp (seconds)
*/
public function getDecodeTimestamp(): int
{
return $this->decodeTimestamp;
}
/**
* Increment delivery attempt (used internally for retry tracking).
*
* @return void
*/
public function incrementDeliveryAttempt(): void
{
$this->deliveryAttempt++;
}
/**
* Convert to string representation for logging.
*
* @return string
*/
public function __toString(): string
{
$parts = [
'topic=' . $this->getTopic(),
'messageId=' . $this->getMessageId(),
];
$tag = $this->getTag();
if ($tag !== null) {
$parts[] = 'tag=' . $tag;
}
$group = $this->getMessageGroup();
if ($group !== null) {
$parts[] = 'group=' . $group;
}
if ($this->corrupted) {
$parts[] = 'CORRUPTED';
}
return 'MessageView{' . implode(', ', $parts) . '}';
}
}