fetch_request.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 IsolationLevel
  29. blocks map[string]map[int32]*fetchRequestBlock
  30. }
  31. type IsolationLevel int8
  32. const (
  33. ReadUncommitted IsolationLevel = 0
  34. ReadCommitted IsolationLevel = 1
  35. )
  36. func (r *FetchRequest) encode(pe packetEncoder) (err error) {
  37. pe.putInt32(-1) // replica ID is always -1 for clients
  38. pe.putInt32(r.MaxWaitTime)
  39. pe.putInt32(r.MinBytes)
  40. if r.Version >= 3 {
  41. pe.putInt32(r.MaxBytes)
  42. }
  43. if r.Version >= 4 {
  44. pe.putInt8(int8(r.Isolation))
  45. }
  46. err = pe.putArrayLength(len(r.blocks))
  47. if err != nil {
  48. return err
  49. }
  50. for topic, blocks := range r.blocks {
  51. err = pe.putString(topic)
  52. if err != nil {
  53. return err
  54. }
  55. err = pe.putArrayLength(len(blocks))
  56. if err != nil {
  57. return err
  58. }
  59. for partition, block := range blocks {
  60. pe.putInt32(partition)
  61. err = block.encode(pe)
  62. if err != nil {
  63. return err
  64. }
  65. }
  66. }
  67. return nil
  68. }
  69. func (r *FetchRequest) decode(pd packetDecoder, version int16) (err error) {
  70. r.Version = version
  71. if _, err = pd.getInt32(); err != nil {
  72. return err
  73. }
  74. if r.MaxWaitTime, err = pd.getInt32(); err != nil {
  75. return err
  76. }
  77. if r.MinBytes, err = pd.getInt32(); err != nil {
  78. return err
  79. }
  80. if r.Version >= 3 {
  81. if r.MaxBytes, err = pd.getInt32(); err != nil {
  82. return err
  83. }
  84. }
  85. if r.Version >= 4 {
  86. isolation, err := pd.getInt8()
  87. if err != nil {
  88. return err
  89. }
  90. r.Isolation = IsolationLevel(isolation)
  91. }
  92. topicCount, err := pd.getArrayLength()
  93. if err != nil {
  94. return err
  95. }
  96. if topicCount == 0 {
  97. return nil
  98. }
  99. r.blocks = make(map[string]map[int32]*fetchRequestBlock)
  100. for i := 0; i < topicCount; i++ {
  101. topic, err := pd.getString()
  102. if err != nil {
  103. return err
  104. }
  105. partitionCount, err := pd.getArrayLength()
  106. if err != nil {
  107. return err
  108. }
  109. r.blocks[topic] = make(map[int32]*fetchRequestBlock)
  110. for j := 0; j < partitionCount; j++ {
  111. partition, err := pd.getInt32()
  112. if err != nil {
  113. return err
  114. }
  115. fetchBlock := &fetchRequestBlock{}
  116. if err = fetchBlock.decode(pd); err != nil {
  117. return err
  118. }
  119. r.blocks[topic][partition] = fetchBlock
  120. }
  121. }
  122. return nil
  123. }
  124. func (r *FetchRequest) key() int16 {
  125. return 1
  126. }
  127. func (r *FetchRequest) version() int16 {
  128. return r.Version
  129. }
  130. func (r *FetchRequest) requiredVersion() KafkaVersion {
  131. switch r.Version {
  132. case 1:
  133. return V0_9_0_0
  134. case 2:
  135. return V0_10_0_0
  136. case 3:
  137. return V0_10_1_0
  138. case 4:
  139. return V0_11_0_0
  140. default:
  141. return MinVersion
  142. }
  143. }
  144. func (r *FetchRequest) AddBlock(topic string, partitionID int32, fetchOffset int64, maxBytes int32) {
  145. if r.blocks == nil {
  146. r.blocks = make(map[string]map[int32]*fetchRequestBlock)
  147. }
  148. if r.blocks[topic] == nil {
  149. r.blocks[topic] = make(map[int32]*fetchRequestBlock)
  150. }
  151. tmp := new(fetchRequestBlock)
  152. tmp.maxBytes = maxBytes
  153. tmp.fetchOffset = fetchOffset
  154. r.blocks[topic][partitionID] = tmp
  155. }