offset_fetch_response.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package sarama
  2. import "time"
  3. type OffsetFetchResponseBlock struct {
  4. Offset int64
  5. LeaderEpoch int32
  6. Metadata string
  7. Err KError
  8. }
  9. func (b *OffsetFetchResponseBlock) decode(pd packetDecoder, version int16) (err error) {
  10. isFlexible := version >= 6
  11. b.Offset, err = pd.getInt64()
  12. if err != nil {
  13. return err
  14. }
  15. if version >= 5 {
  16. b.LeaderEpoch, err = pd.getInt32()
  17. if err != nil {
  18. return err
  19. }
  20. } else {
  21. b.LeaderEpoch = -1
  22. }
  23. if isFlexible {
  24. b.Metadata, err = pd.getCompactString()
  25. } else {
  26. b.Metadata, err = pd.getString()
  27. }
  28. if err != nil {
  29. return err
  30. }
  31. tmp, err := pd.getInt16()
  32. if err != nil {
  33. return err
  34. }
  35. b.Err = KError(tmp)
  36. if isFlexible {
  37. if _, err := pd.getEmptyTaggedFieldArray(); err != nil {
  38. return err
  39. }
  40. }
  41. return nil
  42. }
  43. func (b *OffsetFetchResponseBlock) encode(pe packetEncoder, version int16) (err error) {
  44. isFlexible := version >= 6
  45. pe.putInt64(b.Offset)
  46. if version >= 5 {
  47. pe.putInt32(b.LeaderEpoch)
  48. }
  49. if isFlexible {
  50. err = pe.putCompactString(b.Metadata)
  51. } else {
  52. err = pe.putString(b.Metadata)
  53. }
  54. if err != nil {
  55. return err
  56. }
  57. pe.putInt16(int16(b.Err))
  58. if isFlexible {
  59. pe.putEmptyTaggedFieldArray()
  60. }
  61. return nil
  62. }
  63. type OffsetFetchResponse struct {
  64. Version int16
  65. ThrottleTimeMs int32
  66. Blocks map[string]map[int32]*OffsetFetchResponseBlock
  67. Err KError
  68. }
  69. func (r *OffsetFetchResponse) encode(pe packetEncoder) (err error) {
  70. isFlexible := r.Version >= 6
  71. if r.Version >= 3 {
  72. pe.putInt32(r.ThrottleTimeMs)
  73. }
  74. if isFlexible {
  75. pe.putCompactArrayLength(len(r.Blocks))
  76. } else {
  77. err = pe.putArrayLength(len(r.Blocks))
  78. }
  79. if err != nil {
  80. return err
  81. }
  82. for topic, partitions := range r.Blocks {
  83. if isFlexible {
  84. err = pe.putCompactString(topic)
  85. } else {
  86. err = pe.putString(topic)
  87. }
  88. if err != nil {
  89. return err
  90. }
  91. if isFlexible {
  92. pe.putCompactArrayLength(len(partitions))
  93. } else {
  94. err = pe.putArrayLength(len(partitions))
  95. }
  96. if err != nil {
  97. return err
  98. }
  99. for partition, block := range partitions {
  100. pe.putInt32(partition)
  101. if err := block.encode(pe, r.Version); err != nil {
  102. return err
  103. }
  104. }
  105. if isFlexible {
  106. pe.putEmptyTaggedFieldArray()
  107. }
  108. }
  109. if r.Version >= 2 {
  110. pe.putInt16(int16(r.Err))
  111. }
  112. if isFlexible {
  113. pe.putEmptyTaggedFieldArray()
  114. }
  115. return nil
  116. }
  117. func (r *OffsetFetchResponse) decode(pd packetDecoder, version int16) (err error) {
  118. r.Version = version
  119. isFlexible := version >= 6
  120. if version >= 3 {
  121. r.ThrottleTimeMs, err = pd.getInt32()
  122. if err != nil {
  123. return err
  124. }
  125. }
  126. var numTopics int
  127. if isFlexible {
  128. numTopics, err = pd.getCompactArrayLength()
  129. } else {
  130. numTopics, err = pd.getArrayLength()
  131. }
  132. if err != nil {
  133. return err
  134. }
  135. if numTopics > 0 {
  136. r.Blocks = make(map[string]map[int32]*OffsetFetchResponseBlock, numTopics)
  137. for i := 0; i < numTopics; i++ {
  138. var name string
  139. if isFlexible {
  140. name, err = pd.getCompactString()
  141. } else {
  142. name, err = pd.getString()
  143. }
  144. if err != nil {
  145. return err
  146. }
  147. var numBlocks int
  148. if isFlexible {
  149. numBlocks, err = pd.getCompactArrayLength()
  150. } else {
  151. numBlocks, err = pd.getArrayLength()
  152. }
  153. if err != nil {
  154. return err
  155. }
  156. r.Blocks[name] = nil
  157. if numBlocks > 0 {
  158. r.Blocks[name] = make(map[int32]*OffsetFetchResponseBlock, numBlocks)
  159. }
  160. for j := 0; j < numBlocks; j++ {
  161. id, err := pd.getInt32()
  162. if err != nil {
  163. return err
  164. }
  165. block := new(OffsetFetchResponseBlock)
  166. err = block.decode(pd, version)
  167. if err != nil {
  168. return err
  169. }
  170. r.Blocks[name][id] = block
  171. }
  172. if isFlexible {
  173. if _, err := pd.getEmptyTaggedFieldArray(); err != nil {
  174. return err
  175. }
  176. }
  177. }
  178. }
  179. if version >= 2 {
  180. kerr, err := pd.getInt16()
  181. if err != nil {
  182. return err
  183. }
  184. r.Err = KError(kerr)
  185. }
  186. if isFlexible {
  187. if _, err := pd.getEmptyTaggedFieldArray(); err != nil {
  188. return err
  189. }
  190. }
  191. return nil
  192. }
  193. func (r *OffsetFetchResponse) key() int16 {
  194. return 9
  195. }
  196. func (r *OffsetFetchResponse) version() int16 {
  197. return r.Version
  198. }
  199. func (r *OffsetFetchResponse) headerVersion() int16 {
  200. if r.Version >= 6 {
  201. return 1
  202. }
  203. return 0
  204. }
  205. func (r *OffsetFetchResponse) isValidVersion() bool {
  206. return r.Version >= 0 && r.Version <= 7
  207. }
  208. func (r *OffsetFetchResponse) requiredVersion() KafkaVersion {
  209. switch r.Version {
  210. case 7:
  211. return V2_5_0_0
  212. case 6:
  213. return V2_4_0_0
  214. case 5:
  215. return V2_1_0_0
  216. case 4:
  217. return V2_0_0_0
  218. case 3:
  219. return V0_11_0_0
  220. case 2:
  221. return V0_10_2_0
  222. case 1:
  223. return V0_8_2_0
  224. case 0:
  225. return V0_8_2_0
  226. default:
  227. return V2_5_0_0
  228. }
  229. }
  230. func (r *OffsetFetchResponse) throttleTime() time.Duration {
  231. return time.Duration(r.ThrottleTimeMs) * time.Millisecond
  232. }
  233. func (r *OffsetFetchResponse) GetBlock(topic string, partition int32) *OffsetFetchResponseBlock {
  234. if r.Blocks == nil {
  235. return nil
  236. }
  237. if r.Blocks[topic] == nil {
  238. return nil
  239. }
  240. return r.Blocks[topic][partition]
  241. }
  242. func (r *OffsetFetchResponse) AddBlock(topic string, partition int32, block *OffsetFetchResponseBlock) {
  243. if r.Blocks == nil {
  244. r.Blocks = make(map[string]map[int32]*OffsetFetchResponseBlock)
  245. }
  246. partitions := r.Blocks[topic]
  247. if partitions == nil {
  248. partitions = make(map[int32]*OffsetFetchResponseBlock)
  249. r.Blocks[topic] = partitions
  250. }
  251. partitions[partition] = block
  252. }