describe_configs_response.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package sarama
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. type ConfigSource int8
  7. func (s ConfigSource) String() string {
  8. switch s {
  9. case SourceUnknown:
  10. return "Unknown"
  11. case SourceTopic:
  12. return "Topic"
  13. case SourceDynamicBroker:
  14. return "DynamicBroker"
  15. case SourceDynamicDefaultBroker:
  16. return "DynamicDefaultBroker"
  17. case SourceStaticBroker:
  18. return "StaticBroker"
  19. case SourceDefault:
  20. return "Default"
  21. }
  22. return fmt.Sprintf("Source Invalid: %d", int(s))
  23. }
  24. const (
  25. SourceUnknown ConfigSource = iota
  26. SourceTopic
  27. SourceDynamicBroker
  28. SourceDynamicDefaultBroker
  29. SourceStaticBroker
  30. SourceDefault
  31. )
  32. type DescribeConfigsResponse struct {
  33. Version int16
  34. ThrottleTime time.Duration
  35. Resources []*ResourceResponse
  36. }
  37. type ResourceResponse struct {
  38. ErrorCode int16
  39. ErrorMsg string
  40. Type ConfigResourceType
  41. Name string
  42. Configs []*ConfigEntry
  43. }
  44. type ConfigEntry struct {
  45. Name string
  46. Value string
  47. ReadOnly bool
  48. Default bool
  49. Source ConfigSource
  50. Sensitive bool
  51. Synonyms []*ConfigSynonym
  52. }
  53. type ConfigSynonym struct {
  54. ConfigName string
  55. ConfigValue string
  56. Source ConfigSource
  57. }
  58. func (r *DescribeConfigsResponse) encode(pe packetEncoder) (err error) {
  59. pe.putInt32(int32(r.ThrottleTime / time.Millisecond))
  60. if err = pe.putArrayLength(len(r.Resources)); err != nil {
  61. return err
  62. }
  63. for _, c := range r.Resources {
  64. if err = c.encode(pe, r.Version); err != nil {
  65. return err
  66. }
  67. }
  68. return nil
  69. }
  70. func (r *DescribeConfigsResponse) decode(pd packetDecoder, version int16) (err error) {
  71. r.Version = version
  72. throttleTime, err := pd.getInt32()
  73. if err != nil {
  74. return err
  75. }
  76. r.ThrottleTime = time.Duration(throttleTime) * time.Millisecond
  77. n, err := pd.getArrayLength()
  78. if err != nil {
  79. return err
  80. }
  81. r.Resources = make([]*ResourceResponse, n)
  82. for i := 0; i < n; i++ {
  83. rr := &ResourceResponse{}
  84. if err := rr.decode(pd, version); err != nil {
  85. return err
  86. }
  87. r.Resources[i] = rr
  88. }
  89. return nil
  90. }
  91. func (r *DescribeConfigsResponse) key() int16 {
  92. return 32
  93. }
  94. func (r *DescribeConfigsResponse) version() int16 {
  95. return r.Version
  96. }
  97. func (r *DescribeConfigsResponse) headerVersion() int16 {
  98. return 0
  99. }
  100. func (r *DescribeConfigsResponse) requiredVersion() KafkaVersion {
  101. switch r.Version {
  102. case 1:
  103. return V1_0_0_0
  104. case 2:
  105. return V2_0_0_0
  106. default:
  107. return V0_11_0_0
  108. }
  109. }
  110. func (r *ResourceResponse) encode(pe packetEncoder, version int16) (err error) {
  111. pe.putInt16(r.ErrorCode)
  112. if err = pe.putString(r.ErrorMsg); err != nil {
  113. return err
  114. }
  115. pe.putInt8(int8(r.Type))
  116. if err = pe.putString(r.Name); err != nil {
  117. return err
  118. }
  119. if err = pe.putArrayLength(len(r.Configs)); err != nil {
  120. return err
  121. }
  122. for _, c := range r.Configs {
  123. if err = c.encode(pe, version); err != nil {
  124. return err
  125. }
  126. }
  127. return nil
  128. }
  129. func (r *ResourceResponse) decode(pd packetDecoder, version int16) (err error) {
  130. ec, err := pd.getInt16()
  131. if err != nil {
  132. return err
  133. }
  134. r.ErrorCode = ec
  135. em, err := pd.getString()
  136. if err != nil {
  137. return err
  138. }
  139. r.ErrorMsg = em
  140. t, err := pd.getInt8()
  141. if err != nil {
  142. return err
  143. }
  144. r.Type = ConfigResourceType(t)
  145. name, err := pd.getString()
  146. if err != nil {
  147. return err
  148. }
  149. r.Name = name
  150. n, err := pd.getArrayLength()
  151. if err != nil {
  152. return err
  153. }
  154. r.Configs = make([]*ConfigEntry, n)
  155. for i := 0; i < n; i++ {
  156. c := &ConfigEntry{}
  157. if err := c.decode(pd, version); err != nil {
  158. return err
  159. }
  160. r.Configs[i] = c
  161. }
  162. return nil
  163. }
  164. func (r *ConfigEntry) encode(pe packetEncoder, version int16) (err error) {
  165. if err = pe.putString(r.Name); err != nil {
  166. return err
  167. }
  168. if err = pe.putString(r.Value); err != nil {
  169. return err
  170. }
  171. pe.putBool(r.ReadOnly)
  172. if version <= 0 {
  173. pe.putBool(r.Default)
  174. pe.putBool(r.Sensitive)
  175. } else {
  176. pe.putInt8(int8(r.Source))
  177. pe.putBool(r.Sensitive)
  178. if err := pe.putArrayLength(len(r.Synonyms)); err != nil {
  179. return err
  180. }
  181. for _, c := range r.Synonyms {
  182. if err = c.encode(pe, version); err != nil {
  183. return err
  184. }
  185. }
  186. }
  187. return nil
  188. }
  189. //https://cwiki.apache.org/confluence/display/KAFKA/KIP-226+-+Dynamic+Broker+Configuration
  190. func (r *ConfigEntry) decode(pd packetDecoder, version int16) (err error) {
  191. if version == 0 {
  192. r.Source = SourceUnknown
  193. }
  194. name, err := pd.getString()
  195. if err != nil {
  196. return err
  197. }
  198. r.Name = name
  199. value, err := pd.getString()
  200. if err != nil {
  201. return err
  202. }
  203. r.Value = value
  204. read, err := pd.getBool()
  205. if err != nil {
  206. return err
  207. }
  208. r.ReadOnly = read
  209. if version == 0 {
  210. defaultB, err := pd.getBool()
  211. if err != nil {
  212. return err
  213. }
  214. r.Default = defaultB
  215. if defaultB {
  216. r.Source = SourceDefault
  217. }
  218. } else {
  219. source, err := pd.getInt8()
  220. if err != nil {
  221. return err
  222. }
  223. r.Source = ConfigSource(source)
  224. r.Default = r.Source == SourceDefault
  225. }
  226. sensitive, err := pd.getBool()
  227. if err != nil {
  228. return err
  229. }
  230. r.Sensitive = sensitive
  231. if version > 0 {
  232. n, err := pd.getArrayLength()
  233. if err != nil {
  234. return err
  235. }
  236. r.Synonyms = make([]*ConfigSynonym, n)
  237. for i := 0; i < n; i++ {
  238. s := &ConfigSynonym{}
  239. if err := s.decode(pd, version); err != nil {
  240. return err
  241. }
  242. r.Synonyms[i] = s
  243. }
  244. }
  245. return nil
  246. }
  247. func (c *ConfigSynonym) encode(pe packetEncoder, version int16) (err error) {
  248. err = pe.putString(c.ConfigName)
  249. if err != nil {
  250. return err
  251. }
  252. err = pe.putString(c.ConfigValue)
  253. if err != nil {
  254. return err
  255. }
  256. pe.putInt8(int8(c.Source))
  257. return nil
  258. }
  259. func (c *ConfigSynonym) decode(pd packetDecoder, version int16) error {
  260. name, err := pd.getString()
  261. if err != nil {
  262. return nil
  263. }
  264. c.ConfigName = name
  265. value, err := pd.getString()
  266. if err != nil {
  267. return nil
  268. }
  269. c.ConfigValue = value
  270. source, err := pd.getInt8()
  271. if err != nil {
  272. return nil
  273. }
  274. c.Source = ConfigSource(source)
  275. return nil
  276. }