offset_commit_request.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. type offsetCommitRequestBlock struct {
  7. offset int64
  8. timestamp int64
  9. metadata string
  10. }
  11. func (r *offsetCommitRequestBlock) encode(pe packetEncoder, version int16) error {
  12. pe.putInt64(r.offset)
  13. if version == 1 {
  14. pe.putInt64(r.timestamp)
  15. } else if r.timestamp != 0 {
  16. Logger.Println("Non-zero timestamp specified for OffsetCommitRequest not v1, it will be ignored")
  17. }
  18. return pe.putString(r.metadata)
  19. }
  20. func (r *offsetCommitRequestBlock) decode(pd packetDecoder, version int16) (err error) {
  21. if r.offset, err = pd.getInt64(); err != nil {
  22. return err
  23. }
  24. if version == 1 {
  25. if r.timestamp, err = pd.getInt64(); err != nil {
  26. return err
  27. }
  28. }
  29. r.metadata, err = pd.getString()
  30. return err
  31. }
  32. type OffsetCommitRequest struct {
  33. ConsumerGroup string
  34. ConsumerGroupGeneration int32 // v1 or later
  35. ConsumerID string // v1 or later
  36. RetentionTime int64 // v2 or later
  37. // Version can be:
  38. // - 0 (kafka 0.8.1 and later)
  39. // - 1 (kafka 0.8.2 and later)
  40. // - 2 (kafka 0.8.3 and later)
  41. Version int16
  42. blocks map[string]map[int32]*offsetCommitRequestBlock
  43. }
  44. func (r *OffsetCommitRequest) encode(pe packetEncoder) error {
  45. if r.Version < 0 || r.Version > 2 {
  46. return PacketEncodingError{"invalid or unsupported OffsetCommitRequest version field"}
  47. }
  48. if err := pe.putString(r.ConsumerGroup); err != nil {
  49. return err
  50. }
  51. if r.Version >= 1 {
  52. pe.putInt32(r.ConsumerGroupGeneration)
  53. if err := pe.putString(r.ConsumerID); err != nil {
  54. return err
  55. }
  56. } else {
  57. if r.ConsumerGroupGeneration != 0 {
  58. Logger.Println("Non-zero ConsumerGroupGeneration specified for OffsetCommitRequest v0, it will be ignored")
  59. }
  60. if r.ConsumerID != "" {
  61. Logger.Println("Non-empty ConsumerID specified for OffsetCommitRequest v0, it will be ignored")
  62. }
  63. }
  64. if r.Version >= 2 {
  65. pe.putInt64(r.RetentionTime)
  66. } else if r.RetentionTime != 0 {
  67. Logger.Println("Non-zero RetentionTime specified for OffsetCommitRequest version <2, it will be ignored")
  68. }
  69. if err := pe.putArrayLength(len(r.blocks)); err != nil {
  70. return err
  71. }
  72. for topic, partitions := range r.blocks {
  73. if err := pe.putString(topic); err != nil {
  74. return err
  75. }
  76. if err := pe.putArrayLength(len(partitions)); err != nil {
  77. return err
  78. }
  79. for partition, block := range partitions {
  80. pe.putInt32(partition)
  81. if err := block.encode(pe, r.Version); err != nil {
  82. return err
  83. }
  84. }
  85. }
  86. return nil
  87. }
  88. func (r *OffsetCommitRequest) decode(pd packetDecoder) (err error) {
  89. if r.ConsumerGroup, err = pd.getString(); err != nil {
  90. return err
  91. }
  92. if r.Version >= 1 {
  93. if r.ConsumerGroupGeneration, err = pd.getInt32(); err != nil {
  94. return err
  95. }
  96. if r.ConsumerID, err = pd.getString(); err != nil {
  97. return err
  98. }
  99. }
  100. if r.Version >= 2 {
  101. if r.RetentionTime, err = pd.getInt64(); err != nil {
  102. return err
  103. }
  104. }
  105. topicCount, err := pd.getArrayLength()
  106. if err != nil {
  107. return err
  108. }
  109. if topicCount == 0 {
  110. return nil
  111. }
  112. r.blocks = make(map[string]map[int32]*offsetCommitRequestBlock)
  113. for i := 0; i < topicCount; i++ {
  114. topic, err := pd.getString()
  115. if err != nil {
  116. return err
  117. }
  118. partitionCount, err := pd.getArrayLength()
  119. if err != nil {
  120. return err
  121. }
  122. r.blocks[topic] = make(map[int32]*offsetCommitRequestBlock)
  123. for j := 0; j < partitionCount; j++ {
  124. partition, err := pd.getInt32()
  125. if err != nil {
  126. return err
  127. }
  128. block := &offsetCommitRequestBlock{}
  129. if err := block.decode(pd, r.Version); err != nil {
  130. return err
  131. }
  132. r.blocks[topic][partition] = block
  133. }
  134. }
  135. return nil
  136. }
  137. func (r *OffsetCommitRequest) key() int16 {
  138. return 8
  139. }
  140. func (r *OffsetCommitRequest) version() int16 {
  141. return r.Version
  142. }
  143. func (r *OffsetCommitRequest) AddBlock(topic string, partitionID int32, offset int64, timestamp int64, metadata string) {
  144. if r.blocks == nil {
  145. r.blocks = make(map[string]map[int32]*offsetCommitRequestBlock)
  146. }
  147. if r.blocks[topic] == nil {
  148. r.blocks[topic] = make(map[int32]*offsetCommitRequestBlock)
  149. }
  150. r.blocks[topic][partitionID] = &offsetCommitRequestBlock{offset, timestamp, metadata}
  151. }