fetch_request.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package sarama
  2. type fetchRequestBlock struct {
  3. fetchOffset int64
  4. maxBytes int32
  5. }
  6. func (b *fetchRequestBlock) encode(pe packetEncoder) error {
  7. pe.putInt64(b.fetchOffset)
  8. pe.putInt32(b.maxBytes)
  9. return nil
  10. }
  11. func (b *fetchRequestBlock) decode(pd packetDecoder) (err error) {
  12. if b.fetchOffset, err = pd.getInt64(); err != nil {
  13. return err
  14. }
  15. if b.maxBytes, err = pd.getInt32(); err != nil {
  16. return err
  17. }
  18. return nil
  19. }
  20. // FetchRequest (API key 1) will fetch Kafka messages. Version 3 introduced the MaxBytes field. See
  21. // https://issues.apache.org/jira/browse/KAFKA-2063 for a discussion of the issues leading up to that. The KIP is at
  22. // https://cwiki.apache.org/confluence/display/KAFKA/KIP-74%3A+Add+Fetch+Response+Size+Limit+in+Bytes
  23. type FetchRequest struct {
  24. MaxWaitTime int32
  25. MinBytes int32
  26. MaxBytes int32
  27. Version int16
  28. Isolation int8
  29. blocks map[string]map[int32]*fetchRequestBlock
  30. }
  31. const (
  32. ReadUncommitted = 0
  33. ReadCommitted = 1
  34. )
  35. func (r *FetchRequest) encode(pe packetEncoder) (err error) {
  36. pe.putInt32(-1) // replica ID is always -1 for clients
  37. pe.putInt32(r.MaxWaitTime)
  38. pe.putInt32(r.MinBytes)
  39. if r.Version >= 3 {
  40. pe.putInt32(r.MaxBytes)
  41. }
  42. if r.Version >= 4 {
  43. pe.putInt8(r.Isolation)
  44. }
  45. err = pe.putArrayLength(len(r.blocks))
  46. if err != nil {
  47. return err
  48. }
  49. for topic, blocks := range r.blocks {
  50. err = pe.putString(topic)
  51. if err != nil {
  52. return err
  53. }
  54. err = pe.putArrayLength(len(blocks))
  55. if err != nil {
  56. return err
  57. }
  58. for partition, block := range blocks {
  59. pe.putInt32(partition)
  60. err = block.encode(pe)
  61. if err != nil {
  62. return err
  63. }
  64. }
  65. }
  66. return nil
  67. }
  68. func (r *FetchRequest) decode(pd packetDecoder, version int16) (err error) {
  69. r.Version = version
  70. if _, err = pd.getInt32(); err != nil {
  71. return err
  72. }
  73. if r.MaxWaitTime, err = pd.getInt32(); err != nil {
  74. return err
  75. }
  76. if r.MinBytes, err = pd.getInt32(); err != nil {
  77. return err
  78. }
  79. if r.Version >= 3 {
  80. if r.MaxBytes, err = pd.getInt32(); err != nil {
  81. return err
  82. }
  83. }
  84. if r.Version >= 4 {
  85. if r.Isolation, err = pd.getInt8(); err != nil {
  86. return err
  87. }
  88. }
  89. topicCount, err := pd.getArrayLength()
  90. if err != nil {
  91. return err
  92. }
  93. if topicCount == 0 {
  94. return nil
  95. }
  96. r.blocks = make(map[string]map[int32]*fetchRequestBlock)
  97. for i := 0; i < topicCount; i++ {
  98. topic, err := pd.getString()
  99. if err != nil {
  100. return err
  101. }
  102. partitionCount, err := pd.getArrayLength()
  103. if err != nil {
  104. return err
  105. }
  106. r.blocks[topic] = make(map[int32]*fetchRequestBlock)
  107. for j := 0; j < partitionCount; j++ {
  108. partition, err := pd.getInt32()
  109. if err != nil {
  110. return err
  111. }
  112. fetchBlock := &fetchRequestBlock{}
  113. if err = fetchBlock.decode(pd); err != nil {
  114. return err
  115. }
  116. r.blocks[topic][partition] = fetchBlock
  117. }
  118. }
  119. return nil
  120. }
  121. func (r *FetchRequest) key() int16 {
  122. return 1
  123. }
  124. func (r *FetchRequest) version() int16 {
  125. return r.Version
  126. }
  127. func (r *FetchRequest) requiredVersion() KafkaVersion {
  128. switch r.Version {
  129. case 1:
  130. return V0_9_0_0
  131. case 2:
  132. return V0_10_0_0
  133. case 3:
  134. return V0_10_1_0
  135. case 4:
  136. return V0_11_0_0
  137. default:
  138. return minVersion
  139. }
  140. }
  141. func (r *FetchRequest) AddBlock(topic string, partitionID int32, fetchOffset int64, maxBytes int32) {
  142. if r.blocks == nil {
  143. r.blocks = make(map[string]map[int32]*fetchRequestBlock)
  144. }
  145. if r.blocks[topic] == nil {
  146. r.blocks[topic] = make(map[int32]*fetchRequestBlock)
  147. }
  148. tmp := new(fetchRequestBlock)
  149. tmp.maxBytes = maxBytes
  150. tmp.fetchOffset = fetchOffset
  151. r.blocks[topic][partitionID] = tmp
  152. }