offset_response.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package sarama
  2. type OffsetResponseBlock struct {
  3. Err KError
  4. Offsets []int64 // Version 0
  5. Offset int64 // Version 1
  6. Timestamp int64 // Version 1
  7. }
  8. func (b *OffsetResponseBlock) decode(pd packetDecoder, version int16) (err error) {
  9. tmp, err := pd.getInt16()
  10. if err != nil {
  11. return err
  12. }
  13. b.Err = KError(tmp)
  14. if version == 0 {
  15. b.Offsets, err = pd.getInt64Array()
  16. return err
  17. }
  18. b.Timestamp, err = pd.getInt64()
  19. if err != nil {
  20. return err
  21. }
  22. b.Offset, err = pd.getInt64()
  23. if err != nil {
  24. return err
  25. }
  26. // For backwards compatibility put the offset in the offsets array too
  27. b.Offsets = []int64{b.Offset}
  28. return nil
  29. }
  30. func (b *OffsetResponseBlock) encode(pe packetEncoder, version int16) (err error) {
  31. pe.putInt16(int16(b.Err))
  32. if version == 0 {
  33. return pe.putInt64Array(b.Offsets)
  34. }
  35. pe.putInt64(b.Timestamp)
  36. pe.putInt64(b.Offset)
  37. return nil
  38. }
  39. type OffsetResponse struct {
  40. Version int16
  41. Blocks map[string]map[int32]*OffsetResponseBlock
  42. }
  43. func (r *OffsetResponse) decode(pd packetDecoder, version int16) (err error) {
  44. numTopics, err := pd.getArrayLength()
  45. if err != nil {
  46. return err
  47. }
  48. r.Blocks = make(map[string]map[int32]*OffsetResponseBlock, numTopics)
  49. for i := 0; i < numTopics; i++ {
  50. name, err := pd.getString()
  51. if err != nil {
  52. return err
  53. }
  54. numBlocks, err := pd.getArrayLength()
  55. if err != nil {
  56. return err
  57. }
  58. r.Blocks[name] = make(map[int32]*OffsetResponseBlock, numBlocks)
  59. for j := 0; j < numBlocks; j++ {
  60. id, err := pd.getInt32()
  61. if err != nil {
  62. return err
  63. }
  64. block := new(OffsetResponseBlock)
  65. err = block.decode(pd, version)
  66. if err != nil {
  67. return err
  68. }
  69. r.Blocks[name][id] = block
  70. }
  71. }
  72. return nil
  73. }
  74. func (r *OffsetResponse) GetBlock(topic string, partition int32) *OffsetResponseBlock {
  75. if r.Blocks == nil {
  76. return nil
  77. }
  78. if r.Blocks[topic] == nil {
  79. return nil
  80. }
  81. return r.Blocks[topic][partition]
  82. }
  83. /*
  84. // [0 0 0 1 ntopics
  85. 0 8 109 121 95 116 111 112 105 99 topic
  86. 0 0 0 1 npartitions
  87. 0 0 0 0 id
  88. 0 0
  89. 0 0 0 1 0 0 0 0
  90. 0 1 1 1 0 0 0 1
  91. 0 8 109 121 95 116 111 112
  92. 105 99 0 0 0 1 0 0
  93. 0 0 0 0 0 0 0 1
  94. 0 0 0 0 0 1 1 1] <nil>
  95. */
  96. func (r *OffsetResponse) encode(pe packetEncoder) (err error) {
  97. if err = pe.putArrayLength(len(r.Blocks)); err != nil {
  98. return err
  99. }
  100. for topic, partitions := range r.Blocks {
  101. if err = pe.putString(topic); err != nil {
  102. return err
  103. }
  104. if err = pe.putArrayLength(len(partitions)); err != nil {
  105. return err
  106. }
  107. for partition, block := range partitions {
  108. pe.putInt32(partition)
  109. if err = block.encode(pe, r.version()); err != nil {
  110. return err
  111. }
  112. }
  113. }
  114. return nil
  115. }
  116. func (r *OffsetResponse) key() int16 {
  117. return 2
  118. }
  119. func (r *OffsetResponse) version() int16 {
  120. return r.Version
  121. }
  122. func (r *OffsetResponse) requiredVersion() KafkaVersion {
  123. switch r.Version {
  124. case 1:
  125. return V0_10_1_0
  126. default:
  127. return MinVersion
  128. }
  129. }
  130. // testing API
  131. func (r *OffsetResponse) AddTopicPartition(topic string, partition int32, offset int64) {
  132. if r.Blocks == nil {
  133. r.Blocks = make(map[string]map[int32]*OffsetResponseBlock)
  134. }
  135. byTopic, ok := r.Blocks[topic]
  136. if !ok {
  137. byTopic = make(map[int32]*OffsetResponseBlock)
  138. r.Blocks[topic] = byTopic
  139. }
  140. byTopic[partition] = &OffsetResponseBlock{Offsets: []int64{offset}, Offset: offset}
  141. }