YutaLin commented on code in PR #19807:
URL: https://github.com/apache/kafka/pull/19807#discussion_r2155180404


##########
server/src/main/java/org/apache/kafka/server/ClientQuotaManager.java:
##########
@@ -0,0 +1,942 @@
+/*
+ * 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;
+
+import org.apache.kafka.common.Cluster;
+import org.apache.kafka.common.MetricName;
+import org.apache.kafka.common.internals.Plugin;
+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.Avg;
+import org.apache.kafka.common.metrics.stats.CumulativeSum;
+import org.apache.kafka.common.metrics.stats.Rate;
+import org.apache.kafka.common.security.auth.KafkaPrincipal;
+import org.apache.kafka.common.utils.Sanitizer;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.network.Session;
+import org.apache.kafka.server.config.ClientQuotaManagerConfig;
+import org.apache.kafka.server.quota.ClientQuotaCallback;
+import org.apache.kafka.server.quota.ClientQuotaEntity;
+import org.apache.kafka.server.quota.ClientQuotaType;
+import org.apache.kafka.server.quota.QuotaType;
+import org.apache.kafka.server.quota.QuotaUtils;
+import org.apache.kafka.server.quota.SensorAccess;
+import org.apache.kafka.server.quota.ThrottleCallback;
+import org.apache.kafka.server.quota.ThrottledChannel;
+import org.apache.kafka.server.util.ShutdownableThread;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.DelayQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.Consumer;
+
+final class QuotaTypes {
+    static final int NO_QUOTAS = 0;
+    static final int CLIENT_ID_QUOTA_ENABLED = 1;
+    static final int USER_QUOTA_ENABLED = 2;
+    static final int USER_CLIENT_ID_QUOTA_ENABLED = 4;
+    static final int CUSTOM_QUOTAS = 8; // No metric update optimizations are 
used with custom quotas
+}
+
+public class ClientQuotaManager {
+
+    private static final Logger log = 
LoggerFactory.getLogger(ClientQuotaManager.class);
+
+    // Purge sensors after 1 hour of inactivity
+    public static final int INACTIVE_SENSOR_EXPIRATION_TIME_SECONDS = 3600;
+    private static final String DEFAULT_NAME = "<default>";
+
+    public static final KafkaQuotaEntity DEFAULT_CLIENT_ID_QUOTA_ENTITY =
+            new KafkaQuotaEntity(null, DefaultClientIdEntity.INSTANCE);
+    public static final KafkaQuotaEntity DEFAULT_USER_QUOTA_ENTITY =
+            new KafkaQuotaEntity(DefaultUserEntity.INSTANCE, null);
+    public static final KafkaQuotaEntity DEFAULT_USER_CLIENT_ID_QUOTA_ENTITY =
+            new KafkaQuotaEntity(DefaultUserEntity.INSTANCE, 
DefaultClientIdEntity.INSTANCE);
+
+    public interface BaseUserEntity extends ClientQuotaEntity.ConfigEntity { }
+
+    public static class UserEntity implements BaseUserEntity {
+        private final String sanitizedUser;
+
+        public UserEntity(String sanitizedUser) {
+            this.sanitizedUser = sanitizedUser;
+        }
+
+        @Override
+        public ClientQuotaEntity.ConfigEntityType entityType() {
+            return ClientQuotaEntity.ConfigEntityType.USER;
+        }
+
+        @Override
+        public String name() {
+            return Sanitizer.desanitize(sanitizedUser);
+        }
+
+        public String getSanitizedUser() {
+            return sanitizedUser;
+        }
+
+        @Override
+        public String toString() {
+            return "user " + sanitizedUser;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) return true;
+            if (obj == null || getClass() != obj.getClass()) return false;
+            UserEntity that = (UserEntity) obj;
+            return sanitizedUser.equals(that.sanitizedUser);
+        }
+
+        @Override
+        public int hashCode() {
+            return sanitizedUser.hashCode();
+        }
+    }
+
+    public static class ClientIdEntity implements 
ClientQuotaEntity.ConfigEntity {
+        private final String clientId;
+
+        public ClientIdEntity(String clientId) {
+            this.clientId = clientId;
+        }
+
+        @Override
+        public ClientQuotaEntity.ConfigEntityType entityType() {
+            return ClientQuotaEntity.ConfigEntityType.CLIENT_ID;
+        }
+
+        @Override
+        public String name() {
+            return clientId;
+        }
+
+        public String getClientId() {
+            return clientId;
+        }
+
+        @Override
+        public String toString() {
+            return "client-id " + clientId;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) return true;
+            if (obj == null || getClass() != obj.getClass()) return false;
+            ClientIdEntity that = (ClientIdEntity) obj;
+            return clientId.equals(that.clientId);
+        }
+
+        @Override
+        public int hashCode() {
+            return clientId.hashCode();
+        }
+    }
+
+    public static class DefaultUserEntity implements BaseUserEntity {
+        public static final DefaultUserEntity INSTANCE = new 
DefaultUserEntity();
+
+        private DefaultUserEntity() {}
+
+        @Override
+        public ClientQuotaEntity.ConfigEntityType entityType() {
+            return ClientQuotaEntity.ConfigEntityType.DEFAULT_USER;
+        }
+
+        @Override
+        public String name() {
+            return DEFAULT_NAME;
+        }
+
+        @Override
+        public String toString() {
+            return "default user";
+        }
+    }
+
+    public static class DefaultClientIdEntity implements 
ClientQuotaEntity.ConfigEntity {
+        public static final DefaultClientIdEntity INSTANCE = new 
DefaultClientIdEntity();
+
+        private DefaultClientIdEntity() {}
+
+        @Override
+        public ClientQuotaEntity.ConfigEntityType entityType() {
+            return ClientQuotaEntity.ConfigEntityType.DEFAULT_CLIENT_ID;
+        }
+
+        @Override
+        public String name() {
+            return DEFAULT_NAME;
+        }
+
+        @Override
+        public String toString() {
+            return "default client-id";
+        }
+    }
+
+    public static class KafkaQuotaEntity implements ClientQuotaEntity {
+        private final BaseUserEntity userEntity;
+        private final ClientQuotaEntity.ConfigEntity clientIdEntity;
+
+        public KafkaQuotaEntity(BaseUserEntity userEntity, 
ClientQuotaEntity.ConfigEntity clientIdEntity) {
+            this.userEntity = userEntity;
+            this.clientIdEntity = clientIdEntity;
+        }
+
+        @Override
+        public List<ConfigEntity> configEntities() {
+            List<ClientQuotaEntity.ConfigEntity> entities = new ArrayList<>();
+            if (userEntity != null) {
+                entities.add(userEntity);
+            }
+            if (clientIdEntity != null) {
+                entities.add(clientIdEntity);
+            }
+            return entities;
+        }
+
+        public String sanitizedUser() {
+            if (userEntity instanceof UserEntity) {
+                return ((UserEntity) userEntity).getSanitizedUser();
+            } else if (userEntity == DefaultUserEntity.INSTANCE) {
+                return DEFAULT_NAME;
+            }
+            return "";
+        }
+
+        public String clientId() {
+            return clientIdEntity != null ? clientIdEntity.name() : "";
+        }
+
+        @Override
+        public String toString() {
+            String user = userEntity != null ? userEntity.toString() : "";
+            String clientId = clientIdEntity != null ? 
clientIdEntity.toString() : "";
+            return (user + " " + clientId).trim();
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) return true;
+            if (obj == null || getClass() != obj.getClass()) return false;
+            KafkaQuotaEntity that = (KafkaQuotaEntity) obj;
+            return java.util.Objects.equals(userEntity, that.userEntity) &&
+                    java.util.Objects.equals(clientIdEntity, 
that.clientIdEntity);
+        }
+
+        @Override
+        public int hashCode() {
+            return java.util.Objects.hash(userEntity, clientIdEntity);
+        }
+    }
+
+    public static class DefaultTags {
+        public static final String USER = "user";
+        public static final String CLIENT_ID = "client-id";
+    }
+
+    private final ClientQuotaManagerConfig config;
+    protected final Metrics metrics;
+    private final QuotaType quotaType;
+    protected final Time time;
+    private final Optional<Plugin<ClientQuotaCallback>> 
clientQuotaCallbackPlugin;

Review Comment:
   Hi @chia7712 
   According to my understanding, `clientQuotaCallbackPlugin` is for 
customization of how many quotas are calculated by brokers. It will affect 
quota limit. There's also a `Strimzi's StaticQuotaCallback` plugin might use 
it. Do we need to remove it?
   https://github.com/strimzi/kafka-quotas-plugin/blob/main/README.md



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to