delete_records_response.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package sarama
  2. import (
  3. "sort"
  4. "time"
  5. )
  6. // response message format is:
  7. // throttleMs(int32) [topic]
  8. // where topic is:
  9. // name(string) [partition]
  10. // where partition is:
  11. // id(int32) low_watermark(int64) error_code(int16)
  12. type DeleteRecordsResponse struct {
  13. Version int16
  14. ThrottleTime time.Duration
  15. Topics map[string]*DeleteRecordsResponseTopic
  16. }
  17. func (d *DeleteRecordsResponse) encode(pe packetEncoder) error {
  18. pe.putInt32(int32(d.ThrottleTime / time.Millisecond))
  19. if err := pe.putArrayLength(len(d.Topics)); err != nil {
  20. return err
  21. }
  22. keys := make([]string, 0, len(d.Topics))
  23. for topic := range d.Topics {
  24. keys = append(keys, topic)
  25. }
  26. sort.Strings(keys)
  27. for _, topic := range keys {
  28. if err := pe.putString(topic); err != nil {
  29. return err
  30. }
  31. if err := d.Topics[topic].encode(pe); err != nil {
  32. return err
  33. }
  34. }
  35. return nil
  36. }
  37. func (d *DeleteRecordsResponse) decode(pd packetDecoder, version int16) error {
  38. d.Version = version
  39. throttleTime, err := pd.getInt32()
  40. if err != nil {
  41. return err
  42. }
  43. d.ThrottleTime = time.Duration(throttleTime) * time.Millisecond
  44. n, err := pd.getArrayLength()
  45. if err != nil {
  46. return err
  47. }
  48. if n > 0 {
  49. d.Topics = make(map[string]*DeleteRecordsResponseTopic, n)
  50. for i := 0; i < n; i++ {
  51. topic, err := pd.getString()
  52. if err != nil {
  53. return err
  54. }
  55. details := new(DeleteRecordsResponseTopic)
  56. if err = details.decode(pd, version); err != nil {
  57. return err
  58. }
  59. d.Topics[topic] = details
  60. }
  61. }
  62. return nil
  63. }
  64. func (d *DeleteRecordsResponse) key() int16 {
  65. return 21
  66. }
  67. func (d *DeleteRecordsResponse) version() int16 {
  68. return 0
  69. }
  70. func (d *DeleteRecordsResponse) headerVersion() int16 {
  71. return 0
  72. }
  73. func (d *DeleteRecordsResponse) requiredVersion() KafkaVersion {
  74. return V0_11_0_0
  75. }
  76. type DeleteRecordsResponseTopic struct {
  77. Partitions map[int32]*DeleteRecordsResponsePartition
  78. }
  79. func (t *DeleteRecordsResponseTopic) encode(pe packetEncoder) error {
  80. if err := pe.putArrayLength(len(t.Partitions)); err != nil {
  81. return err
  82. }
  83. keys := make([]int32, 0, len(t.Partitions))
  84. for partition := range t.Partitions {
  85. keys = append(keys, partition)
  86. }
  87. sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
  88. for _, partition := range keys {
  89. pe.putInt32(partition)
  90. if err := t.Partitions[partition].encode(pe); err != nil {
  91. return err
  92. }
  93. }
  94. return nil
  95. }
  96. func (t *DeleteRecordsResponseTopic) decode(pd packetDecoder, version int16) error {
  97. n, err := pd.getArrayLength()
  98. if err != nil {
  99. return err
  100. }
  101. if n > 0 {
  102. t.Partitions = make(map[int32]*DeleteRecordsResponsePartition, n)
  103. for i := 0; i < n; i++ {
  104. partition, err := pd.getInt32()
  105. if err != nil {
  106. return err
  107. }
  108. details := new(DeleteRecordsResponsePartition)
  109. if err = details.decode(pd, version); err != nil {
  110. return err
  111. }
  112. t.Partitions[partition] = details
  113. }
  114. }
  115. return nil
  116. }
  117. type DeleteRecordsResponsePartition struct {
  118. LowWatermark int64
  119. Err KError
  120. }
  121. func (t *DeleteRecordsResponsePartition) encode(pe packetEncoder) error {
  122. pe.putInt64(t.LowWatermark)
  123. pe.putInt16(int16(t.Err))
  124. return nil
  125. }
  126. func (t *DeleteRecordsResponsePartition) decode(pd packetDecoder, version int16) error {
  127. lowWatermark, err := pd.getInt64()
  128. if err != nil {
  129. return err
  130. }
  131. t.LowWatermark = lowWatermark
  132. kErr, err := pd.getInt16()
  133. if err != nil {
  134. return err
  135. }
  136. t.Err = KError(kErr)
  137. return nil
  138. }