World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#10133] Fix TopicMetaService crash on malformed topic namespace (#10215)
Ramesh Reddy Adutlac15979fd54f4
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/TopicMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/TopicMetaService.javaindex c443c7b668..ede75548b1 100644--- a/core/src/main/java/org/apache/gravitino/storage/relational/service/TopicMetaService.java+++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/TopicMetaService.java@@ -166,6 +166,10 @@ public class TopicMetaService {}private List<TopicPO> listTopicPOsByFullQualifiedName(Namespace namespace) {+ if (namespace == null || namespace.length() != 3) {+ throw new NoSuchEntityException(+ "Topic namespace must have 3 levels, the input namespace is %s", namespace);+ }String[] namespaceLevels = namespace.levels();List<TopicPO> topicPOs =SessionUtils.getWithoutCommit(@@ -190,6 +194,12 @@ public class TopicMetaService {}private TopicPO getTopicPOByFullQualifiedName(NameIdentifier identifier) {+ if (identifier == null+ || identifier.namespace() == null+ || identifier.namespace().length() != 3) {+ throw new NoSuchEntityException(+ "Topic identifier must have a 3-level namespace, the input identifier is %s", identifier);+ }String[] namespaceLevels = identifier.namespace().levels();TopicPO topicPO =SessionUtils.getWithoutCommit(diff --git a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestTopicMetaService.java b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestTopicMetaService.javaindex 14a1cdcf80..a8a3c6fc1f 100644--- a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestTopicMetaService.java+++ b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestTopicMetaService.java@@ -19,14 +19,20 @@package org.apache.gravitino.storage.relational.service;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 java.io.IOException;+import java.lang.reflect.InvocationTargetException;+import java.lang.reflect.Method;import java.time.Instant;import java.util.List;import org.apache.gravitino.Entity;import org.apache.gravitino.EntityAlreadyExistsException;+import org.apache.gravitino.NameIdentifier;+import org.apache.gravitino.Namespace;+import org.apache.gravitino.exceptions.NoSuchEntityException;import org.apache.gravitino.meta.TopicEntity;import org.apache.gravitino.storage.RandomIdGenerator;import org.apache.gravitino.storage.relational.TestJDBCBackend;@@ -145,4 +151,23 @@ public class TestTopicMetaService extends TestJDBCBackend {e ->createTopicEntity(topicCopy.id(), topicCopy.namespace(), "topic", AUDIT_INFO)));}++ @TestTemplate+ public void testGetTopicByFullQualifiedNameMalformedNamespaceThrowsNoSuchEntityException()+ throws Exception {+ Method method =+ TopicMetaService.class.getDeclaredMethod(+ "getTopicPOByFullQualifiedName", NameIdentifier.class);+ method.setAccessible(true);++ NameIdentifier malformedIdentifier =+ NameIdentifier.of(Namespace.of(metalakeName, catalogName), "topic");++ InvocationTargetException invocationTargetException =+ assertThrows(+ InvocationTargetException.class,+ () -> method.invoke(TopicMetaService.getInstance(), malformedIdentifier));++ assertInstanceOf(NoSuchEntityException.class, invocationTargetException.getCause());+ }}