Search in sources :

Example 1 with RequestMetadata

use of org.apache.kafka.server.policy.AlterConfigPolicy.RequestMetadata 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 2 with RequestMetadata

use of org.apache.kafka.server.policy.AlterConfigPolicy.RequestMetadata in project kafka by apache.

the class ConfigurationControlManagerTest method testIncrementalAlterConfigsWithPolicy.

@Test
public void testIncrementalAlterConfigsWithPolicy() {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    MockAlterConfigsPolicy policy = new MockAlterConfigsPolicy(asList(new RequestMetadata(MYTOPIC, Collections.emptyMap()), new RequestMetadata(BROKER0, toMap(entry("foo.bar", "123"), entry("quux", "456")))));
    ConfigurationControlManager manager = new ConfigurationControlManager(new LogContext(), snapshotRegistry, CONFIGS, Optional.of(policy), ConfigurationValidator.NO_OP);
    assertEquals(ControllerResult.atomicOf(asList(new ApiMessageAndVersion(new ConfigRecord().setResourceType(BROKER.id()).setResourceName("0").setName("foo.bar").setValue("123"), (short) 0), new ApiMessageAndVersion(new ConfigRecord().setResourceType(BROKER.id()).setResourceName("0").setName("quux").setValue("456"), (short) 0)), toMap(entry(MYTOPIC, new ApiError(Errors.POLICY_VIOLATION, "Expected: AlterConfigPolicy.RequestMetadata(resource=ConfigResource(" + "type=TOPIC, name='mytopic'), configs={}). Got: " + "AlterConfigPolicy.RequestMetadata(resource=ConfigResource(" + "type=TOPIC, name='mytopic'), configs={foo.bar=123})")), entry(BROKER0, ApiError.NONE))), manager.incrementalAlterConfigs(toMap(entry(MYTOPIC, toMap(entry("foo.bar", entry(SET, "123")))), entry(BROKER0, toMap(entry("foo.bar", entry(SET, "123")), entry("quux", entry(SET, "456"))))), NO_OP_EXISTENCE_CHECKER));
}
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) ApiError(org.apache.kafka.common.requests.ApiError) RequestMetadata(org.apache.kafka.server.policy.AlterConfigPolicy.RequestMetadata) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigRecord (org.apache.kafka.common.metadata.ConfigRecord)2 ApiError (org.apache.kafka.common.requests.ApiError)2 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)2 RequestMetadata (org.apache.kafka.server.policy.AlterConfigPolicy.RequestMetadata)2 HashMap (java.util.HashMap)1 ConfigException (org.apache.kafka.common.config.ConfigException)1 LogContext (org.apache.kafka.common.utils.LogContext)1 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)1 TimelineHashMap (org.apache.kafka.timeline.TimelineHashMap)1 Test (org.junit.jupiter.api.Test)1