Search in sources :

Example 1 with ConfigRecord

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);
}
Also used : ConfigRecord(org.apache.kafka.common.metadata.ConfigRecord) Entry(java.util.Map.Entry) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) OpType(org.apache.kafka.clients.admin.AlterConfigOp.OpType) ApiError(org.apache.kafka.common.requests.ApiError)

Example 2 with ConfigRecord

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;
}
Also used : ConfigRecord(org.apache.kafka.common.metadata.ConfigRecord) TimelineHashMap(org.apache.kafka.timeline.TimelineHashMap) HashMap(java.util.HashMap) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ConfigException(org.apache.kafka.common.config.ConfigException) ApiError(org.apache.kafka.common.requests.ApiError) RequestMetadata(org.apache.kafka.server.policy.AlterConfigPolicy.RequestMetadata)

Example 3 with ConfigRecord

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));
}
Also used : ConfigRecord(org.apache.kafka.common.metadata.ConfigRecord) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) LogContext(org.apache.kafka.common.utils.LogContext) Test(org.junit.jupiter.api.Test)

Example 4 with ConfigRecord

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);
}
Also used : ConfigRecord(org.apache.kafka.common.metadata.ConfigRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 5 with ConfigRecord

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"));
}
Also used : ConfigRecord(org.apache.kafka.common.metadata.ConfigRecord)

Aggregations

ConfigRecord (org.apache.kafka.common.metadata.ConfigRecord)10 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)8 ApiError (org.apache.kafka.common.requests.ApiError)5 LogContext (org.apache.kafka.common.utils.LogContext)4 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)4 Test (org.junit.jupiter.api.Test)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RequestMetadata (org.apache.kafka.server.policy.AlterConfigPolicy.RequestMetadata)2 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 OpType (org.apache.kafka.clients.admin.AlterConfigOp.OpType)1 ConfigException (org.apache.kafka.common.config.ConfigException)1 TimelineHashMap (org.apache.kafka.timeline.TimelineHashMap)1