admin.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package sarama
  2. import "errors"
  3. // ClusterAdmin is the administrative client for Kafka, which supports managing and inspecting topics,
  4. // brokers, configurations and ACLs. The minimum broker version required is 0.10.0.0.
  5. // Methods with stricter requirements will specify the minimum broker version required.
  6. // You MUST call Close() on a client to avoid leaks
  7. type ClusterAdmin interface {
  8. // Creates a new topic. This operation is supported by brokers with version 0.10.1.0 or higher.
  9. // It may take several seconds after CreateTopic returns success for all the brokers
  10. // to become aware that the topic has been created. During this time, listTopics
  11. // may not return information about the new topic.The validateOnly option is supported from version 0.10.2.0.
  12. CreateTopic(topic string, detail *TopicDetail, validateOnly bool) error
  13. // Delete a topic. It may take several seconds after the DeleteTopic to returns success
  14. // and for all the brokers to become aware that the topics are gone.
  15. // During this time, listTopics may continue to return information about the deleted topic.
  16. // If delete.topic.enable is false on the brokers, deleteTopic will mark
  17. // the topic for deletion, but not actually delete them.
  18. // This operation is supported by brokers with version 0.10.1.0 or higher.
  19. DeleteTopic(topic string) error
  20. // Increase the number of partitions of the topics according to the corresponding values.
  21. // If partitions are increased for a topic that has a key, the partition logic or ordering of
  22. // the messages will be affected. It may take several seconds after this method returns
  23. // success for all the brokers to become aware that the partitions have been created.
  24. // During this time, ClusterAdmin#describeTopics may not return information about the
  25. // new partitions. This operation is supported by brokers with version 1.0.0 or higher.
  26. CreatePartitions(topic string, count int32, assignment [][]int32, validateOnly bool) error
  27. // Delete records whose offset is smaller than the given offset of the corresponding partition.
  28. // This operation is supported by brokers with version 0.11.0.0 or higher.
  29. DeleteRecords(topic string, partitionOffsets map[int32]int64) error
  30. // Get the configuration for the specified resources.
  31. // The returned configuration includes default values and the Default is true
  32. // can be used to distinguish them from user supplied values.
  33. // Config entries where ReadOnly is true cannot be updated.
  34. // The value of config entries where Sensitive is true is always nil so
  35. // sensitive information is not disclosed.
  36. // This operation is supported by brokers with version 0.11.0.0 or higher.
  37. DescribeConfig(resource ConfigResource) ([]ConfigEntry, error)
  38. // Update the configuration for the specified resources with the default options.
  39. // This operation is supported by brokers with version 0.11.0.0 or higher.
  40. // The resources with their configs (topic is the only resource type with configs
  41. // that can be updated currently Updates are not transactional so they may succeed
  42. // for some resources while fail for others. The configs for a particular resource are updated automatically.
  43. AlterConfig(resourceType ConfigResourceType, name string, entries map[string]*string, validateOnly bool) error
  44. // Creates access control lists (ACLs) which are bound to specific resources.
  45. // This operation is not transactional so it may succeed for some ACLs while fail for others.
  46. // If you attempt to add an ACL that duplicates an existing ACL, no error will be raised, but
  47. // no changes will be made. This operation is supported by brokers with version 0.11.0.0 or higher.
  48. CreateACL(resource Resource, acl Acl) error
  49. // Lists access control lists (ACLs) according to the supplied filter.
  50. // it may take some time for changes made by createAcls or deleteAcls to be reflected in the output of ListAcls
  51. // This operation is supported by brokers with version 0.11.0.0 or higher.
  52. ListAcls(filter AclFilter) ([]ResourceAcls, error)
  53. // Deletes access control lists (ACLs) according to the supplied filters.
  54. // This operation is not transactional so it may succeed for some ACLs while fail for others.
  55. // This operation is supported by brokers with version 0.11.0.0 or higher.
  56. DeleteACL(filter AclFilter, validateOnly bool) ([]MatchingAcl, error)
  57. // Close shuts down the admin and closes underlying client.
  58. Close() error
  59. }
  60. type clusterAdmin struct {
  61. client Client
  62. conf *Config
  63. }
  64. // NewClusterAdmin creates a new ClusterAdmin using the given broker addresses and configuration.
  65. func NewClusterAdmin(addrs []string, conf *Config) (ClusterAdmin, error) {
  66. client, err := NewClient(addrs, conf)
  67. if err != nil {
  68. return nil, err
  69. }
  70. //make sure we can retrieve the controller
  71. _, err = client.Controller()
  72. if err != nil {
  73. return nil, err
  74. }
  75. ca := &clusterAdmin{
  76. client: client,
  77. conf: client.Config(),
  78. }
  79. return ca, nil
  80. }
  81. func (ca *clusterAdmin) Close() error {
  82. return ca.client.Close()
  83. }
  84. func (ca *clusterAdmin) Controller() (*Broker, error) {
  85. return ca.client.Controller()
  86. }
  87. func (ca *clusterAdmin) CreateTopic(topic string, detail *TopicDetail, validateOnly bool) error {
  88. if topic == "" {
  89. return ErrInvalidTopic
  90. }
  91. if detail == nil {
  92. return errors.New("You must specify topic details")
  93. }
  94. topicDetails := make(map[string]*TopicDetail)
  95. topicDetails[topic] = detail
  96. request := &CreateTopicsRequest{
  97. TopicDetails: topicDetails,
  98. ValidateOnly: validateOnly,
  99. }
  100. if ca.conf.Version.IsAtLeast(V0_11_0_0) {
  101. request.Version = 1
  102. }
  103. if ca.conf.Version.IsAtLeast(V1_0_0_0) {
  104. request.Version = 2
  105. }
  106. b, err := ca.Controller()
  107. if err != nil {
  108. return err
  109. }
  110. rsp, err := b.CreateTopics(request)
  111. if err != nil {
  112. return err
  113. }
  114. topicErr, ok := rsp.TopicErrors[topic]
  115. if !ok {
  116. return ErrIncompleteResponse
  117. }
  118. if topicErr.Err != ErrNoError {
  119. return topicErr.Err
  120. }
  121. return nil
  122. }
  123. func (ca *clusterAdmin) DeleteTopic(topic string) error {
  124. if topic == "" {
  125. return ErrInvalidTopic
  126. }
  127. request := &DeleteTopicsRequest{Topics: []string{topic}}
  128. if ca.conf.Version.IsAtLeast(V0_11_0_0) {
  129. request.Version = 1
  130. }
  131. b, err := ca.Controller()
  132. if err != nil {
  133. return err
  134. }
  135. rsp, err := b.DeleteTopics(request)
  136. if err != nil {
  137. return err
  138. }
  139. topicErr, ok := rsp.TopicErrorCodes[topic]
  140. if !ok {
  141. return ErrIncompleteResponse
  142. }
  143. if topicErr != ErrNoError {
  144. return topicErr
  145. }
  146. return nil
  147. }
  148. func (ca *clusterAdmin) CreatePartitions(topic string, count int32, assignment [][]int32, validateOnly bool) error {
  149. if topic == "" {
  150. return ErrInvalidTopic
  151. }
  152. topicPartitions := make(map[string]*TopicPartition)
  153. topicPartitions[topic] = &TopicPartition{Count: count, Assignment: assignment}
  154. request := &CreatePartitionsRequest{
  155. TopicPartitions: topicPartitions,
  156. }
  157. b, err := ca.Controller()
  158. if err != nil {
  159. return err
  160. }
  161. rsp, err := b.CreatePartitions(request)
  162. if err != nil {
  163. return err
  164. }
  165. topicErr, ok := rsp.TopicPartitionErrors[topic]
  166. if !ok {
  167. return ErrIncompleteResponse
  168. }
  169. if topicErr.Err != ErrNoError {
  170. return topicErr.Err
  171. }
  172. return nil
  173. }
  174. func (ca *clusterAdmin) DeleteRecords(topic string, partitionOffsets map[int32]int64) error {
  175. if topic == "" {
  176. return ErrInvalidTopic
  177. }
  178. topics := make(map[string]*DeleteRecordsRequestTopic)
  179. topics[topic] = &DeleteRecordsRequestTopic{PartitionOffsets: partitionOffsets}
  180. request := &DeleteRecordsRequest{
  181. Topics: topics}
  182. b, err := ca.Controller()
  183. if err != nil {
  184. return err
  185. }
  186. rsp, err := b.DeleteRecords(request)
  187. if err != nil {
  188. return err
  189. }
  190. _, ok := rsp.Topics[topic]
  191. if !ok {
  192. return ErrIncompleteResponse
  193. }
  194. //todo since we are dealing with couple of partitions it would be good if we return slice of errors
  195. //for each partition instead of one error
  196. return nil
  197. }
  198. func (ca *clusterAdmin) DescribeConfig(resource ConfigResource) ([]ConfigEntry, error) {
  199. var entries []ConfigEntry
  200. var resources []*ConfigResource
  201. resources = append(resources, &resource)
  202. request := &DescribeConfigsRequest{
  203. Resources: resources,
  204. }
  205. b, err := ca.Controller()
  206. if err != nil {
  207. return nil, err
  208. }
  209. rsp, err := b.DescribeConfigs(request)
  210. if err != nil {
  211. return nil, err
  212. }
  213. for _, rspResource := range rsp.Resources {
  214. if rspResource.Name == resource.Name {
  215. if rspResource.ErrorMsg != "" {
  216. return nil, errors.New(rspResource.ErrorMsg)
  217. }
  218. for _, cfgEntry := range rspResource.Configs {
  219. entries = append(entries, *cfgEntry)
  220. }
  221. }
  222. }
  223. return entries, nil
  224. }
  225. func (ca *clusterAdmin) AlterConfig(resourceType ConfigResourceType, name string, entries map[string]*string, validateOnly bool) error {
  226. var resources []*AlterConfigsResource
  227. resources = append(resources, &AlterConfigsResource{
  228. Type: resourceType,
  229. Name: name,
  230. ConfigEntries: entries,
  231. })
  232. request := &AlterConfigsRequest{
  233. Resources: resources,
  234. ValidateOnly: validateOnly,
  235. }
  236. b, err := ca.Controller()
  237. if err != nil {
  238. return err
  239. }
  240. rsp, err := b.AlterConfigs(request)
  241. if err != nil {
  242. return err
  243. }
  244. for _, rspResource := range rsp.Resources {
  245. if rspResource.Name == name {
  246. if rspResource.ErrorMsg != "" {
  247. return errors.New(rspResource.ErrorMsg)
  248. }
  249. }
  250. }
  251. return nil
  252. }
  253. func (ca *clusterAdmin) CreateACL(resource Resource, acl Acl) error {
  254. var acls []*AclCreation
  255. acls = append(acls, &AclCreation{resource, acl})
  256. request := &CreateAclsRequest{AclCreations: acls}
  257. b, err := ca.Controller()
  258. if err != nil {
  259. return err
  260. }
  261. _, err = b.CreateAcls(request)
  262. return err
  263. }
  264. func (ca *clusterAdmin) ListAcls(filter AclFilter) ([]ResourceAcls, error) {
  265. request := &DescribeAclsRequest{AclFilter: filter}
  266. b, err := ca.Controller()
  267. if err != nil {
  268. return nil, err
  269. }
  270. rsp, err := b.DescribeAcls(request)
  271. if err != nil {
  272. return nil, err
  273. }
  274. var lAcls []ResourceAcls
  275. for _, rAcl := range rsp.ResourceAcls {
  276. lAcls = append(lAcls, *rAcl)
  277. }
  278. return lAcls, nil
  279. }
  280. func (ca *clusterAdmin) DeleteACL(filter AclFilter, validateOnly bool) ([]MatchingAcl, error) {
  281. var filters []*AclFilter
  282. filters = append(filters, &filter)
  283. request := &DeleteAclsRequest{Filters: filters}
  284. b, err := ca.Controller()
  285. if err != nil {
  286. return nil, err
  287. }
  288. rsp, err := b.DeleteAcls(request)
  289. if err != nil {
  290. return nil, err
  291. }
  292. var mAcls []MatchingAcl
  293. for _, fr := range rsp.FilterResponses {
  294. for _, mACL := range fr.MatchingAcls {
  295. mAcls = append(mAcls, *mACL)
  296. }
  297. }
  298. return mAcls, nil
  299. }