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);
}
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;
}
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));
}
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);
}
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));
}
Aggregations