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