offset_commit_request.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 (r *offsetCommitRequestBlock) encode(pe packetEncoder, version int16) error {
  16. pe.putInt64(r.offset)
  17. if version == 1 {
  18. pe.putInt64(r.timestamp)
  19. } else if r.timestamp != 0 {
  20. Logger.Println("Non-zero timestamp specified for OffsetCommitRequest not v1, it will be ignored")
  21. }
  22. return pe.putString(r.metadata)
  23. }
  24. func (r *offsetCommitRequestBlock) decode(pd packetDecoder, version int16) (err error) {
  25. if r.offset, err = pd.getInt64(); err != nil {
  26. return err
  27. }
  28. if version == 1 {
  29. if r.timestamp, err = pd.getInt64(); err != nil {
  30. return err
  31. }
  32. }
  33. r.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.8.3 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) (err error) {
  93. if r.ConsumerGroup, err = pd.getString(); err != nil {
  94. return err
  95. }
  96. if r.Version >= 1 {
  97. if r.ConsumerGroupGeneration, err = pd.getInt32(); err != nil {
  98. return err
  99. }
  100. if r.ConsumerID, err = pd.getString(); err != nil {
  101. return err
  102. }
  103. }
  104. if r.Version >= 2 {
  105. if r.RetentionTime, err = pd.getInt64(); err != nil {
  106. return err
  107. }
  108. }
  109. topicCount, err := pd.getArrayLength()
  110. if err != nil {
  111. return err
  112. }
  113. if topicCount == 0 {
  114. return nil
  115. }
  116. r.blocks = make(map[string]map[int32]*offsetCommitRequestBlock)
  117. for i := 0; i < topicCount; i++ {
  118. topic, err := pd.getString()
  119. if err != nil {
  120. return err
  121. }
  122. partitionCount, err := pd.getArrayLength()
  123. if err != nil {
  124. return err
  125. }
  126. r.blocks[topic] = make(map[int32]*offsetCommitRequestBlock)
  127. for j := 0; j < partitionCount; j++ {
  128. partition, err := pd.getInt32()
  129. if err != nil {
  130. return err
  131. }
  132. block := &offsetCommitRequestBlock{}
  133. if err := block.decode(pd, r.Version); err != nil {
  134. return err
  135. }
  136. r.blocks[topic][partition] = block
  137. }
  138. }
  139. return nil
  140. }
  141. func (r *OffsetCommitRequest) key() int16 {
  142. return 8
  143. }
  144. func (r *OffsetCommitRequest) version() int16 {
  145. return r.Version
  146. }
  147. func (r *OffsetCommitRequest) AddBlock(topic string, partitionID int32, offset int64, timestamp int64, metadata string) {
  148. if r.blocks == nil {
  149. r.blocks = make(map[string]map[int32]*offsetCommitRequestBlock)
  150. }
  151. if r.blocks[topic] == nil {
  152. r.blocks[topic] = make(map[int32]*offsetCommitRequestBlock)
  153. }
  154. r.blocks[topic][partitionID] = &offsetCommitRequestBlock{offset, timestamp, metadata}
  155. }