offset_commit_request.go 4.7 KB

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