produce_set.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package sarama
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "time"
  6. )
  7. type partitionSet struct {
  8. msgs []*ProducerMessage
  9. recordsToSend Records
  10. bufferBytes int
  11. }
  12. type produceSet struct {
  13. parent *asyncProducer
  14. msgs map[string]map[int32]*partitionSet
  15. bufferBytes int
  16. bufferCount int
  17. }
  18. func newProduceSet(parent *asyncProducer) *produceSet {
  19. return &produceSet{
  20. msgs: make(map[string]map[int32]*partitionSet),
  21. parent: parent,
  22. }
  23. }
  24. func (ps *produceSet) add(msg *ProducerMessage) error {
  25. var err error
  26. var key, val []byte
  27. if msg.Key != nil {
  28. if key, err = msg.Key.Encode(); err != nil {
  29. return err
  30. }
  31. }
  32. if msg.Value != nil {
  33. if val, err = msg.Value.Encode(); err != nil {
  34. return err
  35. }
  36. }
  37. timestamp := msg.Timestamp
  38. if timestamp.IsZero() {
  39. timestamp = time.Now()
  40. }
  41. timestamp = timestamp.Truncate(time.Millisecond)
  42. partitions := ps.msgs[msg.Topic]
  43. if partitions == nil {
  44. partitions = make(map[int32]*partitionSet)
  45. ps.msgs[msg.Topic] = partitions
  46. }
  47. var size int
  48. set := partitions[msg.Partition]
  49. if set == nil {
  50. if ps.parent.conf.Version.IsAtLeast(V0_11_0_0) {
  51. batch := &RecordBatch{
  52. FirstTimestamp: timestamp,
  53. Version: 2,
  54. Codec: ps.parent.conf.Producer.Compression,
  55. CompressionLevel: ps.parent.conf.Producer.CompressionLevel,
  56. ProducerID: ps.parent.txnmgr.producerID,
  57. ProducerEpoch: ps.parent.txnmgr.producerEpoch,
  58. }
  59. if ps.parent.conf.Producer.Idempotent {
  60. batch.FirstSequence = msg.sequenceNumber
  61. }
  62. set = &partitionSet{recordsToSend: newDefaultRecords(batch)}
  63. size = recordBatchOverhead
  64. } else {
  65. set = &partitionSet{recordsToSend: newLegacyRecords(new(MessageSet))}
  66. }
  67. partitions[msg.Partition] = set
  68. }
  69. set.msgs = append(set.msgs, msg)
  70. if ps.parent.conf.Version.IsAtLeast(V0_11_0_0) {
  71. if ps.parent.conf.Producer.Idempotent && msg.sequenceNumber < set.recordsToSend.RecordBatch.FirstSequence {
  72. return errors.New("assertion failed: message out of sequence added to a batch")
  73. }
  74. // We are being conservative here to avoid having to prep encode the record
  75. size += maximumRecordOverhead
  76. rec := &Record{
  77. Key: key,
  78. Value: val,
  79. TimestampDelta: timestamp.Sub(set.recordsToSend.RecordBatch.FirstTimestamp),
  80. }
  81. size += len(key) + len(val)
  82. if len(msg.Headers) > 0 {
  83. rec.Headers = make([]*RecordHeader, len(msg.Headers))
  84. for i := range msg.Headers {
  85. rec.Headers[i] = &msg.Headers[i]
  86. size += len(rec.Headers[i].Key) + len(rec.Headers[i].Value) + 2*binary.MaxVarintLen32
  87. }
  88. }
  89. set.recordsToSend.RecordBatch.addRecord(rec)
  90. } else {
  91. msgToSend := &Message{Codec: CompressionNone, Key: key, Value: val}
  92. if ps.parent.conf.Version.IsAtLeast(V0_10_0_0) {
  93. msgToSend.Timestamp = timestamp
  94. msgToSend.Version = 1
  95. }
  96. set.recordsToSend.MsgSet.addMessage(msgToSend)
  97. size = producerMessageOverhead + len(key) + len(val)
  98. }
  99. set.bufferBytes += size
  100. ps.bufferBytes += size
  101. ps.bufferCount++
  102. return nil
  103. }
  104. func (ps *produceSet) buildRequest() *ProduceRequest {
  105. req := &ProduceRequest{
  106. RequiredAcks: ps.parent.conf.Producer.RequiredAcks,
  107. Timeout: int32(ps.parent.conf.Producer.Timeout / time.Millisecond),
  108. }
  109. if ps.parent.conf.Version.IsAtLeast(V0_10_0_0) {
  110. req.Version = 2
  111. }
  112. if ps.parent.conf.Version.IsAtLeast(V0_11_0_0) {
  113. req.Version = 3
  114. }
  115. if ps.parent.conf.Producer.Compression == CompressionZSTD && ps.parent.conf.Version.IsAtLeast(V2_1_0_0) {
  116. req.Version = 7
  117. }
  118. for topic, partitionSets := range ps.msgs {
  119. for partition, set := range partitionSets {
  120. if req.Version >= 3 {
  121. // If the API version we're hitting is 3 or greater, we need to calculate
  122. // offsets for each record in the batch relative to FirstOffset.
  123. // Additionally, we must set LastOffsetDelta to the value of the last offset
  124. // in the batch. Since the OffsetDelta of the first record is 0, we know that the
  125. // final record of any batch will have an offset of (# of records in batch) - 1.
  126. // (See https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-Messagesets
  127. // under the RecordBatch section for details.)
  128. rb := set.recordsToSend.RecordBatch
  129. if len(rb.Records) > 0 {
  130. rb.LastOffsetDelta = int32(len(rb.Records) - 1)
  131. for i, record := range rb.Records {
  132. record.OffsetDelta = int64(i)
  133. }
  134. }
  135. req.AddBatch(topic, partition, rb)
  136. continue
  137. }
  138. if ps.parent.conf.Producer.Compression == CompressionNone {
  139. req.AddSet(topic, partition, set.recordsToSend.MsgSet)
  140. } else {
  141. // When compression is enabled, the entire set for each partition is compressed
  142. // and sent as the payload of a single fake "message" with the appropriate codec
  143. // set and no key. When the server sees a message with a compression codec, it
  144. // decompresses the payload and treats the result as its message set.
  145. if ps.parent.conf.Version.IsAtLeast(V0_10_0_0) {
  146. // If our version is 0.10 or later, assign relative offsets
  147. // to the inner messages. This lets the broker avoid
  148. // recompressing the message set.
  149. // (See https://cwiki.apache.org/confluence/display/KAFKA/KIP-31+-+Move+to+relative+offsets+in+compressed+message+sets
  150. // for details on relative offsets.)
  151. for i, msg := range set.recordsToSend.MsgSet.Messages {
  152. msg.Offset = int64(i)
  153. }
  154. }
  155. payload, err := encode(set.recordsToSend.MsgSet, ps.parent.conf.MetricRegistry)
  156. if err != nil {
  157. Logger.Println(err) // if this happens, it's basically our fault.
  158. panic(err)
  159. }
  160. compMsg := &Message{
  161. Codec: ps.parent.conf.Producer.Compression,
  162. CompressionLevel: ps.parent.conf.Producer.CompressionLevel,
  163. Key: nil,
  164. Value: payload,
  165. Set: set.recordsToSend.MsgSet, // Provide the underlying message set for accurate metrics
  166. }
  167. if ps.parent.conf.Version.IsAtLeast(V0_10_0_0) {
  168. compMsg.Version = 1
  169. compMsg.Timestamp = set.recordsToSend.MsgSet.Messages[0].Msg.Timestamp
  170. }
  171. req.AddMessage(topic, partition, compMsg)
  172. }
  173. }
  174. }
  175. return req
  176. }
  177. func (ps *produceSet) eachPartition(cb func(topic string, partition int32, pSet *partitionSet)) {
  178. for topic, partitionSet := range ps.msgs {
  179. for partition, set := range partitionSet {
  180. cb(topic, partition, set)
  181. }
  182. }
  183. }
  184. func (ps *produceSet) dropPartition(topic string, partition int32) []*ProducerMessage {
  185. if ps.msgs[topic] == nil {
  186. return nil
  187. }
  188. set := ps.msgs[topic][partition]
  189. if set == nil {
  190. return nil
  191. }
  192. ps.bufferBytes -= set.bufferBytes
  193. ps.bufferCount -= len(set.msgs)
  194. delete(ps.msgs[topic], partition)
  195. return set.msgs
  196. }
  197. func (ps *produceSet) wouldOverflow(msg *ProducerMessage) bool {
  198. version := 1
  199. if ps.parent.conf.Version.IsAtLeast(V0_11_0_0) {
  200. version = 2
  201. }
  202. switch {
  203. // Would we overflow our maximum possible size-on-the-wire? 10KiB is arbitrary overhead for safety.
  204. case ps.bufferBytes+msg.byteSize(version) >= int(MaxRequestSize-(10*1024)):
  205. return true
  206. // Would we overflow the size-limit of a message-batch for this partition?
  207. case ps.msgs[msg.Topic] != nil && ps.msgs[msg.Topic][msg.Partition] != nil &&
  208. ps.msgs[msg.Topic][msg.Partition].bufferBytes+msg.byteSize(version) >= ps.parent.conf.Producer.MaxMessageBytes:
  209. return true
  210. // Would we overflow simply in number of messages?
  211. case ps.parent.conf.Producer.Flush.MaxMessages > 0 && ps.bufferCount >= ps.parent.conf.Producer.Flush.MaxMessages:
  212. return true
  213. default:
  214. return false
  215. }
  216. }
  217. func (ps *produceSet) readyToFlush() bool {
  218. switch {
  219. // If we don't have any messages, nothing else matters
  220. case ps.empty():
  221. return false
  222. // If all three config values are 0, we always flush as-fast-as-possible
  223. case ps.parent.conf.Producer.Flush.Frequency == 0 && ps.parent.conf.Producer.Flush.Bytes == 0 && ps.parent.conf.Producer.Flush.Messages == 0:
  224. return true
  225. // If we've passed the message trigger-point
  226. case ps.parent.conf.Producer.Flush.Messages > 0 && ps.bufferCount >= ps.parent.conf.Producer.Flush.Messages:
  227. return true
  228. // If we've passed the byte trigger-point
  229. case ps.parent.conf.Producer.Flush.Bytes > 0 && ps.bufferBytes >= ps.parent.conf.Producer.Flush.Bytes:
  230. return true
  231. default:
  232. return false
  233. }
  234. }
  235. func (ps *produceSet) empty() bool {
  236. return ps.bufferCount == 0
  237. }