fetch_response.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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.RecordsSet
  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. n, err := records.numRecords()
  88. if err != nil {
  89. return err
  90. }
  91. if n > 0 || (partial && len(b.RecordsSet) == 0) {
  92. b.RecordsSet = append(b.RecordsSet, records)
  93. if b.Records == nil {
  94. b.Records = records
  95. }
  96. }
  97. overflow, err := records.isOverflow()
  98. if err != nil {
  99. return err
  100. }
  101. if partial || overflow {
  102. break
  103. }
  104. }
  105. return nil
  106. }
  107. func (b *FetchResponseBlock) numRecords() (int, error) {
  108. sum := 0
  109. for _, records := range b.RecordsSet {
  110. count, err := records.numRecords()
  111. if err != nil {
  112. return 0, err
  113. }
  114. sum += count
  115. }
  116. return sum, nil
  117. }
  118. func (b *FetchResponseBlock) isPartial() (bool, error) {
  119. if b.Partial {
  120. return true, nil
  121. }
  122. if len(b.RecordsSet) == 1 {
  123. return b.RecordsSet[0].isPartial()
  124. }
  125. return false, nil
  126. }
  127. func (b *FetchResponseBlock) encode(pe packetEncoder, version int16) (err error) {
  128. pe.putInt16(int16(b.Err))
  129. pe.putInt64(b.HighWaterMarkOffset)
  130. if version >= 4 {
  131. pe.putInt64(b.LastStableOffset)
  132. if err = pe.putArrayLength(len(b.AbortedTransactions)); err != nil {
  133. return err
  134. }
  135. for _, transact := range b.AbortedTransactions {
  136. if err = transact.encode(pe); err != nil {
  137. return err
  138. }
  139. }
  140. }
  141. pe.push(&lengthField{})
  142. for _, records := range b.RecordsSet {
  143. err = records.encode(pe)
  144. if err != nil {
  145. return err
  146. }
  147. }
  148. return pe.pop()
  149. }
  150. type FetchResponse struct {
  151. Blocks map[string]map[int32]*FetchResponseBlock
  152. ThrottleTime time.Duration
  153. Version int16 // v1 requires 0.9+, v2 requires 0.10+
  154. }
  155. func (r *FetchResponse) decode(pd packetDecoder, version int16) (err error) {
  156. r.Version = version
  157. if r.Version >= 1 {
  158. throttle, err := pd.getInt32()
  159. if err != nil {
  160. return err
  161. }
  162. r.ThrottleTime = time.Duration(throttle) * time.Millisecond
  163. }
  164. numTopics, err := pd.getArrayLength()
  165. if err != nil {
  166. return err
  167. }
  168. r.Blocks = make(map[string]map[int32]*FetchResponseBlock, numTopics)
  169. for i := 0; i < numTopics; i++ {
  170. name, err := pd.getString()
  171. if err != nil {
  172. return err
  173. }
  174. numBlocks, err := pd.getArrayLength()
  175. if err != nil {
  176. return err
  177. }
  178. r.Blocks[name] = make(map[int32]*FetchResponseBlock, numBlocks)
  179. for j := 0; j < numBlocks; j++ {
  180. id, err := pd.getInt32()
  181. if err != nil {
  182. return err
  183. }
  184. block := new(FetchResponseBlock)
  185. err = block.decode(pd, version)
  186. if err != nil {
  187. return err
  188. }
  189. r.Blocks[name][id] = block
  190. }
  191. }
  192. return nil
  193. }
  194. func (r *FetchResponse) encode(pe packetEncoder) (err error) {
  195. if r.Version >= 1 {
  196. pe.putInt32(int32(r.ThrottleTime / time.Millisecond))
  197. }
  198. err = pe.putArrayLength(len(r.Blocks))
  199. if err != nil {
  200. return err
  201. }
  202. for topic, partitions := range r.Blocks {
  203. err = pe.putString(topic)
  204. if err != nil {
  205. return err
  206. }
  207. err = pe.putArrayLength(len(partitions))
  208. if err != nil {
  209. return err
  210. }
  211. for id, block := range partitions {
  212. pe.putInt32(id)
  213. err = block.encode(pe, r.Version)
  214. if err != nil {
  215. return err
  216. }
  217. }
  218. }
  219. return nil
  220. }
  221. func (r *FetchResponse) key() int16 {
  222. return 1
  223. }
  224. func (r *FetchResponse) version() int16 {
  225. return r.Version
  226. }
  227. func (r *FetchResponse) requiredVersion() KafkaVersion {
  228. switch r.Version {
  229. case 1:
  230. return V0_9_0_0
  231. case 2:
  232. return V0_10_0_0
  233. case 3:
  234. return V0_10_1_0
  235. case 4:
  236. return V0_11_0_0
  237. default:
  238. return MinVersion
  239. }
  240. }
  241. func (r *FetchResponse) GetBlock(topic string, partition int32) *FetchResponseBlock {
  242. if r.Blocks == nil {
  243. return nil
  244. }
  245. if r.Blocks[topic] == nil {
  246. return nil
  247. }
  248. return r.Blocks[topic][partition]
  249. }
  250. func (r *FetchResponse) AddError(topic string, partition int32, err KError) {
  251. if r.Blocks == nil {
  252. r.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  253. }
  254. partitions, ok := r.Blocks[topic]
  255. if !ok {
  256. partitions = make(map[int32]*FetchResponseBlock)
  257. r.Blocks[topic] = partitions
  258. }
  259. frb, ok := partitions[partition]
  260. if !ok {
  261. frb = new(FetchResponseBlock)
  262. partitions[partition] = frb
  263. }
  264. frb.Err = err
  265. }
  266. func (r *FetchResponse) getOrCreateBlock(topic string, partition int32) *FetchResponseBlock {
  267. if r.Blocks == nil {
  268. r.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  269. }
  270. partitions, ok := r.Blocks[topic]
  271. if !ok {
  272. partitions = make(map[int32]*FetchResponseBlock)
  273. r.Blocks[topic] = partitions
  274. }
  275. frb, ok := partitions[partition]
  276. if !ok {
  277. frb = new(FetchResponseBlock)
  278. partitions[partition] = frb
  279. }
  280. return frb
  281. }
  282. func encodeKV(key, value Encoder) ([]byte, []byte) {
  283. var kb []byte
  284. var vb []byte
  285. if key != nil {
  286. kb, _ = key.Encode()
  287. }
  288. if value != nil {
  289. vb, _ = value.Encode()
  290. }
  291. return kb, vb
  292. }
  293. func (r *FetchResponse) AddMessage(topic string, partition int32, key, value Encoder, offset int64) {
  294. frb := r.getOrCreateBlock(topic, partition)
  295. kb, vb := encodeKV(key, value)
  296. msg := &Message{Key: kb, Value: vb}
  297. msgBlock := &MessageBlock{Msg: msg, Offset: offset}
  298. if len(frb.RecordsSet) == 0 {
  299. records := newLegacyRecords(&MessageSet{})
  300. frb.RecordsSet = []*Records{&records}
  301. }
  302. set := frb.RecordsSet[0].MsgSet
  303. set.Messages = append(set.Messages, msgBlock)
  304. }
  305. func (r *FetchResponse) AddRecord(topic string, partition int32, key, value Encoder, offset int64) {
  306. frb := r.getOrCreateBlock(topic, partition)
  307. kb, vb := encodeKV(key, value)
  308. rec := &Record{Key: kb, Value: vb, OffsetDelta: offset}
  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.addRecord(rec)
  315. }
  316. func (r *FetchResponse) SetLastOffsetDelta(topic string, partition int32, offset int32) {
  317. frb := r.getOrCreateBlock(topic, partition)
  318. if len(frb.RecordsSet) == 0 {
  319. records := newDefaultRecords(&RecordBatch{Version: 2})
  320. frb.RecordsSet = []*Records{&records}
  321. }
  322. batch := frb.RecordsSet[0].RecordBatch
  323. batch.LastOffsetDelta = offset
  324. }
  325. func (r *FetchResponse) SetLastStableOffset(topic string, partition int32, offset int64) {
  326. frb := r.getOrCreateBlock(topic, partition)
  327. frb.LastStableOffset = offset
  328. }