fetch_response.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. func (r *FetchResponseBlock) IsAborted(offset int64) bool {
  151. for _, abortedTransaction := range r.AbortedTransactions {
  152. if abortedTransaction != nil && abortedTransaction.FirstOffset == offset {
  153. return true
  154. }
  155. }
  156. return false
  157. }
  158. type FetchResponse struct {
  159. Blocks map[string]map[int32]*FetchResponseBlock
  160. ThrottleTime time.Duration
  161. Version int16 // v1 requires 0.9+, v2 requires 0.10+
  162. LogAppendTime bool
  163. Timestamp time.Time
  164. }
  165. func (r *FetchResponse) decode(pd packetDecoder, version int16) (err error) {
  166. r.Version = version
  167. if r.Version >= 1 {
  168. throttle, err := pd.getInt32()
  169. if err != nil {
  170. return err
  171. }
  172. r.ThrottleTime = time.Duration(throttle) * time.Millisecond
  173. }
  174. numTopics, err := pd.getArrayLength()
  175. if err != nil {
  176. return err
  177. }
  178. r.Blocks = make(map[string]map[int32]*FetchResponseBlock, numTopics)
  179. for i := 0; i < numTopics; i++ {
  180. name, err := pd.getString()
  181. if err != nil {
  182. return err
  183. }
  184. numBlocks, err := pd.getArrayLength()
  185. if err != nil {
  186. return err
  187. }
  188. r.Blocks[name] = make(map[int32]*FetchResponseBlock, numBlocks)
  189. for j := 0; j < numBlocks; j++ {
  190. id, err := pd.getInt32()
  191. if err != nil {
  192. return err
  193. }
  194. block := new(FetchResponseBlock)
  195. err = block.decode(pd, version)
  196. if err != nil {
  197. return err
  198. }
  199. r.Blocks[name][id] = block
  200. }
  201. }
  202. return nil
  203. }
  204. func (r *FetchResponse) encode(pe packetEncoder) (err error) {
  205. if r.Version >= 1 {
  206. pe.putInt32(int32(r.ThrottleTime / time.Millisecond))
  207. }
  208. err = pe.putArrayLength(len(r.Blocks))
  209. if err != nil {
  210. return err
  211. }
  212. for topic, partitions := range r.Blocks {
  213. err = pe.putString(topic)
  214. if err != nil {
  215. return err
  216. }
  217. err = pe.putArrayLength(len(partitions))
  218. if err != nil {
  219. return err
  220. }
  221. for id, block := range partitions {
  222. pe.putInt32(id)
  223. err = block.encode(pe, r.Version)
  224. if err != nil {
  225. return err
  226. }
  227. }
  228. }
  229. return nil
  230. }
  231. func (r *FetchResponse) key() int16 {
  232. return 1
  233. }
  234. func (r *FetchResponse) version() int16 {
  235. return r.Version
  236. }
  237. func (r *FetchResponse) requiredVersion() KafkaVersion {
  238. switch r.Version {
  239. case 1:
  240. return V0_9_0_0
  241. case 2:
  242. return V0_10_0_0
  243. case 3:
  244. return V0_10_1_0
  245. case 4:
  246. return V0_11_0_0
  247. default:
  248. return MinVersion
  249. }
  250. }
  251. func (r *FetchResponse) GetBlock(topic string, partition int32) *FetchResponseBlock {
  252. if r.Blocks == nil {
  253. return nil
  254. }
  255. if r.Blocks[topic] == nil {
  256. return nil
  257. }
  258. return r.Blocks[topic][partition]
  259. }
  260. func (r *FetchResponse) AddError(topic string, partition int32, err KError) {
  261. if r.Blocks == nil {
  262. r.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  263. }
  264. partitions, ok := r.Blocks[topic]
  265. if !ok {
  266. partitions = make(map[int32]*FetchResponseBlock)
  267. r.Blocks[topic] = partitions
  268. }
  269. frb, ok := partitions[partition]
  270. if !ok {
  271. frb = new(FetchResponseBlock)
  272. partitions[partition] = frb
  273. }
  274. frb.Err = err
  275. }
  276. func (r *FetchResponse) getOrCreateBlock(topic string, partition int32) *FetchResponseBlock {
  277. if r.Blocks == nil {
  278. r.Blocks = make(map[string]map[int32]*FetchResponseBlock)
  279. }
  280. partitions, ok := r.Blocks[topic]
  281. if !ok {
  282. partitions = make(map[int32]*FetchResponseBlock)
  283. r.Blocks[topic] = partitions
  284. }
  285. frb, ok := partitions[partition]
  286. if !ok {
  287. frb = new(FetchResponseBlock)
  288. partitions[partition] = frb
  289. }
  290. return frb
  291. }
  292. func encodeKV(key, value Encoder) ([]byte, []byte) {
  293. var kb []byte
  294. var vb []byte
  295. if key != nil {
  296. kb, _ = key.Encode()
  297. }
  298. if value != nil {
  299. vb, _ = value.Encode()
  300. }
  301. return kb, vb
  302. }
  303. func (r *FetchResponse) AddMessageWithTimestamp(topic string, partition int32, key, value Encoder, offset int64, timestamp time.Time, version int8) {
  304. frb := r.getOrCreateBlock(topic, partition)
  305. kb, vb := encodeKV(key, value)
  306. if r.LogAppendTime {
  307. timestamp = r.Timestamp
  308. }
  309. msg := &Message{Key: kb, Value: vb, LogAppendTime: r.LogAppendTime, Timestamp: timestamp, Version: version}
  310. msgBlock := &MessageBlock{Msg: msg, Offset: offset}
  311. if len(frb.RecordsSet) == 0 {
  312. records := newLegacyRecords(&MessageSet{})
  313. frb.RecordsSet = []*Records{&records}
  314. }
  315. set := frb.RecordsSet[0].MsgSet
  316. set.Messages = append(set.Messages, msgBlock)
  317. }
  318. func (r *FetchResponse) AddRecordWithTimestamp(topic string, partition int32, key, value Encoder, offset int64, timestamp time.Time) {
  319. frb := r.getOrCreateBlock(topic, partition)
  320. kb, vb := encodeKV(key, value)
  321. if len(frb.RecordsSet) == 0 {
  322. records := newDefaultRecords(&RecordBatch{Version: 2, LogAppendTime: r.LogAppendTime, FirstTimestamp: timestamp, MaxTimestamp: r.Timestamp})
  323. frb.RecordsSet = []*Records{&records}
  324. }
  325. batch := frb.RecordsSet[0].RecordBatch
  326. rec := &Record{Key: kb, Value: vb, OffsetDelta: offset, TimestampDelta: timestamp.Sub(batch.FirstTimestamp)}
  327. batch.addRecord(rec)
  328. }
  329. func (r *FetchResponse) AddMessage(topic string, partition int32, key, value Encoder, offset int64) {
  330. r.AddMessageWithTimestamp(topic, partition, key, value, offset, time.Time{}, 0)
  331. }
  332. func (r *FetchResponse) AddRecord(topic string, partition int32, key, value Encoder, offset int64) {
  333. r.AddRecordWithTimestamp(topic, partition, key, value, offset, time.Time{})
  334. }
  335. func (r *FetchResponse) SetLastOffsetDelta(topic string, partition int32, offset int32) {
  336. frb := r.getOrCreateBlock(topic, partition)
  337. if len(frb.RecordsSet) == 0 {
  338. records := newDefaultRecords(&RecordBatch{Version: 2})
  339. frb.RecordsSet = []*Records{&records}
  340. }
  341. batch := frb.RecordsSet[0].RecordBatch
  342. batch.LastOffsetDelta = offset
  343. }
  344. func (r *FetchResponse) SetLastStableOffset(topic string, partition int32, offset int64) {
  345. frb := r.getOrCreateBlock(topic, partition)
  346. frb.LastStableOffset = offset
  347. }