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

[pulsar-broker] Fix bug that namespace policies does not take effect due to NPE #5408

Merged
merged 2 commits into from
Oct 23, 2019

Conversation

massakam
Copy link
Contributor

Motivation

When produce/consume permissions for a role on a namespace are revoked, producers and consumers connected to the topic under the namespace using that role should be disconnected from the broker. However, I noticed that producers/consumers can stay connected in some topics even if the permissions are revoked.

As a result of the investigation, I found that NPE was thrown in the following line:

subscription.getDispatcher().initializeDispatchRateLimiterIfNeeded(policies));

In subscriptions that consumer has never connected to since the broker started, the dispatcher has not been initialized, so subscription.getDispatcher() returns null.

As a result, the process of PersistentTopic#onPoliciesUpdate() is aborted.

initializeDispatchRateLimiterIfNeeded(Optional.ofNullable(data));
producers.forEach(producer -> {
producer.checkPermissions();
producer.checkEncryption();
});
subscriptions.forEach((subName, sub) -> {
sub.getConsumers().forEach(Consumer::checkPermissions);
if (sub.getDispatcher().getRateLimiter().isPresent()) {
sub.getDispatcher().getRateLimiter().get().onPoliciesUpdate(data);
}
});
replicators.forEach((name, replicator) ->
replicator.getRateLimiter().get().onPoliciesUpdate(data)
);

Modifications

Before calling a dispatcher method, make sure that the dispatcher is not null.

@massakam massakam added type/bug The PR fixed a bug or issue reported a bug area/broker labels Oct 17, 2019
@massakam massakam added this to the 2.4.2 milestone Oct 17, 2019
@massakam massakam self-assigned this Oct 17, 2019
@massakam
Copy link
Contributor Author

retest this please

Comment on lines 1375 to 1377
if (dispatcher != null && dispatcher.getRateLimiter().isPresent()) {
dispatcher.getRateLimiter().get().updateDispatchRate();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

To make it even more sure, can we do like:

Dispatcher dispatcher = persistentSubscription.getDispatcher();
if (dispatcher != null) {
    dispatcher.getRateLimiter().ifPresent(DispatchRateLimiter::updateDispatchRate);
}

@@ -1673,7 +1676,7 @@ private boolean shouldTopicBeRetained() {
});
subscriptions.forEach((subName, sub) -> {
sub.getConsumers().forEach(Consumer::checkPermissions);
if (sub.getDispatcher().getRateLimiter().isPresent()) {
if (sub.getDispatcher() != null && sub.getDispatcher().getRateLimiter().isPresent()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, protect against dispatcher turning null

subscription.getDispatcher().initializeDispatchRateLimiterIfNeeded(policies));
subscriptions.forEach((name, subscription) -> {
if (subscription.getDispatcher() != null) {
subscription.getDispatcher().initializeDispatchRateLimiterIfNeeded(policies);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, protect against dispatcher turning null in between checks

@massakam
Copy link
Contributor Author

@merlimat Addressed your comments. PTAL

@massakam
Copy link
Contributor Author

retest this please

Copy link
Contributor

@merlimat merlimat left a comment

Choose a reason for hiding this comment

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

👍

@codelipenghui
Copy link
Contributor

run Integration tests

2 similar comments
@massakam
Copy link
Contributor Author

run Integration tests

@massakam
Copy link
Contributor Author

run Integration tests

@nkurihar
Copy link
Contributor

rerun integration tests

2 similar comments
@massakam
Copy link
Contributor Author

rerun integration tests

@massakam
Copy link
Contributor Author

rerun integration tests

@nkurihar nkurihar merged commit 84a519f into apache:master Oct 23, 2019
@massakam massakam deleted the fix-npe branch October 23, 2019 07:54
wolfstudy pushed a commit that referenced this pull request Nov 20, 2019
…due to NPE (#5408)

* Fix bug that namespace policies does not take effect due to NPE

* Prevent NPE if Dispatcher and DispatchRateLimiter return to null

(cherry picked from commit 84a519f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/broker type/bug The PR fixed a bug or issue reported a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants