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

Conversation

Technoboy-
Copy link
Contributor

@Technoboy- Technoboy- commented Feb 14, 2022

Motivation

When Rest API call AdminResource#validateGlobalNamespaceOwnership, broker will execute PulsarWebResource#checkLocalOrGetPeerReplicationCluster.
In PulsarWebResource#checkLocalOrGetPeerReplicationCluster:

if (policiesResult.isPresent()) {
Policies policies = policiesResult.get();
if (policies.replication_clusters.isEmpty()) {
String msg = String.format(
"Namespace does not have any clusters configured : local_cluster=%s ns=%s",
localCluster, namespace.toString());
log.warn(msg);
validationFuture.completeExceptionally(new RestException(Status.PRECONDITION_FAILED, msg));
} else if (!policies.replication_clusters.contains(localCluster)) {
ClusterDataImpl ownerPeerCluster = getOwnerFromPeerClusterList(pulsarService,
policies.replication_clusters);
if (ownerPeerCluster != null) {
// found a peer that own this namespace
validationFuture.complete(ownerPeerCluster);
return;
}
String msg = String.format(
"Namespace missing local cluster name in clusters list: local_cluster=%s ns=%s clusters=%s",
localCluster, namespace.toString(), policies.replication_clusters);
log.warn(msg);
validationFuture.completeExceptionally(new RestException(Status.PRECONDITION_FAILED, msg));
} else {
validationFuture.complete(null);
}
} else {
String msg = String.format("Policies not found for %s namespace", namespace.toString());
log.warn(msg);
validationFuture.completeExceptionally(new RestException(Status.NOT_FOUND, msg));
}

Line 780, 794, and 801 has thrown RestException.
But validateGlobalNamespaceOwnership has wrapped the exception :

protected void validateGlobalNamespaceOwnership() {
try {
validateGlobalNamespaceOwnership(this.namespaceName);
} 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");
} 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");
}
}

This could make the user confused that the log printed is not matched with the REST API.

Modification

  • Add a relative test.

Documentation

  • no-need-doc

  • doc-not-needed

@Technoboy- Technoboy- self-assigned this Feb 14, 2022
@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Feb 14, 2022
@codelipenghui codelipenghui added this to the 2.11.0 milestone Feb 14, 2022
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

Copy link
Member

@nodece nodece left a comment

Choose a reason for hiding this comment

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

LGTM

@codelipenghui codelipenghui merged commit 18d9f1b into apache:master Feb 15, 2022
codelipenghui pushed a commit that referenced this pull request Feb 16, 2022
### Motivation
When Rest API call `AdminResource#validateGlobalNamespaceOwnership`, broker will execute `PulsarWebResource#checkLocalOrGetPeerReplicationCluster`.
In `PulsarWebResource#checkLocalOrGetPeerReplicationCluster`:
https://github.com/apache/pulsar/blob/6d717a08ef8cfcac032caee06105285594baf09f/pulsar-broker/src/main/java/org/apache/pulsar/broker/web/PulsarWebResource.java#L773-L802

Line 780, 794, and 801 has thrown RestException.
But `validateGlobalNamespaceOwnership ` has wrapped the exception :
https://github.com/apache/pulsar/blob/6d717a08ef8cfcac032caee06105285594baf09f/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java#L202-L216

This could make the user confused that the log printed is not matched with the REST API.

(cherry picked from commit 18d9f1b)
@codelipenghui codelipenghui added cherry-picked/branch-2.8 Archived: 2.8 is end of life cherry-picked/branch-2.9 Archived: 2.9 is end of life and removed cherry-picked/branch-2.8 Archived: 2.8 is end of life labels Feb 16, 2022
@codelipenghui codelipenghui modified the milestones: 2.11.0, 2.10.0 Feb 17, 2022
@michaeljmarshall
Copy link
Member

I am unable to cherry-pick this to 2.8.3 because of test errors in the mocking. I am moving it to 2.8.4 for now. After cherry-picking and resolving conflicts, I run mvn -pl :pulsar-broker test -Dtest="PersistentTopicsTest#testCreateTopicWithReplicationCluster" and get the following test error:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.apache.pulsar.broker.admin.PersistentTopicsTest
[ERROR] Tests run: 4, Failures: 1, Errors: 0, Skipped: 3, Time elapsed: 13.607 s <<< FAILURE! - in org.apache.pulsar.broker.admin.PersistentTopicsTest
[ERROR] testCreateTopicWithReplicationCluster(org.apache.pulsar.broker.admin.PersistentTopicsTest)  Time elapsed: 0.007 s  <<< FAILURE!
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:

NamespaceResources$MockitoMock$634217304 cannot be returned by getPulsarResources()
getPulsarResources() should return PulsarResources
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

	at org.apache.pulsar.broker.admin.PersistentTopicsTest.testCreateTopicWithReplicationCluster(PersistentTopicsTest.java:389)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
	at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:45)
	at org.testng.internal.InvokeMethodRunnable.call(InvokeMethodRunnable.java:73)
	at org.testng.internal.InvokeMethodRunnable.call(InvokeMethodRunnable.java:11)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
	at java.base/java.lang.Thread.run(Thread.java:832)

[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] org.apache.pulsar.broker.admin.PersistentTopicsTest.testCreateTopicWithReplicationCluster(org.apache.pulsar.broker.admin.PersistentTopicsTest)
[INFO]   Run 1: PASS
[ERROR]   Run 2: PersistentTopicsTest.testCreateTopicWithReplicationCluster:389 WrongTypeOfReturnValue
[INFO]
[INFO]
[ERROR] Tests run: 3, Failures: 1, Errors: 0, Skipped: 2

@codelipenghui codelipenghui added the cherry-picked/branch-2.8 Archived: 2.8 is end of life label Mar 9, 2022
Nicklee007 pushed a commit to Nicklee007/pulsar that referenced this pull request Apr 20, 2022
)

### Motivation
When Rest API call `AdminResource#validateGlobalNamespaceOwnership`, broker will execute `PulsarWebResource#checkLocalOrGetPeerReplicationCluster`.
In `PulsarWebResource#checkLocalOrGetPeerReplicationCluster`:
https://github.com/apache/pulsar/blob/6d717a08ef8cfcac032caee06105285594baf09f/pulsar-broker/src/main/java/org/apache/pulsar/broker/web/PulsarWebResource.java#L773-L802

Line 780, 794, and 801 has thrown RestException.
But `validateGlobalNamespaceOwnership ` has wrapped the exception :
https://github.com/apache/pulsar/blob/6d717a08ef8cfcac032caee06105285594baf09f/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java#L202-L216

This could make the user confused that the log printed is not matched with the REST API.
@Jason918 Jason918 added release/2.7.5 cherry-picked/branch-2.7 Archived: 2.7 is end of life and removed release/2.7.6 labels Jul 29, 2022
@Technoboy- Technoboy- deleted the fix-replication-cluster-exception-issue branch August 10, 2022 05:53
@BewareMyPower BewareMyPower removed cherry-picked/branch-2.8 Archived: 2.8 is end of life release/2.8.4 labels Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-picked/branch-2.7 Archived: 2.7 is end of life cherry-picked/branch-2.9 Archived: 2.9 is end of life doc-not-needed Your PR changes do not impact docs release/2.7.5 release/2.9.2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants