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

[Enhancement](fe exception) write a java annotation to catch throwable from a method and print log #17797

Conversation

NetShrimp06
Copy link
Contributor

@NetShrimp06 NetShrimp06 commented Mar 14, 2023

Proposed changes

Issue Number: close #17238

Problem summary

  1. How it works?
    Aspectj is used to implement the aspect function of annotations. During the compilation process, the aspectj-maven-plugin plugin will automatically weave the code with aspect annotations into the generated classes file.
  2. When to use to?
    When a method wants to add a try catch to save exception information, the LogException annotation can be used. When there is a method that does not allow errors, the NoException annotation can be used.
  3. What is the result when adding this annotation?
    Use the LogException annotation to automatically capture exceptions into the Log file, and the code can be more concise. Use the NoException annotation to automatically capture the exception to the Log file and exit the program when an exception occurs.

image

Checklist(Required)

  • Does it affect the original behavior
  • Has unit tests been added
  • Has document been added or modified
  • Does it need to update dependencies
  • Is this PR support rollback (If NO, please explain WHY)

Further comments

If this is a relatively large or complex change, kick off the discussion at dev@doris.apache.org by explaining why you chose the solution you did and what alternatives you considered, etc...

@github-actions github-actions bot added the area/load Issues or PRs related to all kinds of load label Mar 14, 2023
@morningman
Copy link
Contributor

Could you please describe:

  1. How it works?
  2. When to use to?
  3. What is the result when adding this annotation?


@AfterThrowing(value = "throwingLogPointCut()", throwing = "e")
public void exceptionLog(Exception e) {
LOG.error(e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

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

Print 2 logs in one line.
And do we need to print stack trace for each exception? Maybe we can use a flag to control it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a parameter to let the user decide whether to print the stack information, the default is not to print

@NetShrimp06
Copy link
Contributor Author

NetShrimp06 commented Mar 15, 2023

  1. How it works?
    Aspectj is used to implement the aspect function of annotations. During the compilation process, the aspectj-maven-plugin plugin will automatically weave the code with aspect annotations into the generated classes file.
  2. When to use to?
    When a method wants to add a try catch to save exception information, the LogException annotation can be used. When there is a method that does not allow errors, the NoException annotation can be used.
  3. What is the result when adding this annotation?
    Use the LogException annotation to automatically capture exceptions into the Log file, and the code can be more concise. Use the NoException annotation to automatically capture the exception to the Log file and exit the program when an exception occurs.

@NetShrimp06 NetShrimp06 changed the title [Enhancement] (fe exception) write a java annotation to catch throwable from a method and print log [Enhancement](fe exception) write a java annotation to catch throwable from a method and print log Mar 15, 2023
@hello-stephen
Copy link
Contributor

hello-stephen commented Mar 16, 2023

TeamCity pipeline, clickbench performance test result:
the sum of best hot time: 34.05 seconds
stream load tsv: 455 seconds loaded 74807831229 Bytes, about 156 MB/s
stream load json: 23 seconds loaded 2358488459 Bytes, about 97 MB/s
stream load orc: 73 seconds loaded 1101869774 Bytes, about 14 MB/s
stream load parquet: 31 seconds loaded 861443392 Bytes, about 26 MB/s
https://doris-community-test-1308700295.cos.ap-hongkong.myqcloud.com/tmp/20230316123041_clickbench_pr_115409.html

ExceptionLog exceptionLog = method.getAnnotation(ExceptionLog.class);
LOG.warn(e.getMessage());
if (exceptionLog != null && exceptionLog.isGetStackTrace()) {
LOG.warn(getStackTrace(e));
Copy link
Contributor

Choose a reason for hiding this comment

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

LOG.warn(e) will print stack trace automatically.

@@ -181,6 +182,7 @@ public Set<String> getTableNamesForShow() {
.collect(Collectors.toSet());
}

@ExceptionLog
Copy link
Contributor

Choose a reason for hiding this comment

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

using @LogException instead.

}

@AfterThrowing(value = "throwingLogPointCut()", throwing = "e")
public void exceptionLog(JoinPoint point, Exception e) {
Copy link
Contributor

@yiguolei yiguolei Mar 16, 2023

Choose a reason for hiding this comment

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

using Throwable intead of Exception, Exception is only subset of errors.

import java.lang.reflect.Method;

@Aspect
public class LogAspect {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add another annotation.
@NoException
If a method throws any exceptions, should print log and call system.exit.

Copy link
Contributor

@yiguolei yiguolei left a comment

Choose a reason for hiding this comment

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

LGTM

@github-actions github-actions bot added the approved Indicates a PR has been approved by one committer. label Mar 16, 2023
@github-actions
Copy link
Contributor

PR approved by at least one committer and no changes requested.

@github-actions
Copy link
Contributor

PR approved by anyone and no changes requested.

@yiguolei
Copy link
Contributor

run buildall

@yiguolei yiguolei merged commit 0ec10d4 into apache:master Mar 17, 2023
@englefly
Copy link
Contributor

it does not work on mac, or newer version of JDK

yiguolei pushed a commit that referenced this pull request Mar 19, 2023
In #17797 , we introduced aspectj to help log exception easily.
However, the plugin version 1.11 do not support jdk9 and later.
For support compile FE with jdk11

update aspectj-maven-plugin to 1.14.0 version
add new dependency org.aspectj.aspectjrt 1.9.7 to fe-core
according to:

aspectj java version compatibility
aspectj-maven-plugin issue
aspectj release note
intro to aspectj
gnehil pushed a commit to gnehil/doris that referenced this pull request Apr 21, 2023
…e from a method and print log (apache#17797)

How it works?
Aspectj is used to implement the aspect function of annotations. During the compilation process, the aspectj-maven-plugin plugin will automatically weave the code with aspect annotations into the generated classes file.
When to use to?
When a method wants to add a try catch to save exception information, the LogException annotation can be used. When there is a method that does not allow errors, the NoException annotation can be used.
What is the result when adding this annotation?
Use the LogException annotation to automatically capture exceptions into the Log file, and the code can be more concise. Use the NoException annotation to automatically capture the exception to the Log file and exit the program when an exception occurs.
gnehil pushed a commit to gnehil/doris that referenced this pull request Apr 21, 2023
In apache#17797 , we introduced aspectj to help log exception easily.
However, the plugin version 1.11 do not support jdk9 and later.
For support compile FE with jdk11

update aspectj-maven-plugin to 1.14.0 version
add new dependency org.aspectj.aspectjrt 1.9.7 to fe-core
according to:

aspectj java version compatibility
aspectj-maven-plugin issue
aspectj release note
intro to aspectj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by one committer. area/load Issues or PRs related to all kinds of load dev/1.2.3 reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement] (fe exception) write a java annotation to catch throwable from a method and print log
5 participants