Search in sources :

Example 1 with ClientQuotaEntity

use of org.apache.kafka.common.quota.ClientQuotaEntity 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 ClientQuotaEntity

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

the class AlterClientQuotasResponse method complete.

public void complete(Map<ClientQuotaEntity, KafkaFutureImpl<Void>> futures) {
    for (EntryData entryData : data.entries()) {
        Map<String, String> entityEntries = new HashMap<>(entryData.entity().size());
        for (EntityData entityData : entryData.entity()) {
            entityEntries.put(entityData.entityType(), entityData.entityName());
        }
        ClientQuotaEntity entity = new ClientQuotaEntity(entityEntries);
        KafkaFutureImpl<Void> future = futures.get(entity);
        if (future == null) {
            throw new IllegalArgumentException("Future map must contain entity " + entity);
        }
        Errors error = Errors.forCode(entryData.errorCode());
        if (error == Errors.NONE) {
            future.complete(null);
        } else {
            future.completeExceptionally(error.exception(entryData.errorMessage()));
        }
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) EntryData(org.apache.kafka.common.message.AlterClientQuotasResponseData.EntryData) HashMap(java.util.HashMap) EntityData(org.apache.kafka.common.message.AlterClientQuotasResponseData.EntityData) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity)

Example 3 with ClientQuotaEntity

use of org.apache.kafka.common.quota.ClientQuotaEntity 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 4 with ClientQuotaEntity

use of org.apache.kafka.common.quota.ClientQuotaEntity 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 5 with ClientQuotaEntity

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

the class KafkaAdminClientTest method testDescribeClientQuotas.

@Test
public void testDescribeClientQuotas() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        final String value = "value";
        Map<ClientQuotaEntity, Map<String, Double>> responseData = new HashMap<>();
        ClientQuotaEntity entity1 = newClientQuotaEntity(ClientQuotaEntity.USER, "user-1", ClientQuotaEntity.CLIENT_ID, value);
        ClientQuotaEntity entity2 = newClientQuotaEntity(ClientQuotaEntity.USER, "user-2", ClientQuotaEntity.CLIENT_ID, value);
        responseData.put(entity1, Collections.singletonMap("consumer_byte_rate", 10000.0));
        responseData.put(entity2, Collections.singletonMap("producer_byte_rate", 20000.0));
        env.kafkaClient().prepareResponse(DescribeClientQuotasResponse.fromQuotaEntities(responseData, 0));
        ClientQuotaFilter filter = ClientQuotaFilter.contains(asList(ClientQuotaFilterComponent.ofEntity(ClientQuotaEntity.USER, value)));
        DescribeClientQuotasResult result = env.adminClient().describeClientQuotas(filter);
        Map<ClientQuotaEntity, Map<String, Double>> resultData = result.entities().get();
        assertEquals(resultData.size(), 2);
        assertTrue(resultData.containsKey(entity1));
        Map<String, Double> config1 = resultData.get(entity1);
        assertEquals(config1.size(), 1);
        assertEquals(config1.get("consumer_byte_rate"), 10000.0, 1e-6);
        assertTrue(resultData.containsKey(entity2));
        Map<String, Double> config2 = resultData.get(entity2);
        assertEquals(config2.size(), 1);
        assertEquals(config2.get("producer_byte_rate"), 20000.0, 1e-6);
    }
}
Also used : ClientQuotaFilter(org.apache.kafka.common.quota.ClientQuotaFilter) HashMap(java.util.HashMap) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) Map(java.util.Map) HashMap(java.util.HashMap) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

ClientQuotaEntity (org.apache.kafka.common.quota.ClientQuotaEntity)22 HashMap (java.util.HashMap)15 ClientQuotaAlteration (org.apache.kafka.common.quota.ClientQuotaAlteration)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)7 Test (org.junit.jupiter.api.Test)7 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)6 LogContext (org.apache.kafka.common.utils.LogContext)5 Errors (org.apache.kafka.common.protocol.Errors)4 ClientQuotaRecord (org.apache.kafka.common.metadata.ClientQuotaRecord)3 EntityData (org.apache.kafka.common.metadata.ClientQuotaRecord.EntityData)3 ApiError (org.apache.kafka.common.requests.ApiError)3 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)3 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 QuotaConfigs (org.apache.kafka.common.config.internals.QuotaConfigs)2 InvalidRequestException (org.apache.kafka.common.errors.InvalidRequestException)2 EntryData (org.apache.kafka.common.message.AlterClientQuotasResponseData.EntryData)2