Search in sources :

Example 11 with ClientQuotaEntity

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

the class ClientQuotasImage method write.

public void write(Consumer<List<ApiMessageAndVersion>> out) {
    for (Entry<ClientQuotaEntity, ClientQuotaImage> entry : entities.entrySet()) {
        ClientQuotaEntity entity = entry.getKey();
        ClientQuotaImage clientQuotaImage = entry.getValue();
        clientQuotaImage.write(entity, out);
    }
}
Also used : ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity)

Example 12 with ClientQuotaEntity

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

the class ClientQuotasDelta method replay.

public void replay(ClientQuotaRecord record) {
    ClientQuotaEntity entity = ClientQuotaImage.dataToEntity(record.entity());
    ClientQuotaDelta change = changes.computeIfAbsent(entity, __ -> new ClientQuotaDelta(image.entities().getOrDefault(entity, ClientQuotaImage.EMPTY)));
    change.replay(record);
}
Also used : ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity)

Example 13 with ClientQuotaEntity

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

the class ClientQuotasDelta method finishSnapshot.

public void finishSnapshot() {
    for (Entry<ClientQuotaEntity, ClientQuotaImage> entry : image.entities().entrySet()) {
        ClientQuotaEntity entity = entry.getKey();
        ClientQuotaImage quotaImage = entry.getValue();
        ClientQuotaDelta quotaDelta = changes.computeIfAbsent(entity, __ -> new ClientQuotaDelta(quotaImage));
        quotaDelta.finishSnapshot();
    }
}
Also used : ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity)

Example 14 with ClientQuotaEntity

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

the class KafkaAdminClientTest method testAlterClientQuotas.

@Test
public void testAlterClientQuotas() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        ClientQuotaEntity goodEntity = newClientQuotaEntity(ClientQuotaEntity.USER, "user-1");
        ClientQuotaEntity unauthorizedEntity = newClientQuotaEntity(ClientQuotaEntity.USER, "user-0");
        ClientQuotaEntity invalidEntity = newClientQuotaEntity("", "user-0");
        Map<ClientQuotaEntity, ApiError> responseData = new HashMap<>(2);
        responseData.put(goodEntity, new ApiError(Errors.CLUSTER_AUTHORIZATION_FAILED, "Authorization failed"));
        responseData.put(unauthorizedEntity, new ApiError(Errors.CLUSTER_AUTHORIZATION_FAILED, "Authorization failed"));
        responseData.put(invalidEntity, new ApiError(Errors.INVALID_REQUEST, "Invalid quota entity"));
        env.kafkaClient().prepareResponse(AlterClientQuotasResponse.fromQuotaEntities(responseData, 0));
        List<ClientQuotaAlteration> entries = new ArrayList<>(3);
        entries.add(new ClientQuotaAlteration(goodEntity, singleton(new ClientQuotaAlteration.Op("consumer_byte_rate", 10000.0))));
        entries.add(new ClientQuotaAlteration(unauthorizedEntity, singleton(new ClientQuotaAlteration.Op("producer_byte_rate", 10000.0))));
        entries.add(new ClientQuotaAlteration(invalidEntity, singleton(new ClientQuotaAlteration.Op("producer_byte_rate", 100.0))));
        AlterClientQuotasResult result = env.adminClient().alterClientQuotas(entries);
        result.values().get(goodEntity);
        TestUtils.assertFutureError(result.values().get(unauthorizedEntity), ClusterAuthorizationException.class);
        TestUtils.assertFutureError(result.values().get(invalidEntity), InvalidRequestException.class);
    }
}
Also used : ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) ApiError(org.apache.kafka.common.requests.ApiError) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 15 with ClientQuotaEntity

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

the class ClientQuotaControlManagerTest method testInvalidEntityTypes.

@Test
public void testInvalidEntityTypes() {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    ClientQuotaControlManager manager = new ClientQuotaControlManager(snapshotRegistry);
    // Unknown type "foo"
    assertInvalidEntity(manager, entity("foo", "bar"));
    // Null type
    assertInvalidEntity(manager, entity(null, "null"));
    // Valid + unknown combo
    assertInvalidEntity(manager, entity(ClientQuotaEntity.USER, "user-1", "foo", "bar"));
    assertInvalidEntity(manager, entity("foo", "bar", ClientQuotaEntity.IP, "1.2.3.4"));
    // Invalid combinations
    assertInvalidEntity(manager, entity(ClientQuotaEntity.USER, "user-1", ClientQuotaEntity.IP, "1.2.3.4"));
    assertInvalidEntity(manager, entity(ClientQuotaEntity.CLIENT_ID, "user-1", ClientQuotaEntity.IP, "1.2.3.4"));
    // Empty
    assertInvalidEntity(manager, new ClientQuotaEntity(Collections.emptyMap()));
}
Also used : SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) LogContext(org.apache.kafka.common.utils.LogContext) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) 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