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 potential NPE when disabling the liveness check #21840

Merged
merged 3 commits into from
Jan 8, 2024

Conversation

mattisonchao
Copy link
Member

@mattisonchao mattisonchao commented Jan 3, 2024

Motivation

Fix potential NPE when disabling the liveness check

Modifications

  • Fix potential NPE when disabling the liveness check

Verifying this change

  • Make sure that the change passes the CI checks.

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Copy link

github-actions bot commented Jan 3, 2024

@mattisonchao Please add the following content to your PR description and select a checkbox:

- [ ] `doc` <!-- Your PR contains doc changes -->
- [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
- [ ] `doc-not-needed` <!-- Your PR changes do not impact docs -->
- [ ] `doc-complete` <!-- Docs have been already added -->

@mattisonchao mattisonchao changed the title [fix][broker] Fix potential NPE when diable liveness check [fix][broker] Fix potential NPE when disabling the liveness check Jan 3, 2024
@mattisonchao mattisonchao self-assigned this Jan 3, 2024
@mattisonchao mattisonchao added this to the 3.3.0 milestone Jan 3, 2024
@mattisonchao mattisonchao added doc-not-needed Your PR changes do not impact docs ready-to-test and removed doc-label-missing labels Jan 3, 2024
@mattisonchao mattisonchao reopened this Jan 3, 2024
@mattisonchao mattisonchao requested review from codelipenghui, lhotari and Technoboy- and removed request for codelipenghui January 3, 2024 08:58
Copy link
Member

@RobertIndie RobertIndie left a comment

Choose a reason for hiding this comment

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

LGTM.

Just want to make sure. The NPE will only be thrown here:

Right?

Copy link
Member

@lhotari lhotari left a comment

Choose a reason for hiding this comment

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

I think that there was a reason to return null when this was implemented. Please postpone merging until I have reviewed this.

@lhotari
Copy link
Member

lhotari commented Jan 3, 2024

@mattisonchao is there a way to have a test case for this?

@Technoboy-
Copy link
Contributor

@mattisonchao is there a way to have a test case for this?

I don't think we need this kind of test, the method should return Boolean, not null.

@dao-jun
Copy link
Member

dao-jun commented Jan 4, 2024

LGTM, but also need to remove the null checker?
https://github.com/apache/pulsar/blob/master/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/AbstractDispatcherSingleActiveConsumer.java#L173

@lhotari
Copy link
Member

lhotari commented Jan 5, 2024

I now had a chance to review the reason why I had originally used null.
It's documented here:

/***
* Check if the connection is still alive
* by actively sending a Ping message to the client.
*
* @return a completable future where the result is true if the connection is alive, false otherwise. The result
* is null if the connection liveness check is disabled.
*/
CompletableFuture<Boolean> checkConnectionLiveness();

I'll suggest that we change the return value from CompletableFuture<Boolean> to CompletableFuture<Optional<Boolean>> if there's a desire to get rid of the special null usage.
@mattisonchao Could you make this change?

@Technoboy-
Copy link
Contributor

I now had a chance to review the reason why I had originally used null. It's documented here:

/***
* Check if the connection is still alive
* by actively sending a Ping message to the client.
*
* @return a completable future where the result is true if the connection is alive, false otherwise. The result
* is null if the connection liveness check is disabled.
*/
CompletableFuture<Boolean> checkConnectionLiveness();

I'll suggest that we change the return value from CompletableFuture<Boolean> to CompletableFuture<Optional<Boolean>> if there's a desire to get rid of the special null usage. @mattisonchao Could you make this change?

ok, then we find a new place need to fix to avoid NPE

private CompletableFuture<Void> tryOverwriteOldProducer(Producer oldProducer, Producer newProducer) {
if (newProducer.isSuccessorTo(oldProducer)) {
oldProducer.close(false);
if (!producers.replace(newProducer.getProducerName(), oldProducer, newProducer)) {
// Met concurrent update, throw exception here so that client can try reconnect later.
return CompletableFuture.failedFuture(new BrokerServiceException.NamingException("Producer with name '"
+ newProducer.getProducerName() + "' replace concurrency error"));
} else {
handleProducerRemoved(oldProducer);
return CompletableFuture.completedFuture(null);
}
} else {
// If a producer with the same name tries to use a new connection, async check the old connection is
// available. The producers related the connection that not available are automatically cleaned up.
if (!Objects.equals(oldProducer.getCnx(), newProducer.getCnx())) {
return oldProducer.getCnx().checkConnectionLiveness().thenCompose(previousIsActive -> {
if (previousIsActive) {
return CompletableFuture.failedFuture(new BrokerServiceException.NamingException(
"Producer with name '" + newProducer.getProducerName()
+ "' is already connected to topic"));
} else {
// If the connection of the previous producer is not active, the method

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (1a4a9d9) 73.66% compared to head (278372e) 73.60%.
Report is 10 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #21840      +/-   ##
============================================
- Coverage     73.66%   73.60%   -0.07%     
+ Complexity    32343    32321      -22     
============================================
  Files          1858     1858              
  Lines        138146   138174      +28     
  Branches      15141    15148       +7     
============================================
- Hits         101763   101699      -64     
- Misses        28544    28606      +62     
- Partials       7839     7869      +30     
Flag Coverage Δ
inttests 24.20% <33.33%> (+0.01%) ⬆️
systests 23.70% <0.00%> (-0.17%) ⬇️
unittests 72.89% <100.00%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...ervice/AbstractDispatcherSingleActiveConsumer.java 90.55% <100.00%> (ø)
...rg/apache/pulsar/broker/service/AbstractTopic.java 87.71% <100.00%> (-0.21%) ⬇️
...va/org/apache/pulsar/broker/service/ServerCnx.java 72.30% <100.00%> (-0.06%) ⬇️

... and 69 files with indirect coverage changes

@Technoboy- Technoboy- merged commit aae0e9d into apache:master Jan 8, 2024
47 checks passed
@mattisonchao
Copy link
Member Author

mattisonchao commented Jan 11, 2024

Hi, @lhotari @Technoboy-

      * @return a completable future where the result is true if the connection is alive, false otherwise. The result 
      * is null if the connection liveness check is disabled. 
  1. I found no particular usage for the value null. I am trying to understand why we should have a third status here.
  2. In my opinion, if we turned off the connection liveness check, that logic should be equal to the connection always being alive.

@lhotari
Copy link
Member

lhotari commented Jan 11, 2024

  1. In my opinion, if we turned off the connection liveness check, that logic should be equal to the connection always being alive.

@mattisonchao I disagree. That would be bad API design. There might be future use cases where this matters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/broker doc-not-needed Your PR changes do not impact docs ready-to-test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants