offset_response.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ThrottleTimeMs int32
  42. Blocks map[string]map[int32]*OffsetResponseBlock
  43. }
  44. func (r *OffsetResponse) decode(pd packetDecoder, version int16) (err error) {
  45. if version >= 2 {
  46. r.ThrottleTimeMs, err = pd.getInt32()
  47. if err != nil {
  48. return err
  49. }
  50. }
  51. numTopics, err := pd.getArrayLength()
  52. if err != nil {
  53. return err
  54. }
  55. r.Blocks = make(map[string]map[int32]*OffsetResponseBlock, numTopics)
  56. for i := 0; i < numTopics; i++ {
  57. name, err := pd.getString()
  58. if err != nil {
  59. return err
  60. }
  61. numBlocks, err := pd.getArrayLength()
  62. if err != nil {
  63. return err
  64. }
  65. r.Blocks[name] = make(map[int32]*OffsetResponseBlock, numBlocks)
  66. for j := 0; j < numBlocks; j++ {
  67. id, err := pd.getInt32()
  68. if err != nil {
  69. return err
  70. }
  71. block := new(OffsetResponseBlock)
  72. err = block.decode(pd, version)
  73. if err != nil {
  74. return err
  75. }
  76. r.Blocks[name][id] = block
  77. }
  78. }
  79. return nil
  80. }
  81. func (r *OffsetResponse) GetBlock(topic string, partition int32) *OffsetResponseBlock {
  82. if r.Blocks == nil {
  83. return nil
  84. }
  85. if r.Blocks[topic] == nil {
  86. return nil
  87. }
  88. return r.Blocks[topic][partition]
  89. }
  90. /*
  91. // [0 0 0 1 ntopics
  92. 0 8 109 121 95 116 111 112 105 99 topic
  93. 0 0 0 1 npartitions
  94. 0 0 0 0 id
  95. 0 0
  96. 0 0 0 1 0 0 0 0
  97. 0 1 1 1 0 0 0 1
  98. 0 8 109 121 95 116 111 112
  99. 105 99 0 0 0 1 0 0
  100. 0 0 0 0 0 0 0 1
  101. 0 0 0 0 0 1 1 1] <nil>
  102. */
  103. func (r *OffsetResponse) encode(pe packetEncoder) (err error) {
  104. if r.Version >= 2 {
  105. pe.putInt32(r.ThrottleTimeMs)
  106. }
  107. if err = pe.putArrayLength(len(r.Blocks)); err != nil {
  108. return err
  109. }
  110. for topic, partitions := range r.Blocks {
  111. if err = pe.putString(topic); err != nil {
  112. return err
  113. }
  114. if err = pe.putArrayLength(len(partitions)); err != nil {
  115. return err
  116. }
  117. for partition, block := range partitions {
  118. pe.putInt32(partition)
  119. if err = block.encode(pe, r.version()); err != nil {
  120. return err
  121. }
  122. }
  123. }
  124. return nil
  125. }
  126. func (r *OffsetResponse) key() int16 {
  127. return 2
  128. }
  129. func (r *OffsetResponse) version() int16 {
  130. return r.Version
  131. }
  132. func (r *OffsetResponse) headerVersion() int16 {
  133. return 0
  134. }
  135. func (r *OffsetResponse) requiredVersion() KafkaVersion {
  136. switch r.Version {
  137. case 1:
  138. return V0_10_1_0
  139. case 2:
  140. return V0_11_0_0
  141. default:
  142. return MinVersion
  143. }
  144. }
  145. // testing API
  146. func (r *OffsetResponse) AddTopicPartition(topic string, partition int32, offset int64) {
  147. if r.Blocks == nil {
  148. r.Blocks = make(map[string]map[int32]*OffsetResponseBlock)
  149. }
  150. byTopic, ok := r.Blocks[topic]
  151. if !ok {
  152. byTopic = make(map[int32]*OffsetResponseBlock)
  153. r.Blocks[topic] = byTopic
  154. }
  155. byTopic[partition] = &OffsetResponseBlock{Offsets: []int64{offset}, Offset: offset}
  156. }