join_group_request.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package sarama
  2. type GroupProtocol struct {
  3. // Name contains the protocol name.
  4. Name string
  5. // Metadata contains the protocol metadata.
  6. Metadata []byte
  7. }
  8. func (p *GroupProtocol) decode(pd packetDecoder) (err error) {
  9. p.Name, err = pd.getString()
  10. if err != nil {
  11. return err
  12. }
  13. p.Metadata, err = pd.getBytes()
  14. return err
  15. }
  16. func (p *GroupProtocol) encode(pe packetEncoder) (err error) {
  17. if err := pe.putString(p.Name); err != nil {
  18. return err
  19. }
  20. if err := pe.putBytes(p.Metadata); err != nil {
  21. return err
  22. }
  23. return nil
  24. }
  25. type JoinGroupRequest struct {
  26. // Version defines the protocol version to use for encode and decode
  27. Version int16
  28. // GroupId contains the group identifier.
  29. GroupId string
  30. // SessionTimeout specifies that the coordinator should consider the consumer
  31. // dead if it receives no heartbeat after this timeout in milliseconds.
  32. SessionTimeout int32
  33. // RebalanceTimeout contains the maximum time in milliseconds that the
  34. // coordinator will wait for each member to rejoin when rebalancing the
  35. // group.
  36. RebalanceTimeout int32
  37. // MemberId contains the member id assigned by the group coordinator.
  38. MemberId string
  39. // GroupInstanceId contains the unique identifier of the consumer instance
  40. // provided by end user.
  41. GroupInstanceId *string
  42. // ProtocolType contains the unique name the for class of protocols
  43. // implemented by the group we want to join.
  44. ProtocolType string
  45. // GroupProtocols contains the list of protocols that the member supports.
  46. // deprecated; use OrderedGroupProtocols
  47. GroupProtocols map[string][]byte
  48. // OrderedGroupProtocols contains an ordered list of protocols that the member
  49. // supports.
  50. OrderedGroupProtocols []*GroupProtocol
  51. }
  52. func (r *JoinGroupRequest) encode(pe packetEncoder) error {
  53. if err := pe.putString(r.GroupId); err != nil {
  54. return err
  55. }
  56. pe.putInt32(r.SessionTimeout)
  57. if r.Version >= 1 {
  58. pe.putInt32(r.RebalanceTimeout)
  59. }
  60. if err := pe.putString(r.MemberId); err != nil {
  61. return err
  62. }
  63. if r.Version >= 5 {
  64. if err := pe.putNullableString(r.GroupInstanceId); err != nil {
  65. return err
  66. }
  67. }
  68. if err := pe.putString(r.ProtocolType); err != nil {
  69. return err
  70. }
  71. if len(r.GroupProtocols) > 0 {
  72. if len(r.OrderedGroupProtocols) > 0 {
  73. return PacketDecodingError{"cannot specify both GroupProtocols and OrderedGroupProtocols on JoinGroupRequest"}
  74. }
  75. if err := pe.putArrayLength(len(r.GroupProtocols)); err != nil {
  76. return err
  77. }
  78. for name, metadata := range r.GroupProtocols {
  79. if err := pe.putString(name); err != nil {
  80. return err
  81. }
  82. if err := pe.putBytes(metadata); err != nil {
  83. return err
  84. }
  85. }
  86. } else {
  87. if err := pe.putArrayLength(len(r.OrderedGroupProtocols)); err != nil {
  88. return err
  89. }
  90. for _, protocol := range r.OrderedGroupProtocols {
  91. if err := protocol.encode(pe); err != nil {
  92. return err
  93. }
  94. }
  95. }
  96. return nil
  97. }
  98. func (r *JoinGroupRequest) decode(pd packetDecoder, version int16) (err error) {
  99. r.Version = version
  100. if r.GroupId, err = pd.getString(); err != nil {
  101. return
  102. }
  103. if r.SessionTimeout, err = pd.getInt32(); err != nil {
  104. return
  105. }
  106. if version >= 1 {
  107. if r.RebalanceTimeout, err = pd.getInt32(); err != nil {
  108. return err
  109. }
  110. }
  111. if r.MemberId, err = pd.getString(); err != nil {
  112. return
  113. }
  114. if version >= 5 {
  115. if r.GroupInstanceId, err = pd.getNullableString(); err != nil {
  116. return
  117. }
  118. }
  119. if r.ProtocolType, err = pd.getString(); err != nil {
  120. return
  121. }
  122. n, err := pd.getArrayLength()
  123. if err != nil {
  124. return err
  125. }
  126. if n == 0 {
  127. return nil
  128. }
  129. r.GroupProtocols = make(map[string][]byte)
  130. for i := 0; i < n; i++ {
  131. protocol := &GroupProtocol{}
  132. if err := protocol.decode(pd); err != nil {
  133. return err
  134. }
  135. r.GroupProtocols[protocol.Name] = protocol.Metadata
  136. r.OrderedGroupProtocols = append(r.OrderedGroupProtocols, protocol)
  137. }
  138. return nil
  139. }
  140. func (r *JoinGroupRequest) key() int16 {
  141. return 11
  142. }
  143. func (r *JoinGroupRequest) version() int16 {
  144. return r.Version
  145. }
  146. func (r *JoinGroupRequest) headerVersion() int16 {
  147. return 1
  148. }
  149. func (r *JoinGroupRequest) isValidVersion() bool {
  150. return r.Version >= 0 && r.Version <= 5
  151. }
  152. func (r *JoinGroupRequest) requiredVersion() KafkaVersion {
  153. switch r.Version {
  154. case 5:
  155. return V2_3_0_0
  156. case 4:
  157. return V2_2_0_0
  158. case 3:
  159. return V2_0_0_0
  160. case 2:
  161. return V0_11_0_0
  162. case 1:
  163. return V0_10_1_0
  164. case 0:
  165. return V0_10_0_0
  166. default:
  167. return V2_3_0_0
  168. }
  169. }
  170. func (r *JoinGroupRequest) AddGroupProtocol(name string, metadata []byte) {
  171. r.OrderedGroupProtocols = append(r.OrderedGroupProtocols, &GroupProtocol{
  172. Name: name,
  173. Metadata: metadata,
  174. })
  175. }
  176. func (r *JoinGroupRequest) AddGroupProtocolMetadata(name string, metadata *ConsumerGroupMemberMetadata) error {
  177. bin, err := encode(metadata, nil)
  178. if err != nil {
  179. return err
  180. }
  181. r.AddGroupProtocol(name, bin)
  182. return nil
  183. }