You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "fvaleri (via GitHub)" <gi...@apache.org> on 2023/01/25 13:52:29 UTC

[GitHub] [kafka] fvaleri commented on a diff in pull request #13158: KAFKA-14647: Moving TopicFilter to server-common/utils

fvaleri commented on code in PR #13158:
URL: https://github.com/apache/kafka/pull/13158#discussion_r1086613346


##########
server-common/src/test/java/org/apache/kafka/server/util/TopicFilterTest.java:
##########
@@ -0,0 +1,50 @@
+/*
+ * 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.util;
+
+import org.apache.kafka.common.internals.Topic;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+public class TopicFilterTest {
+
+    @Test
+    public void testIncludeLists() {
+
+        IncludeList topicFilter1 = new IncludeList("yes1,yes2");
+        assertTrue(topicFilter1.isTopicAllowed("yes2", true));
+        assertTrue(topicFilter1.isTopicAllowed("yes2", false));
+        assertFalse(topicFilter1.isTopicAllowed("no1", true));
+        assertFalse(topicFilter1.isTopicAllowed("no1", false));
+
+        IncludeList topicFilter2 = new IncludeList(".+");
+        assertTrue(topicFilter2.isTopicAllowed("alltopics", true));
+        assertFalse(topicFilter2.isTopicAllowed(Topic.GROUP_METADATA_TOPIC_NAME, true));

Review Comment:
   What about the other internal topic? You can use the INTERNAL_TOPICS set and test all of them.



##########
server-common/src/main/java/org/apache/kafka/server/util/TopicFilter.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.util;
+
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+public abstract class TopicFilter {

Review Comment:
   We cannot replicate the sealed behavior until Java 17, but we can have a single file containing all implementations like this:
   
   ```sh
   public interface TopicFilter {
       boolean isTopicAllowed(String topic, boolean excludeInternalTopics);
       
       public static class IncludeList implements TopicFilter {
         // ...
       }
   }
   ```



##########
server-common/src/main/java/org/apache/kafka/server/util/TopicFilter.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.util;
+
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+public abstract class TopicFilter {
+
+    protected final String regex;
+
+    public TopicFilter(String rawRegex) {
+        this.regex = rawRegex
+                .trim()
+                .replace(',', '|')
+                .replace(" ", "")
+                .replaceAll("^[\"']+", "")
+                .replaceAll("[\"']+$", ""); // property files may bring quotes
+        try {
+            Pattern.compile(regex);
+        } catch (PatternSyntaxException e) {
+            throw new RuntimeException(regex + " is an invalid regex.");
+        }
+    }
+
+    public abstract boolean isTopicAllowed(String topic, boolean excludeInternalTopics);
+
+    @Override
+    public String toString() {
+        return this.regex;
+    }
+
+    public String getRegex() {

Review Comment:
   Why we need this method? I think we can remove it.



##########
server-common/src/main/java/org/apache/kafka/server/util/IncludeList.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.util;
+
+import org.apache.kafka.common.internals.Topic;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class IncludeList extends TopicFilter {
+

Review Comment:
   Please, remove this extra line.



##########
server-common/src/test/java/org/apache/kafka/server/util/TopicFilterTest.java:
##########
@@ -0,0 +1,50 @@
+/*
+ * 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.util;
+
+import org.apache.kafka.common.internals.Topic;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+public class TopicFilterTest {
+

Review Comment:
   Please, remove this extra line and all the others at the start/end of classes/methods.



##########
server-common/src/main/java/org/apache/kafka/server/util/TopicFilter.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.util;
+
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+public abstract class TopicFilter {
+
+    protected final String regex;
+
+    public TopicFilter(String rawRegex) {
+        this.regex = rawRegex
+                .trim()
+                .replace(',', '|')
+                .replace(" ", "")
+                .replaceAll("^[\"']+", "")
+                .replaceAll("[\"']+$", ""); // property files may bring quotes
+        try {
+            Pattern.compile(regex);
+        } catch (PatternSyntaxException e) {
+            throw new RuntimeException(regex + " is an invalid regex.");
+        }
+    }
+
+    public abstract boolean isTopicAllowed(String topic, boolean excludeInternalTopics);

Review Comment:
   It's probably useful to add an overloaded method that only takes the topic name and excludes internal topics by default. This would also simplify tests.



-- 
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-unsubscribe@kafka.apache.org

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