chia7712 commented on code in PR #22403: URL: https://github.com/apache/kafka/pull/22403#discussion_r3374331397
########## server/src/test/java/org/apache/kafka/server/quota/ControllerMutationQuotaManagerTest.java: ########## @@ -0,0 +1,249 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.server.quota; + +import org.apache.kafka.common.MetricName; +import org.apache.kafka.common.errors.ThrottlingQuotaExceededException; +import org.apache.kafka.common.metrics.KafkaMetric; +import org.apache.kafka.common.metrics.MetricConfig; +import org.apache.kafka.common.metrics.Metrics; +import org.apache.kafka.common.metrics.Quota; +import org.apache.kafka.common.metrics.QuotaViolationException; +import org.apache.kafka.common.metrics.Sensor; +import org.apache.kafka.common.metrics.stats.TokenBucket; +import org.apache.kafka.common.utils.MockTime; +import org.apache.kafka.server.config.ClientQuotaManagerConfig; + +import org.junit.jupiter.api.Test; + +import java.net.UnknownHostException; +import java.util.Optional; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +import static org.apache.kafka.server.quota.ControllerMutationQuotaManager.throttleTimeMs; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +public class ControllerMutationQuotaManagerTest extends BaseClientQuotaManagerTest { + + private static final String USER = "ANONYMOUS"; + private static final String CLIENT_ID = "test-client"; + + private final ClientQuotaManagerConfig config = new ClientQuotaManagerConfig(10, 1); + + @Test + public void testStrictControllerMutationQuotaViolation() { + MockTime time = new MockTime(0, System.currentTimeMillis(), 0); + try (Metrics metrics = new Metrics(time)) { + Sensor sensor = metrics.sensor("sensor", new MetricConfig() + .quota(Quota.upperBound(10)) + .timeWindow(1, TimeUnit.SECONDS) + .samples(10)); + MetricName metricName = metrics.metricName("rate", "test-group"); + assertTrue(sensor.add(metricName, new TokenBucket())); + + StrictControllerMutationQuota quota = new StrictControllerMutationQuota(time, sensor); + assertFalse(quota.isExceeded()); + + // Recording a first value at T to bring the tokens to 10. Value is accepted + // because the quota is not exhausted yet. + quota.record(90); + assertFalse(quota.isExceeded()); + assertEquals(0, quota.throttleTime()); + + // Recording a second value at T to bring the tokens to -80. Value is accepted + quota.record(90); + assertFalse(quota.isExceeded()); + assertEquals(0, quota.throttleTime()); + + // Recording a third value at T is rejected immediately because there are no + // tokens available in the bucket. + assertThrows(ThrottlingQuotaExceededException.class, () -> quota.record(90)); + assertTrue(quota.isExceeded()); + assertEquals(8000, quota.throttleTime()); + + // Throttle time is adjusted with time + time.sleep(5000); + assertEquals(3000, quota.throttleTime()); + } + } + + @Test + public void testPermissiveControllerMutationQuotaViolation() { + MockTime time = new MockTime(0, System.currentTimeMillis(), 0); + try (Metrics metrics = new Metrics(time)) { + Sensor sensor = metrics.sensor("sensor", new MetricConfig() + .quota(Quota.upperBound(10)) + .timeWindow(1, TimeUnit.SECONDS) + .samples(10)); + MetricName metricName = metrics.metricName("rate", "test-group"); + assertTrue(sensor.add(metricName, new TokenBucket())); + + PermissiveControllerMutationQuota quota = new PermissiveControllerMutationQuota(time, sensor); + assertFalse(quota.isExceeded()); + + // Recording a first value at T to bring the tokens 10. Value is accepted + // because the quota is not exhausted yet. + quota.record(90); + assertFalse(quota.isExceeded()); + assertEquals(0, quota.throttleTime()); + + // Recording a second value at T to bring the tokens to -80. Value is accepted + quota.record(90); + assertFalse(quota.isExceeded()); + assertEquals(8000, quota.throttleTime()); + + // Recording a third value at T to bring the tokens to -170. Value is accepted + // even though the quota is exhausted. + quota.record(90); + assertFalse(quota.isExceeded()); // quota is never exceeded + assertEquals(17000, quota.throttleTime()); + + // Throttle time is adjusted with time + time.sleep(5000); + assertEquals(12000, quota.throttleTime()); + } + } + + private void withQuotaManager(Consumer<ControllerMutationQuotaManager> f) { + ControllerMutationQuotaManager quotaManager = new ControllerMutationQuotaManager(config, metrics, time, "", Optional.empty()); + try { + f.accept(quotaManager); + } finally { + quotaManager.shutdown(); + } + } + + @Test + public void testThrottleTime() { + MockTime time = new MockTime(0, System.currentTimeMillis(), 0); + try (Metrics metrics = new Metrics(time)) { + Sensor sensor = metrics.sensor("sensor"); + MetricName metricName = metrics.metricName("tokens", "test-group"); + sensor.add(metricName, new TokenBucket()); + KafkaMetric metric = metrics.metric(metricName); + + assertEquals(0, throttleTimeMs(new QuotaViolationException(metric, 0, 10))); + assertEquals(500, throttleTimeMs(new QuotaViolationException(metric, -5, 10))); + assertEquals(1000, throttleTimeMs(new QuotaViolationException(metric, -10, 10))); + } + } + + @Test + public void testControllerMutationQuotaViolation() { + withQuotaManager(quotaManager -> { + quotaManager.updateQuota( + Optional.of(new ClientQuotaManager.UserEntity(USER)), + Optional.of(new ClientQuotaManager.ClientIdEntity(CLIENT_ID)), + Optional.of(Quota.upperBound(10)) + ); + KafkaMetric queueSizeMetric = metrics.metrics().get( + metrics.metricName("queue-size", QuotaType.CONTROLLER_MUTATION.toString(), "")); + + // Verify that there is no quota violation if we remain under the quota. + for (int i = 0; i < 10; i++) { + assertEquals(0, maybeRecord(quotaManager, USER, CLIENT_ID, 10)); + time.sleep(1000); + } + assertEquals(0, ((Double) queueSizeMetric.metricValue()).intValue()); + + // Create a spike worth of 110 mutations. + // Current tokens in the bucket = 100 + // As we use the Strict enforcement, the quota is checked before updating the rate. Hence, + // the spike is accepted and no quota violation error is raised. + int throttleTime = maybeRecord(quotaManager, USER, CLIENT_ID, 110); + assertEquals(0, throttleTime, "Should not be throttled"); + + // Create a spike worth of 110 mutations. + // Current tokens in the bucket = 100 - 110 = -10 + // As the quota is already violated, the spike is rejected immediately without updating the + // rate. The client must wait: + // 10 / 10 = 1s + throttleTime = maybeRecord(quotaManager, USER, CLIENT_ID, 110); + assertEquals(1000, throttleTime, "Should be throttled"); + + // Throttle + try { + throttle(quotaManager, throttleTime, callback); + } catch (UnknownHostException e) { Review Comment: Why not throw the exception as-is? ########## server/src/test/java/org/apache/kafka/server/quota/BaseClientQuotaManagerTest.java: ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.server.quota; + +import org.apache.kafka.common.memory.MemoryPool; +import org.apache.kafka.common.metrics.MetricConfig; +import org.apache.kafka.common.metrics.Metrics; +import org.apache.kafka.common.network.ClientInformation; +import org.apache.kafka.common.network.ListenerName; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.requests.AbstractRequest; +import org.apache.kafka.common.requests.FetchRequest; +import org.apache.kafka.common.requests.RequestContext; +import org.apache.kafka.common.requests.RequestHeader; +import org.apache.kafka.common.security.auth.KafkaPrincipal; +import org.apache.kafka.common.security.auth.SecurityProtocol; +import org.apache.kafka.common.utils.MockTime; +import org.apache.kafka.network.Request; +import org.apache.kafka.network.Session; +import org.apache.kafka.network.metrics.RequestChannelMetrics; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; + +import static org.mockito.Mockito.mock; + +public class BaseClientQuotaManagerTest { + + protected final MockTime time = new MockTime(); + protected int numCallbacks = 0; + protected Metrics metrics; + + @BeforeEach + public void setup() { + metrics = new Metrics(new MetricConfig(), List.of(), time); + } + + @AfterEach + public void tearDown() { + metrics.close(); + } + + protected ThrottleCallback callback = new ThrottleCallback() { + @Override + public void startThrottling() {} + + @Override + public void endThrottling() { + // Count how many times this callback is called for notifyThrottlingDone(). + numCallbacks += 1; + } + }; + + private <T extends AbstractRequest> Request buildRequest(AbstractRequest.Builder<T> builder) throws UnknownHostException { + return buildRequest(builder, ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT)); + } + + private <T extends AbstractRequest> Request buildRequest(AbstractRequest.Builder<T> builder, ListenerName listenerName) throws UnknownHostException { + T request = builder.build(); + ByteBuffer buffer = request.serializeWithHeader(new RequestHeader(builder.apiKey(), request.version(), "", 0)); + RequestChannelMetrics requestChannelMetrics = mock(RequestChannelMetrics.class); + + // read the header from the buffer first so that the body can be read next from the Request constructor + RequestHeader header = RequestHeader.parse(buffer); + RequestContext context = new RequestContext(header, "1", InetAddress.getLocalHost(), KafkaPrincipal.ANONYMOUS, + listenerName, SecurityProtocol.PLAINTEXT, ClientInformation.EMPTY, false); + return new Request(1, context, 0, MemoryPool.NONE, buffer, requestChannelMetrics); + } + + protected Session buildSession(String user) { + KafkaPrincipal principal = new KafkaPrincipal(KafkaPrincipal.USER_TYPE, user); + return new Session(principal, null); + } + + protected int maybeRecord(ClientQuotaManager quotaManager, String user, String clientId, double value) { + return quotaManager.maybeRecordAndGetThrottleTimeMs(buildSession(user), clientId, value, time.milliseconds()); + } + + protected void throttle(ClientQuotaManager quotaManager, int throttleTimeMs, ThrottleCallback channelThrottlingCallback) throws UnknownHostException { Review Comment: We can merge them into a single method to decorate this class, and then we can get rid of the two `buildRequest` methods. ```java protected void throttle(ClientQuotaManager quotaManager, int throttleTimeMs, ThrottleCallback channelThrottlingCallback) throws UnknownHostException { var builder = FetchRequest.Builder.forConsumer(ApiKeys.FETCH.latestVersion(), 0, 1000, Map.of()); var fetchRequest = builder.build(); var buffer = fetchRequest.serializeWithHeader(new RequestHeader(builder.apiKey(), fetchRequest.version(), "", 0)); var requestChannelMetrics = mock(RequestChannelMetrics.class); var listenerName = ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT); var header = RequestHeader.parse(buffer); var context = new RequestContext(header, "1", InetAddress.getLocalHost(), KafkaPrincipal.ANONYMOUS, listenerName, SecurityProtocol.PLAINTEXT, ClientInformation.EMPTY, false); var request = new Request(1, context, 0, MemoryPool.NONE, buffer, requestChannelMetrics); quotaManager.throttle(request.header().clientId(), request.session(), channelThrottlingCallback, throttleTimeMs); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
