Search in sources :

Example 1 with ClientQuotaAlteration

use of org.apache.kafka.common.quota.ClientQuotaAlteration in project kafka by apache.

the class RequestResponseTest method createAlterClientQuotasRequest.

private AlterClientQuotasRequest createAlterClientQuotasRequest(short version) {
    ClientQuotaEntity entity = new ClientQuotaEntity(Collections.singletonMap(ClientQuotaEntity.USER, "user"));
    ClientQuotaAlteration.Op op = new ClientQuotaAlteration.Op("request_percentage", 2.0);
    ClientQuotaAlteration alteration = new ClientQuotaAlteration(entity, Collections.singleton(op));
    return new AlterClientQuotasRequest.Builder(Collections.singleton(alteration), false).build(version);
}
Also used : ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity)

Example 2 with ClientQuotaAlteration

use of org.apache.kafka.common.quota.ClientQuotaAlteration in project kafka by apache.

the class AlterClientQuotasRequest method entries.

public List<ClientQuotaAlteration> entries() {
    List<ClientQuotaAlteration> entries = new ArrayList<>(data.entries().size());
    for (EntryData entryData : data.entries()) {
        Map<String, String> entity = new HashMap<>(entryData.entity().size());
        for (EntityData entityData : entryData.entity()) {
            entity.put(entityData.entityType(), entityData.entityName());
        }
        List<ClientQuotaAlteration.Op> ops = new ArrayList<>(entryData.ops().size());
        for (OpData opData : entryData.ops()) {
            Double value = opData.remove() ? null : opData.value();
            ops.add(new ClientQuotaAlteration.Op(opData.key(), value));
        }
        entries.add(new ClientQuotaAlteration(new ClientQuotaEntity(entity), ops));
    }
    return entries;
}
Also used : ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) EntryData(org.apache.kafka.common.message.AlterClientQuotasRequestData.EntryData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EntityData(org.apache.kafka.common.message.AlterClientQuotasRequestData.EntityData) OpData(org.apache.kafka.common.message.AlterClientQuotasRequestData.OpData) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity)

Example 3 with ClientQuotaAlteration

use of org.apache.kafka.common.quota.ClientQuotaAlteration in project kafka by apache.

the class KafkaAdminClientTest method callClientQuotasApisAndExpectAnAuthenticationError.

private void callClientQuotasApisAndExpectAnAuthenticationError(AdminClientUnitTestEnv env) throws InterruptedException {
    ExecutionException e = assertThrows(ExecutionException.class, () -> env.adminClient().describeClientQuotas(ClientQuotaFilter.all()).entities().get());
    assertTrue(e.getCause() instanceof AuthenticationException, "Expected an authentication error, but got " + Utils.stackTrace(e));
    ClientQuotaEntity entity = new ClientQuotaEntity(Collections.singletonMap(ClientQuotaEntity.USER, "user"));
    ClientQuotaAlteration alteration = new ClientQuotaAlteration(entity, asList(new ClientQuotaAlteration.Op("consumer_byte_rate", 1000.0)));
    e = assertThrows(ExecutionException.class, () -> env.adminClient().alterClientQuotas(asList(alteration)).all().get());
    assertTrue(e.getCause() instanceof AuthenticationException, "Expected an authentication error, but got " + Utils.stackTrace(e));
}
Also used : ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) AuthenticationException(org.apache.kafka.common.errors.AuthenticationException) SaslAuthenticationException(org.apache.kafka.common.errors.SaslAuthenticationException) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ClientQuotaAlteration

use of org.apache.kafka.common.quota.ClientQuotaAlteration in project kafka by apache.

the class ClientQuotaControlManagerTest method testAlterAndRemove.

@Test
public void testAlterAndRemove() {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    ClientQuotaControlManager manager = new ClientQuotaControlManager(snapshotRegistry);
    ClientQuotaEntity userEntity = userEntity("user-1");
    List<ClientQuotaAlteration> alters = new ArrayList<>();
    // Add one quota
    entityQuotaToAlterations(userEntity, quotas(QuotaConfigs.PRODUCER_BYTE_RATE_OVERRIDE_CONFIG, 10000.0), alters::add);
    alterQuotas(alters, manager);
    assertEquals(1, manager.clientQuotaData.get(userEntity).size());
    assertEquals(10000.0, manager.clientQuotaData.get(userEntity).get(QuotaConfigs.PRODUCER_BYTE_RATE_OVERRIDE_CONFIG), 1e-6);
    // Replace it and add another
    alters.clear();
    entityQuotaToAlterations(userEntity, quotas(QuotaConfigs.PRODUCER_BYTE_RATE_OVERRIDE_CONFIG, 10001.0, QuotaConfigs.CONSUMER_BYTE_RATE_OVERRIDE_CONFIG, 20000.0), alters::add);
    alterQuotas(alters, manager);
    assertEquals(2, manager.clientQuotaData.get(userEntity).size());
    assertEquals(10001.0, manager.clientQuotaData.get(userEntity).get(QuotaConfigs.PRODUCER_BYTE_RATE_OVERRIDE_CONFIG), 1e-6);
    assertEquals(20000.0, manager.clientQuotaData.get(userEntity).get(QuotaConfigs.CONSUMER_BYTE_RATE_OVERRIDE_CONFIG), 1e-6);
    // Remove one of the quotas, the other remains
    alters.clear();
    entityQuotaToAlterations(userEntity, quotas(QuotaConfigs.PRODUCER_BYTE_RATE_OVERRIDE_CONFIG, null), alters::add);
    alterQuotas(alters, manager);
    assertEquals(1, manager.clientQuotaData.get(userEntity).size());
    assertEquals(20000.0, manager.clientQuotaData.get(userEntity).get(QuotaConfigs.CONSUMER_BYTE_RATE_OVERRIDE_CONFIG), 1e-6);
    // Remove non-existent quota, no change
    alters.clear();
    entityQuotaToAlterations(userEntity, quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, null), alters::add);
    alterQuotas(alters, manager);
    assertEquals(1, manager.clientQuotaData.get(userEntity).size());
    assertEquals(20000.0, manager.clientQuotaData.get(userEntity).get(QuotaConfigs.CONSUMER_BYTE_RATE_OVERRIDE_CONFIG), 1e-6);
    // All quotas removed, we should cleanup the map
    alters.clear();
    entityQuotaToAlterations(userEntity, quotas(QuotaConfigs.CONSUMER_BYTE_RATE_OVERRIDE_CONFIG, null), alters::add);
    alterQuotas(alters, manager);
    assertFalse(manager.clientQuotaData.containsKey(userEntity));
    // Remove non-existent quota, again no change
    alters.clear();
    entityQuotaToAlterations(userEntity, quotas(QuotaConfigs.CONSUMER_BYTE_RATE_OVERRIDE_CONFIG, null), alters::add);
    alterQuotas(alters, manager);
    assertFalse(manager.clientQuotaData.containsKey(userEntity));
    // Mixed update
    alters.clear();
    Map<String, Double> quotas = new HashMap<>(4);
    quotas.put(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 99.0);
    quotas.put(QuotaConfigs.CONTROLLER_MUTATION_RATE_OVERRIDE_CONFIG, null);
    quotas.put(QuotaConfigs.PRODUCER_BYTE_RATE_OVERRIDE_CONFIG, 10002.0);
    quotas.put(QuotaConfigs.CONSUMER_BYTE_RATE_OVERRIDE_CONFIG, 20001.0);
    entityQuotaToAlterations(userEntity, quotas, alters::add);
    alterQuotas(alters, manager);
    assertEquals(3, manager.clientQuotaData.get(userEntity).size());
    assertEquals(20001.0, manager.clientQuotaData.get(userEntity).get(QuotaConfigs.CONSUMER_BYTE_RATE_OVERRIDE_CONFIG), 1e-6);
    assertEquals(10002.0, manager.clientQuotaData.get(userEntity).get(QuotaConfigs.PRODUCER_BYTE_RATE_OVERRIDE_CONFIG), 1e-6);
    assertEquals(99.0, manager.clientQuotaData.get(userEntity).get(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG), 1e-6);
}
Also used : ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LogContext(org.apache.kafka.common.utils.LogContext) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) Test(org.junit.jupiter.api.Test)

Example 5 with ClientQuotaAlteration

use of org.apache.kafka.common.quota.ClientQuotaAlteration in project kafka by apache.

the class ClientQuotaControlManagerTest method entityQuotaToAlterations.

static void entityQuotaToAlterations(ClientQuotaEntity entity, Map<String, Double> quota, Consumer<ClientQuotaAlteration> acceptor) {
    Collection<ClientQuotaAlteration.Op> ops = quota.entrySet().stream().map(quotaEntry -> new ClientQuotaAlteration.Op(quotaEntry.getKey(), quotaEntry.getValue())).collect(Collectors.toList());
    acceptor.accept(new ClientQuotaAlteration(entity, ops));
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) ApiError(org.apache.kafka.common.requests.ApiError) ArrayList(java.util.ArrayList) ClientQuotaRecord(org.apache.kafka.common.metadata.ClientQuotaRecord) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) QuotaConfigs(org.apache.kafka.common.config.internals.QuotaConfigs) ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) EntityData(org.apache.kafka.common.metadata.ClientQuotaRecord.EntityData) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) List(java.util.List) RecordTestUtils(org.apache.kafka.metadata.RecordTestUtils) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Errors(org.apache.kafka.common.protocol.Errors) Timeout(org.junit.jupiter.api.Timeout) Collections(java.util.Collections) ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration)

Aggregations

ClientQuotaAlteration (org.apache.kafka.common.quota.ClientQuotaAlteration)8 ClientQuotaEntity (org.apache.kafka.common.quota.ClientQuotaEntity)8 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)4 LogContext (org.apache.kafka.common.utils.LogContext)3 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)3 Map (java.util.Map)2 ClientQuotaRecord (org.apache.kafka.common.metadata.ClientQuotaRecord)2 EntityData (org.apache.kafka.common.metadata.ClientQuotaRecord.EntityData)2 ApiError (org.apache.kafka.common.requests.ApiError)2 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 QuotaConfigs (org.apache.kafka.common.config.internals.QuotaConfigs)1