delete_records_response.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) requiredVersion() KafkaVersion {
  71. return V0_11_0_0
  72. }
  73. type DeleteRecordsResponseTopic struct {
  74. Partitions map[int32]*DeleteRecordsResponsePartition
  75. }
  76. func (t *DeleteRecordsResponseTopic) encode(pe packetEncoder) error {
  77. if err := pe.putArrayLength(len(t.Partitions)); err != nil {
  78. return err
  79. }
  80. keys := make([]int32, 0, len(t.Partitions))
  81. for partition := range t.Partitions {
  82. keys = append(keys, partition)
  83. }
  84. sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
  85. for _, partition := range keys {
  86. pe.putInt32(partition)
  87. if err := t.Partitions[partition].encode(pe); err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. func (t *DeleteRecordsResponseTopic) decode(pd packetDecoder, version int16) error {
  94. n, err := pd.getArrayLength()
  95. if err != nil {
  96. return err
  97. }
  98. if n > 0 {
  99. t.Partitions = make(map[int32]*DeleteRecordsResponsePartition, n)
  100. for i := 0; i < n; i++ {
  101. partition, err := pd.getInt32()
  102. if err != nil {
  103. return err
  104. }
  105. details := new(DeleteRecordsResponsePartition)
  106. if err = details.decode(pd, version); err != nil {
  107. return err
  108. }
  109. t.Partitions[partition] = details
  110. }
  111. }
  112. return nil
  113. }
  114. type DeleteRecordsResponsePartition struct {
  115. LowWatermark int64
  116. Err KError
  117. }
  118. func (t *DeleteRecordsResponsePartition) encode(pe packetEncoder) error {
  119. pe.putInt64(t.LowWatermark)
  120. pe.putInt16(int16(t.Err))
  121. return nil
  122. }
  123. func (t *DeleteRecordsResponsePartition) decode(pd packetDecoder, version int16) error {
  124. lowWatermark, err := pd.getInt64()
  125. if err != nil {
  126. return err
  127. }
  128. t.LowWatermark = lowWatermark
  129. kErr, err := pd.getInt16()
  130. if err != nil {
  131. return err
  132. }
  133. t.Err = KError(kErr)
  134. return nil
  135. }