offset_request.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package sarama
  2. type offsetRequestBlock struct {
  3. // currentLeaderEpoch contains the current leader epoch (used in version 4+).
  4. currentLeaderEpoch int32
  5. // timestamp contains the current timestamp.
  6. timestamp int64
  7. // maxNumOffsets contains the maximum number of offsets to report.
  8. maxNumOffsets int32 // Only used in version 0
  9. }
  10. func (b *offsetRequestBlock) encode(pe packetEncoder, version int16) error {
  11. if version >= 4 {
  12. pe.putInt32(b.currentLeaderEpoch)
  13. }
  14. pe.putInt64(b.timestamp)
  15. if version == 0 {
  16. pe.putInt32(b.maxNumOffsets)
  17. }
  18. return nil
  19. }
  20. func (b *offsetRequestBlock) decode(pd packetDecoder, version int16) (err error) {
  21. b.currentLeaderEpoch = -1
  22. if version >= 4 {
  23. if b.currentLeaderEpoch, err = pd.getInt32(); err != nil {
  24. return err
  25. }
  26. }
  27. if b.timestamp, err = pd.getInt64(); err != nil {
  28. return err
  29. }
  30. if version == 0 {
  31. if b.maxNumOffsets, err = pd.getInt32(); err != nil {
  32. return err
  33. }
  34. }
  35. return nil
  36. }
  37. type OffsetRequest struct {
  38. Version int16
  39. IsolationLevel IsolationLevel
  40. replicaID int32
  41. isReplicaIDSet bool
  42. blocks map[string]map[int32]*offsetRequestBlock
  43. }
  44. func (r *OffsetRequest) encode(pe packetEncoder) error {
  45. if r.isReplicaIDSet {
  46. pe.putInt32(r.replicaID)
  47. } else {
  48. // default replica ID is always -1 for clients
  49. pe.putInt32(-1)
  50. }
  51. if r.Version >= 2 {
  52. pe.putBool(r.IsolationLevel == ReadCommitted)
  53. }
  54. err := pe.putArrayLength(len(r.blocks))
  55. if err != nil {
  56. return err
  57. }
  58. for topic, partitions := range r.blocks {
  59. err = pe.putString(topic)
  60. if err != nil {
  61. return err
  62. }
  63. err = pe.putArrayLength(len(partitions))
  64. if err != nil {
  65. return err
  66. }
  67. for partition, block := range partitions {
  68. pe.putInt32(partition)
  69. if err = block.encode(pe, r.Version); err != nil {
  70. return err
  71. }
  72. }
  73. }
  74. return nil
  75. }
  76. func (r *OffsetRequest) decode(pd packetDecoder, version int16) error {
  77. r.Version = version
  78. replicaID, err := pd.getInt32()
  79. if err != nil {
  80. return err
  81. }
  82. if replicaID >= 0 {
  83. r.SetReplicaID(replicaID)
  84. }
  85. if r.Version >= 2 {
  86. tmp, err := pd.getBool()
  87. if err != nil {
  88. return err
  89. }
  90. r.IsolationLevel = ReadUncommitted
  91. if tmp {
  92. r.IsolationLevel = ReadCommitted
  93. }
  94. }
  95. blockCount, err := pd.getArrayLength()
  96. if err != nil {
  97. return err
  98. }
  99. if blockCount == 0 {
  100. return nil
  101. }
  102. r.blocks = make(map[string]map[int32]*offsetRequestBlock)
  103. for i := 0; i < blockCount; i++ {
  104. topic, err := pd.getString()
  105. if err != nil {
  106. return err
  107. }
  108. partitionCount, err := pd.getArrayLength()
  109. if err != nil {
  110. return err
  111. }
  112. r.blocks[topic] = make(map[int32]*offsetRequestBlock)
  113. for j := 0; j < partitionCount; j++ {
  114. partition, err := pd.getInt32()
  115. if err != nil {
  116. return err
  117. }
  118. block := &offsetRequestBlock{}
  119. if err := block.decode(pd, version); err != nil {
  120. return err
  121. }
  122. r.blocks[topic][partition] = block
  123. }
  124. }
  125. return nil
  126. }
  127. func (r *OffsetRequest) key() int16 {
  128. return 2
  129. }
  130. func (r *OffsetRequest) version() int16 {
  131. return r.Version
  132. }
  133. func (r *OffsetRequest) headerVersion() int16 {
  134. return 1
  135. }
  136. func (r *OffsetRequest) isValidVersion() bool {
  137. return r.Version >= 0 && r.Version <= 4
  138. }
  139. func (r *OffsetRequest) requiredVersion() KafkaVersion {
  140. switch r.Version {
  141. case 4:
  142. return V2_1_0_0
  143. case 3:
  144. return V2_0_0_0
  145. case 2:
  146. return V0_11_0_0
  147. case 1:
  148. return V0_10_1_0
  149. case 0:
  150. return V0_8_2_0
  151. default:
  152. return V2_0_0_0
  153. }
  154. }
  155. func (r *OffsetRequest) SetReplicaID(id int32) {
  156. r.replicaID = id
  157. r.isReplicaIDSet = true
  158. }
  159. func (r *OffsetRequest) ReplicaID() int32 {
  160. if r.isReplicaIDSet {
  161. return r.replicaID
  162. }
  163. return -1
  164. }
  165. func (r *OffsetRequest) AddBlock(topic string, partitionID int32, timestamp int64, maxOffsets int32) {
  166. if r.blocks == nil {
  167. r.blocks = make(map[string]map[int32]*offsetRequestBlock)
  168. }
  169. if r.blocks[topic] == nil {
  170. r.blocks[topic] = make(map[int32]*offsetRequestBlock)
  171. }
  172. tmp := new(offsetRequestBlock)
  173. tmp.currentLeaderEpoch = -1
  174. tmp.timestamp = timestamp
  175. if r.Version == 0 {
  176. tmp.maxNumOffsets = maxOffsets
  177. }
  178. r.blocks[topic][partitionID] = tmp
  179. }