Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] [broker] fix subscribe a non-existent cluster/namespace/topic. #51

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ protected CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadat
return validateTopicOperationAsync(topicName, TopicOperation.LOOKUP)
.thenCompose(__ -> validateClusterOwnershipAsync(topicName.getCluster()))
.thenCompose(__ -> validateGlobalNamespaceOwnershipAsync(topicName.getNamespaceObject()))
.thenCompose(__ -> validateNamespaceExists(topicName.getNamespaceObject()))
.thenCompose(__ -> {
if (checkAllowAutoCreation) {
return pulsar().getBrokerService()
Expand All @@ -506,6 +507,18 @@ protected void validateClusterExists(String cluster) {
}
}

protected CompletableFuture<Void> validateNamespaceExists(NamespaceName namespace) {
try {
if (namespaceResources().namespaceExists(namespace)) {
return CompletableFuture.completedFuture(null);
} else {
return FutureUtil.failedFuture(new RestException(Status.NOT_FOUND, "Namespace does not exist"));
}
} catch (Exception e) {
return FutureUtil.failedFuture(e);
}
}

protected Policies getNamespacePolicies(String tenant, String cluster, String namespace) {
NamespaceName ns = NamespaceName.get(tenant, cluster, namespace);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@Slf4j
Expand Down Expand Up @@ -245,7 +246,7 @@ public void testConsumerStatsOutput() throws Exception {
"connectedSince",
"clientVersion");

final String topicName = "persistent://prop/use/ns-abc/testConsumerStatsOutput";
final String topicName = "persistent://my-property/my-ns/testConsumerStatsOutput";
final String subName = "my-subscription";

Consumer<byte[]> consumer = pulsarClient.newConsumer()
Expand All @@ -269,6 +270,30 @@ public void testConsumerStatsOutput() throws Exception {
consumer.close();
}

@DataProvider(name = "invalidTopicName")
public static Object[][] invalidTopicName() {
// some topic names in non-exist namespace
return new Object[][]{
{"persistent://prop/use/ns-abc/testNonExistNamespace"},
{"persistent://prop/ns-abc/testNonExistNamespace"}
};
}

@Test(dataProvider = "invalidTopicName")
public void testNonExistNamespace(String invalidTopicName) throws Exception {
final String subName = "my-subscription";
try {
Consumer<byte[]> consumer = pulsarClient.newConsumer()
.topic(invalidTopicName)
.subscriptionType(SubscriptionType.Shared)
.subscriptionName(subName)
.subscribe();
Assert.fail("should have failed");
} catch (Exception e) {
// ok
}
}


@Test
public void testPersistentTopicMessageAckRateMetricTopicLevel() throws Exception {
Expand Down
Loading