offset_commit_request.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package sarama
  2. import "errors"
  3. // ReceiveTime is a special value for the timestamp field of Offset Commit Requests which
  4. // tells the broker to set the timestamp to the time at which the request was received.
  5. // The timestamp is only used if message version 1 is used, which requires kafka 0.8.2.
  6. const ReceiveTime int64 = -1
  7. // GroupGenerationUndefined is a special value for the group generation field of
  8. // Offset Commit Requests that should be used when a consumer group does not rely
  9. // on Kafka for partition management.
  10. const GroupGenerationUndefined = -1
  11. type offsetCommitRequestBlock struct {
  12. offset int64
  13. timestamp int64
  14. metadata string
  15. }
  16. func (b *offsetCommitRequestBlock) encode(pe packetEncoder, version int16) error {
  17. pe.putInt64(b.offset)
  18. if version == 1 {
  19. pe.putInt64(b.timestamp)
  20. } else if b.timestamp != 0 {
  21. Logger.Println("Non-zero timestamp specified for OffsetCommitRequest not v1, it will be ignored")
  22. }
  23. return pe.putString(b.metadata)
  24. }
  25. func (b *offsetCommitRequestBlock) decode(pd packetDecoder, version int16) (err error) {
  26. if b.offset, err = pd.getInt64(); err != nil {
  27. return err
  28. }
  29. if version == 1 {
  30. if b.timestamp, err = pd.getInt64(); err != nil {
  31. return err
  32. }
  33. }
  34. b.metadata, err = pd.getString()
  35. return err
  36. }
  37. type OffsetCommitRequest struct {
  38. ConsumerGroup string
  39. ConsumerGroupGeneration int32 // v1 or later
  40. ConsumerID string // v1 or later
  41. RetentionTime int64 // v2 or later
  42. // Version can be:
  43. // - 0 (kafka 0.8.1 and later)
  44. // - 1 (kafka 0.8.2 and later)
  45. // - 2 (kafka 0.9.0 and later)
  46. Version int16
  47. blocks map[string]map[int32]*offsetCommitRequestBlock
  48. }
  49. func (r *OffsetCommitRequest) encode(pe packetEncoder) error {
  50. if r.Version < 0 || r.Version > 2 {
  51. return PacketEncodingError{"invalid or unsupported OffsetCommitRequest version field"}
  52. }
  53. if err := pe.putString(r.ConsumerGroup); err != nil {
  54. return err
  55. }
  56. if r.Version >= 1 {
  57. pe.putInt32(r.ConsumerGroupGeneration)
  58. if err := pe.putString(r.ConsumerID); err != nil {
  59. return err
  60. }
  61. } else {
  62. if r.ConsumerGroupGeneration != 0 {
  63. Logger.Println("Non-zero ConsumerGroupGeneration specified for OffsetCommitRequest v0, it will be ignored")
  64. }
  65. if r.ConsumerID != "" {
  66. Logger.Println("Non-empty ConsumerID specified for OffsetCommitRequest v0, it will be ignored")
  67. }
  68. }
  69. if r.Version >= 2 {
  70. pe.putInt64(r.RetentionTime)
  71. } else if r.RetentionTime != 0 {
  72. Logger.Println("Non-zero RetentionTime specified for OffsetCommitRequest version <2, it will be ignored")
  73. }
  74. if err := pe.putArrayLength(len(r.blocks)); err != nil {
  75. return err
  76. }
  77. for topic, partitions := range r.blocks {
  78. if err := pe.putString(topic); err != nil {
  79. return err
  80. }
  81. if err := pe.putArrayLength(len(partitions)); err != nil {
  82. return err
  83. }
  84. for partition, block := range partitions {
  85. pe.putInt32(partition)
  86. if err := block.encode(pe, r.Version); err != nil {
  87. return err
  88. }
  89. }
  90. }
  91. return nil
  92. }
  93. func (r *OffsetCommitRequest) decode(pd packetDecoder, version int16) (err error) {
  94. r.Version = version
  95. if r.ConsumerGroup, err = pd.getString(); err != nil {
  96. return err
  97. }
  98. if r.Version >= 1 {
  99. if r.ConsumerGroupGeneration, err = pd.getInt32(); err != nil {
  100. return err
  101. }
  102. if r.ConsumerID, err = pd.getString(); err != nil {
  103. return err
  104. }
  105. }
  106. if r.Version >= 2 {
  107. if r.RetentionTime, err = pd.getInt64(); err != nil {
  108. return err
  109. }
  110. }
  111. topicCount, err := pd.getArrayLength()
  112. if err != nil {
  113. return err
  114. }
  115. if topicCount == 0 {
  116. return nil
  117. }
  118. r.blocks = make(map[string]map[int32]*offsetCommitRequestBlock)
  119. for i := 0; i < topicCount; i++ {
  120. topic, err := pd.getString()
  121. if err != nil {
  122. return err
  123. }
  124. partitionCount, err := pd.getArrayLength()
  125. if err != nil {
  126. return err
  127. }
  128. r.blocks[topic] = make(map[int32]*offsetCommitRequestBlock)
  129. for j := 0; j < partitionCount; j++ {
  130. partition, err := pd.getInt32()
  131. if err != nil {
  132. return err
  133. }
  134. block := &offsetCommitRequestBlock{}
  135. if err := block.decode(pd, r.Version); err != nil {
  136. return err
  137. }
  138. r.blocks[topic][partition] = block
  139. }
  140. }
  141. return nil
  142. }
  143. func (r *OffsetCommitRequest) key() int16 {
  144. return 8
  145. }
  146. func (r *OffsetCommitRequest) version() int16 {
  147. return r.Version
  148. }
  149. func (r *OffsetCommitRequest) requiredVersion() KafkaVersion {
  150. switch r.Version {
  151. case 1:
  152. return V0_8_2_0
  153. case 2:
  154. return V0_9_0_0
  155. default:
  156. return MinVersion
  157. }
  158. }
  159. func (r *OffsetCommitRequest) AddBlock(topic string, partitionID int32, offset int64, timestamp int64, metadata string) {
  160. if r.blocks == nil {
  161. r.blocks = make(map[string]map[int32]*offsetCommitRequestBlock)
  162. }
  163. if r.blocks[topic] == nil {
  164. r.blocks[topic] = make(map[int32]*offsetCommitRequestBlock)
  165. }
  166. r.blocks[topic][partitionID] = &offsetCommitRequestBlock{offset, timestamp, metadata}
  167. }
  168. func (r *OffsetCommitRequest) Offset(topic string, partitionID int32) (int64, string, error) {
  169. partitions := r.blocks[topic]
  170. if partitions == nil {
  171. return 0, "", errors.New("No such offset")
  172. }
  173. block := partitions[partitionID]
  174. if block == nil {
  175. return 0, "", errors.New("No such offset")
  176. }
  177. return block.offset, block.metadata, nil
  178. }