Search in sources :

Example 16 with ClientQuotaEntity

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

the class ClientQuotaControlManagerTest method testInvalidQuotaKeys.

@Test
public void testInvalidQuotaKeys() {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    ClientQuotaControlManager manager = new ClientQuotaControlManager(snapshotRegistry);
    ClientQuotaEntity entity = entity(ClientQuotaEntity.USER, "user-1");
    // Invalid + valid keys
    assertInvalidQuota(manager, entity, quotas("not.a.quota.key", 0.0, QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 99.9));
    // Valid + invalid keys
    assertInvalidQuota(manager, entity, quotas(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, 99.9, "not.a.quota.key", 0.0));
    // Null key
    assertInvalidQuota(manager, entity, quotas(null, 99.9));
}
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)

Example 17 with ClientQuotaEntity

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

the class AlterClientQuotasResponse method fromQuotaEntities.

public static AlterClientQuotasResponse fromQuotaEntities(Map<ClientQuotaEntity, ApiError> result, int throttleTimeMs) {
    List<EntryData> entries = new ArrayList<>(result.size());
    for (Map.Entry<ClientQuotaEntity, ApiError> entry : result.entrySet()) {
        ApiError e = entry.getValue();
        entries.add(new EntryData().setErrorCode(e.error().code()).setErrorMessage(e.message()).setEntity(toEntityData(entry.getKey())));
    }
    return new AlterClientQuotasResponse(new AlterClientQuotasResponseData().setThrottleTimeMs(throttleTimeMs).setEntries(entries));
}
Also used : EntryData(org.apache.kafka.common.message.AlterClientQuotasResponseData.EntryData) AlterClientQuotasResponseData(org.apache.kafka.common.message.AlterClientQuotasResponseData) ArrayList(java.util.ArrayList) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with ClientQuotaEntity

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

the class ClientQuotasImage method describe.

public DescribeClientQuotasResponseData describe(DescribeClientQuotasRequestData request) {
    DescribeClientQuotasResponseData response = new DescribeClientQuotasResponseData();
    Map<String, String> exactMatch = new HashMap<>();
    Set<String> typeMatch = new HashSet<>();
    for (DescribeClientQuotasRequestData.ComponentData component : request.components()) {
        if (component.entityType().isEmpty()) {
            throw new InvalidRequestException("Invalid empty entity type.");
        } else if (exactMatch.containsKey(component.entityType()) || typeMatch.contains(component.entityType())) {
            throw new InvalidRequestException("Entity type " + component.entityType() + " cannot appear more than once in the filter.");
        }
        if (!(component.entityType().equals(IP) || component.entityType().equals(USER) || component.entityType().equals(CLIENT_ID))) {
            throw new UnsupportedVersionException("Unsupported entity type " + component.entityType());
        }
        switch(component.matchType()) {
            case MATCH_TYPE_EXACT:
                if (component.match() == null) {
                    throw new InvalidRequestException("Request specified " + "MATCH_TYPE_EXACT, but set match string to null.");
                }
                exactMatch.put(component.entityType(), component.match());
                break;
            case MATCH_TYPE_DEFAULT:
                if (component.match() != null) {
                    throw new InvalidRequestException("Request specified " + "MATCH_TYPE_DEFAULT, but also specified a match string.");
                }
                exactMatch.put(component.entityType(), null);
                break;
            case MATCH_TYPE_SPECIFIED:
                if (component.match() != null) {
                    throw new InvalidRequestException("Request specified " + "MATCH_TYPE_SPECIFIED, but also specified a match string.");
                }
                typeMatch.add(component.entityType());
                break;
            default:
                throw new InvalidRequestException("Unknown match type " + component.matchType());
        }
    }
    if (exactMatch.containsKey(IP) || typeMatch.contains(IP)) {
        if ((exactMatch.containsKey(USER) || typeMatch.contains(USER)) || (exactMatch.containsKey(CLIENT_ID) || typeMatch.contains(CLIENT_ID))) {
            throw new InvalidRequestException("Invalid entity filter component " + "combination. IP filter component should not be used with " + "user or clientId filter component.");
        }
    }
    // TODO: this is O(N). We should add indexing here to speed it up. See KAFKA-13022.
    for (Entry<ClientQuotaEntity, ClientQuotaImage> entry : entities.entrySet()) {
        ClientQuotaEntity entity = entry.getKey();
        ClientQuotaImage quotaImage = entry.getValue();
        if (matches(entity, exactMatch, typeMatch, request.strict())) {
            response.entries().add(toDescribeEntry(entity, quotaImage));
        }
    }
    return response;
}
Also used : HashMap(java.util.HashMap) DescribeClientQuotasRequestData(org.apache.kafka.common.message.DescribeClientQuotasRequestData) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) DescribeClientQuotasResponseData(org.apache.kafka.common.message.DescribeClientQuotasResponseData) HashSet(java.util.HashSet) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 19 with ClientQuotaEntity

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

the class ClientQuotasDelta method apply.

public ClientQuotasImage apply() {
    Map<ClientQuotaEntity, ClientQuotaImage> newEntities = new HashMap<>(image.entities().size());
    for (Entry<ClientQuotaEntity, ClientQuotaImage> entry : image.entities().entrySet()) {
        ClientQuotaEntity entity = entry.getKey();
        ClientQuotaDelta change = changes.get(entity);
        if (change == null) {
            newEntities.put(entity, entry.getValue());
        } else {
            ClientQuotaImage quotaImage = change.apply();
            if (!quotaImage.isEmpty()) {
                newEntities.put(entity, quotaImage);
            }
        }
    }
    for (Entry<ClientQuotaEntity, ClientQuotaDelta> entry : changes.entrySet()) {
        ClientQuotaEntity entity = entry.getKey();
        if (!newEntities.containsKey(entity)) {
            ClientQuotaImage quotaImage = entry.getValue().apply();
            if (!quotaImage.isEmpty()) {
                newEntities.put(entity, quotaImage);
            }
        }
    }
    return new ClientQuotasImage(newEntities);
}
Also used : HashMap(java.util.HashMap) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity)

Example 20 with ClientQuotaEntity

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

the class DescribeClientQuotasResponse method fromQuotaEntities.

public static DescribeClientQuotasResponse fromQuotaEntities(Map<ClientQuotaEntity, Map<String, Double>> entities, int throttleTimeMs) {
    List<EntryData> entries = new ArrayList<>(entities.size());
    for (Map.Entry<ClientQuotaEntity, Map<String, Double>> entry : entities.entrySet()) {
        ClientQuotaEntity quotaEntity = entry.getKey();
        List<EntityData> entityData = new ArrayList<>(quotaEntity.entries().size());
        for (Map.Entry<String, String> entityEntry : quotaEntity.entries().entrySet()) {
            entityData.add(new EntityData().setEntityType(entityEntry.getKey()).setEntityName(entityEntry.getValue()));
        }
        Map<String, Double> quotaValues = entry.getValue();
        List<ValueData> valueData = new ArrayList<>(quotaValues.size());
        for (Map.Entry<String, Double> valuesEntry : entry.getValue().entrySet()) {
            valueData.add(new ValueData().setKey(valuesEntry.getKey()).setValue(valuesEntry.getValue()));
        }
        entries.add(new EntryData().setEntity(entityData).setValues(valueData));
    }
    return new DescribeClientQuotasResponse(new DescribeClientQuotasResponseData().setThrottleTimeMs(throttleTimeMs).setErrorCode((short) 0).setErrorMessage(null).setEntries(entries));
}
Also used : EntryData(org.apache.kafka.common.message.DescribeClientQuotasResponseData.EntryData) ArrayList(java.util.ArrayList) EntityData(org.apache.kafka.common.message.DescribeClientQuotasResponseData.EntityData) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) HashMap(java.util.HashMap) Map(java.util.Map) ValueData(org.apache.kafka.common.message.DescribeClientQuotasResponseData.ValueData) DescribeClientQuotasResponseData(org.apache.kafka.common.message.DescribeClientQuotasResponseData)

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