offset_fetch_request.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package sarama
  2. type OffsetFetchRequest struct {
  3. Version int16
  4. ConsumerGroup string
  5. RequireStable bool // requires v7+
  6. partitions map[string][]int32
  7. }
  8. func NewOffsetFetchRequest(
  9. version KafkaVersion,
  10. group string,
  11. partitions map[string][]int32,
  12. ) *OffsetFetchRequest {
  13. request := &OffsetFetchRequest{
  14. ConsumerGroup: group,
  15. partitions: partitions,
  16. }
  17. if version.IsAtLeast(V2_5_0_0) {
  18. // Version 7 is adding the require stable flag.
  19. request.Version = 7
  20. } else if version.IsAtLeast(V2_4_0_0) {
  21. // Version 6 is the first flexible version.
  22. request.Version = 6
  23. } else if version.IsAtLeast(V2_1_0_0) {
  24. // Version 3, 4, and 5 are the same as version 2.
  25. request.Version = 5
  26. } else if version.IsAtLeast(V2_0_0_0) {
  27. request.Version = 4
  28. } else if version.IsAtLeast(V0_11_0_0) {
  29. request.Version = 3
  30. } else if version.IsAtLeast(V0_10_2_0) {
  31. // Starting in version 2, the request can contain a null topics array to indicate that offsets
  32. // for all topics should be fetched. It also returns a top level error code
  33. // for group or coordinator level errors.
  34. request.Version = 2
  35. } else if version.IsAtLeast(V0_8_2_0) {
  36. // In version 0, the request read offsets from ZK.
  37. //
  38. // Starting in version 1, the broker supports fetching offsets from the internal __consumer_offsets topic.
  39. request.Version = 1
  40. }
  41. return request
  42. }
  43. func (r *OffsetFetchRequest) encode(pe packetEncoder) (err error) {
  44. if r.Version < 0 || r.Version > 7 {
  45. return PacketEncodingError{"invalid or unsupported OffsetFetchRequest version field"}
  46. }
  47. isFlexible := r.Version >= 6
  48. if isFlexible {
  49. err = pe.putCompactString(r.ConsumerGroup)
  50. } else {
  51. err = pe.putString(r.ConsumerGroup)
  52. }
  53. if err != nil {
  54. return err
  55. }
  56. if isFlexible {
  57. if r.partitions == nil {
  58. pe.putUVarint(0)
  59. } else {
  60. pe.putCompactArrayLength(len(r.partitions))
  61. }
  62. } else {
  63. if r.partitions == nil && r.Version >= 2 {
  64. pe.putInt32(-1)
  65. } else {
  66. if err = pe.putArrayLength(len(r.partitions)); err != nil {
  67. return err
  68. }
  69. }
  70. }
  71. for topic, partitions := range r.partitions {
  72. if isFlexible {
  73. err = pe.putCompactString(topic)
  74. } else {
  75. err = pe.putString(topic)
  76. }
  77. if err != nil {
  78. return err
  79. }
  80. //
  81. if isFlexible {
  82. err = pe.putCompactInt32Array(partitions)
  83. } else {
  84. err = pe.putInt32Array(partitions)
  85. }
  86. if err != nil {
  87. return err
  88. }
  89. if isFlexible {
  90. pe.putEmptyTaggedFieldArray()
  91. }
  92. }
  93. if r.RequireStable && r.Version < 7 {
  94. return PacketEncodingError{"requireStable is not supported. use version 7 or later"}
  95. }
  96. if r.Version >= 7 {
  97. pe.putBool(r.RequireStable)
  98. }
  99. if isFlexible {
  100. pe.putEmptyTaggedFieldArray()
  101. }
  102. return nil
  103. }
  104. func (r *OffsetFetchRequest) decode(pd packetDecoder, version int16) (err error) {
  105. r.Version = version
  106. isFlexible := r.Version >= 6
  107. if isFlexible {
  108. r.ConsumerGroup, err = pd.getCompactString()
  109. } else {
  110. r.ConsumerGroup, err = pd.getString()
  111. }
  112. if err != nil {
  113. return err
  114. }
  115. var partitionCount int
  116. if isFlexible {
  117. partitionCount, err = pd.getCompactArrayLength()
  118. } else {
  119. partitionCount, err = pd.getArrayLength()
  120. }
  121. if err != nil {
  122. return err
  123. }
  124. if (partitionCount == 0 && version < 2) || partitionCount < 0 {
  125. return nil
  126. }
  127. r.partitions = make(map[string][]int32, partitionCount)
  128. for i := 0; i < partitionCount; i++ {
  129. var topic string
  130. if isFlexible {
  131. topic, err = pd.getCompactString()
  132. } else {
  133. topic, err = pd.getString()
  134. }
  135. if err != nil {
  136. return err
  137. }
  138. var partitions []int32
  139. if isFlexible {
  140. partitions, err = pd.getCompactInt32Array()
  141. } else {
  142. partitions, err = pd.getInt32Array()
  143. }
  144. if err != nil {
  145. return err
  146. }
  147. if isFlexible {
  148. _, err = pd.getEmptyTaggedFieldArray()
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. r.partitions[topic] = partitions
  154. }
  155. if r.Version >= 7 {
  156. r.RequireStable, err = pd.getBool()
  157. if err != nil {
  158. return err
  159. }
  160. }
  161. if isFlexible {
  162. _, err = pd.getEmptyTaggedFieldArray()
  163. if err != nil {
  164. return err
  165. }
  166. }
  167. return nil
  168. }
  169. func (r *OffsetFetchRequest) key() int16 {
  170. return 9
  171. }
  172. func (r *OffsetFetchRequest) version() int16 {
  173. return r.Version
  174. }
  175. func (r *OffsetFetchRequest) headerVersion() int16 {
  176. if r.Version >= 6 {
  177. return 2
  178. }
  179. return 1
  180. }
  181. func (r *OffsetFetchRequest) isValidVersion() bool {
  182. return r.Version >= 0 && r.Version <= 7
  183. }
  184. func (r *OffsetFetchRequest) requiredVersion() KafkaVersion {
  185. switch r.Version {
  186. case 7:
  187. return V2_5_0_0
  188. case 6:
  189. return V2_4_0_0
  190. case 5:
  191. return V2_1_0_0
  192. case 4:
  193. return V2_0_0_0
  194. case 3:
  195. return V0_11_0_0
  196. case 2:
  197. return V0_10_2_0
  198. case 1:
  199. return V0_8_2_0
  200. case 0:
  201. return V0_8_2_0
  202. default:
  203. return V2_5_0_0
  204. }
  205. }
  206. func (r *OffsetFetchRequest) ZeroPartitions() {
  207. if r.partitions == nil && r.Version >= 2 {
  208. r.partitions = make(map[string][]int32)
  209. }
  210. }
  211. func (r *OffsetFetchRequest) AddPartition(topic string, partitionID int32) {
  212. if r.partitions == nil {
  213. r.partitions = make(map[string][]int32)
  214. }
  215. r.partitions[topic] = append(r.partitions[topic], partitionID)
  216. }