produce_set.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package sarama
  2. import "time"
  3. type partitionSet struct {
  4. msgs []*ProducerMessage
  5. setToSend *MessageSet
  6. bufferBytes int
  7. }
  8. type produceSet struct {
  9. parent *asyncProducer
  10. msgs map[string]map[int32]*partitionSet
  11. bufferBytes int
  12. bufferCount int
  13. }
  14. func newProduceSet(parent *asyncProducer) *produceSet {
  15. return &produceSet{
  16. msgs: make(map[string]map[int32]*partitionSet),
  17. parent: parent,
  18. }
  19. }
  20. func (ps *produceSet) add(msg *ProducerMessage) error {
  21. var err error
  22. var key, val []byte
  23. if msg.Key != nil {
  24. if key, err = msg.Key.Encode(); err != nil {
  25. return err
  26. }
  27. }
  28. if msg.Value != nil {
  29. if val, err = msg.Value.Encode(); err != nil {
  30. return err
  31. }
  32. }
  33. partitions := ps.msgs[msg.Topic]
  34. if partitions == nil {
  35. partitions = make(map[int32]*partitionSet)
  36. ps.msgs[msg.Topic] = partitions
  37. }
  38. set := partitions[msg.Partition]
  39. if set == nil {
  40. set = &partitionSet{setToSend: new(MessageSet)}
  41. partitions[msg.Partition] = set
  42. }
  43. set.msgs = append(set.msgs, msg)
  44. msgToSend := &Message{Codec: CompressionNone, Key: key, Value: val}
  45. if ps.parent.conf.Version.IsAtLeast(V0_10_0_0) && !msg.Timestamp.IsZero() {
  46. msgToSend.Timestamp = msg.Timestamp
  47. msgToSend.Version = 1
  48. }
  49. set.setToSend.addMessage(msgToSend)
  50. size := producerMessageOverhead + len(key) + len(val)
  51. set.bufferBytes += size
  52. ps.bufferBytes += size
  53. ps.bufferCount++
  54. return nil
  55. }
  56. func (ps *produceSet) buildRequest() *ProduceRequest {
  57. req := &ProduceRequest{
  58. RequiredAcks: ps.parent.conf.Producer.RequiredAcks,
  59. Timeout: int32(ps.parent.conf.Producer.Timeout / time.Millisecond),
  60. }
  61. if ps.parent.conf.Version.IsAtLeast(V0_10_0_0) {
  62. req.Version = 2
  63. }
  64. for topic, partitionSet := range ps.msgs {
  65. for partition, set := range partitionSet {
  66. if ps.parent.conf.Producer.Compression == CompressionNone {
  67. req.AddSet(topic, partition, set.setToSend)
  68. } else {
  69. // When compression is enabled, the entire set for each partition is compressed
  70. // and sent as the payload of a single fake "message" with the appropriate codec
  71. // set and no key. When the server sees a message with a compression codec, it
  72. // decompresses the payload and treats the result as its message set.
  73. payload, err := encode(set.setToSend)
  74. if err != nil {
  75. Logger.Println(err) // if this happens, it's basically our fault.
  76. panic(err)
  77. }
  78. req.AddMessage(topic, partition, &Message{
  79. Codec: ps.parent.conf.Producer.Compression,
  80. Key: nil,
  81. Value: payload,
  82. })
  83. }
  84. }
  85. }
  86. return req
  87. }
  88. func (ps *produceSet) eachPartition(cb func(topic string, partition int32, msgs []*ProducerMessage)) {
  89. for topic, partitionSet := range ps.msgs {
  90. for partition, set := range partitionSet {
  91. cb(topic, partition, set.msgs)
  92. }
  93. }
  94. }
  95. func (ps *produceSet) dropPartition(topic string, partition int32) []*ProducerMessage {
  96. if ps.msgs[topic] == nil {
  97. return nil
  98. }
  99. set := ps.msgs[topic][partition]
  100. if set == nil {
  101. return nil
  102. }
  103. ps.bufferBytes -= set.bufferBytes
  104. ps.bufferCount -= len(set.msgs)
  105. delete(ps.msgs[topic], partition)
  106. return set.msgs
  107. }
  108. func (ps *produceSet) wouldOverflow(msg *ProducerMessage) bool {
  109. switch {
  110. // Would we overflow our maximum possible size-on-the-wire? 10KiB is arbitrary overhead for safety.
  111. case ps.bufferBytes+msg.byteSize() >= int(MaxRequestSize-(10*1024)):
  112. return true
  113. // Would we overflow the size-limit of a compressed message-batch for this partition?
  114. case ps.parent.conf.Producer.Compression != CompressionNone &&
  115. ps.msgs[msg.Topic] != nil && ps.msgs[msg.Topic][msg.Partition] != nil &&
  116. ps.msgs[msg.Topic][msg.Partition].bufferBytes+msg.byteSize() >= ps.parent.conf.Producer.MaxMessageBytes:
  117. return true
  118. // Would we overflow simply in number of messages?
  119. case ps.parent.conf.Producer.Flush.MaxMessages > 0 && ps.bufferCount >= ps.parent.conf.Producer.Flush.MaxMessages:
  120. return true
  121. default:
  122. return false
  123. }
  124. }
  125. func (ps *produceSet) readyToFlush() bool {
  126. switch {
  127. // If we don't have any messages, nothing else matters
  128. case ps.empty():
  129. return false
  130. // If all three config values are 0, we always flush as-fast-as-possible
  131. case ps.parent.conf.Producer.Flush.Frequency == 0 && ps.parent.conf.Producer.Flush.Bytes == 0 && ps.parent.conf.Producer.Flush.Messages == 0:
  132. return true
  133. // If we've passed the message trigger-point
  134. case ps.parent.conf.Producer.Flush.Messages > 0 && ps.bufferCount >= ps.parent.conf.Producer.Flush.Messages:
  135. return true
  136. // If we've passed the byte trigger-point
  137. case ps.parent.conf.Producer.Flush.Bytes > 0 && ps.bufferBytes >= ps.parent.conf.Producer.Flush.Bytes:
  138. return true
  139. default:
  140. return false
  141. }
  142. }
  143. func (ps *produceSet) empty() bool {
  144. return ps.bufferCount == 0
  145. }