Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

200 řádky
5.1 KiB

  1. package kafka
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "net"
  7. "time"
  8. "github.com/segmentio/kafka-go/protocol/deletetopics"
  9. )
  10. // DeleteTopicsRequest represents a request sent to a kafka broker to delete
  11. // topics.
  12. type DeleteTopicsRequest struct {
  13. // Address of the kafka broker to send the request to.
  14. Addr net.Addr
  15. // Names of topics to delete.
  16. Topics []string
  17. }
  18. // DeleteTopicsResponse represents a response from a kafka broker to a topic
  19. // deletion request.
  20. type DeleteTopicsResponse struct {
  21. // The amount of time that the broker throttled the request.
  22. //
  23. // This field will be zero if the kafka broker did not support the
  24. // DeleteTopics API in version 1 or above.
  25. Throttle time.Duration
  26. // Mapping of topic names to errors that occurred while attempting to delete
  27. // the topics.
  28. //
  29. // The errors contain the kafka error code. Programs may use the standard
  30. // errors.Is function to test the error against kafka error codes.
  31. Errors map[string]error
  32. }
  33. // DeleteTopics sends a topic deletion request to a kafka broker and returns the
  34. // response.
  35. func (c *Client) DeleteTopics(ctx context.Context, req *DeleteTopicsRequest) (*DeleteTopicsResponse, error) {
  36. m, err := c.roundTrip(ctx, req.Addr, &deletetopics.Request{
  37. TopicNames: req.Topics,
  38. TimeoutMs: c.timeoutMs(ctx, defaultDeleteTopicsTimeout),
  39. })
  40. if err != nil {
  41. return nil, fmt.Errorf("kafka.(*Client).DeleteTopics: %w", err)
  42. }
  43. res := m.(*deletetopics.Response)
  44. ret := &DeleteTopicsResponse{
  45. Throttle: makeDuration(res.ThrottleTimeMs),
  46. Errors: make(map[string]error, len(res.Responses)),
  47. }
  48. for _, t := range res.Responses {
  49. if t.ErrorCode == 0 {
  50. ret.Errors[t.Name] = nil
  51. } else {
  52. ret.Errors[t.Name] = Error(t.ErrorCode)
  53. }
  54. }
  55. return ret, nil
  56. }
  57. // See http://kafka.apache.org/protocol.html#The_Messages_DeleteTopics
  58. type deleteTopicsRequest struct {
  59. // Topics holds the topic names
  60. Topics []string
  61. // Timeout holds the time in ms to wait for a topic to be completely deleted
  62. // on the controller node. Values <= 0 will trigger topic deletion and return
  63. // immediately.
  64. Timeout int32
  65. }
  66. func (t deleteTopicsRequest) size() int32 {
  67. return sizeofStringArray(t.Topics) +
  68. sizeofInt32(t.Timeout)
  69. }
  70. func (t deleteTopicsRequest) writeTo(wb *writeBuffer) {
  71. wb.writeStringArray(t.Topics)
  72. wb.writeInt32(t.Timeout)
  73. }
  74. type deleteTopicsResponse struct {
  75. v apiVersion // v0, v1
  76. ThrottleTime int32
  77. // TopicErrorCodes holds per topic error codes
  78. TopicErrorCodes []deleteTopicsResponseV0TopicErrorCode
  79. }
  80. func (t deleteTopicsResponse) size() int32 {
  81. sz := sizeofArray(len(t.TopicErrorCodes), func(i int) int32 { return t.TopicErrorCodes[i].size() })
  82. if t.v >= v1 {
  83. sz += sizeofInt32(t.ThrottleTime)
  84. }
  85. return sz
  86. }
  87. func (t *deleteTopicsResponse) readFrom(r *bufio.Reader, size int) (remain int, err error) {
  88. fn := func(withReader *bufio.Reader, withSize int) (fnRemain int, fnErr error) {
  89. var item deleteTopicsResponseV0TopicErrorCode
  90. if fnRemain, fnErr = (&item).readFrom(withReader, withSize); fnErr != nil {
  91. return
  92. }
  93. t.TopicErrorCodes = append(t.TopicErrorCodes, item)
  94. return
  95. }
  96. remain = size
  97. if t.v >= v1 {
  98. if remain, err = readInt32(r, size, &t.ThrottleTime); err != nil {
  99. return
  100. }
  101. }
  102. if remain, err = readArrayWith(r, remain, fn); err != nil {
  103. return
  104. }
  105. return
  106. }
  107. func (t deleteTopicsResponse) writeTo(wb *writeBuffer) {
  108. if t.v >= v1 {
  109. wb.writeInt32(t.ThrottleTime)
  110. }
  111. wb.writeArray(len(t.TopicErrorCodes), func(i int) { t.TopicErrorCodes[i].writeTo(wb) })
  112. }
  113. type deleteTopicsResponseV0TopicErrorCode struct {
  114. // Topic holds the topic name
  115. Topic string
  116. // ErrorCode holds the error code
  117. ErrorCode int16
  118. }
  119. func (t deleteTopicsResponseV0TopicErrorCode) size() int32 {
  120. return sizeofString(t.Topic) +
  121. sizeofInt16(t.ErrorCode)
  122. }
  123. func (t *deleteTopicsResponseV0TopicErrorCode) readFrom(r *bufio.Reader, size int) (remain int, err error) {
  124. if remain, err = readString(r, size, &t.Topic); err != nil {
  125. return
  126. }
  127. if remain, err = readInt16(r, remain, &t.ErrorCode); err != nil {
  128. return
  129. }
  130. return
  131. }
  132. func (t deleteTopicsResponseV0TopicErrorCode) writeTo(wb *writeBuffer) {
  133. wb.writeString(t.Topic)
  134. wb.writeInt16(t.ErrorCode)
  135. }
  136. // deleteTopics deletes the specified topics.
  137. //
  138. // See http://kafka.apache.org/protocol.html#The_Messages_DeleteTopics
  139. func (c *Conn) deleteTopics(request deleteTopicsRequest) (deleteTopicsResponse, error) {
  140. version, err := c.negotiateVersion(deleteTopics, v0, v1)
  141. if err != nil {
  142. return deleteTopicsResponse{}, err
  143. }
  144. response := deleteTopicsResponse{
  145. v: version,
  146. }
  147. err = c.writeOperation(
  148. func(deadline time.Time, id int32) error {
  149. if request.Timeout == 0 {
  150. now := time.Now()
  151. deadline = adjustDeadlineForRTT(deadline, now, defaultRTT)
  152. request.Timeout = milliseconds(deadlineToTimeout(deadline, now))
  153. }
  154. return c.writeRequest(deleteTopics, version, id, request)
  155. },
  156. func(deadline time.Time, size int) error {
  157. return expectZeroSize(func() (remain int, err error) {
  158. return (&response).readFrom(&c.rbuf, size)
  159. }())
  160. },
  161. )
  162. if err != nil {
  163. return deleteTopicsResponse{}, err
  164. }
  165. for _, c := range response.TopicErrorCodes {
  166. if c.ErrorCode != 0 {
  167. return response, Error(c.ErrorCode)
  168. }
  169. }
  170. return response, nil
  171. }