Search in sources :

Example 6 with ClientQuotaAlteration

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

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

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

the class KafkaAdminClient method alterClientQuotas.

@Override
public AlterClientQuotasResult alterClientQuotas(Collection<ClientQuotaAlteration> entries, AlterClientQuotasOptions options) {
    Map<ClientQuotaEntity, KafkaFutureImpl<Void>> futures = new HashMap<>(entries.size());
    for (ClientQuotaAlteration entry : entries) {
        futures.put(entry.entity(), new KafkaFutureImpl<>());
    }
    final long now = time.milliseconds();
    runnable.call(new Call("alterClientQuotas", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        @Override
        AlterClientQuotasRequest.Builder createRequest(int timeoutMs) {
            return new AlterClientQuotasRequest.Builder(entries, options.validateOnly());
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            AlterClientQuotasResponse response = (AlterClientQuotasResponse) abstractResponse;
            response.complete(futures);
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(futures.values(), throwable);
        }
    }, now);
    return new AlterClientQuotasResult(Collections.unmodifiableMap(futures));
}
Also used : ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) HashMap(java.util.HashMap) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) AlterClientQuotasResponse(org.apache.kafka.common.requests.AlterClientQuotasResponse) AlterClientQuotasRequest(org.apache.kafka.common.requests.AlterClientQuotasRequest) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity)

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