Search in sources :

Example 1 with ClientQuotaRecord

use of org.apache.kafka.common.metadata.ClientQuotaRecord in project kafka by apache.

the class ClientQuotaControlManager method alterClientQuotaEntity.

private void alterClientQuotaEntity(ClientQuotaEntity entity, Map<String, Double> newQuotaConfigs, List<ApiMessageAndVersion> outputRecords, Map<ClientQuotaEntity, ApiError> outputResults) {
    // Check entity types and sanitize the names
    Map<String, String> validatedEntityMap = new HashMap<>(3);
    ApiError error = validateEntity(entity, validatedEntityMap);
    if (error.isFailure()) {
        outputResults.put(entity, error);
        return;
    }
    // Check the combination of entity types and get the config keys
    Map<String, ConfigDef.ConfigKey> configKeys = new HashMap<>(4);
    error = configKeysForEntityType(validatedEntityMap, configKeys);
    if (error.isFailure()) {
        outputResults.put(entity, error);
        return;
    }
    // Don't share objects between different records
    Supplier<List<EntityData>> recordEntitySupplier = () -> validatedEntityMap.entrySet().stream().map(mapEntry -> new EntityData().setEntityType(mapEntry.getKey()).setEntityName(mapEntry.getValue())).collect(Collectors.toList());
    List<ApiMessageAndVersion> newRecords = new ArrayList<>(newQuotaConfigs.size());
    Map<String, Double> currentQuotas = clientQuotaData.containsKey(entity) ? clientQuotaData.get(entity) : Collections.emptyMap();
    for (Map.Entry<String, Double> entry : newQuotaConfigs.entrySet()) {
        String key = entry.getKey();
        Double newValue = entry.getValue();
        if (newValue == null) {
            if (currentQuotas.containsKey(key)) {
                // Null value indicates removal
                newRecords.add(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(recordEntitySupplier.get()).setKey(key).setRemove(true), CLIENT_QUOTA_RECORD.highestSupportedVersion()));
            }
        } else {
            ApiError validationError = validateQuotaKeyValue(configKeys, key, newValue);
            if (validationError.isFailure()) {
                outputResults.put(entity, validationError);
                return;
            } else {
                final Double currentValue = currentQuotas.get(key);
                if (!Objects.equals(currentValue, newValue)) {
                    // Only record the new value if it has changed
                    newRecords.add(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(recordEntitySupplier.get()).setKey(key).setValue(newValue), CLIENT_QUOTA_RECORD.highestSupportedVersion()));
                }
            }
        }
    }
    outputRecords.addAll(newRecords);
    outputResults.put(entity, ApiError.NONE);
}
Also used : TimelineHashMap(org.apache.kafka.timeline.TimelineHashMap) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ApiError(org.apache.kafka.common.requests.ApiError) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) ClientQuotaRecord(org.apache.kafka.common.metadata.ClientQuotaRecord) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) Map(java.util.Map) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) NoSuchElementException(java.util.NoSuchElementException) QuotaConfigs(org.apache.kafka.common.config.internals.QuotaConfigs) ConfigDef(org.apache.kafka.common.config.ConfigDef) ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) Iterator(java.util.Iterator) EntityData(org.apache.kafka.common.metadata.ClientQuotaRecord.EntityData) Collection(java.util.Collection) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) Objects(java.util.Objects) List(java.util.List) CLIENT_QUOTA_RECORD(org.apache.kafka.common.metadata.MetadataRecordType.CLIENT_QUOTA_RECORD) Entry(java.util.Map.Entry) Errors(org.apache.kafka.common.protocol.Errors) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) Collections(java.util.Collections) TimelineHashMap(org.apache.kafka.timeline.TimelineHashMap) HashMap(java.util.HashMap) EntityData(org.apache.kafka.common.metadata.ClientQuotaRecord.EntityData) ArrayList(java.util.ArrayList) ClientQuotaRecord(org.apache.kafka.common.metadata.ClientQuotaRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) List(java.util.List) ApiError(org.apache.kafka.common.requests.ApiError) TimelineHashMap(org.apache.kafka.timeline.TimelineHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ClientQuotaRecord

use of org.apache.kafka.common.metadata.ClientQuotaRecord in project kafka by apache.

the class ClientQuotaControlManagerTest method testEntityTypes.

@Test
public void testEntityTypes() throws Exception {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    ClientQuotaControlManager manager = new ClientQuotaControlManager(snapshotRegistry);
    Map<ClientQuotaEntity, Map<String, Double>> quotasToTest = new HashMap<>();
    quotasToTest.put(userClientEntity("user-1", "client-id-1"), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 50.50));
    quotasToTest.put(userClientEntity("user-2", "client-id-1"), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 51.51));
    quotasToTest.put(userClientEntity("user-3", "client-id-2"), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 52.52));
    quotasToTest.put(userClientEntity(null, "client-id-1"), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 53.53));
    quotasToTest.put(userClientEntity("user-1", null), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 54.54));
    quotasToTest.put(userClientEntity("user-3", null), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 55.55));
    quotasToTest.put(userEntity("user-1"), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 56.56));
    quotasToTest.put(userEntity("user-2"), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 57.57));
    quotasToTest.put(userEntity("user-3"), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 58.58));
    quotasToTest.put(userEntity(null), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 59.59));
    quotasToTest.put(clientEntity("client-id-2"), quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 60.60));
    List<ClientQuotaAlteration> alters = new ArrayList<>();
    quotasToTest.forEach((entity, quota) -> entityQuotaToAlterations(entity, quota, alters::add));
    alterQuotas(alters, manager);
    RecordTestUtils.assertBatchIteratorContains(Arrays.asList(Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName("user-1"), new EntityData().setEntityType("client-id").setEntityName("client-id-1"))).setKey("request_percentage").setValue(50.5).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName("user-2"), new EntityData().setEntityType("client-id").setEntityName("client-id-1"))).setKey("request_percentage").setValue(51.51).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName("user-3"), new EntityData().setEntityType("client-id").setEntityName("client-id-2"))).setKey("request_percentage").setValue(52.52).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName(null), new EntityData().setEntityType("client-id").setEntityName("client-id-1"))).setKey("request_percentage").setValue(53.53).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName("user-1"), new EntityData().setEntityType("client-id").setEntityName(null))).setKey("request_percentage").setValue(54.54).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName("user-3"), new EntityData().setEntityType("client-id").setEntityName(null))).setKey("request_percentage").setValue(55.55).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName("user-1"))).setKey("request_percentage").setValue(56.56).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName("user-2"))).setKey("request_percentage").setValue(57.57).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName("user-3"))).setKey("request_percentage").setValue(58.58).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("user").setEntityName(null))).setKey("request_percentage").setValue(59.59).setRemove(false), (short) 0)), Arrays.asList(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(Arrays.asList(new EntityData().setEntityType("client-id").setEntityName("client-id-2"))).setKey("request_percentage").setValue(60.60).setRemove(false), (short) 0))), manager.iterator(Long.MAX_VALUE));
}
Also used : ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EntityData(org.apache.kafka.common.metadata.ClientQuotaRecord.EntityData) LogContext(org.apache.kafka.common.utils.LogContext) ClientQuotaRecord(org.apache.kafka.common.metadata.ClientQuotaRecord) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with ClientQuotaRecord

use of org.apache.kafka.common.metadata.ClientQuotaRecord in project kafka by apache.

the class ClientQuotaImage method write.

public void write(ClientQuotaEntity entity, Consumer<List<ApiMessageAndVersion>> out) {
    List<ApiMessageAndVersion> records = new ArrayList<>(quotas.size());
    for (Entry<String, Double> entry : quotas.entrySet()) {
        records.add(new ApiMessageAndVersion(new ClientQuotaRecord().setEntity(entityToData(entity)).setKey(entry.getKey()).setValue(entry.getValue()).setRemove(false), CLIENT_QUOTA_RECORD.highestSupportedVersion()));
    }
    out.accept(records);
}
Also used : ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ArrayList(java.util.ArrayList) ClientQuotaRecord(org.apache.kafka.common.metadata.ClientQuotaRecord)

Example 4 with ClientQuotaRecord

use of org.apache.kafka.common.metadata.ClientQuotaRecord in project kafka by apache.

the class MetadataNodeManagerTest method testClientQuotaRecord.

@Test
public void testClientQuotaRecord() {
    ClientQuotaRecord record = new ClientQuotaRecord().setEntity(Arrays.asList(new ClientQuotaRecord.EntityData().setEntityType("user").setEntityName("kraft"), new ClientQuotaRecord.EntityData().setEntityType("client").setEntityName("kstream"))).setKey("producer_byte_rate").setValue(1000.0);
    metadataNodeManager.handleMessage(record);
    assertEquals("1000.0", metadataNodeManager.getData().root().directory("client-quotas", "client", "kstream", "user", "kraft").file("producer_byte_rate").contents());
    metadataNodeManager.handleMessage(record.setRemove(true));
    assertFalse(metadataNodeManager.getData().root().directory("client-quotas", "client", "kstream", "user", "kraft").children().containsKey("producer_byte_rate"));
    record = new ClientQuotaRecord().setEntity(Collections.singletonList(new ClientQuotaRecord.EntityData().setEntityType("user").setEntityName(null))).setKey("producer_byte_rate").setValue(2000.0);
    metadataNodeManager.handleMessage(record);
    assertEquals("2000.0", metadataNodeManager.getData().root().directory("client-quotas", "user", "<default>").file("producer_byte_rate").contents());
}
Also used : ClientQuotaRecord(org.apache.kafka.common.metadata.ClientQuotaRecord) Test(org.junit.jupiter.api.Test)

Aggregations

ClientQuotaRecord (org.apache.kafka.common.metadata.ClientQuotaRecord)4 ArrayList (java.util.ArrayList)3 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EntityData (org.apache.kafka.common.metadata.ClientQuotaRecord.EntityData)2 ClientQuotaAlteration (org.apache.kafka.common.quota.ClientQuotaAlteration)2 ClientQuotaEntity (org.apache.kafka.common.quota.ClientQuotaEntity)2 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)2 Test (org.junit.jupiter.api.Test)2 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 NoSuchElementException (java.util.NoSuchElementException)1 Objects (java.util.Objects)1 Supplier (java.util.function.Supplier)1