Skip to content

[SPARK-25986][Build] Add rules to ban throw Errors in application code #22989

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

Closed
wants to merge 5 commits into from

Conversation

xuanyuanking
Copy link
Member

@xuanyuanking xuanyuanking commented Nov 9, 2018

What changes were proposed in this pull request?

Add scala and java lint check rules to ban the usage of throw new xxxErrors and fix up all exists instance followed by #22989 (comment). See more details in #22969.

How was this patch tested?

Local test with lint-scala and lint-java.

@SparkQA
Copy link

SparkQA commented Nov 9, 2018

Test build #98637 has finished for PR 22989 at commit d678751.

  • This patch fails due to an unknown error code, -9.
  • This patch merges cleanly.
  • This patch adds no public classes.

@xuanyuanking
Copy link
Member Author

retest this please.

@SparkQA
Copy link

SparkQA commented Nov 9, 2018

Test build #98649 has finished for PR 22989 at commit d678751.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

Copy link
Member

@srowen srowen left a comment

Choose a reason for hiding this comment

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

Where is it thrown directly though?

@srowen
Copy link
Member

srowen commented Nov 9, 2018

Honestly it is bad style to throw any Error class, except in sepcial situations. I'd prefer a rule banning all of these and fix up any instances in the code or else exclude them

@felixcheung
Copy link
Member

and catching Error or Throwable..

@@ -64,6 +64,11 @@
<property name="message" value="No trailing whitespace allowed."/>
</module>

<module name="RegexpSingleline">
<property name="format" value="throw new OutOfMemoryError"/>
Copy link
Member

Choose a reason for hiding this comment

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

Per my suggestion below, how about the regex throw new \\w+Error\\(

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, done in 6e49bb8.

@xuanyuanking
Copy link
Member Author

Sorry for late reply, great thanks for all reviewer's advise, will address them soon.

@xuanyuanking
Copy link
Member Author

xuanyuanking commented Nov 12, 2018

cc all reviewer, as @srowen's suggestion, add a rule to ban all of new Error cases.
List currently throw new XXXError in Spark source below and record fix up or exclude for review conveniently:

ErrorName Count In Test/Source Code Fix Up or Exclude
AssertionError 32 29 in test code Fix up by replacing to fail()
2 in UnsafeAlignedOffset legitimate, exclude them
1 in KafkaUtils Fix up by changing to IllegalArgumentException
NotImplementedError 22 7 in source code/ 15 in test cases Fix up by changing them to UnsupportedOperationException
OutOfMemoryError 1 1 in test code Exclude
LinkageError 1 1 in test code Exclude
SparkOutOfMemoryError 5 5 in source code Exclude
UnknownError 9 5 in source code/ 4 in test cases Fix up by changing to IllegalAccessException and IllegalArgumentException

All above done in 6e49bb8.
Addressed Sean's suggestion, edit the above table and done in ff234d3.

@SparkQA
Copy link

SparkQA commented Nov 12, 2018

Test build #98731 has finished for PR 22989 at commit 6e49bb8.

  • This patch fails to build.
  • This patch merges cleanly.
  • This patch adds no public classes.

@srowen
Copy link
Member

srowen commented Nov 12, 2018

AssertionError in tests is wrong. It should really just call fail(). If you're willing to fix that, it would be a great cleanup.

The AssertionError in UnsafeAlignedOffset is legitimate and can be excluded.

I would fix the AssertionError in KafkaUtils. That one is clearly an illegal argument check.

Agree about the rest. Thanks for the great analysis @xuanyuanking

@xuanyuanking xuanyuanking changed the title [SPARK-25986][Build] Banning throw new OutOfMemoryErrors [SPARK-25986][Build] Add rules to ban throw Errors in application code Nov 13, 2018
@xuanyuanking
Copy link
Member Author

@srowen Great thanks for your guidance, address all your suggestion in ff234d3 and update the record table in #22989 (comment).

Copy link
Member

@srowen srowen left a comment

Choose a reason for hiding this comment

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

Looks quite good to me, just a few comments.

@@ -46,4 +46,12 @@
files="sql/catalyst/src/main/java/org/apache/spark/sql/streaming/GroupStateTimeout.java"/>
<suppress checks="MethodName"
files="sql/core/src/main/java/org/apache/spark/sql/streaming/Trigger.java"/>
<suppress checks="RegexpSingleline"
Copy link
Member

Choose a reason for hiding this comment

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

I think this might suppress many checks? is it difficult to suppress with comments in these files?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, I met some problem during suppress with comments, just work around by suppress files, I will find the problem again.

Copy link
Member Author

Choose a reason for hiding this comment

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

I found the problem is RegexpSingleline can't put into TreeWalker, so SuppressionCommentFilter rule can't effect on it, RegexpSinglelineJava is the right choice. Also spend some time on comment syntax, maybe the usage description can be improved a little, please have a check whether I understand this rule correctly. Done in a4f49ce.

@SparkQA
Copy link

SparkQA commented Nov 13, 2018

Test build #98749 has finished for PR 22989 at commit ff234d3.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Nov 13, 2018

Test build #98770 has finished for PR 22989 at commit a4f49ce.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -180,5 +180,10 @@
<module name="UnusedImports"/>
<module name="RedundantImport"/>
<module name="RedundantModifier"/>
<module name="RegexpSinglelineJava">
<property name="format" value="throw new \w+Error\("/>
<property name="message" value="Avoid throwing error in application code."/>
Copy link
Member

Choose a reason for hiding this comment

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

What application code means here?

Copy link
Member Author

Choose a reason for hiding this comment

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

application code against JVM here, as Error subclasses generally represent internal JVM errors.

@SparkQA
Copy link

SparkQA commented Nov 14, 2018

Test build #98826 has finished for PR 22989 at commit 210d942.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@srowen
Copy link
Member

srowen commented Nov 14, 2018

Merged to master

@asfgit asfgit closed this in 2977e23 Nov 14, 2018
@xuanyuanking
Copy link
Member Author

Thanks @HyukjinKwon @viirya @felixcheung @srowen for your review and advise!

@xuanyuanking xuanyuanking deleted the SPARK-25986 branch November 15, 2018 01:56
@@ -39,7 +39,9 @@ public static int getSize(Object object, long offset) {
case 8:
return (int)Platform.getLong(object, offset);
default:
// checkstyle.off: RegexpSinglelineJava
throw new AssertionError("Illegal UAO_SIZE");
Copy link
Contributor

Choose a reason for hiding this comment

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

shall we throw IllegalStateException here?

Copy link
Member

Choose a reason for hiding this comment

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

I think these are ok as AssertionError because they shouldn't be able to happen in any JVM state

Copy link
Contributor

Choose a reason for hiding this comment

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

yea, that's exactly the use case of IllegalStateException, which can also pass the style check here.

Copy link
Member

Choose a reason for hiding this comment

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

It would be OK too. This is just picking nits now but I thought AssertionError was still better. ISE is about application state. Errors are about JVM state.

@@ -52,7 +54,9 @@ public static void putSize(Object object, long offset, int value) {
Platform.putLong(object, offset, value);
break;
default:
// checkstyle.off: RegexpSinglelineJava
throw new AssertionError("Illegal UAO_SIZE");
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

jackylee-ch pushed a commit to jackylee-ch/spark that referenced this pull request Feb 18, 2019
## What changes were proposed in this pull request?

Add scala and java lint check rules to ban the usage of `throw new xxxErrors` and fix up all exists instance followed by apache#22989 (comment). See more details in apache#22969.

## How was this patch tested?

Local test with lint-scala and lint-java.

Closes apache#22989 from xuanyuanking/SPARK-25986.

Authored-by: Yuanjian Li <[email protected]>
Signed-off-by: Sean Owen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants