create_topics_request.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package sarama
  2. import (
  3. "time"
  4. )
  5. type CreateTopicsRequest struct {
  6. Version int16
  7. TopicDetails map[string]*TopicDetail
  8. Timeout time.Duration
  9. ValidateOnly bool
  10. }
  11. func (c *CreateTopicsRequest) encode(pe packetEncoder) error {
  12. if err := pe.putArrayLength(len(c.TopicDetails)); err != nil {
  13. return err
  14. }
  15. for topic, detail := range c.TopicDetails {
  16. if err := pe.putString(topic); err != nil {
  17. return err
  18. }
  19. if err := detail.encode(pe); err != nil {
  20. return err
  21. }
  22. }
  23. pe.putInt32(int32(c.Timeout / time.Millisecond))
  24. if c.Version >= 1 {
  25. pe.putBool(c.ValidateOnly)
  26. }
  27. return nil
  28. }
  29. func (c *CreateTopicsRequest) decode(pd packetDecoder, version int16) (err error) {
  30. n, err := pd.getArrayLength()
  31. if err != nil {
  32. return err
  33. }
  34. c.TopicDetails = make(map[string]*TopicDetail, n)
  35. for i := 0; i < n; i++ {
  36. topic, err := pd.getString()
  37. if err != nil {
  38. return err
  39. }
  40. c.TopicDetails[topic] = new(TopicDetail)
  41. if err = c.TopicDetails[topic].decode(pd, version); err != nil {
  42. return err
  43. }
  44. }
  45. timeout, err := pd.getInt32()
  46. if err != nil {
  47. return err
  48. }
  49. c.Timeout = time.Duration(timeout) * time.Millisecond
  50. if version >= 1 {
  51. c.ValidateOnly, err = pd.getBool()
  52. if err != nil {
  53. return err
  54. }
  55. c.Version = version
  56. }
  57. return nil
  58. }
  59. func (c *CreateTopicsRequest) key() int16 {
  60. return 19
  61. }
  62. func (c *CreateTopicsRequest) version() int16 {
  63. return c.Version
  64. }
  65. func (r *CreateTopicsRequest) headerVersion() int16 {
  66. return 1
  67. }
  68. func (c *CreateTopicsRequest) requiredVersion() KafkaVersion {
  69. switch c.Version {
  70. case 2:
  71. return V1_0_0_0
  72. case 1:
  73. return V0_11_0_0
  74. default:
  75. return V0_10_1_0
  76. }
  77. }
  78. type TopicDetail struct {
  79. NumPartitions int32
  80. ReplicationFactor int16
  81. ReplicaAssignment map[int32][]int32
  82. ConfigEntries map[string]*string
  83. }
  84. func (t *TopicDetail) encode(pe packetEncoder) error {
  85. pe.putInt32(t.NumPartitions)
  86. pe.putInt16(t.ReplicationFactor)
  87. if err := pe.putArrayLength(len(t.ReplicaAssignment)); err != nil {
  88. return err
  89. }
  90. for partition, assignment := range t.ReplicaAssignment {
  91. pe.putInt32(partition)
  92. if err := pe.putInt32Array(assignment); err != nil {
  93. return err
  94. }
  95. }
  96. if err := pe.putArrayLength(len(t.ConfigEntries)); err != nil {
  97. return err
  98. }
  99. for configKey, configValue := range t.ConfigEntries {
  100. if err := pe.putString(configKey); err != nil {
  101. return err
  102. }
  103. if err := pe.putNullableString(configValue); err != nil {
  104. return err
  105. }
  106. }
  107. return nil
  108. }
  109. func (t *TopicDetail) decode(pd packetDecoder, version int16) (err error) {
  110. if t.NumPartitions, err = pd.getInt32(); err != nil {
  111. return err
  112. }
  113. if t.ReplicationFactor, err = pd.getInt16(); err != nil {
  114. return err
  115. }
  116. n, err := pd.getArrayLength()
  117. if err != nil {
  118. return err
  119. }
  120. if n > 0 {
  121. t.ReplicaAssignment = make(map[int32][]int32, n)
  122. for i := 0; i < n; i++ {
  123. replica, err := pd.getInt32()
  124. if err != nil {
  125. return err
  126. }
  127. if t.ReplicaAssignment[replica], err = pd.getInt32Array(); err != nil {
  128. return err
  129. }
  130. }
  131. }
  132. n, err = pd.getArrayLength()
  133. if err != nil {
  134. return err
  135. }
  136. if n > 0 {
  137. t.ConfigEntries = make(map[string]*string, n)
  138. for i := 0; i < n; i++ {
  139. configKey, err := pd.getString()
  140. if err != nil {
  141. return err
  142. }
  143. if t.ConfigEntries[configKey], err = pd.getNullableString(); err != nil {
  144. return err
  145. }
  146. }
  147. }
  148. return nil
  149. }