produce_set.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. set.setToSend.addMessage(&Message{Codec: CompressionNone, Key: key, Value: val})
  45. size := producerMessageOverhead + len(key) + len(val)
  46. set.bufferBytes += size
  47. ps.bufferBytes += size
  48. ps.bufferCount++
  49. return nil
  50. }
  51. func (ps *produceSet) buildRequest() *ProduceRequest {
  52. req := &ProduceRequest{
  53. RequiredAcks: ps.parent.conf.Producer.RequiredAcks,
  54. Timeout: int32(ps.parent.conf.Producer.Timeout / time.Millisecond),
  55. }
  56. for topic, partitionSet := range ps.msgs {
  57. for partition, set := range partitionSet {
  58. if ps.parent.conf.Producer.Compression == CompressionNone {
  59. req.AddSet(topic, partition, set.setToSend)
  60. } else {
  61. // When compression is enabled, the entire set for each partition is compressed
  62. // and sent as the payload of a single fake "message" with the appropriate codec
  63. // set and no key. When the server sees a message with a compression codec, it
  64. // decompresses the payload and treats the result as its message set.
  65. payload, err := encode(set.setToSend)
  66. if err != nil {
  67. Logger.Println(err) // if this happens, it's basically our fault.
  68. panic(err)
  69. }
  70. req.AddMessage(topic, partition, &Message{
  71. Codec: ps.parent.conf.Producer.Compression,
  72. Key: nil,
  73. Value: payload,
  74. })
  75. }
  76. }
  77. }
  78. return req
  79. }
  80. func (ps *produceSet) eachPartition(cb func(topic string, partition int32, msgs []*ProducerMessage)) {
  81. for topic, partitionSet := range ps.msgs {
  82. for partition, set := range partitionSet {
  83. cb(topic, partition, set.msgs)
  84. }
  85. }
  86. }
  87. func (ps *produceSet) dropPartition(topic string, partition int32) []*ProducerMessage {
  88. if ps.msgs[topic] == nil {
  89. return nil
  90. }
  91. set := ps.msgs[topic][partition]
  92. if set == nil {
  93. return nil
  94. }
  95. ps.bufferBytes -= set.bufferBytes
  96. ps.bufferCount -= len(set.msgs)
  97. delete(ps.msgs[topic], partition)
  98. return set.msgs
  99. }
  100. func (ps *produceSet) wouldOverflow(msg *ProducerMessage) bool {
  101. switch {
  102. // Would we overflow our maximum possible size-on-the-wire? 10KiB is arbitrary overhead for safety.
  103. case ps.bufferBytes+msg.byteSize() >= int(MaxRequestSize-(10*1024)):
  104. return true
  105. // Would we overflow the size-limit of a compressed message-batch for this partition?
  106. case ps.parent.conf.Producer.Compression != CompressionNone &&
  107. ps.msgs[msg.Topic] != nil && ps.msgs[msg.Topic][msg.Partition] != nil &&
  108. ps.msgs[msg.Topic][msg.Partition].bufferBytes+msg.byteSize() >= ps.parent.conf.Producer.MaxMessageBytes:
  109. return true
  110. // Would we overflow simply in number of messages?
  111. case ps.parent.conf.Producer.Flush.MaxMessages > 0 && ps.bufferCount >= ps.parent.conf.Producer.Flush.MaxMessages:
  112. return true
  113. default:
  114. return false
  115. }
  116. }
  117. func (ps *produceSet) readyToFlush() bool {
  118. switch {
  119. // If we don't have any messages, nothing else matters
  120. case ps.empty():
  121. return false
  122. // If all three config values are 0, we always flush as-fast-as-possible
  123. case ps.parent.conf.Producer.Flush.Frequency == 0 && ps.parent.conf.Producer.Flush.Bytes == 0 && ps.parent.conf.Producer.Flush.Messages == 0:
  124. return true
  125. // If we've passed the message trigger-point
  126. case ps.parent.conf.Producer.Flush.Messages > 0 && ps.bufferCount >= ps.parent.conf.Producer.Flush.Messages:
  127. return true
  128. // If we've passed the byte trigger-point
  129. case ps.parent.conf.Producer.Flush.Bytes > 0 && ps.bufferBytes >= ps.parent.conf.Producer.Flush.Bytes:
  130. return true
  131. default:
  132. return false
  133. }
  134. }
  135. func (ps *produceSet) empty() bool {
  136. return ps.bufferCount == 0
  137. }