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 validateGlobalNamespaceOwnership wrap exception issue. #14269

Merged
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 @@ -205,10 +205,7 @@ protected void validateGlobalNamespaceOwnership() {
} catch (IllegalArgumentException e) {
throw new RestException(Status.PRECONDITION_FAILED, "Tenant name or namespace is not valid");
} catch (RestException re) {
if (re.getResponse().getStatus() == Status.NOT_FOUND.getStatusCode()) {
throw new RestException(Status.NOT_FOUND, "Namespace not found");
}
throw new RestException(Status.PRECONDITION_FAILED, "Namespace does not have any clusters configured");
throw re;
} catch (Exception e) {
log.warn("Failed to validate global cluster configuration : ns={} emsg={}", namespaceName, e.getMessage());
throw new RestException(Status.SERVICE_UNAVAILABLE, "Failed to validate global cluster configuration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,9 @@ public static CompletableFuture<ClusterDataImpl> checkLocalOrGetPeerReplicationC
validationFuture.complete(null);
}
} else {
String msg = String.format("Policies not found for %s namespace", namespace.toString());
String msg = String.format("Namespace %s not found", namespace.toString());
log.warn(msg);
validationFuture.completeExceptionally(new RestException(Status.NOT_FOUND, msg));
validationFuture.completeExceptionally(new RestException(Status.NOT_FOUND, "Namespace not found"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this section? The old comment out looks great. If the policies is non-exist, we shouldn't throw the Namespace %s not found.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not looks great. Because policies mean namespace. If there is a namespace, there are policies. For user, it's about namespace here.
And some logs in Namespaces methods are also the same, not policies but namespace. Please help confirm.

Copy link
Member

@nodece nodece Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, there is have a premise is that every namespace has policies.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i also have the same doubt as @nodece .
the if condition on line 773 is checking if the policies exist for the namespace. this means the namespace already exists but there are no policies. it seems like the original error message is more accurate. perhaps i am missing something?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, just saw your comment @nodece
thanks

}
}).exceptionally(ex -> {
String msg = String.format("Failed to validate global cluster configuration : cluster=%s ns=%s emsg=%s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.pulsar.broker.admin.v2.PersistentTopics;
import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
import org.apache.pulsar.broker.authentication.AuthenticationDataHttps;
import org.apache.pulsar.broker.resources.NamespaceResources;
import org.apache.pulsar.broker.resources.PulsarResources;
import org.apache.pulsar.broker.resources.TopicResources;
import org.apache.pulsar.broker.service.BrokerService;
Expand Down Expand Up @@ -108,6 +109,7 @@ public class PersistentTopicsTest extends MockedPulsarServiceBaseTest {
protected Field uriField;
protected UriInfo uriInfo;
private NonPersistentTopics nonPersistentTopic;
private NamespaceResources namespaceResources;

@BeforeClass
public void initPersistentTopics() throws Exception {
Expand All @@ -133,6 +135,7 @@ protected void setup() throws Exception {
nonPersistentTopic = spy(NonPersistentTopics.class);
nonPersistentTopic.setServletContext(new MockServletContext());
nonPersistentTopic.setPulsar(pulsar);
namespaceResources = mock(NamespaceResources.class);
doReturn(false).when(nonPersistentTopic).isRequestHttps();
doReturn(null).when(nonPersistentTopic).originalPrincipal();
doReturn("test").when(nonPersistentTopic).clientAppId();
Expand Down Expand Up @@ -446,6 +449,33 @@ public void testCreatePartitionedTopic() {
});
}

@Test
public void testCreateTopicWithReplicationCluster() {
final String topicName = "test-topic-ownership";
NamespaceName namespaceName = NamespaceName.get(testTenant, testNamespace);
CompletableFuture<Optional<Policies>> policyFuture = new CompletableFuture<>();
Policies policies = new Policies();
policyFuture.complete(Optional.of(policies));
when(pulsar.getPulsarResources().getNamespaceResources()).thenReturn(namespaceResources);
doReturn(policyFuture).when(namespaceResources).getPoliciesAsync(namespaceName);
AsyncResponse response = mock(AsyncResponse.class);
ArgumentCaptor<RestException> errCaptor = ArgumentCaptor.forClass(RestException.class);
persistentTopics.createPartitionedTopic(response, testTenant, testNamespace, topicName, 2, true);
verify(response, timeout(5000).times(1)).resume(errCaptor.capture());
Assert.assertEquals(errCaptor.getValue().getResponse().getStatus(), Response.Status.PRECONDITION_FAILED.getStatusCode());
Assert.assertTrue(errCaptor.getValue().getMessage().contains("Namespace does not have any clusters configured"));
// Test policy not exist and return 'Namespace not found'
CompletableFuture<Optional<Policies>> policyFuture2 = new CompletableFuture<>();
policyFuture2.complete(Optional.empty());
doReturn(policyFuture2).when(namespaceResources).getPoliciesAsync(namespaceName);
response = mock(AsyncResponse.class);
errCaptor = ArgumentCaptor.forClass(RestException.class);
persistentTopics.createPartitionedTopic(response, testTenant, testNamespace, topicName, 2, true);
verify(response, timeout(5000).times(1)).resume(errCaptor.capture());
Assert.assertEquals(errCaptor.getValue().getResponse().getStatus(), Response.Status.NOT_FOUND.getStatusCode());
Assert.assertTrue(errCaptor.getValue().getMessage().contains("Namespace not found"));
}

@Test(expectedExceptions = RestException.class)
public void testCreateNonPartitionedTopicWithInvalidName() {
final String topicName = "standard-topic-partition-10";
Expand Down