offset_request.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package sarama
  2. type offsetRequestBlock struct {
  3. time int64
  4. maxOffsets int32 // Only used in version 0
  5. }
  6. func (b *offsetRequestBlock) encode(pe packetEncoder, version int16) error {
  7. pe.putInt64(int64(b.time))
  8. if version == 0 {
  9. pe.putInt32(b.maxOffsets)
  10. }
  11. return nil
  12. }
  13. func (b *offsetRequestBlock) decode(pd packetDecoder, version int16) (err error) {
  14. if b.time, err = pd.getInt64(); err != nil {
  15. return err
  16. }
  17. if version == 0 {
  18. if b.maxOffsets, err = pd.getInt32(); err != nil {
  19. return err
  20. }
  21. }
  22. return nil
  23. }
  24. type OffsetRequest struct {
  25. Version int16
  26. blocks map[string]map[int32]*offsetRequestBlock
  27. }
  28. func (r *OffsetRequest) encode(pe packetEncoder) error {
  29. pe.putInt32(-1) // replica ID is always -1 for clients
  30. err := pe.putArrayLength(len(r.blocks))
  31. if err != nil {
  32. return err
  33. }
  34. for topic, partitions := range r.blocks {
  35. err = pe.putString(topic)
  36. if err != nil {
  37. return err
  38. }
  39. err = pe.putArrayLength(len(partitions))
  40. if err != nil {
  41. return err
  42. }
  43. for partition, block := range partitions {
  44. pe.putInt32(partition)
  45. if err = block.encode(pe, r.Version); err != nil {
  46. return err
  47. }
  48. }
  49. }
  50. return nil
  51. }
  52. func (r *OffsetRequest) decode(pd packetDecoder, version int16) error {
  53. r.Version = version
  54. // Ignore replica ID
  55. if _, err := pd.getInt32(); err != nil {
  56. return err
  57. }
  58. blockCount, err := pd.getArrayLength()
  59. if err != nil {
  60. return err
  61. }
  62. if blockCount == 0 {
  63. return nil
  64. }
  65. r.blocks = make(map[string]map[int32]*offsetRequestBlock)
  66. for i := 0; i < blockCount; i++ {
  67. topic, err := pd.getString()
  68. if err != nil {
  69. return err
  70. }
  71. partitionCount, err := pd.getArrayLength()
  72. if err != nil {
  73. return err
  74. }
  75. r.blocks[topic] = make(map[int32]*offsetRequestBlock)
  76. for j := 0; j < partitionCount; j++ {
  77. partition, err := pd.getInt32()
  78. if err != nil {
  79. return err
  80. }
  81. block := &offsetRequestBlock{}
  82. if err := block.decode(pd, version); err != nil {
  83. return err
  84. }
  85. r.blocks[topic][partition] = block
  86. }
  87. }
  88. return nil
  89. }
  90. func (r *OffsetRequest) key() int16 {
  91. return 2
  92. }
  93. func (r *OffsetRequest) version() int16 {
  94. return r.Version
  95. }
  96. func (r *OffsetRequest) requiredVersion() KafkaVersion {
  97. switch r.Version {
  98. case 1:
  99. return V0_10_1_0
  100. default:
  101. return MinVersion
  102. }
  103. }
  104. func (r *OffsetRequest) AddBlock(topic string, partitionID int32, time int64, maxOffsets int32) {
  105. if r.blocks == nil {
  106. r.blocks = make(map[string]map[int32]*offsetRequestBlock)
  107. }
  108. if r.blocks[topic] == nil {
  109. r.blocks[topic] = make(map[int32]*offsetRequestBlock)
  110. }
  111. tmp := new(offsetRequestBlock)
  112. tmp.time = time
  113. if r.Version == 0 {
  114. tmp.maxOffsets = maxOffsets
  115. }
  116. r.blocks[topic][partitionID] = tmp
  117. }