forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
838 lines (710 loc) · 16.7 KB
/
protocol.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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
package kafka
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"hash/crc32"
"io"
"math"
"reflect"
"time"
)
type apiKey int16
const (
produceRequest apiKey = 0
fetchRequest apiKey = 1
offsetRequest apiKey = 2
metadataRequest apiKey = 3
offsetCommitRequest apiKey = 8
offsetFetchRequest apiKey = 9
groupCoordinatorRequest apiKey = 10
joinGroupRequest apiKey = 11
heartbeatRequest apiKey = 12
leaveGroupRequest apiKey = 13
syncGroupRequest apiKey = 14
describeGroupsRequest apiKey = 15
listGroupsRequest apiKey = 16
)
type apiVersion int16
const (
v0 apiVersion = 0
v1 apiVersion = 1
v2 apiVersion = 2
)
type requestHeader struct {
Size int32
ApiKey int16
ApiVersion int16
CorrelationID int32
ClientID string
}
// Message types
type messageSet []messageSetItem
type messageSetItem struct {
Offset int64
MessageSize int32
Message message
}
type message struct {
CRC int32
MagicByte int8
Attributes int8
Timestamp int64
Key []byte
Value []byte
}
func (m message) crc32() int32 {
sum := uint32(0)
sum = crc32Int8(sum, m.MagicByte)
sum = crc32Int8(sum, m.Attributes)
sum = crc32Int64(sum, m.Timestamp)
sum = crc32Bytes(sum, m.Key)
sum = crc32Bytes(sum, m.Value)
return int32(sum)
}
func crc32Int8(sum uint32, v int8) uint32 {
b := [1]byte{byte(v)}
return crc32Add(sum, b[:])
}
func crc32Int32(sum uint32, v int32) uint32 {
b := [4]byte{}
binary.BigEndian.PutUint32(b[:], uint32(v))
return crc32Add(sum, b[:])
}
func crc32Int64(sum uint32, v int64) uint32 {
b := [8]byte{}
binary.BigEndian.PutUint64(b[:], uint64(v))
return crc32Add(sum, b[:])
}
func crc32Bytes(sum uint32, b []byte) uint32 {
if b == nil {
sum = crc32Int32(sum, -1)
} else {
sum = crc32Int32(sum, int32(len(b)))
}
return crc32Add(sum, b)
}
func crc32Add(sum uint32, b []byte) uint32 {
return crc32.Update(sum, crc32.IEEETable, b[:])
}
// Metadata API (v0)
type topicMetadataRequestV0 []string
type metadataResponseV0 struct {
Brokers []brokerMetadataV0
Topics []topicMetadataV0
}
type brokerMetadataV0 struct {
NodeID int32
Host string
Port int32
}
type topicMetadataV0 struct {
TopicErrorCode int16
TopicName string
Partitions []partitionMetadataV0
}
type partitionMetadataV0 struct {
PartitionErrorCode int16
PartitionID int32
Leader int32
Replicas []int32
Isr []int32
}
// Produce API (v2)
type produceRequestV2 struct {
RequiredAcks int16
Timeout int32
Topics []produceRequestTopicV2
}
type produceRequestTopicV2 struct {
TopicName string
Partitions []produceRequestPartitionV2
}
type produceRequestPartitionV2 struct {
Partition int32
MessageSetSize int32
MessageSet messageSet
}
type produceResponseV2 struct {
Topics []produceResponseTopicV2
ThrottleTime int32
}
type produceResponseTopicV2 struct {
TopicName string
Partitions []produceResponsePartitionV2
}
type produceResponsePartitionV2 struct {
Partition int32
ErrorCode int16
Offset int64
Timestamp int64
}
// Fetch API (v1)
type fetchRequestV1 struct {
ReplicaID int32
MaxWaitTime int32
MinBytes int32
Topics []fetchRequestTopicV1
}
type fetchRequestTopicV1 struct {
TopicName string
Partitions []fetchRequestPartitionV1
}
type fetchRequestPartitionV1 struct {
Partition int32
FetchOffset int64
MaxBytes int32
}
type fetchResponseV1 struct {
ThrottleTime int32
Topics []fetchResponseTopicV1
}
type fetchResponseTopicV1 struct {
TopicName string
Partitions []fetchResponsePartitionV1
}
type fetchResponsePartitionV1 struct {
Partition int32
ErrorCode int16
HighwaterMarkOffset int64
MessageSetSize int32
MessageSet messageSet
}
// Offset API (v1)
type listOffsetRequestV1 struct {
ReplicaID int32
Topics []listOffsetRequestTopicV1
}
type listOffsetRequestTopicV1 struct {
TopicName string
Partitions []listOffsetRequestPartitionV1
}
type listOffsetRequestPartitionV1 struct {
Partition int32
Time int64
}
type listOffsetResponseV1 struct {
TopicName string
PartitionOffsets []partitionOffsetV1
}
type partitionOffsetV1 struct {
Partition int32
ErrorCode int16
Timestamp int64
Offset int64
}
var (
errShortRead = errors.New("not enough bytes available to load the response")
)
func makeInt8(b []byte) int8 {
return int8(b[0])
}
func makeInt16(b []byte) int16 {
return int16(binary.BigEndian.Uint16(b))
}
func makeInt32(b []byte) int32 {
return int32(binary.BigEndian.Uint32(b))
}
func makeInt64(b []byte) int64 {
return int64(binary.BigEndian.Uint64(b))
}
func peekRead(r *bufio.Reader, sz int, n int, f func([]byte)) (int, error) {
if n > sz {
return sz, errShortRead
}
b, err := r.Peek(n)
if err != nil {
return sz, err
}
f(b)
return discardN(r, sz, n)
}
func readInt8(r *bufio.Reader, sz int, v *int8) (int, error) {
return peekRead(r, sz, 1, func(b []byte) { *v = makeInt8(b) })
}
func readInt16(r *bufio.Reader, sz int, v *int16) (int, error) {
return peekRead(r, sz, 2, func(b []byte) { *v = makeInt16(b) })
}
func readInt32(r *bufio.Reader, sz int, v *int32) (int, error) {
return peekRead(r, sz, 4, func(b []byte) { *v = makeInt32(b) })
}
func readInt64(r *bufio.Reader, sz int, v *int64) (int, error) {
return peekRead(r, sz, 8, func(b []byte) { *v = makeInt64(b) })
}
func readString(r *bufio.Reader, sz int, v *string) (int, error) {
var err error
var len int16
var b []byte
if sz, err = readInt16(r, sz, &len); err != nil {
return sz, err
}
if n := int(len); n >= 0 {
if n > sz {
return sz, errShortRead
}
b = make([]byte, n)
if _, err = io.ReadFull(r, b); err != nil {
return sz, err
}
sz -= n
}
*v = string(b)
return sz, nil
}
func readBytes(r *bufio.Reader, sz int, v *[]byte) (int, error) {
var err error
var len int32
var b []byte
if sz, err = readInt32(r, sz, &len); err != nil {
return sz, err
}
if n := int(len); n >= 0 {
if n > sz {
return sz, errShortRead
}
b = make([]byte, n)
if _, err = io.ReadFull(r, b); err != nil {
return sz, err
}
sz -= n
}
*v = b
return sz, nil
}
func read(r *bufio.Reader, sz int, a interface{}) (int, error) {
switch v := a.(type) {
case *int8:
return readInt8(r, sz, v)
case *int16:
return readInt16(r, sz, v)
case *int32:
return readInt32(r, sz, v)
case *int64:
return readInt64(r, sz, v)
case *string:
return readString(r, sz, v)
case *[]byte:
return readBytes(r, sz, v)
}
switch v := reflect.ValueOf(a).Elem(); v.Kind() {
case reflect.Struct:
return readStruct(r, sz, v)
case reflect.Slice:
return readSlice(r, sz, v)
default:
panic(fmt.Sprintf("unsupported type: %T", a))
}
}
func readStruct(r *bufio.Reader, sz int, v reflect.Value) (int, error) {
var err error
for i, n := 0, v.NumField(); i != n; i++ {
if sz, err = read(r, sz, v.Field(i).Addr().Interface()); err != nil {
return sz, err
}
}
return sz, nil
}
func readSlice(r *bufio.Reader, sz int, v reflect.Value) (int, error) {
var err error
var len int32
if sz, err = readInt32(r, sz, &len); err != nil {
return sz, err
}
if n := int(len); n < 0 {
v.Set(reflect.Zero(v.Type()))
} else {
v.Set(reflect.MakeSlice(v.Type(), n, n))
for i := 0; i != n; i++ {
if sz, err = read(r, sz, v.Index(i).Addr().Interface()); err != nil {
return sz, err
}
}
}
return sz, nil
}
func readMessageOffsetAndSize(r *bufio.Reader, sz int) (offset int64, size int32, remain int, err error) {
if remain, err = readInt64(r, sz, &offset); err != nil {
return
}
remain, err = readInt32(r, remain, &size)
return
}
func readMessageHeader(r *bufio.Reader, sz int) (attributes int8, timestamp int64, remain int, err error) {
var version int8
// TCP is already taking care of ensuring data integrirty, no need to waste
// resources doing it a second time so we just skip the message CRC.
if remain, err = discardInt32(r, sz); err != nil {
return
}
if remain, err = readInt8(r, remain, &version); err != nil {
return
}
if remain, err = readInt8(r, remain, &attributes); err != nil {
return
}
switch version {
case 0:
case 1:
if remain, err = readInt64(r, remain, ×tamp); err != nil {
return
}
default:
err = fmt.Errorf("unsupported message version: %d", version)
}
return
}
func readMessageBytes(r *bufio.Reader, sz int, b []byte) (n int, remain int, err error) {
var bytesLen int32
var limit int
var count int
if remain, err = readInt32(r, sz, &bytesLen); err != nil {
return
}
if bytesLen > 0 {
n = int(bytesLen)
}
if n > remain {
limit = remain
} else {
limit = n
}
if limit > len(b) {
limit = len(b)
}
count, err = io.ReadFull(r, b[:limit])
remain -= count
return
}
func readResponse(r *bufio.Reader, sz int, res interface{}) error {
sz, err := read(r, sz, res)
switch err.(type) {
case Error:
sz, err = discardN(r, sz, sz)
}
return expectZeroSize(sz, err)
}
func expectZeroSize(sz int, err error) error {
if err == nil && sz != 0 {
err = fmt.Errorf("reading a response left %d unread bytes", sz)
}
return err
}
func ignoreShortRead(sz int, err error) (int, error) {
if err == errShortRead {
err = nil
}
return sz, err
}
func streamArray(r *bufio.Reader, sz int, cb func(*bufio.Reader, int) (int, error)) (int, error) {
var err error
var len int32
if sz, err = readInt32(r, sz, &len); err != nil {
return sz, err
}
for n := int(len); n > 0; n-- {
if sz, err = cb(r, sz); err != nil {
break
}
}
return sz, err
}
func streamString(r *bufio.Reader, sz int, cb func(*bufio.Reader, int, int16) (int, error)) (int, error) {
var err error
var len int16
if sz, err = readInt16(r, sz, &len); err != nil {
return sz, err
}
sz0 := sz
sz1 := sz - int(len)
if sz, err = cb(r, sz, len); err != nil {
return sz, err
}
if sz != sz1 {
return sz, fmt.Errorf("streaming callback of string of length %d consumed %d bytes", len, sz0-sz)
}
return sz, nil
}
func streamBytes(r *bufio.Reader, sz int, cb func(*bufio.Reader, int, int32) (int, error)) (int, error) {
var err error
var len int32
if sz, err = readInt32(r, sz, &len); err != nil {
return sz, err
}
sz0 := sz
sz1 := sz - int(len)
if sz, err = cb(r, sz, len); err != nil {
return sz, err
}
if sz != sz1 {
return sz, fmt.Errorf("streaming callback of string of length %d consumed %d bytes", len, sz0-sz)
}
return sz, nil
}
func discardN(r *bufio.Reader, sz int, n int) (int, error) {
n, err := r.Discard(n)
return sz - n, err
}
func discardInt8(r *bufio.Reader, sz int) (int, error) {
return discardN(r, sz, 1)
}
func discardInt16(r *bufio.Reader, sz int) (int, error) {
return discardN(r, sz, 2)
}
func discardInt32(r *bufio.Reader, sz int) (int, error) {
return discardN(r, sz, 4)
}
func discardInt64(r *bufio.Reader, sz int) (int, error) {
return discardN(r, sz, 8)
}
func discardString(r *bufio.Reader, sz int) (int, error) {
return streamString(r, sz, func(r *bufio.Reader, sz int, len int16) (int, error) {
return discardN(r, sz, int(len))
})
}
func discardBytes(r *bufio.Reader, sz int) (int, error) {
return streamBytes(r, sz, func(r *bufio.Reader, sz int, len int32) (int, error) {
return discardN(r, sz, int(len))
})
}
func discard(r *bufio.Reader, sz int, a interface{}) (int, error) {
switch a.(type) {
case int8:
return discardInt8(r, sz)
case int16:
return discardInt16(r, sz)
case int32:
return discardInt32(r, sz)
case int64:
return discardInt64(r, sz)
case string:
return discardString(r, sz)
case []byte:
return discardBytes(r, sz)
}
switch v := reflect.ValueOf(a); v.Kind() {
case reflect.Struct:
return discardStruct(r, sz, v)
case reflect.Slice:
return discardSlice(r, sz, v)
default:
panic(fmt.Sprintf("unsupported type: %T", a))
}
}
func discardStruct(r *bufio.Reader, sz int, v reflect.Value) (int, error) {
var err error
for i, n := 0, v.NumField(); i != n; i++ {
if sz, err = discard(r, sz, v.Field(i)); err != nil {
break
}
}
return sz, err
}
func discardSlice(r *bufio.Reader, sz int, v reflect.Value) (int, error) {
var zero = reflect.Zero(v.Type().Elem())
var err error
var len int32
if sz, err = readInt32(r, sz, &len); err != nil {
return sz, err
}
for n := int(len); n > 0; n-- {
if sz, err = discard(r, sz, zero); err != nil {
break
}
}
return sz, err
}
func writeInt8(w *bufio.Writer, i int8) error {
return w.WriteByte(byte(i))
}
func writeInt16(w *bufio.Writer, i int16) error {
var b [2]byte
binary.BigEndian.PutUint16(b[:], uint16(i))
return writeSmallBuffer(w, b[:])
}
func writeInt32(w *bufio.Writer, i int32) error {
var b [4]byte
binary.BigEndian.PutUint32(b[:], uint32(i))
return writeSmallBuffer(w, b[:])
}
func writeInt64(w *bufio.Writer, i int64) error {
var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(i))
return writeSmallBuffer(w, b[:])
}
func writeString(w *bufio.Writer, s string) error {
if err := writeInt16(w, int16(len(s))); err != nil {
return err
}
_, err := w.WriteString(s)
return err
}
func writeBytes(w *bufio.Writer, b []byte) error {
n := int32(len(b))
if b == nil {
n = -1
}
if err := writeInt32(w, n); err != nil {
return err
}
_, err := w.Write(b)
return err
}
func writeMessageSet(w *bufio.Writer, mset messageSet) error {
// message sets are not preceded by their size for some reason
for _, m := range mset {
if err := write(w, m); err != nil {
return err
}
}
return nil
}
func writeSmallBuffer(w *bufio.Writer, b []byte) error {
// faster for short byte slices because it doesn't cause a memory allocation
// because the byte slice may be passed to the underlying io.Writer.
for _, c := range b {
if err := w.WriteByte(c); err != nil {
return err
}
}
return nil
}
func write(w *bufio.Writer, a interface{}) error {
switch v := a.(type) {
case int8:
return writeInt8(w, v)
case int16:
return writeInt16(w, v)
case int32:
return writeInt32(w, v)
case int64:
return writeInt64(w, v)
case string:
return writeString(w, v)
case []byte:
return writeBytes(w, v)
case messageSet:
return writeMessageSet(w, v)
}
switch v := reflect.ValueOf(a); v.Kind() {
case reflect.Struct:
return writeStruct(w, v)
case reflect.Slice:
return writeSlice(w, v)
default:
panic(fmt.Sprintf("unsupported type: %T", a))
}
}
func writeStruct(w *bufio.Writer, v reflect.Value) error {
for i, n := 0, v.NumField(); i != n; i++ {
if err := write(w, v.Field(i).Interface()); err != nil {
return err
}
}
return nil
}
func writeSlice(w *bufio.Writer, v reflect.Value) error {
n := v.Len()
if err := writeInt32(w, int32(n)); err != nil {
return err
}
for i := 0; i != n; i++ {
if err := write(w, v.Index(i).Interface()); err != nil {
return err
}
}
return nil
}
func writeRequest(w *bufio.Writer, hdr requestHeader, req interface{}) error {
hdr.Size = (sizeof(hdr) + sizeof(req)) - 4
write(w, hdr)
write(w, req)
return w.Flush()
}
func sizeof(a interface{}) int32 {
switch v := a.(type) {
case int8:
return 1
case int16:
return 2
case int32:
return 4
case int64:
return 8
case string:
return 2 + int32(len(v))
case []byte:
return 4 + int32(len(v))
case messageSet:
return sizeofMessageSet(v)
}
switch v := reflect.ValueOf(a); v.Kind() {
case reflect.Struct:
return sizeofStruct(v)
case reflect.Slice:
return sizeofSlice(v)
default:
panic(fmt.Sprintf("unsupported type: %T", a))
}
}
func sizeofMessageSet(set messageSet) (size int32) {
for _, msg := range set {
size += sizeof(msg)
}
return
}
func sizeofStruct(v reflect.Value) (size int32) {
for i, n := 0, v.NumField(); i != n; i++ {
size += sizeof(v.Field(i).Interface())
}
return
}
func sizeofSlice(v reflect.Value) (size int32) {
size = 4
for i, n := 0, v.Len(); i != n; i++ {
size += sizeof(v.Index(i).Interface())
}
return
}
const (
maxTimeout = time.Duration(math.MaxInt32) * time.Millisecond
minTimeout = time.Duration(math.MinInt32) * time.Millisecond
defaultRTT = 1 * time.Second
)
func timestamp() int64 {
return timeToTimestamp(time.Now())
}
func timeToTimestamp(t time.Time) int64 {
if t.IsZero() {
return 0
}
return t.UnixNano() / int64(time.Millisecond)
}
func timestampToTime(t int64) time.Time {
return time.Unix(t/1000, (t%1000)*int64(time.Millisecond))
}
func milliseconds(d time.Duration) int32 {
switch {
case d > maxTimeout:
d = maxTimeout
case d < minTimeout:
d = minTimeout
}
return int32(d / time.Millisecond)
}
func deadlineToTimeout(deadline time.Time, now time.Time) time.Duration {
if deadline.IsZero() {
return maxTimeout
}
return deadline.Sub(now)
}
func adjustDeadlineForRTT(deadline time.Time, now time.Time, rtt time.Duration) time.Time {
if !deadline.IsZero() {
timeout := deadline.Sub(now)
if timeout < rtt {
rtt = timeout / 4
}
deadline = deadline.Add(-rtt)
}
return deadline
}