offset_commit_request.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. type OffsetCommitRequest struct {
  21. ConsumerGroup string
  22. ConsumerGroupGeneration int32 // v1 or later
  23. ConsumerID string // v1 or later
  24. RetentionTime int64 // v2 or later
  25. // Version can be:
  26. // - 0 (kafka 0.8.1 and later)
  27. // - 1 (kafka 0.8.2 and later)
  28. // - 2 (kafka 0.8.3 and later)
  29. Version int16
  30. blocks map[string]map[int32]*offsetCommitRequestBlock
  31. }
  32. func (r *OffsetCommitRequest) encode(pe packetEncoder) error {
  33. if r.Version < 0 || r.Version > 2 {
  34. return PacketEncodingError{"invalid or unsupported OffsetCommitRequest version field"}
  35. }
  36. if err := pe.putString(r.ConsumerGroup); err != nil {
  37. return err
  38. }
  39. if r.Version >= 1 {
  40. pe.putInt32(r.ConsumerGroupGeneration)
  41. if err := pe.putString(r.ConsumerID); err != nil {
  42. return err
  43. }
  44. } else {
  45. if r.ConsumerGroupGeneration != 0 {
  46. Logger.Println("Non-zero ConsumerGroupGeneration specified for OffsetCommitRequest v0, it will be ignored")
  47. }
  48. if r.ConsumerID != "" {
  49. Logger.Println("Non-empty ConsumerID specified for OffsetCommitRequest v0, it will be ignored")
  50. }
  51. }
  52. if r.Version >= 2 {
  53. pe.putInt64(r.RetentionTime)
  54. } else if r.RetentionTime != 0 {
  55. Logger.Println("Non-zero RetentionTime specified for OffsetCommitRequest version <2, it will be ignored")
  56. }
  57. if err := pe.putArrayLength(len(r.blocks)); err != nil {
  58. return err
  59. }
  60. for topic, partitions := range r.blocks {
  61. if err := pe.putString(topic); err != nil {
  62. return err
  63. }
  64. if err := pe.putArrayLength(len(partitions)); err != nil {
  65. return err
  66. }
  67. for partition, block := range partitions {
  68. pe.putInt32(partition)
  69. if err := block.encode(pe, r.Version); err != nil {
  70. return err
  71. }
  72. }
  73. }
  74. return nil
  75. }
  76. func (r *OffsetCommitRequest) key() int16 {
  77. return 8
  78. }
  79. func (r *OffsetCommitRequest) version() int16 {
  80. return r.Version
  81. }
  82. func (r *OffsetCommitRequest) AddBlock(topic string, partitionID int32, offset int64, timestamp int64, metadata string) {
  83. if r.blocks == nil {
  84. r.blocks = make(map[string]map[int32]*offsetCommitRequestBlock)
  85. }
  86. if r.blocks[topic] == nil {
  87. r.blocks[topic] = make(map[int32]*offsetCommitRequestBlock)
  88. }
  89. r.blocks[topic][partitionID] = &offsetCommitRequestBlock{offset, timestamp, metadata}
  90. }