produce_response.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package sarama
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. type ProduceResponseBlock struct {
  7. Err KError
  8. Offset int64
  9. // only provided if Version >= 2 and the broker is configured with `LogAppendTime`
  10. Timestamp time.Time
  11. }
  12. func (b *ProduceResponseBlock) decode(pd packetDecoder, version int16) (err error) {
  13. tmp, err := pd.getInt16()
  14. if err != nil {
  15. return err
  16. }
  17. b.Err = KError(tmp)
  18. b.Offset, err = pd.getInt64()
  19. if err != nil {
  20. return err
  21. }
  22. if version >= 2 {
  23. if millis, err := pd.getInt64(); err != nil {
  24. return err
  25. } else if millis != -1 {
  26. b.Timestamp = time.Unix(millis/1000, (millis%1000)*int64(time.Millisecond))
  27. }
  28. }
  29. return nil
  30. }
  31. func (b *ProduceResponseBlock) encode(pe packetEncoder, version int16) (err error) {
  32. pe.putInt16(int16(b.Err))
  33. pe.putInt64(b.Offset)
  34. if version >= 2 {
  35. timestamp := int64(-1)
  36. if !b.Timestamp.Before(time.Unix(0, 0)) {
  37. timestamp = b.Timestamp.UnixNano() / int64(time.Millisecond)
  38. } else if !b.Timestamp.IsZero() {
  39. return PacketEncodingError{fmt.Sprintf("invalid timestamp (%v)", b.Timestamp)}
  40. }
  41. pe.putInt64(timestamp)
  42. }
  43. return nil
  44. }
  45. type ProduceResponse struct {
  46. Blocks map[string]map[int32]*ProduceResponseBlock
  47. Version int16
  48. ThrottleTime time.Duration // only provided if Version >= 1
  49. }
  50. func (r *ProduceResponse) decode(pd packetDecoder, version int16) (err error) {
  51. r.Version = version
  52. numTopics, err := pd.getArrayLength()
  53. if err != nil {
  54. return err
  55. }
  56. r.Blocks = make(map[string]map[int32]*ProduceResponseBlock, numTopics)
  57. for i := 0; i < numTopics; i++ {
  58. name, err := pd.getString()
  59. if err != nil {
  60. return err
  61. }
  62. numBlocks, err := pd.getArrayLength()
  63. if err != nil {
  64. return err
  65. }
  66. r.Blocks[name] = make(map[int32]*ProduceResponseBlock, numBlocks)
  67. for j := 0; j < numBlocks; j++ {
  68. id, err := pd.getInt32()
  69. if err != nil {
  70. return err
  71. }
  72. block := new(ProduceResponseBlock)
  73. err = block.decode(pd, version)
  74. if err != nil {
  75. return err
  76. }
  77. r.Blocks[name][id] = block
  78. }
  79. }
  80. if r.Version >= 1 {
  81. millis, err := pd.getInt32()
  82. if err != nil {
  83. return err
  84. }
  85. r.ThrottleTime = time.Duration(millis) * time.Millisecond
  86. }
  87. return nil
  88. }
  89. func (r *ProduceResponse) encode(pe packetEncoder) error {
  90. err := pe.putArrayLength(len(r.Blocks))
  91. if err != nil {
  92. return err
  93. }
  94. for topic, partitions := range r.Blocks {
  95. err = pe.putString(topic)
  96. if err != nil {
  97. return err
  98. }
  99. err = pe.putArrayLength(len(partitions))
  100. if err != nil {
  101. return err
  102. }
  103. for id, prb := range partitions {
  104. pe.putInt32(id)
  105. err = prb.encode(pe, r.Version)
  106. if err != nil {
  107. return err
  108. }
  109. }
  110. }
  111. if r.Version >= 1 {
  112. pe.putInt32(int32(r.ThrottleTime / time.Millisecond))
  113. }
  114. return nil
  115. }
  116. func (r *ProduceResponse) key() int16 {
  117. return 0
  118. }
  119. func (r *ProduceResponse) version() int16 {
  120. return r.Version
  121. }
  122. func (r *ProduceResponse) requiredVersion() KafkaVersion {
  123. switch r.Version {
  124. case 1:
  125. return V0_9_0_0
  126. case 2:
  127. return V0_10_0_0
  128. case 3:
  129. return V0_11_0_0
  130. default:
  131. return MinVersion
  132. }
  133. }
  134. func (r *ProduceResponse) GetBlock(topic string, partition int32) *ProduceResponseBlock {
  135. if r.Blocks == nil {
  136. return nil
  137. }
  138. if r.Blocks[topic] == nil {
  139. return nil
  140. }
  141. return r.Blocks[topic][partition]
  142. }
  143. // Testing API
  144. func (r *ProduceResponse) AddTopicPartition(topic string, partition int32, err KError) {
  145. if r.Blocks == nil {
  146. r.Blocks = make(map[string]map[int32]*ProduceResponseBlock)
  147. }
  148. byTopic, ok := r.Blocks[topic]
  149. if !ok {
  150. byTopic = make(map[int32]*ProduceResponseBlock)
  151. r.Blocks[topic] = byTopic
  152. }
  153. block := &ProduceResponseBlock{
  154. Err: err,
  155. }
  156. if r.Version >= 2 {
  157. block.Timestamp = time.Now()
  158. }
  159. byTopic[partition] = block
  160. }