fetch_response.go 8.6 KB

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