fetch_response.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package sarama
  2. import "time"
  3. type AbortedTransaction struct {
  4. ProducerID int64
  5. FirstOffset int64
  6. }
  7. func (t *AbortedTransaction) decode(pd packetDecoder) (err error) {
  8. if t.ProducerID, err = pd.getInt64(); err != nil {
  9. return err
  10. }
  11. if t.FirstOffset, err = pd.getInt64(); err != nil {
  12. return err
  13. }
  14. return nil
  15. }
  16. func (t *AbortedTransaction) encode(pe packetEncoder) (err error) {
  17. pe.putInt64(t.ProducerID)
  18. pe.putInt64(t.FirstOffset)
  19. return nil
  20. }
  21. type FetchResponseBlock struct {
  22. Err KError
  23. HighWaterMarkOffset int64
  24. LastStableOffset int64
  25. AbortedTransactions []*AbortedTransaction
  26. Records Records
  27. }
  28. func (b *FetchResponseBlock) decode(pd packetDecoder, version int16) (err error) {
  29. tmp, err := pd.getInt16()
  30. if err != nil {
  31. return err
  32. }
  33. b.Err = KError(tmp)
  34. b.HighWaterMarkOffset, err = pd.getInt64()
  35. if err != nil {
  36. return err
  37. }
  38. if version >= 4 {
  39. b.LastStableOffset, err = pd.getInt64()
  40. if err != nil {
  41. return err
  42. }
  43. numTransact, err := pd.getArrayLength()
  44. if err != nil {
  45. return err
  46. }
  47. if numTransact >= 0 {
  48. b.AbortedTransactions = make([]*AbortedTransaction, numTransact)
  49. }
  50. for i := 0; i < numTransact; i++ {
  51. transact := new(AbortedTransaction)
  52. if err = transact.decode(pd); err != nil {
  53. return err
  54. }
  55. b.AbortedTransactions[i] = transact
  56. }
  57. }
  58. recordsSize, err := pd.getInt32()
  59. if err != nil {
  60. return err
  61. }
  62. recordsDecoder, err := pd.getSubset(int(recordsSize))
  63. if err != nil {
  64. return err
  65. }
  66. if recordsSize > 0 {
  67. if err = b.Records.decode(recordsDecoder); err != nil {
  68. return err
  69. }
  70. }
  71. return nil
  72. }
  73. func (b *FetchResponseBlock) encode(pe packetEncoder, version int16) (err error) {
  74. pe.putInt16(int16(b.Err))
  75. pe.putInt64(b.HighWaterMarkOffset)
  76. if version >= 4 {
  77. pe.putInt64(b.LastStableOffset)
  78. if err = pe.putArrayLength(len(b.AbortedTransactions)); err != nil {
  79. return err
  80. }
  81. for _, transact := range b.AbortedTransactions {
  82. if err = transact.encode(pe); err != nil {
  83. return err
  84. }
  85. }
  86. }
  87. pe.push(&lengthField{})
  88. err = b.Records.encode(pe)
  89. if err != nil {
  90. return err
  91. }
  92. return pe.pop()
  93. }
  94. type FetchResponse struct {
  95. Blocks map[string]map[int32]*FetchResponseBlock
  96. ThrottleTime time.Duration
  97. Version int16 // v1 requires 0.9+, v2 requires 0.10+
  98. }
  99. func (r *FetchResponse) decode(pd packetDecoder, version int16) (err error) {
  100. r.Version = version
  101. if r.Version >= 1 {
  102. throttle, err := pd.getInt32()
  103. if err != nil {
  104. return err
  105. }
  106. r.ThrottleTime = time.Duration(throttle) * time.Millisecond
  107. }
  108. numTopics, err := pd.getArrayLength()
  109. if err != nil {
  110. return err
  111. }
  112. r.Blocks = make(map[string]map[int32]*FetchResponseBlock, numTopics)
  113. for i := 0; i < numTopics; i++ {
  114. name, err := pd.getString()
  115. if err != nil {
  116. return err
  117. }
  118. numBlocks, err := pd.getArrayLength()
  119. if err != nil {
  120. return err
  121. }
  122. r.Blocks[name] = make(map[int32]*FetchResponseBlock, numBlocks)
  123. for j := 0; j < numBlocks; j++ {
  124. id, err := pd.getInt32()
  125. if err != nil {
  126. return err
  127. }
  128. block := new(FetchResponseBlock)
  129. err = block.decode(pd, version)
  130. if err != nil {
  131. return err
  132. }
  133. r.Blocks[name][id] = block
  134. }
  135. }
  136. return nil
  137. }
  138. func (r *FetchResponse) encode(pe packetEncoder) (err error) {
  139. if r.Version >= 1 {
  140. pe.putInt32(int32(r.ThrottleTime / time.Millisecond))
  141. }
  142. err = pe.putArrayLength(len(r.Blocks))
  143. if err != nil {
  144. return err
  145. }
  146. for topic, partitions := range r.Blocks {
  147. err = pe.putString(topic)
  148. if err != nil {
  149. return err
  150. }
  151. err = pe.putArrayLength(len(partitions))
  152. if err != nil {
  153. return err
  154. }
  155. for id, block := range partitions {
  156. pe.putInt32(id)
  157. err = block.encode(pe, r.Version)
  158. if err != nil {
  159. return err
  160. }
  161. }
  162. }
  163. return nil
  164. }
  165. func (r *FetchResponse) key() int16 {
  166. return 1
  167. }
  168. func (r *FetchResponse) version() int16 {
  169. return r.Version
  170. }
  171. func (r *FetchResponse) requiredVersion() KafkaVersion {
  172. switch r.Version {
  173. case 1:
  174. return V0_9_0_0
  175. case 2:
  176. return V0_10_0_0
  177. case 3:
  178. return V0_10_1_0
  179. case 4:
  180. return V0_11_0_0
  181. default:
  182. return minVersion
  183. }
  184. }
  185. func (r *FetchResponse) GetBlock(topic string, partition int32) *FetchResponseBlock {
  186. if r.Blocks == nil {
  187. return nil
  188. }
  189. if r.Blocks[topic] == nil {
  190. return nil
  191. }
  192. return r.Blocks[topic][partition]
  193. }
  194. func (r *FetchResponse) AddError(topic string, partition int32, err KError) {
  195. if r.Blocks == nil {
  196. r.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  197. }
  198. partitions, ok := r.Blocks[topic]
  199. if !ok {
  200. partitions = make(map[int32]*FetchResponseBlock)
  201. r.Blocks[topic] = partitions
  202. }
  203. frb, ok := partitions[partition]
  204. if !ok {
  205. frb = new(FetchResponseBlock)
  206. partitions[partition] = frb
  207. }
  208. frb.Err = err
  209. }
  210. func (r *FetchResponse) getOrCreateBlock(topic string, partition int32) *FetchResponseBlock {
  211. if r.Blocks == nil {
  212. r.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  213. }
  214. partitions, ok := r.Blocks[topic]
  215. if !ok {
  216. partitions = make(map[int32]*FetchResponseBlock)
  217. r.Blocks[topic] = partitions
  218. }
  219. frb, ok := partitions[partition]
  220. if !ok {
  221. frb = new(FetchResponseBlock)
  222. partitions[partition] = frb
  223. }
  224. return frb
  225. }
  226. func encodeKV(key, value Encoder) ([]byte, []byte) {
  227. var kb []byte
  228. var vb []byte
  229. if key != nil {
  230. kb, _ = key.Encode()
  231. }
  232. if value != nil {
  233. vb, _ = value.Encode()
  234. }
  235. return kb, vb
  236. }
  237. func (r *FetchResponse) AddMessage(topic string, partition int32, key, value Encoder, offset int64) {
  238. frb := r.getOrCreateBlock(topic, partition)
  239. kb, vb := encodeKV(key, value)
  240. msg := &Message{Key: kb, Value: vb}
  241. msgBlock := &MessageBlock{Msg: msg, Offset: offset}
  242. set := frb.Records.msgSet
  243. if set == nil {
  244. set = &MessageSet{}
  245. frb.Records = newLegacyRecords(set)
  246. }
  247. set.Messages = append(set.Messages, msgBlock)
  248. }
  249. func (r *FetchResponse) AddRecord(topic string, partition int32, key, value Encoder, offset int64) {
  250. frb := r.getOrCreateBlock(topic, partition)
  251. kb, vb := encodeKV(key, value)
  252. rec := &Record{Key: kb, Value: vb, OffsetDelta: offset}
  253. batch := frb.Records.recordBatch
  254. if batch == nil {
  255. batch = &RecordBatch{Version: 2}
  256. frb.Records = newDefaultRecords(batch)
  257. }
  258. batch.addRecord(rec)
  259. }
  260. func (r *FetchResponse) SetLastStableOffset(topic string, partition int32, offset int64) {
  261. frb := r.getOrCreateBlock(topic, partition)
  262. frb.LastStableOffset = offset
  263. }