Skip to content

MINOR: Catch null pointer exception for empty leader URL when assignment is null#4798

Merged
guozhangwang merged 1 commit intoapache:trunkfrom
asdf2014:null_leader_url
Nov 16, 2018
Merged

MINOR: Catch null pointer exception for empty leader URL when assignment is null#4798
guozhangwang merged 1 commit intoapache:trunkfrom
asdf2014:null_leader_url

Conversation

@asdf2014
Copy link
Copy Markdown
Member

Catch null pointer exception for empty leader URL when assignment is null.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

@asdf2014 asdf2014 changed the title MINRO: Catch null pointer exception for empty leader URL when assignment is null MINOR: Catch null pointer exception for empty leader URL when assignment is null Mar 30, 2018
@asdf2014
Copy link
Copy Markdown
Member Author

asdf2014 commented Apr 4, 2018

Hi, @guozhangwang . PTAL.

@guozhangwang
Copy link
Copy Markdown
Contributor

cc @rhauch @kkonstantine @ewencp

Copy link
Copy Markdown
Contributor

@kkonstantine kkonstantine left a comment

Choose a reason for hiding this comment

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

Thanks @asdf2014 for spotting this. I left a comment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd rather not use exception handling for branching. I think I'd prefer to address this case with an earlier check for leaderUrl() == null and instantiate a more informative exception to pass to cb.onCompletion

@asdf2014
Copy link
Copy Markdown
Member Author

asdf2014 commented Apr 8, 2018

Hi, @kkonstantine @guozhangwang . Already using more informative exception instead of NullPointerException. PTAL.

Copy link
Copy Markdown
Contributor

@rhauch rhauch left a comment

Choose a reason for hiding this comment

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

One minor suggestion to make it even more readable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe leaderUrl.trim().isEmpty() instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Indeed, isEmpty() is better.

Copy link
Copy Markdown
Contributor

@kkonstantine kkonstantine left a comment

Choose a reason for hiding this comment

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

I left a few minor comments to improve the exception message and the readability of the code.

Finally, an observation I have is that we now throw an exception which we know it should (and will) be caught right below. Another way to write this, would be to pass the exception to cb.onCompletion immediately. E.g.

  Exception e = new ConnectException(...);
  cb.onCompletion(e, null);

Since this is not expected to have an impact on the efficiency of the code here, this is more of a stylistic comment than anything else. Up to you @asdf2014

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: , (comma) doesn't seem required in the sentence.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

All right. I remove it :-)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: because the URL of the leader's REST interface is empty
(otherwise it should have been due to the URL of the leader's REST interface being empty)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep, you are right. It is better.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Although the checkstyle rules currently do not enforce curly brackets in if/else blocks that contain a single statement, because this statement here spans multiple lines, I feel it'd be best to enclose it within { }, even if that's optional.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, i will follow the code style.

@asdf2014
Copy link
Copy Markdown
Member Author

asdf2014 commented Apr 9, 2018

Thanks for your suggestions. Already fixed. @kkonstantine

…signment is null

* Using more informative exception instead of `NullPointerException`

* Using isEmpty instead of `lenght == 0`

* Add brackets for if clause and improve the connect exception message
@asdf2014
Copy link
Copy Markdown
Member Author

Hi, @rhauch @kkonstantine . This PR has been squashed and rebased. PTAL.

Copy link
Copy Markdown
Contributor

@rhauch rhauch left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks, @asdf2014!

@guozhangwang guozhangwang merged commit b21e933 into apache:trunk Nov 16, 2018
@asdf2014 asdf2014 deleted the null_leader_url branch November 17, 2018 02:38
pengxiaolong pushed a commit to pengxiaolong/kafka that referenced this pull request Jun 14, 2019
…signment is null (apache#4798)

Catch null pointer exception for empty leader URL when assignment is null.

Reviewers: Randall Hauch <rhauch@gmail.com>, Konstantine Karantasis <konstantine@confluent.io>, Guozhang Wang <wangguoz@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants