fetch_request.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. type FetchRequest struct {
  21. MaxWaitTime int32
  22. MinBytes int32
  23. Version int16
  24. blocks map[string]map[int32]*fetchRequestBlock
  25. }
  26. func (r *FetchRequest) encode(pe packetEncoder) (err error) {
  27. pe.putInt32(-1) // replica ID is always -1 for clients
  28. pe.putInt32(r.MaxWaitTime)
  29. pe.putInt32(r.MinBytes)
  30. err = pe.putArrayLength(len(r.blocks))
  31. if err != nil {
  32. return err
  33. }
  34. for topic, blocks := range r.blocks {
  35. err = pe.putString(topic)
  36. if err != nil {
  37. return err
  38. }
  39. err = pe.putArrayLength(len(blocks))
  40. if err != nil {
  41. return err
  42. }
  43. for partition, block := range blocks {
  44. pe.putInt32(partition)
  45. err = block.encode(pe)
  46. if err != nil {
  47. return err
  48. }
  49. }
  50. }
  51. return nil
  52. }
  53. func (r *FetchRequest) decode(pd packetDecoder, version int16) (err error) {
  54. r.Version = version
  55. if _, err = pd.getInt32(); err != nil {
  56. return err
  57. }
  58. if r.MaxWaitTime, err = pd.getInt32(); err != nil {
  59. return err
  60. }
  61. if r.MinBytes, err = pd.getInt32(); err != nil {
  62. return err
  63. }
  64. topicCount, err := pd.getArrayLength()
  65. if err != nil {
  66. return err
  67. }
  68. if topicCount == 0 {
  69. return nil
  70. }
  71. r.blocks = make(map[string]map[int32]*fetchRequestBlock)
  72. for i := 0; i < topicCount; i++ {
  73. topic, err := pd.getString()
  74. if err != nil {
  75. return err
  76. }
  77. partitionCount, err := pd.getArrayLength()
  78. if err != nil {
  79. return err
  80. }
  81. r.blocks[topic] = make(map[int32]*fetchRequestBlock)
  82. for j := 0; j < partitionCount; j++ {
  83. partition, err := pd.getInt32()
  84. if err != nil {
  85. return err
  86. }
  87. fetchBlock := &fetchRequestBlock{}
  88. if err = fetchBlock.decode(pd); err != nil {
  89. return err
  90. }
  91. r.blocks[topic][partition] = fetchBlock
  92. }
  93. }
  94. return nil
  95. }
  96. func (r *FetchRequest) key() int16 {
  97. return 1
  98. }
  99. func (r *FetchRequest) version() int16 {
  100. return r.Version
  101. }
  102. func (r *FetchRequest) requiredVersion() KafkaVersion {
  103. switch r.Version {
  104. case 1:
  105. return V0_9_0_0
  106. case 2:
  107. return V0_10_0_0
  108. default:
  109. return minVersion
  110. }
  111. }
  112. func (r *FetchRequest) AddBlock(topic string, partitionID int32, fetchOffset int64, maxBytes int32) {
  113. if r.blocks == nil {
  114. r.blocks = make(map[string]map[int32]*fetchRequestBlock)
  115. }
  116. if r.blocks[topic] == nil {
  117. r.blocks[topic] = make(map[int32]*fetchRequestBlock)
  118. }
  119. tmp := new(fetchRequestBlock)
  120. tmp.maxBytes = maxBytes
  121. tmp.fetchOffset = fetchOffset
  122. r.blocks[topic][partitionID] = tmp
  123. }