/
request.go
716 lines (611 loc) · 20 KB
/
request.go
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*
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.
*/
package internal
import (
"fmt"
"strconv"
"time"
)
const (
ReqSendMessage = int16(10)
ReqPullMessage = int16(11)
ReqQueryMessage = int16(12)
ReqQueryConsumerOffset = int16(14)
ReqUpdateConsumerOffset = int16(15)
ReqCreateTopic = int16(17)
ReqGetBrokerConfig = int16(26)
ReqGetBrokerRuntimeInfo = int16(28)
ReqSearchOffsetByTimestamp = int16(29)
ReqGetMaxOffset = int16(30)
ReqGetMinOffset = int16(31)
ReqViewMessageByID = int16(33)
ReqHeartBeat = int16(34)
ReqConsumerSendMsgBack = int16(36)
ReqENDTransaction = int16(37)
ReqGetConsumerListByGroup = int16(38)
ReqLockBatchMQ = int16(41)
ReqUnlockBatchMQ = int16(42)
ReqGetRouteInfoByTopic = int16(105)
ReqGetBrokerClusterInfo = int16(106)
ReqSendBatchMessage = int16(320)
ReqCheckTransactionState = int16(39)
ReqNotifyConsumerIdsChanged = int16(40)
ReqUpdateCreateSubscriptionGroup = int16(200)
ReqGetAllSubscriptionGroupConfig = int16(201)
ReqGetTopicStats = int16(202)
ReqGetConsumerConnectionList = int16(203)
ReqGetAllTopicListFromNameServer = int16(206)
ReqDeleteGroupInBroker = int16(207)
ReqGetConsumerStatsFromServer = int16(208)
ReqDeleteTopicInBroker = int16(215)
ReqDeleteTopicInNameSrv = int16(216)
ReqResetConsumerOffset = int16(220)
ReqGetConsumerStatsFromClient = int16(221)
ReqInvokeBrokerToResetOffset = int16(222)
QueryTopicConsumeByWho = int16(300)
ReqGetConsumerRunningInfo = int16(307)
ReqConsumeMessageDirectly = int16(309)
ReqSendReplyMessage = int16(324)
ReqSendReplyMessageV2 = int16(325)
ReqPushReplyMessageToClient = int16(326)
)
type SendMessageRequestHeader struct {
ProducerGroup string
Topic string
QueueId int
SysFlag int
BornTimestamp int64
Flag int32
Properties string
ReconsumeTimes int
UnitMode bool
MaxReconsumeTimes int
Batch bool
DefaultTopic string
DefaultTopicQueueNums int
}
func (request *SendMessageRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["producerGroup"] = request.ProducerGroup
maps["topic"] = request.Topic
maps["queueId"] = strconv.Itoa(request.QueueId)
maps["sysFlag"] = fmt.Sprintf("%d", request.SysFlag)
maps["bornTimestamp"] = strconv.FormatInt(request.BornTimestamp, 10)
maps["flag"] = fmt.Sprintf("%d", request.Flag)
maps["reconsumeTimes"] = strconv.Itoa(request.ReconsumeTimes)
maps["unitMode"] = strconv.FormatBool(request.UnitMode)
maps["maxReconsumeTimes"] = strconv.Itoa(request.MaxReconsumeTimes)
maps["defaultTopic"] = "TBW102"
maps["defaultTopicQueueNums"] = "4"
maps["batch"] = strconv.FormatBool(request.Batch)
maps["properties"] = request.Properties
return maps
}
type EndTransactionRequestHeader struct {
ProducerGroup string
TranStateTableOffset int64
CommitLogOffset int64
CommitOrRollback int
FromTransactionCheck bool
MsgID string
TransactionId string
}
type SendMessageRequestV2Header struct {
*SendMessageRequestHeader
}
func (request *SendMessageRequestV2Header) Encode() map[string]string {
maps := make(map[string]string)
maps["a"] = request.ProducerGroup
maps["b"] = request.Topic
maps["c"] = request.DefaultTopic
maps["d"] = strconv.Itoa(request.DefaultTopicQueueNums)
maps["e"] = strconv.Itoa(request.QueueId)
maps["f"] = fmt.Sprintf("%d", request.SysFlag)
maps["g"] = strconv.FormatInt(request.BornTimestamp, 10)
maps["h"] = fmt.Sprintf("%d", request.Flag)
maps["i"] = request.Properties
maps["j"] = strconv.Itoa(request.ReconsumeTimes)
maps["k"] = strconv.FormatBool(request.UnitMode)
maps["l"] = strconv.Itoa(request.MaxReconsumeTimes)
maps["m"] = strconv.FormatBool(request.Batch)
return maps
}
func (request *EndTransactionRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["producerGroup"] = request.ProducerGroup
maps["tranStateTableOffset"] = strconv.FormatInt(request.TranStateTableOffset, 10)
maps["commitLogOffset"] = strconv.Itoa(int(request.CommitLogOffset))
maps["commitOrRollback"] = strconv.Itoa(request.CommitOrRollback)
maps["fromTransactionCheck"] = strconv.FormatBool(request.FromTransactionCheck)
maps["msgId"] = request.MsgID
maps["transactionId"] = request.TransactionId
return maps
}
type CheckTransactionStateRequestHeader struct {
TranStateTableOffset int64
CommitLogOffset int64
MsgId string
TransactionId string
OffsetMsgId string
}
func (request *CheckTransactionStateRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["tranStateTableOffset"] = strconv.FormatInt(request.TranStateTableOffset, 10)
maps["commitLogOffset"] = strconv.FormatInt(request.CommitLogOffset, 10)
maps["msgId"] = request.MsgId
maps["transactionId"] = request.TransactionId
maps["offsetMsgId"] = request.OffsetMsgId
return maps
}
func (request *CheckTransactionStateRequestHeader) Decode(properties map[string]string) {
if len(properties) == 0 {
return
}
if v, existed := properties["tranStateTableOffset"]; existed {
request.TranStateTableOffset, _ = strconv.ParseInt(v, 10, 0)
}
if v, existed := properties["commitLogOffset"]; existed {
request.CommitLogOffset, _ = strconv.ParseInt(v, 10, 0)
}
if v, existed := properties["msgId"]; existed {
request.MsgId = v
}
if v, existed := properties["transactionId"]; existed {
request.MsgId = v
}
if v, existed := properties["offsetMsgId"]; existed {
request.MsgId = v
}
}
type ConsumerSendMsgBackRequestHeader struct {
Group string
Offset int64
DelayLevel int
OriginMsgId string
OriginTopic string
UnitMode bool
MaxReconsumeTimes int32
}
func (request *ConsumerSendMsgBackRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["group"] = request.Group
maps["offset"] = strconv.FormatInt(request.Offset, 10)
maps["delayLevel"] = strconv.Itoa(request.DelayLevel)
maps["originMsgId"] = request.OriginMsgId
maps["originTopic"] = request.OriginTopic
maps["unitMode"] = strconv.FormatBool(request.UnitMode)
maps["maxReconsumeTimes"] = strconv.Itoa(int(request.MaxReconsumeTimes))
return maps
}
type PullMessageRequestHeader struct {
ConsumerGroup string
Topic string
QueueId int32
QueueOffset int64
MaxMsgNums int32
SysFlag int32
CommitOffset int64
SuspendTimeoutMillis time.Duration
SubExpression string
SubVersion int64
ExpressionType string
}
func (request *PullMessageRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["consumerGroup"] = request.ConsumerGroup
maps["topic"] = request.Topic
maps["queueId"] = fmt.Sprintf("%d", request.QueueId)
maps["queueOffset"] = fmt.Sprintf("%d", request.QueueOffset)
maps["maxMsgNums"] = fmt.Sprintf("%d", request.MaxMsgNums)
maps["sysFlag"] = fmt.Sprintf("%d", request.SysFlag)
maps["commitOffset"] = fmt.Sprintf("%d", request.CommitOffset)
maps["suspendTimeoutMillis"] = fmt.Sprintf("%d", request.SuspendTimeoutMillis/time.Millisecond)
maps["subscription"] = request.SubExpression
maps["subVersion"] = fmt.Sprintf("%d", request.SubVersion)
maps["expressionType"] = request.ExpressionType
return maps
}
type GetConsumerListRequestHeader struct {
ConsumerGroup string `json:"consumerGroup"`
}
type GetConsumerConnectionListRequestHeader struct {
ConsumerGroup string `json:"consumerGroup"`
}
func (request *GetConsumerListRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["consumerGroup"] = request.ConsumerGroup
return maps
}
func (request *GetConsumerConnectionListRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["consumerGroup"] = request.ConsumerGroup
return maps
}
type GetMaxOffsetRequestHeader struct {
Topic string
QueueId int
}
func (request *GetMaxOffsetRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
maps["queueId"] = strconv.Itoa(request.QueueId)
return maps
}
type QueryConsumerOffsetRequestHeader struct {
ConsumerGroup string
Topic string
QueueId int
}
func (request *QueryConsumerOffsetRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["consumerGroup"] = request.ConsumerGroup
maps["topic"] = request.Topic
maps["queueId"] = strconv.Itoa(request.QueueId)
return maps
}
type SearchOffsetRequestHeader struct {
Topic string
QueueId int
Timestamp int64
}
func (request *SearchOffsetRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
maps["queueId"] = strconv.Itoa(request.QueueId)
maps["timestamp"] = strconv.FormatInt(request.Timestamp, 10)
return maps
}
type UpdateConsumerOffsetRequestHeader struct {
ConsumerGroup string
Topic string
QueueId int
CommitOffset int64
}
func (request *UpdateConsumerOffsetRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["consumerGroup"] = request.ConsumerGroup
maps["topic"] = request.Topic
maps["queueId"] = strconv.Itoa(request.QueueId)
maps["commitOffset"] = strconv.FormatInt(request.CommitOffset, 10)
return maps
}
type GetRouteInfoRequestHeader struct {
Topic string
}
func (request *GetRouteInfoRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
return maps
}
type GetTopicStatsInfoRequestHeader struct {
Topic string
}
func (request *GetTopicStatsInfoRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
return maps
}
type QueryTopicConsumeByWhoRequestHeader struct {
Topic string
}
func (request *QueryTopicConsumeByWhoRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
return maps
}
type GetConsumerRunningInfoHeader struct {
consumerGroup string
clientID string
jstackEnable bool
}
func (request *GetConsumerRunningInfoHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["consumerGroup"] = request.consumerGroup
maps["clientId"] = request.clientID
maps["jstackEnable"] = strconv.FormatBool(request.jstackEnable)
return maps
}
func (request *GetConsumerRunningInfoHeader) Decode(properties map[string]string) {
if len(properties) == 0 {
return
}
if v, existed := properties["consumerGroup"]; existed {
request.consumerGroup = v
}
if v, existed := properties["clientId"]; existed {
request.clientID = v
}
if v, existed := properties["jstackEnable"]; existed {
parseBool, _ := strconv.ParseBool(v)
request.jstackEnable = parseBool
}
}
type QueryMessageRequestHeader struct {
Topic string
Key string
MaxNum int
BeginTimestamp int64
EndTimestamp int64
}
func (request *QueryMessageRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
maps["key"] = request.Key
maps["maxNum"] = fmt.Sprintf("%d", request.MaxNum)
maps["beginTimestamp"] = strconv.FormatInt(request.BeginTimestamp, 10)
maps["endTimestamp"] = fmt.Sprintf("%d", request.EndTimestamp)
return maps
}
func (request *QueryMessageRequestHeader) Decode(properties map[string]string) error {
return nil
}
type ViewMessageRequestHeader struct {
Offset int64
}
func (request *ViewMessageRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["offset"] = strconv.FormatInt(request.Offset, 10)
return maps
}
type CreateTopicRequestHeader struct {
Topic string
DefaultTopic string
ReadQueueNums int
WriteQueueNums int
Perm int
TopicFilterType string
TopicSysFlag int
Order bool
}
func (request *CreateTopicRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
maps["defaultTopic"] = request.DefaultTopic
maps["readQueueNums"] = fmt.Sprintf("%d", request.ReadQueueNums)
maps["writeQueueNums"] = fmt.Sprintf("%d", request.WriteQueueNums)
maps["perm"] = fmt.Sprintf("%d", request.Perm)
maps["topicFilterType"] = request.TopicFilterType
maps["topicSysFlag"] = fmt.Sprintf("%d", request.TopicSysFlag)
maps["order"] = strconv.FormatBool(request.Order)
return maps
}
type TopicListRequestHeader struct {
Topic string
}
func (request *TopicListRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
return maps
}
type DeleteTopicRequestHeader struct {
Topic string
}
func (request *DeleteTopicRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
return maps
}
type DeleteSubscriptionGroupRequestHeader struct {
GroupName string
}
func (request *DeleteSubscriptionGroupRequestHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["groupName"] = request.GroupName
return maps
}
type ResetOffsetHeader struct {
Topic string
Group string
Timestamp int64
IsForce bool
}
func (request *ResetOffsetHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["topic"] = request.Topic
maps["group"] = request.Group
maps["timestamp"] = strconv.FormatInt(request.Timestamp, 10)
return maps
}
func (request *ResetOffsetHeader) Decode(properties map[string]string) {
if len(properties) == 0 {
return
}
if v, existed := properties["topic"]; existed {
request.Topic = v
}
if v, existed := properties["group"]; existed {
request.Group = v
}
if v, existed := properties["timestamp"]; existed {
request.Timestamp, _ = strconv.ParseInt(v, 10, 0)
}
}
type ConsumeMessageDirectlyHeader struct {
ConsumerGroup string
ClientID string
MsgId string
BrokerName string
}
func (request *ConsumeMessageDirectlyHeader) Encode() map[string]string {
maps := make(map[string]string)
maps["consumerGroup"] = request.ConsumerGroup
maps["clientId"] = request.ClientID
maps["msgId"] = request.MsgId
maps["brokerName"] = request.BrokerName
return maps
}
func (request *ConsumeMessageDirectlyHeader) Decode(properties map[string]string) {
if len(properties) == 0 {
return
}
if v, existed := properties["consumerGroup"]; existed {
request.ConsumerGroup = v
}
if v, existed := properties["clientId"]; existed {
request.ClientID = v
}
if v, existed := properties["msgId"]; existed {
request.MsgId = v
}
if v, existed := properties["brokerName"]; existed {
request.BrokerName = v
}
}
type GetConsumerStatusRequestHeader struct {
topic string
group string
clientAddr string
}
func (request *GetConsumerStatusRequestHeader) Encode() map[string]string {
return map[string]string{
"topic": request.topic,
"group": request.group,
"clientAddr": request.clientAddr,
}
}
func (request *GetConsumerStatusRequestHeader) Decode(properties map[string]string) {
if len(properties) == 0 {
return
}
if v, existed := properties["topic"]; existed {
request.topic = v
}
if v, existed := properties["group"]; existed {
request.group = v
}
if v, existed := properties["clientAddr"]; existed {
request.clientAddr = v
}
}
type GetConsumeStatsRequestHeader struct {
ConsumerGroup string
Topic string
}
func (request *GetConsumeStatsRequestHeader) Encode() map[string]string {
return map[string]string{
"topic": request.Topic,
"consumerGroup": request.ConsumerGroup,
}
}
func (request *GetConsumeStatsRequestHeader) Decode(properties map[string]string) {
if v, existed := properties["topic"]; existed {
request.Topic = v
}
if v, existed := properties["consumerGroup"]; existed {
request.ConsumerGroup = v
}
}
type ReplyMessageRequestHeader struct {
producerGroup string
topic string
defaultTopic string
defaultTopicQueueNums int
queueId int
sysFlag int
bornTimestamp int64
flag int32
properties string
reconsumeTimes int32
unitMode bool
bornHost string
storeHost string
storeTimestamp int64
}
func (request *ReplyMessageRequestHeader) Encode() map[string]string {
return map[string]string{
"producerGroup": request.producerGroup,
"topic": request.topic,
"defaultTopic": request.defaultTopic,
"defaultTopicQueueNums": strconv.Itoa(request.defaultTopicQueueNums),
"queueId": strconv.Itoa(request.queueId),
"sysFlag": strconv.Itoa(request.sysFlag),
"bornTimestamp": strconv.FormatInt(request.bornTimestamp, 10),
"flag": fmt.Sprintf("%d", request.flag),
"properties": request.properties,
"reconsumeTimes": fmt.Sprintf("%d", request.reconsumeTimes),
"bornHost": request.bornHost,
"storeHost": request.storeHost,
"storeTimestamp": strconv.FormatInt(request.storeTimestamp, 10),
}
}
func (request *ReplyMessageRequestHeader) Decode(properties map[string]string) {
if len(properties) == 0 {
return
}
if v, existed := properties["producerGroup"]; existed {
request.producerGroup = v
}
if v, existed := properties["topic"]; existed {
request.topic = v
}
if v, existed := properties["defaultTopic"]; existed {
request.defaultTopic = v
}
if v, existed := properties["defaultTopicQueueNums"]; existed {
request.defaultTopicQueueNums, _ = strconv.Atoi(v)
}
if v, existed := properties["queueId"]; existed {
request.queueId, _ = strconv.Atoi(v)
}
if v, existed := properties["sysFlag"]; existed {
request.sysFlag, _ = strconv.Atoi(v)
}
if v, existed := properties["bornTimestamp"]; existed {
request.bornTimestamp, _ = strconv.ParseInt(v, 10, 0)
}
if v, existed := properties["flag"]; existed {
tmpFlag, _ := strconv.ParseInt(v, 10, 32)
request.flag = int32(tmpFlag)
}
if v, existed := properties["properties"]; existed {
request.properties = v
}
if v, existed := properties["reconsumeTimes"]; existed {
tmpReconsumeTimes, _ := strconv.ParseInt(v, 10, 32)
request.reconsumeTimes = int32(tmpReconsumeTimes)
}
if v, existed := properties["bornHost"]; existed {
request.bornHost = v
}
if v, existed := properties["storeHost"]; existed {
request.storeHost = v
}
if v, existed := properties["storeTimestamp"]; existed {
request.storeTimestamp, _ = strconv.ParseInt(v, 10, 0)
}
}
type GetConsumerRunningInfoRequestHeader struct {
ConsumerGroup string
ClientId string
JstackEnable bool
}
func (request *GetConsumerRunningInfoRequestHeader) Encode() map[string]string {
return map[string]string{
"clientId": request.ClientId,
"consumerGroup": request.ConsumerGroup,
"jstackEnable": strconv.FormatBool(request.JstackEnable),
}
}
func (request *GetConsumerRunningInfoRequestHeader) Decode(properties map[string]string) {
if v, existed := properties["clientId"]; existed {
request.ClientId = v
}
if v, existed := properties["consumerGroup"]; existed {
request.ConsumerGroup = v
}
if v, existed := properties["jstackEnable"]; existed {
request.JstackEnable, _ = strconv.ParseBool(v)
}
}