| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import time |
| 17 |
|
| 18 |
from confluent_kafka.admin import ConfigResource, \ |
| 19 |
ConfigEntry, ResourceType, \ |
| 20 |
AlterConfigOpType |
| 21 |
|
| 22 |
from tests.common import TestUtils |
| 23 |
|
| 24 |
|
| 25 |
def assert_expected_config_entries(fs, num_fs, expected): |
| 26 |
""" |
| 27 |
Verify that the list of non-default entries corresponds |
| 28 |
to the expected one for each config resource. |
| 29 |
""" |
| 30 |
assert len(fs.items()) == num_fs |
| 31 |
for res, f in fs.items(): |
| 32 |
configs = f.result() |
| 33 |
entries = sorted([str(entry) for entry in configs.values() |
| 34 |
if not entry.is_default]) |
| 35 |
assert entries == expected[res] |
| 36 |
|
| 37 |
|
| 38 |
def assert_operation_succeeded(fs, num_fs): |
| 39 |
""" |
| 40 |
Verify that the operation succeeded |
| 41 |
for each resource. |
| 42 |
""" |
| 43 |
assert len(fs.items()) == num_fs |
| 44 |
for _, f in fs.items(): |
| 45 |
assert f.result() is None |
| 46 |
|
| 47 |
|
| 48 |
def test_incremental_alter_configs(kafka_cluster): |
| 49 |
""" |
| 50 |
Incrementally change the configuration entries of two topics |
| 51 |
and verify that the configuration description corresponds. |
| 52 |
""" |
| 53 |
|
| 54 |
topic_prefix = "test-topic" |
| 55 |
topic_prefix2 = "test-topic2" |
| 56 |
num_partitions = 2 |
| 57 |
topic_config = {"compression.type": "gzip"} |
| 58 |
|
| 59 |
our_topic = kafka_cluster.create_topic_and_wait_propogation(topic_prefix, |
| 60 |
{ |
| 61 |
"num_partitions": num_partitions, |
| 62 |
"config": topic_config, |
| 63 |
"replication_factor": 1, |
| 64 |
}) |
| 65 |
our_topic2 = kafka_cluster.create_topic_and_wait_propogation(topic_prefix2, |
| 66 |
{ |
| 67 |
"num_partitions": num_partitions, |
| 68 |
"config": topic_config, |
| 69 |
"replication_factor": 1, |
| 70 |
}) |
| 71 |
|
| 72 |
admin_client = kafka_cluster.admin() |
| 73 |
|
| 74 |
res1 = ConfigResource( |
| 75 |
ResourceType.TOPIC, |
| 76 |
our_topic, |
| 77 |
incremental_configs=[ |
| 78 |
ConfigEntry("cleanup.policy", "compact", |
| 79 |
incremental_operation=AlterConfigOpType.APPEND), |
| 80 |
ConfigEntry("retention.ms", "10000", |
| 81 |
incremental_operation=AlterConfigOpType.SET) |
| 82 |
] |
| 83 |
) |
| 84 |
res2 = ConfigResource( |
| 85 |
ResourceType.TOPIC, |
| 86 |
our_topic2, |
| 87 |
incremental_configs=[ |
| 88 |
ConfigEntry("cleanup.policy", "delete", |
| 89 |
incremental_operation=AlterConfigOpType.SUBTRACT), |
| 90 |
ConfigEntry("retention.ms", "5000", |
| 91 |
incremental_operation=AlterConfigOpType.SET) |
| 92 |
] |
| 93 |
) |
| 94 |
expected = { |
| 95 |
res1: ['cleanup.policy="delete,compact"', |
| 96 |
'compression.type="gzip"', |
| 97 |
'retention.ms="10000"'], |
| 98 |
res2: ['cleanup.policy=""', |
| 99 |
'compression.type="gzip"', |
| 100 |
'retention.ms="5000"'] |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
fs = admin_client.incremental_alter_configs([res1, res2]) |
| 107 |
|
| 108 |
assert_operation_succeeded(fs, 2) |
| 109 |
|
| 110 |
time.sleep(1) |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
fs = admin_client.describe_configs([res1, res2]) |
| 116 |
|
| 117 |
|
| 118 |
assert_expected_config_entries(fs, 2, expected) |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
res2 = ConfigResource( |
| 124 |
ResourceType.TOPIC, |
| 125 |
our_topic2, |
| 126 |
incremental_configs=[ |
| 127 |
ConfigEntry("compression.type", None, |
| 128 |
incremental_operation=AlterConfigOpType.DELETE), |
| 129 |
ConfigEntry("retention.ms", "10000", |
| 130 |
incremental_operation=AlterConfigOpType.SET) |
| 131 |
] |
| 132 |
) |
| 133 |
expected[res2] = ['cleanup.policy=""', |
| 134 |
'retention.ms="10000"'] |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
fs = admin_client.incremental_alter_configs([res2]) |
| 140 |
|
| 141 |
assert_operation_succeeded(fs, 1) |
| 142 |
|
| 143 |
time.sleep(1) |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
fs = admin_client.describe_configs([res2]) |
| 149 |
|
| 150 |
|
| 151 |
assert_expected_config_entries(fs, 1, expected) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if TestUtils.use_group_protocol_consumer(): |
| 156 |
group_id = "test-group" |
| 157 |
|
| 158 |
res_group = ConfigResource( |
| 159 |
ResourceType.GROUP, |
| 160 |
group_id, |
| 161 |
incremental_configs=[ |
| 162 |
ConfigEntry("consumer.session.timeout.ms", "50000", |
| 163 |
incremental_operation=AlterConfigOpType.SET) |
| 164 |
] |
| 165 |
) |
| 166 |
|
| 167 |
expected[res_group] = ['consumer.session.timeout.ms="50000"'] |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
fs = admin_client.incremental_alter_configs([res_group]) |
| 173 |
|
| 174 |
assert_operation_succeeded(fs, 1) |
| 175 |
|
| 176 |
time.sleep(1) |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
fs = admin_client.describe_configs([res_group]) |
| 182 |
|
| 183 |
|
| 184 |
assert_expected_config_entries(fs, 1, expected) |