diff --git a/proxy/src/main/java/org/apache/rocketmq/proxy/service/admin/DefaultAdminService.java b/proxy/src/main/java/org/apache/rocketmq/proxy/service/admin/DefaultAdminService.java index f3c68eab5c4..d1ac5e0d07c 100644 --- a/proxy/src/main/java/org/apache/rocketmq/proxy/service/admin/DefaultAdminService.java +++ b/proxy/src/main/java/org/apache/rocketmq/proxy/service/admin/DefaultAdminService.java @@ -49,8 +49,12 @@ public boolean topicExist(String topic) { try { topicRouteData = this.getTopicRouteDataDirectlyFromNameServer(topic); topicExist = topicRouteData != null; - } catch (Throwable e) { - topicExist = false; + } catch (Exception e) { + if (TopicRouteHelper.isTopicNotExistError(e)) { + topicExist = false; + } else { + throw new IllegalStateException("get topic route for topic='" + topic + "' failed", e); + } } return topicExist; diff --git a/proxy/src/test/java/org/apache/rocketmq/proxy/service/admin/DefaultAdminServiceTest.java b/proxy/src/test/java/org/apache/rocketmq/proxy/service/admin/DefaultAdminServiceTest.java index cdfc7f7fc23..4996b75a6b8 100644 --- a/proxy/src/test/java/org/apache/rocketmq/proxy/service/admin/DefaultAdminServiceTest.java +++ b/proxy/src/test/java/org/apache/rocketmq/proxy/service/admin/DefaultAdminServiceTest.java @@ -27,6 +27,7 @@ import org.apache.rocketmq.remoting.protocol.route.TopicRouteData; import org.apache.rocketmq.client.impl.mqclient.MQClientAPIExt; import org.apache.rocketmq.client.impl.mqclient.MQClientAPIFactory; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,6 +36,7 @@ import org.mockito.junit.MockitoJUnitRunner; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; @@ -87,6 +89,27 @@ public void testCreateTopic() throws Exception { assertEquals(8, topicConfigArgumentCaptor.getValue().getReadQueueNums()); } + @Test + public void testTopicExistReturnsFalseForNotFound() throws Exception { + when(mqClientAPIExt.getTopicRouteInfoFromNameServer(eq("missingTopic"), anyLong())) + .thenThrow(new MQClientException(ResponseCode.TOPIC_NOT_EXIST, "topic not exist")); + + assertFalse(defaultAdminService.topicExist("missingTopic")); + } + + @Test + public void testTopicExistThrowsForUnexpectedRouteLookupFailure() throws Exception { + MQClientException cause = new MQClientException(ResponseCode.SYSTEM_ERROR, "namesrv unavailable"); + when(mqClientAPIExt.getTopicRouteInfoFromNameServer(eq("brokenTopic"), anyLong())) + .thenThrow(cause); + + IllegalStateException exception = Assert.assertThrows(IllegalStateException.class, + () -> defaultAdminService.topicExist("brokenTopic")); + + assertEquals("get topic route for topic='brokenTopic' failed", exception.getMessage()); + assertEquals(cause, exception.getCause()); + } + private TopicRouteData createTopicRouteData(int brokerNum) { TopicRouteData topicRouteData = new TopicRouteData(); for (int i = 0; i < brokerNum; i++) { @@ -100,4 +123,4 @@ private TopicRouteData createTopicRouteData(int brokerNum) { } return topicRouteData; } -} \ No newline at end of file +}