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

NIFI-7825: Support native library loading via absolute path #4540

Closed
wants to merge 2 commits into from

Conversation

turcsanyip
Copy link
Contributor

@turcsanyip turcsanyip commented Sep 21, 2020

Use an AspectJ aspect and agent to intercept the load native library calls
at runtime, copy the native library file to temp folder and proceed with
the newly created file in order to provide classloader isolation.

https://issues.apache.org/jira/browse/NIFI-7825

Thank you for submitting a contribution to Apache NiFi.

Please provide a short description of the PR here:

Description of PR

Enables X functionality; fixes bug NIFI-YYYY.

In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:

For all changes:

  • Is there a JIRA ticket associated with this PR? Is it referenced
    in the commit message?

  • Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.

  • Has your PR been rebased against the latest commit within the target branch (typically main)?

  • Is your initial contribution a single, squashed commit? Additional commits in response to PR reviewer feedback should be made on this branch and pushed to allow change tracking. Do not squash or use --force when pushing to allow for clean monitoring of changes.

For code changes:

  • Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
  • Have you written or updated unit tests to verify your changes?
  • Have you verified that the full build is successful on JDK 8?
  • Have you verified that the full build is successful on JDK 11?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
  • If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
  • If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

Note:

Please ensure that once the PR is submitted, you check GitHub Actions CI for build issues and submit an update to your PR as soon as possible.

# It is needed when a custom jar (eg. JDBC driver) has been configured on a component in the flow and this custom jar depends on a native library
# and tries to load it by its absolute path (java.lang.System.load(String filename) method call).
# Use this Java Agent only if you get "Native Library ... already loaded in another classloader" errors otherwise!
#java.arg.18=-javaagent:./lib/aspectjweaver-${aspectj.version}.jar
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have any concerns about aspectjweaver being directly in the lib directory which puts in on the classpath of every single NAR? Wondering if it should be separated like lib/aspectj/ which would not be added to NiFi's normal classpath, but I don't know enough to say if it still works correctly then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The agent does not require aspectjweaver.jar on the classpath so lib/aspectj directory could work.
If there are concerns about having this jar on the system classpath, I can try out that solution too.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think if we can do the lib/aspectj approach then that is the best option, anything we can do to avoid introducing additional dependencies to the system classpath is good.

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 double checked it and unfortunately the aspect class in nifi-nar-utils.jar depends on aspectjrt.jar.
As nifi-nar-utils.jar is located in the lib folder and loaded by the system classloader, aspectjrt.jar must be on the system classpath too.
And I did not want to separate the 2 AspectJ jars so I put aspectjweaver.jar to the lib folder.

Options:

  1. leave it as it is now (both aspectjrt.jar and aspectjweaver.jar in the lib folder)
  2. leave aspectjrt.jar in lib and move aspectjweaver.jar to lib/aspectj
  3. find another place for the aspect class and move its aspectjrt.jar dependency there, move aspectjweaver.jar to lib/aspectj

Option 2 seems to me a bit weird structure and one of the AspectJ jars is still on the system classpath.
Option 3 could work but I would keep the native library loading stuff (AbstractNativeLibHandlingClassLoader and LoadNativeLibAspect) in the same module.

So I would still vote for option 1.
These jars have no effect if they are not referenced in our code / the agent is not turned on.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well it does have an effect... If the jars are in lib then they are on the classpath of every NAR, so if someone makes a custom NAR that bundles a different version of aspectj, then there will be two versions on the classpath, and there is nothing the NAR can do to avoid the one in lib.

I would actually vote for option 3... create a new module like nifi-nar-aspect-utils or whatever we want to call it, and put the dependency there along with LoadNativeLibAspect, then lib/aspectj can contain nifi-nar-aspect-utils jar and the aspectj jars. Would that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It has been revealed that aspectjrt does not needed at runtime, only aspectjweaver (which is loaded directly by the agent, not from classpath).
So I removed aspectjrt.ajr from lib and moved aspectjweaver.jar to lib/aspectj subdirectory.
In this way, there are no AspectJ libraries on the system classpath by default (when the agent is turned off).

When the agent is turned on, it is being loaded by the system classloader (even if aspectjweaver.jar is not on the system classpath). So there might be collision between different versions in this case. I believe it is acceptable because only the "agent turned on" case affected.

Use an AspectJ aspect and agent to intercept the load native library calls
at runtime, copy the native library file to temp folder and proceed with
the newly created file in order to provide classloader isolation.
@bbende
Copy link
Contributor

bbende commented Oct 26, 2020

Latest changes look good, thanks! Will merge

@asfgit asfgit closed this in 3dd024f Oct 26, 2020
rlamothe pushed a commit to rlamothe/nifi that referenced this pull request Nov 2, 2020
Use an AspectJ aspect and agent to intercept the load native library calls
at runtime, copy the native library file to temp folder and proceed with
the newly created file in order to provide classloader isolation.

Remove AspectJ jars from lib directory, move the necessary jar to lib/aspectj subdirectory.

This closes apache#4540.

Signed-off-by: Bryan Bende <bbende@apache.org>
driesva pushed a commit to driesva/nifi that referenced this pull request Mar 19, 2021
Use an AspectJ aspect and agent to intercept the load native library calls
at runtime, copy the native library file to temp folder and proceed with
the newly created file in order to provide classloader isolation.

Remove AspectJ jars from lib directory, move the necessary jar to lib/aspectj subdirectory.

This closes apache#4540.

Signed-off-by: Bryan Bende <bbende@apache.org>
adenes pushed a commit to adenes/nifi that referenced this pull request Jul 5, 2021
Use an AspectJ aspect and agent to intercept the load native library calls
at runtime, copy the native library file to temp folder and proceed with
the newly created file in order to provide classloader isolation.

Remove AspectJ jars from lib directory, move the necessary jar to lib/aspectj subdirectory.

This closes apache#4540.

Signed-off-by: Bryan Bende <bbende@apache.org>
(cherry picked from commit 3dd024f)
krisztina-zsihovszki pushed a commit to krisztina-zsihovszki/nifi that referenced this pull request Jun 28, 2022
Use an AspectJ aspect and agent to intercept the load native library calls
at runtime, copy the native library file to temp folder and proceed with
the newly created file in order to provide classloader isolation.

Remove AspectJ jars from lib directory, move the necessary jar to lib/aspectj subdirectory.

This closes apache#4540.

Signed-off-by: Bryan Bende <bbende@apache.org>
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.

2 participants