use of org.apache.kafka.common.metadata.ConfigRecord in project kafka by apache.
the class ConfigurationControlManager method incrementalAlterConfigResource.
private void incrementalAlterConfigResource(ConfigResource configResource, Map<String, Entry<OpType, String>> keysToOps, Consumer<ConfigResource> existenceChecker, List<ApiMessageAndVersion> outputRecords, Map<ConfigResource, ApiError> outputResults) {
List<ApiMessageAndVersion> newRecords = new ArrayList<>();
for (Entry<String, Entry<OpType, String>> keysToOpsEntry : keysToOps.entrySet()) {
String key = keysToOpsEntry.getKey();
String currentValue = null;
TimelineHashMap<String, String> currentConfigs = configData.get(configResource);
if (currentConfigs != null) {
currentValue = currentConfigs.get(key);
}
String newValue = currentValue;
Entry<OpType, String> opTypeAndNewValue = keysToOpsEntry.getValue();
OpType opType = opTypeAndNewValue.getKey();
String opValue = opTypeAndNewValue.getValue();
switch(opType) {
case SET:
newValue = opValue;
break;
case DELETE:
newValue = null;
break;
case APPEND:
case SUBTRACT:
if (!isSplittable(configResource.type(), key)) {
outputResults.put(configResource, new ApiError(INVALID_CONFIG, "Can't " + opType + " to " + "key " + key + " because its type is not LIST."));
return;
}
List<String> newValueParts = getParts(newValue, key, configResource);
if (opType == APPEND) {
if (!newValueParts.contains(opValue)) {
newValueParts.add(opValue);
}
newValue = String.join(",", newValueParts);
} else if (newValueParts.remove(opValue)) {
newValue = String.join(",", newValueParts);
}
break;
}
if (!Objects.equals(currentValue, newValue)) {
newRecords.add(new ApiMessageAndVersion(new ConfigRecord().setResourceType(configResource.type().id()).setResourceName(configResource.name()).setName(key).setValue(newValue), CONFIG_RECORD.highestSupportedVersion()));
}
}
ApiError error = validateAlterConfig(configResource, newRecords, existenceChecker);
if (error.isFailure()) {
outputResults.put(configResource, error);
return;
}
outputRecords.addAll(newRecords);
outputResults.put(configResource, ApiError.NONE);
}
use of org.apache.kafka.common.metadata.ConfigRecord in project kafka by apache.
the class ConfigurationControlManager method validateAlterConfig.
private ApiError validateAlterConfig(ConfigResource configResource, List<ApiMessageAndVersion> newRecords, Consumer<ConfigResource> existenceChecker) {
Map<String, String> newConfigs = new HashMap<>();
TimelineHashMap<String, String> existingConfigs = configData.get(configResource);
if (existingConfigs != null)
newConfigs.putAll(existingConfigs);
for (ApiMessageAndVersion newRecord : newRecords) {
ConfigRecord configRecord = (ConfigRecord) newRecord.message();
if (configRecord.value() == null) {
newConfigs.remove(configRecord.name());
} else {
newConfigs.put(configRecord.name(), configRecord.value());
}
}
try {
validator.validate(configResource, newConfigs);
existenceChecker.accept(configResource);
if (alterConfigPolicy.isPresent()) {
alterConfigPolicy.get().validate(new RequestMetadata(configResource, newConfigs));
}
} catch (ConfigException e) {
return new ApiError(INVALID_CONFIG, e.getMessage());
} catch (Throwable e) {
return ApiError.fromThrowable(e);
}
return ApiError.NONE;
}
use of org.apache.kafka.common.metadata.ConfigRecord in project kafka by apache.
the class ConfigurationControlManagerTest method testReplay.
@Test
public void testReplay() throws Exception {
SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
ConfigurationControlManager manager = new ConfigurationControlManager(new LogContext(), snapshotRegistry, CONFIGS, Optional.empty(), ConfigurationValidator.NO_OP);
assertEquals(Collections.emptyMap(), manager.getConfigs(BROKER0));
manager.replay(new ConfigRecord().setResourceType(BROKER.id()).setResourceName("0").setName("foo.bar").setValue("1,2"));
assertEquals(Collections.singletonMap("foo.bar", "1,2"), manager.getConfigs(BROKER0));
manager.replay(new ConfigRecord().setResourceType(BROKER.id()).setResourceName("0").setName("foo.bar").setValue(null));
assertEquals(Collections.emptyMap(), manager.getConfigs(BROKER0));
manager.replay(new ConfigRecord().setResourceType(TOPIC.id()).setResourceName("mytopic").setName("abc").setValue("x,y,z"));
manager.replay(new ConfigRecord().setResourceType(TOPIC.id()).setResourceName("mytopic").setName("def").setValue("blah"));
assertEquals(toMap(entry("abc", "x,y,z"), entry("def", "blah")), manager.getConfigs(MYTOPIC));
RecordTestUtils.assertBatchIteratorContains(asList(asList(new ApiMessageAndVersion(new ConfigRecord().setResourceType(TOPIC.id()).setResourceName("mytopic").setName("abc").setValue("x,y,z"), (short) 0), new ApiMessageAndVersion(new ConfigRecord().setResourceType(TOPIC.id()).setResourceName("mytopic").setName("def").setValue("blah"), (short) 0))), manager.iterator(Long.MAX_VALUE));
}
use of org.apache.kafka.common.metadata.ConfigRecord in project kafka by apache.
the class ConfigurationImage method write.
public void write(ConfigResource configResource, Consumer<List<ApiMessageAndVersion>> out) {
List<ApiMessageAndVersion> records = new ArrayList<>();
for (Map.Entry<String, String> entry : data.entrySet()) {
records.add(new ApiMessageAndVersion(new ConfigRecord().setResourceType(configResource.type().id()).setResourceName(configResource.name()).setName(entry.getKey()).setValue(entry.getValue()), CONFIG_RECORD.highestSupportedVersion()));
}
out.accept(records);
}
use of org.apache.kafka.common.metadata.ConfigRecord in project kafka by apache.
the class MetadataNodeManagerTest method checkValidConfigRecord.
private void checkValidConfigRecord(byte resourceType, String typeString) {
ConfigRecord configRecord = new ConfigRecord().setResourceType(resourceType).setResourceName("0").setName("name").setValue("kraft");
metadataNodeManager.handleMessage(configRecord);
assertEquals("kraft", metadataNodeManager.getData().root().directory("configs", typeString, "0").file("name").contents());
// null value indicates delete
configRecord.setValue(null);
metadataNodeManager.handleMessage(configRecord);
assertFalse(metadataNodeManager.getData().root().directory("configs", typeString, "0").children().containsKey("name"));
}
Aggregations