offset_request.go 2.9 KB

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