forked from confluentinc/confluent-kafka-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadminapi.py
executable file
·476 lines (390 loc) · 16.5 KB
/
adminapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env python
#
# Copyright 2018 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Example use of AdminClient operations.
from confluent_kafka.admin import (AdminClient, NewTopic, NewPartitions, ConfigResource, ConfigSource,
AclBinding, AclBindingFilter, ResourceType, ResourcePatternType,
AclOperation, AclPermissionType)
from confluent_kafka import KafkaException
import sys
import threading
import logging
logging.basicConfig()
def parse_nullable_string(s):
if s == "None":
return None
else:
return s
def example_create_topics(a, topics):
""" Create topics """
new_topics = [NewTopic(topic, num_partitions=3, replication_factor=1) for topic in topics]
# Call create_topics to asynchronously create topics, a dict
# of <topic,future> is returned.
fs = a.create_topics(new_topics)
# Wait for operation to finish.
# Timeouts are preferably controlled by passing request_timeout=15.0
# to the create_topics() call.
# All futures will finish at the same time.
for topic, f in fs.items():
try:
f.result() # The result itself is None
print("Topic {} created".format(topic))
except Exception as e:
print("Failed to create topic {}: {}".format(topic, e))
def example_delete_topics(a, topics):
""" delete topics """
# Call delete_topics to asynchronously delete topics, a future is returned.
# By default this operation on the broker returns immediately while
# topics are deleted in the background. But here we give it some time (30s)
# to propagate in the cluster before returning.
#
# Returns a dict of <topic,future>.
fs = a.delete_topics(topics, operation_timeout=30)
# Wait for operation to finish.
for topic, f in fs.items():
try:
f.result() # The result itself is None
print("Topic {} deleted".format(topic))
except Exception as e:
print("Failed to delete topic {}: {}".format(topic, e))
def example_create_partitions(a, topics):
""" create partitions """
new_parts = [NewPartitions(topic, int(new_total_count)) for
topic, new_total_count in zip(topics[0::2], topics[1::2])]
# Try switching validate_only to True to only validate the operation
# on the broker but not actually perform it.
fs = a.create_partitions(new_parts, validate_only=False)
# Wait for operation to finish.
for topic, f in fs.items():
try:
f.result() # The result itself is None
print("Additional partitions created for topic {}".format(topic))
except Exception as e:
print("Failed to add partitions to topic {}: {}".format(topic, e))
def print_config(config, depth):
print('%40s = %-50s [%s,is:read-only=%r,default=%r,sensitive=%r,synonym=%r,synonyms=%s]' %
((' ' * depth) + config.name, config.value, ConfigSource(config.source),
config.is_read_only, config.is_default,
config.is_sensitive, config.is_synonym,
["%s:%s" % (x.name, ConfigSource(x.source))
for x in iter(config.synonyms.values())]))
def example_describe_configs(a, args):
""" describe configs """
resources = [ConfigResource(restype, resname) for
restype, resname in zip(args[0::2], args[1::2])]
fs = a.describe_configs(resources)
# Wait for operation to finish.
for res, f in fs.items():
try:
configs = f.result()
for config in iter(configs.values()):
print_config(config, 1)
except KafkaException as e:
print("Failed to describe {}: {}".format(res, e))
except Exception:
raise
def example_create_acls(a, args):
""" create acls """
acl_bindings = [
AclBinding(
ResourceType[restype],
parse_nullable_string(resname),
ResourcePatternType[resource_pattern_type],
parse_nullable_string(principal),
parse_nullable_string(host),
AclOperation[operation],
AclPermissionType[permission_type]
)
for restype, resname, resource_pattern_type,
principal, host, operation, permission_type
in zip(
args[0::7],
args[1::7],
args[2::7],
args[3::7],
args[4::7],
args[5::7],
args[6::7],
)
]
try:
fs = a.create_acls(acl_bindings, request_timeout=10)
except ValueError as e:
print(f"create_acls() failed: {e}")
return
# Wait for operation to finish.
for res, f in fs.items():
try:
result = f.result()
if result is None:
print("Created {}".format(res))
except KafkaException as e:
print("Failed to create ACL {}: {}".format(res, e))
except Exception:
raise
def example_describe_acls(a, args):
""" describe acls """
acl_binding_filters = [
AclBindingFilter(
ResourceType[restype],
parse_nullable_string(resname),
ResourcePatternType[resource_pattern_type],
parse_nullable_string(principal),
parse_nullable_string(host),
AclOperation[operation],
AclPermissionType[permission_type]
)
for restype, resname, resource_pattern_type,
principal, host, operation, permission_type
in zip(
args[0::7],
args[1::7],
args[2::7],
args[3::7],
args[4::7],
args[5::7],
args[6::7],
)
]
fs = [
a.describe_acls(acl_binding_filter, request_timeout=10)
for acl_binding_filter in acl_binding_filters
]
# Wait for operations to finish.
for acl_binding_filter, f in zip(acl_binding_filters, fs):
try:
print("Acls matching filter: {}".format(acl_binding_filter))
acl_bindings = f.result()
for acl_binding in acl_bindings:
print(acl_binding)
except KafkaException as e:
print("Failed to describe {}: {}".format(acl_binding_filter, e))
except Exception:
raise
def example_delete_acls(a, args):
""" delete acls """
acl_binding_filters = [
AclBindingFilter(
ResourceType[restype],
parse_nullable_string(resname),
ResourcePatternType[resource_pattern_type],
parse_nullable_string(principal),
parse_nullable_string(host),
AclOperation[operation],
AclPermissionType[permission_type]
)
for restype, resname, resource_pattern_type,
principal, host, operation, permission_type
in zip(
args[0::7],
args[1::7],
args[2::7],
args[3::7],
args[4::7],
args[5::7],
args[6::7],
)
]
try:
fs = a.delete_acls(acl_binding_filters, request_timeout=10)
except ValueError as e:
print(f"delete_acls() failed: {e}")
return
# Wait for operation to finish.
for res, f in fs.items():
try:
acl_bindings = f.result()
print("Deleted acls matching filter: {}".format(res))
for acl_binding in acl_bindings:
print(" ", acl_binding)
except KafkaException as e:
print("Failed to delete {}: {}".format(res, e))
except Exception:
raise
def example_alter_configs(a, args):
""" Alter configs atomically, replacing non-specified
configuration properties with their default values.
"""
resources = []
for restype, resname, configs in zip(args[0::3], args[1::3], args[2::3]):
resource = ConfigResource(restype, resname)
resources.append(resource)
for k, v in [conf.split('=') for conf in configs.split(',')]:
resource.set_config(k, v)
fs = a.alter_configs(resources)
# Wait for operation to finish.
for res, f in fs.items():
try:
f.result() # empty, but raises exception on failure
print("{} configuration successfully altered".format(res))
except Exception:
raise
def example_delta_alter_configs(a, args):
"""
The AlterConfigs Kafka API requires all configuration to be passed,
any left out configuration properties will revert to their default settings.
This example shows how to just modify the supplied configuration entries
by first reading the configuration from the broker, updating the supplied
configuration with the broker configuration (without overwriting), and
then writing it all back.
The async nature of futures is also show-cased, which makes this example
a bit more complex than it needs to be in the synchronous case.
"""
# Convert supplied config to resources.
# We can reuse the same resources both for describe_configs and
# alter_configs.
resources = []
for restype, resname, configs in zip(args[0::3], args[1::3], args[2::3]):
resource = ConfigResource(restype, resname)
resources.append(resource)
for k, v in [conf.split('=') for conf in configs.split(',')]:
resource.set_config(k, v)
# Set up a locked counter and an Event (for signaling) to track when the
# second level of futures are done. This is a bit of contrived example
# due to no other asynchronous mechanism being used, so we'll need
# to wait on something to signal completion.
class WaitZero(object):
def __init__(self, waitcnt):
self.cnt = waitcnt
self.lock = threading.Lock()
self.event = threading.Event()
def decr(self):
""" Decrement cnt by 1"""
with self.lock:
assert self.cnt > 0
self.cnt -= 1
self.event.set()
def wait(self):
""" Wait until cnt reaches 0 """
self.lock.acquire()
while self.cnt > 0:
self.lock.release()
self.event.wait()
self.event.clear()
self.lock.acquire()
self.lock.release()
def __len__(self):
with self.lock:
return self.cnt
wait_zero = WaitZero(len(resources))
# Read existing configuration from cluster
fs = a.describe_configs(resources)
def delta_alter_configs_done(fut, resource):
e = fut.exception()
if e is not None:
print("Config update for {} failed: {}".format(resource, e))
else:
print("Config for {} updated".format(resource))
wait_zero.decr()
def delta_alter_configs(resource, remote_config):
print("Updating {} supplied config entries {} with {} config entries read from cluster".format(
len(resource), resource, len(remote_config)))
# Only set configuration that is not default
for k, entry in [(k, v) for k, v in remote_config.items() if not v.is_default]:
resource.set_config(k, entry.value, overwrite=False)
fs = a.alter_configs([resource])
fs[resource].add_done_callback(lambda fut: delta_alter_configs_done(fut, resource))
# For each resource's future set up a completion callback
# that in turn calls alter_configs() on that single resource.
# This is ineffective since the resources can usually go in
# one single alter_configs() call, but we're also show-casing
# the futures here.
for res, f in fs.items():
f.add_done_callback(lambda fut, resource=res: delta_alter_configs(resource, fut.result()))
# Wait for done callbacks to be triggered and operations to complete.
print("Waiting for {} resource updates to finish".format(len(wait_zero)))
wait_zero.wait()
def example_list(a, args):
""" list topics, groups and cluster metadata """
if len(args) == 0:
what = "all"
else:
what = args[0]
md = a.list_topics(timeout=10)
print("Cluster {} metadata (response from broker {}):".format(md.cluster_id, md.orig_broker_name))
if what in ("all", "brokers"):
print(" {} brokers:".format(len(md.brokers)))
for b in iter(md.brokers.values()):
if b.id == md.controller_id:
print(" {} (controller)".format(b))
else:
print(" {}".format(b))
if what in ("all", "topics"):
print(" {} topics:".format(len(md.topics)))
for t in iter(md.topics.values()):
if t.error is not None:
errstr = ": {}".format(t.error)
else:
errstr = ""
print(" \"{}\" with {} partition(s){}".format(t, len(t.partitions), errstr))
for p in iter(t.partitions.values()):
if p.error is not None:
errstr = ": {}".format(p.error)
else:
errstr = ""
print("partition {} leader: {}, replicas: {},"
" isrs: {} errstr: {}".format(p.id, p.leader, p.replicas,
p.isrs, errstr))
if what in ("all", "groups"):
groups = a.list_groups(timeout=10)
print(" {} consumer groups".format(len(groups)))
for g in groups:
if g.error is not None:
errstr = ": {}".format(t.error)
else:
errstr = ""
print(" \"{}\" with {} member(s), protocol: {}, protocol_type: {}{}".format(
g, len(g.members), g.protocol, g.protocol_type, errstr))
for m in g.members:
print("id {} client_id: {} client_host: {}".format(m.id, m.client_id, m.client_host))
if __name__ == '__main__':
if len(sys.argv) < 3:
sys.stderr.write('Usage: %s <bootstrap-brokers> <operation> <args..>\n\n' % sys.argv[0])
sys.stderr.write('operations:\n')
sys.stderr.write(' create_topics <topic1> <topic2> ..\n')
sys.stderr.write(' delete_topics <topic1> <topic2> ..\n')
sys.stderr.write(' create_partitions <topic1> <new_total_count1> <topic2> <new_total_count2> ..\n')
sys.stderr.write(' describe_configs <resource_type1> <resource_name1> <resource2> <resource_name2> ..\n')
sys.stderr.write(' alter_configs <resource_type1> <resource_name1> ' +
'<config=val,config2=val2> <resource_type2> <resource_name2> <config..> ..\n')
sys.stderr.write(' delta_alter_configs <resource_type1> <resource_name1> ' +
'<config=val,config2=val2> <resource_type2> <resource_name2> <config..> ..\n')
sys.stderr.write(' create_acls <resource_type1> <resource_name1> <resource_patter_type1> ' +
'<principal1> <host1> <operation1> <permission_type1> ..\n')
sys.stderr.write(' describe_acls <resource_type1 <resource_name1> <resource_patter_type1> ' +
'<principal1> <host1> <operation1> <permission_type1> ..\n')
sys.stderr.write(' delete_acls <resource_type1> <resource_name1> <resource_patter_type1> ' +
'<principal1> <host1> <operation1> <permission_type1> ..\n')
sys.stderr.write(' list [<all|topics|brokers|groups>]\n')
sys.exit(1)
broker = sys.argv[1]
operation = sys.argv[2]
args = sys.argv[3:]
# Create Admin client
a = AdminClient({'bootstrap.servers': broker})
opsmap = {'create_topics': example_create_topics,
'delete_topics': example_delete_topics,
'create_partitions': example_create_partitions,
'describe_configs': example_describe_configs,
'alter_configs': example_alter_configs,
'delta_alter_configs': example_delta_alter_configs,
'create_acls': example_create_acls,
'describe_acls': example_describe_acls,
'delete_acls': example_delete_acls,
'list': example_list}
if operation not in opsmap:
sys.stderr.write('Unknown operation: %s\n' % operation)
sys.exit(1)
opsmap[operation](a, args)