cmccabe commented on a change in pull request #10949:
URL: https://github.com/apache/kafka/pull/10949#discussion_r661858804



##########
File path: metadata/src/main/java/org/apache/kafka/image/ClientQuotasImage.java
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.image;
+
+import org.apache.kafka.common.message.DescribeClientQuotasRequestData;
+import org.apache.kafka.common.message.DescribeClientQuotasResponseData;
+import 
org.apache.kafka.common.message.DescribeClientQuotasResponseData.EntityData;
+import 
org.apache.kafka.common.message.DescribeClientQuotasResponseData.EntryData;
+import org.apache.kafka.common.quota.ClientQuotaEntity;
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+
+import static org.apache.kafka.common.protocol.Errors.INVALID_REQUEST;
+import static org.apache.kafka.common.protocol.Errors.UNSUPPORTED_VERSION;
+import static 
org.apache.kafka.common.requests.DescribeClientQuotasRequest.MATCH_TYPE_EXACT;
+import static 
org.apache.kafka.common.requests.DescribeClientQuotasRequest.MATCH_TYPE_DEFAULT;
+import static 
org.apache.kafka.common.requests.DescribeClientQuotasRequest.MATCH_TYPE_SPECIFIED;
+
+
+/**
+ * Represents the client quotas in the metadata image.
+ *
+ * This class is thread-safe.
+ */
+public final class ClientQuotasImage {
+    public final static ClientQuotasImage EMPTY = new 
ClientQuotasImage(Collections.emptyMap());
+
+    private final Map<ClientQuotaEntity, ClientQuotaImage> entities;
+
+    public ClientQuotasImage(Map<ClientQuotaEntity, ClientQuotaImage> 
entities) {
+        this.entities = entities;
+    }
+
+    public boolean isEmpty() {
+        return entities.isEmpty();
+    }
+
+    Map<ClientQuotaEntity, ClientQuotaImage> entities() {
+        return entities;
+    }
+
+    public void write(Consumer<List<ApiMessageAndVersion>> out) throws 
IOException {
+        for (Entry<ClientQuotaEntity, ClientQuotaImage> entry : 
entities.entrySet()) {
+            ClientQuotaEntity entity = entry.getKey();
+            ClientQuotaImage clientQuotaImage = entry.getValue();
+            clientQuotaImage.write(entity, out);
+        }
+    }
+
+    public DescribeClientQuotasResponseData 
describe(DescribeClientQuotasRequestData request) {
+        DescribeClientQuotasResponseData response = new 
DescribeClientQuotasResponseData();
+        Map<String, String> exactMatch = new HashMap<>();
+        Set<String> typeMatch = new HashSet<>();
+        for (DescribeClientQuotasRequestData.ComponentData component : 
request.components()) {
+            if (component.entityType().isEmpty()) {
+                response.setErrorCode(INVALID_REQUEST.code());
+                response.setErrorMessage("Invalid empty entity type.");
+                return response;
+            } else if (exactMatch.containsKey(component.entityType()) ||
+                    typeMatch.contains(component.entityType())) {
+                response.setErrorCode(INVALID_REQUEST.code());
+                response.setErrorMessage("Entity type " + 
component.entityType() +
+                    " cannot appear more than once in the filter.");
+                return response;
+            }
+            switch (component.matchType()) {
+                case MATCH_TYPE_EXACT:
+                    if (component.match() == null) {
+                        response.setErrorCode(INVALID_REQUEST.code());
+                        response.setErrorMessage("Request specified 
MATCH_TYPE_EXACT, " +
+                            "but set match string to null.");
+                        return response;
+                    }
+                    exactMatch.put(component.entityType(), component.match());
+                    break;
+                case MATCH_TYPE_DEFAULT:
+                    if (component.match() != null) {
+                        response.setErrorCode(INVALID_REQUEST.code());
+                        response.setErrorMessage("Request specified 
MATCH_TYPE_DEFAULT, " +
+                            "but also specified a match string.");
+                        return response;
+                    }
+                    exactMatch.put(component.entityType(), null);
+                    break;
+                case MATCH_TYPE_SPECIFIED:
+                    if (component.match() != null) {
+                        response.setErrorCode(INVALID_REQUEST.code());
+                        response.setErrorMessage("Request specified 
MATCH_TYPE_SPECIFIED, " +
+                            "but also specified a match string.");
+                        return response;
+                    }
+                    typeMatch.add(component.entityType());
+                    break;
+                default:
+                    response.setErrorCode(UNSUPPORTED_VERSION.code());
+                    response.setErrorMessage("Unknown match type " + 
component.matchType());
+                    return response;
+            }
+        }
+        // TODO: this is O(N). We should do some indexing here to speed it up.

Review comment:
       Filed KAFKA-13022




-- 
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