Search in sources :

Example 1 with SnapshotRegistry

use of org.apache.kafka.timeline.SnapshotRegistry 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);
}
Also used : ClientQuotaAlteration(org.apache.kafka.common.quota.ClientQuotaAlteration) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LogContext(org.apache.kafka.common.utils.LogContext) ClientQuotaEntity(org.apache.kafka.common.quota.ClientQuotaEntity) Test(org.junit.jupiter.api.Test)

Example 2 with SnapshotRegistry

use of org.apache.kafka.timeline.SnapshotRegistry 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 3 with SnapshotRegistry

use of org.apache.kafka.timeline.SnapshotRegistry in project kafka by apache.

the class ConfigurationControlManagerTest method testReplay.

@Test
public void testReplay() throws Exception {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    ConfigurationControlManager manager = new ConfigurationControlManager(new LogContext(), snapshotRegistry, CONFIGS, Optional.empty(), ConfigurationValidator.NO_OP);
    assertEquals(Collections.emptyMap(), manager.getConfigs(BROKER0));
    manager.replay(new ConfigRecord().setResourceType(BROKER.id()).setResourceName("0").setName("foo.bar").setValue("1,2"));
    assertEquals(Collections.singletonMap("foo.bar", "1,2"), manager.getConfigs(BROKER0));
    manager.replay(new ConfigRecord().setResourceType(BROKER.id()).setResourceName("0").setName("foo.bar").setValue(null));
    assertEquals(Collections.emptyMap(), manager.getConfigs(BROKER0));
    manager.replay(new ConfigRecord().setResourceType(TOPIC.id()).setResourceName("mytopic").setName("abc").setValue("x,y,z"));
    manager.replay(new ConfigRecord().setResourceType(TOPIC.id()).setResourceName("mytopic").setName("def").setValue("blah"));
    assertEquals(toMap(entry("abc", "x,y,z"), entry("def", "blah")), manager.getConfigs(MYTOPIC));
    RecordTestUtils.assertBatchIteratorContains(asList(asList(new ApiMessageAndVersion(new ConfigRecord().setResourceType(TOPIC.id()).setResourceName("mytopic").setName("abc").setValue("x,y,z"), (short) 0), new ApiMessageAndVersion(new ConfigRecord().setResourceType(TOPIC.id()).setResourceName("mytopic").setName("def").setValue("blah"), (short) 0))), manager.iterator(Long.MAX_VALUE));
}
Also used : ConfigRecord(org.apache.kafka.common.metadata.ConfigRecord) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) LogContext(org.apache.kafka.common.utils.LogContext) Test(org.junit.jupiter.api.Test)

Example 4 with SnapshotRegistry

use of org.apache.kafka.timeline.SnapshotRegistry in project kafka by apache.

the class ConfigurationControlManagerTest method testGetConfigValueDefault.

@Test
public void testGetConfigValueDefault() {
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    ConfigurationControlManager manager = new ConfigurationControlManager(new LogContext(), snapshotRegistry, CONFIGS, Optional.empty(), ConfigurationValidator.NO_OP);
    assertEquals("1", manager.getConfigValueDefault(BROKER, "foo.bar"));
    assertEquals(null, manager.getConfigValueDefault(BROKER, "foo.baz.quux"));
    assertEquals(null, manager.getConfigValueDefault(TOPIC, "abc"));
    assertEquals("true", manager.getConfigValueDefault(TOPIC, "ghi"));
}
Also used : SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) LogContext(org.apache.kafka.common.utils.LogContext) Test(org.junit.jupiter.api.Test)

Example 5 with SnapshotRegistry

use of org.apache.kafka.timeline.SnapshotRegistry in project kafka by apache.

the class FeatureControlManagerTest method testReplay.

@Test
public void testReplay() {
    FeatureLevelRecord record = new FeatureLevelRecord().setName("foo").setMinFeatureLevel((short) 1).setMaxFeatureLevel((short) 2);
    SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
    snapshotRegistry.getOrCreateSnapshot(-1);
    FeatureControlManager manager = new FeatureControlManager(rangeMap("foo", 1, 2), snapshotRegistry);
    manager.replay(record);
    snapshotRegistry.getOrCreateSnapshot(123);
    assertEquals(new FeatureMapAndEpoch(new FeatureMap(rangeMap("foo", 1, 2)), 123), manager.finalizedFeatures(123));
}
Also used : FeatureMap(org.apache.kafka.metadata.FeatureMap) SnapshotRegistry(org.apache.kafka.timeline.SnapshotRegistry) LogContext(org.apache.kafka.common.utils.LogContext) FeatureMapAndEpoch(org.apache.kafka.metadata.FeatureMapAndEpoch) FeatureLevelRecord(org.apache.kafka.common.metadata.FeatureLevelRecord) Test(org.junit.jupiter.api.Test)

Aggregations

LogContext (org.apache.kafka.common.utils.LogContext)28 SnapshotRegistry (org.apache.kafka.timeline.SnapshotRegistry)28 Test (org.junit.jupiter.api.Test)24 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)12 HashMap (java.util.HashMap)6 MockTime (org.apache.kafka.common.utils.MockTime)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Random (java.util.Random)5 RegisterBrokerRecord (org.apache.kafka.common.metadata.RegisterBrokerRecord)5 FeatureMap (org.apache.kafka.metadata.FeatureMap)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Endpoint (org.apache.kafka.common.Endpoint)4 ConfigRecord (org.apache.kafka.common.metadata.ConfigRecord)4 FeatureLevelRecord (org.apache.kafka.common.metadata.FeatureLevelRecord)4 BrokerEndpoint (org.apache.kafka.common.metadata.RegisterBrokerRecord.BrokerEndpoint)4 ClientQuotaEntity (org.apache.kafka.common.quota.ClientQuotaEntity)4 ApiError (org.apache.kafka.common.requests.ApiError)4 HashSet (java.util.HashSet)3 List (java.util.List)3