fetch_response.go 7.7 KB

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