produce_request.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package sarama
  2. // RequiredAcks is used in Produce Requests to tell the broker how many replica acknowledgements
  3. // it must see before responding. Any of the constants defined here are valid. On broker versions
  4. // prior to 0.8.2.0 any other positive int16 is also valid (the broker will wait for that many
  5. // acknowledgements) but in 0.8.2.0 and later this will raise an exception (it has been replaced
  6. // by setting the `min.isr` value in the brokers configuration).
  7. type RequiredAcks int16
  8. const (
  9. // NoResponse doesn't send any response, the TCP ACK is all you get.
  10. NoResponse RequiredAcks = 0
  11. // WaitForLocal waits for only the local commit to succeed before responding.
  12. WaitForLocal RequiredAcks = 1
  13. // WaitForAll waits for all replicas to commit before responding.
  14. WaitForAll RequiredAcks = -1
  15. )
  16. type ProduceRequest struct {
  17. RequiredAcks RequiredAcks
  18. Timeout int32
  19. Version int16 // v1 requires Kafka 0.9, v2 requires Kafka 0.10
  20. msgSets map[string]map[int32]*MessageSet
  21. }
  22. func (p *ProduceRequest) encode(pe packetEncoder) error {
  23. pe.putInt16(int16(p.RequiredAcks))
  24. pe.putInt32(p.Timeout)
  25. err := pe.putArrayLength(len(p.msgSets))
  26. if err != nil {
  27. return err
  28. }
  29. for topic, partitions := range p.msgSets {
  30. err = pe.putString(topic)
  31. if err != nil {
  32. return err
  33. }
  34. err = pe.putArrayLength(len(partitions))
  35. if err != nil {
  36. return err
  37. }
  38. for id, msgSet := range partitions {
  39. pe.putInt32(id)
  40. pe.push(&lengthField{})
  41. err = msgSet.encode(pe)
  42. if err != nil {
  43. return err
  44. }
  45. err = pe.pop()
  46. if err != nil {
  47. return err
  48. }
  49. }
  50. }
  51. return nil
  52. }
  53. func (p *ProduceRequest) decode(pd packetDecoder, version int16) error {
  54. requiredAcks, err := pd.getInt16()
  55. if err != nil {
  56. return err
  57. }
  58. p.RequiredAcks = RequiredAcks(requiredAcks)
  59. if p.Timeout, err = pd.getInt32(); err != nil {
  60. return err
  61. }
  62. topicCount, err := pd.getArrayLength()
  63. if err != nil {
  64. return err
  65. }
  66. if topicCount == 0 {
  67. return nil
  68. }
  69. p.msgSets = make(map[string]map[int32]*MessageSet)
  70. for i := 0; i < topicCount; i++ {
  71. topic, err := pd.getString()
  72. if err != nil {
  73. return err
  74. }
  75. partitionCount, err := pd.getArrayLength()
  76. if err != nil {
  77. return err
  78. }
  79. p.msgSets[topic] = make(map[int32]*MessageSet)
  80. for j := 0; j < partitionCount; j++ {
  81. partition, err := pd.getInt32()
  82. if err != nil {
  83. return err
  84. }
  85. messageSetSize, err := pd.getInt32()
  86. if err != nil {
  87. return err
  88. }
  89. msgSetDecoder, err := pd.getSubset(int(messageSetSize))
  90. if err != nil {
  91. return err
  92. }
  93. msgSet := &MessageSet{}
  94. err = msgSet.decode(msgSetDecoder)
  95. if err != nil {
  96. return err
  97. }
  98. p.msgSets[topic][partition] = msgSet
  99. }
  100. }
  101. return nil
  102. }
  103. func (p *ProduceRequest) key() int16 {
  104. return 0
  105. }
  106. func (p *ProduceRequest) version() int16 {
  107. return p.Version
  108. }
  109. func (p *ProduceRequest) requiredVersion() KafkaVersion {
  110. switch p.Version {
  111. case 1:
  112. return V0_9_0_0
  113. case 2:
  114. return V0_10_0_0
  115. default:
  116. return minVersion
  117. }
  118. }
  119. func (p *ProduceRequest) AddMessage(topic string, partition int32, msg *Message) {
  120. if p.msgSets == nil {
  121. p.msgSets = make(map[string]map[int32]*MessageSet)
  122. }
  123. if p.msgSets[topic] == nil {
  124. p.msgSets[topic] = make(map[int32]*MessageSet)
  125. }
  126. set := p.msgSets[topic][partition]
  127. if set == nil {
  128. set = new(MessageSet)
  129. p.msgSets[topic][partition] = set
  130. }
  131. set.addMessage(msg)
  132. }
  133. func (p *ProduceRequest) AddSet(topic string, partition int32, set *MessageSet) {
  134. if p.msgSets == nil {
  135. p.msgSets = make(map[string]map[int32]*MessageSet)
  136. }
  137. if p.msgSets[topic] == nil {
  138. p.msgSets[topic] = make(map[int32]*MessageSet)
  139. }
  140. p.msgSets[topic][partition] = set
  141. }