mirror of https://github.com/apache/nifi.git
NIFI-7825: Support native library loading via absolute path
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 #4540. Signed-off-by: Bryan Bende <bbende@apache.org>
This commit is contained in:
parent
9a11d23c83
commit
3dd024fb66
|
@ -2050,8 +2050,8 @@ Eclipse Public License 1.0
|
|||
The following binary components are provided under the Eclipse Public License 1.0. See project link for details.
|
||||
|
||||
(EPL 1.0) Model-Driven Health Tools ( org.openehealth.ipf.oht.mdht ) https://projects.eclipse.org/proposals/model-driven-health-tools
|
||||
(EPL 1.0) AspectJ Weaver (org.aspectj:aspectjweaver:jar:1.8.14 - http://www.eclipse.org/aspectj/)
|
||||
(EPL 1.0) AspectJ Runtime (org.aspectj:aspectjrt:jar:1.8.0 - http://www.eclipse.org/aspectj/)
|
||||
(EPL 1.0) AspectJ Weaver (org.aspectj:aspectjweaver:jar:1.9.6 - http://www.eclipse.org/aspectj/)
|
||||
(EPL 1.0) AspectJ Runtime (org.aspectj:aspectjrt:jar:1.9.6 - http://www.eclipse.org/aspectj/)
|
||||
(EPL 1.0)(MPL 2.0) H2 Database (com.h2database:h2:jar:1.3.176 - http://www.h2database.com/html/license.html)
|
||||
(EPL 1.0)(LGPL 2.1) Logback Classic (ch.qos.logback:logback-classic:jar:1.2.3 - http://logback.qos.ch/)
|
||||
(EPL 1.0)(LGPL 2.1) Logback Core (ch.qos.logback:logback-core:jar:1.2.3 - http://logback.qos.ch/)
|
||||
|
|
|
@ -783,6 +783,11 @@ language governing permissions and limitations under the License. -->
|
|||
<artifactId>javax.activation-api</artifactId>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
<!-- AspectJ library needed by the Java Agent used for native library loading (see bootstrap.conf) -->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<profiles>
|
||||
<profile>
|
||||
|
@ -1221,6 +1226,8 @@ language governing permissions and limitations under the License. -->
|
|||
<exclude>com.sun.xml.bind:jaxb-core</exclude>
|
||||
<exclude>javax.activation:javax.activation-api</exclude>
|
||||
<exclude>javax.annotation:javax.annotation-api</exclude>
|
||||
<!-- exclude AspectJ library from lib, it'll be included in the aspectj subdir -->
|
||||
<exclude>org.aspectj:aspectjweaver</exclude>
|
||||
</excludes>
|
||||
</dependency>
|
||||
</mapping>
|
||||
|
@ -1283,6 +1290,16 @@ language governing permissions and limitations under the License. -->
|
|||
</includes>
|
||||
</dependency>
|
||||
</mapping>
|
||||
<mapping>
|
||||
<!-- Write out the AspectJ library to its own dir -->
|
||||
<!-- The AspectJ library needed by the Java Agent used for native library loading. It does not need to be on the classpath (see bootstrap.conf) -->
|
||||
<directory>/opt/nifi/nifi-${project.version}/lib/aspectj</directory>
|
||||
<dependency>
|
||||
<includes>
|
||||
<include>org.aspectj:aspectjweaver</include>
|
||||
</includes>
|
||||
</dependency>
|
||||
</mapping>
|
||||
<mapping>
|
||||
<directory>/opt/nifi/nifi-${project.version}/docs</directory>
|
||||
<sources>
|
||||
|
|
|
@ -49,6 +49,20 @@
|
|||
</includes>
|
||||
</dependencySet>
|
||||
|
||||
<!-- Write out the AspectJ library to its own dir -->
|
||||
<!-- The AspectJ library needed by the Java Agent used for native library loading. It does not need to be on the classpath (see bootstrap.conf) -->
|
||||
<dependencySet>
|
||||
<scope>runtime</scope>
|
||||
<useProjectArtifact>false</useProjectArtifact>
|
||||
<outputDirectory>lib/aspectj</outputDirectory>
|
||||
<directoryMode>0770</directoryMode>
|
||||
<fileMode>0664</fileMode>
|
||||
<useTransitiveFiltering>true</useTransitiveFiltering>
|
||||
<includes>
|
||||
<include>org.aspectj:aspectjweaver</include>
|
||||
</includes>
|
||||
</dependencySet>
|
||||
|
||||
<!-- Write out the conf directory contents -->
|
||||
<dependencySet>
|
||||
<scope>runtime</scope>
|
||||
|
|
|
@ -43,6 +43,9 @@
|
|||
<exclude>com.sun.xml.bind:jaxb-core</exclude>
|
||||
<exclude>javax.activation:javax.activation-api</exclude>
|
||||
<exclude>javax.annotation:javax.annotation-api</exclude>
|
||||
|
||||
<!-- exclude AspectJ library from lib, it'll be included in the aspectj subdir -->
|
||||
<exclude>org.aspectj:aspectjweaver</exclude>
|
||||
</excludes>
|
||||
</dependencySet>
|
||||
</dependencySets>
|
||||
|
|
|
@ -219,7 +219,7 @@ Eclipse Public License 1.0
|
|||
|
||||
The following binary components are provided under the Eclipse Public License 1.0. See project link for details.
|
||||
|
||||
(EPL 1.0) AspectJ Weaver (org.aspectj:aspectjweaver:jar:1.8.14 - http://www.aspectj.org)
|
||||
(EPL 1.0) AspectJ Weaver (org.aspectj:aspectjweaver:jar:1.9.6 - http://www.aspectj.org)
|
||||
(EPL 1.0)(MPL 2.0) H2 Database (com.h2database:h2:jar:1.3.176 - http://www.h2database.com/html/license.html)
|
||||
(EPL 1.0)(LGPL 2.1) Logback Classic (ch.qos.logback:logback-classic:jar:1.2.3 - http://logback.qos.ch/)
|
||||
(EPL 1.0)(LGPL 2.1) Logback Core (ch.qos.logback:logback-core:jar:1.2.3 - http://logback.qos.ch/)
|
||||
|
|
|
@ -34,10 +34,17 @@
|
|||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-framework-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Compile-only dependency. At runtime, the agent will use aspectjweaver.jar directly, not from the classpath (see bootstrap.conf).-->
|
||||
<dependency>
|
||||
<groupId>org.apache.nifi</groupId>
|
||||
<artifactId>nifi-server-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
@ -53,6 +60,43 @@
|
|||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<!-- latest (1.11) version of org.codehaus.mojo:aspectj-maven-plugin does not support Java 11, using com.nickwongdev fork instead -->
|
||||
<groupId>com.nickwongdev</groupId>
|
||||
<artifactId>aspectj-maven-plugin</artifactId>
|
||||
<version>1.12.6</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjtools</artifactId>
|
||||
<version>${aspectj.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<complianceLevel>${maven.compiler.source}</complianceLevel>
|
||||
<encoding>UTF-8</encoding>
|
||||
<verbose>true</verbose>
|
||||
<showWeaveInfo>true</showWeaveInfo>
|
||||
<Xlint>ignore</Xlint>
|
||||
<sources>
|
||||
<source>
|
||||
<basedir>${project.basedir}/src/main/java</basedir>
|
||||
<includes>
|
||||
<include>**/*Aspect.java</include>
|
||||
</includes>
|
||||
</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
|
@ -44,6 +44,10 @@ import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
|||
*
|
||||
* Once a library is found an OS-handled temporary copy is created and cached
|
||||
* to maintain consistency and classloader isolation.
|
||||
*
|
||||
* This classloader handles the native library loading when the library is being loaded
|
||||
* by its logical name ({@link System#loadLibrary(String)} / {@link Runtime#loadLibrary(String)} calls).
|
||||
* For loading a native library by its absolute path, see {@link LoadNativeLibAspect}.
|
||||
*/
|
||||
public abstract class AbstractNativeLibHandlingClassLoader extends URLClassLoader implements OSUtil {
|
||||
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.nifi.nar;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||
|
||||
/**
|
||||
* AspectJ aspect to handle native library loading by absolute path in multiple classloaders.
|
||||
*
|
||||
* The aspect intercepts the {@link System#load(String)} / {@link Runtime#load(String)} calls and creates a copy of the native library
|
||||
* in the system temp folder with a unique name, then passes this new path to the original load() method.
|
||||
* In this way, different classloaders will load different native libraries and the "Native Library ... already loaded in another classloader"
|
||||
* error can be avoided.
|
||||
*
|
||||
* To put it into effect, the AspectJ agent needs to be configured in bootstrap.conf (see the necessary config there, commented out by default).
|
||||
*
|
||||
* This aspect handles the native library loading when the library is being loaded by its absolute path.
|
||||
* For loading a native library by its logical name, see {@link AbstractNativeLibHandlingClassLoader}.
|
||||
*/
|
||||
@Aspect
|
||||
public class LoadNativeLibAspect {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Around("call(void java.lang.System.load(String)) || call(void java.lang.Runtime.load(String))")
|
||||
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
String origLibPathStr = (String) joinPoint.getArgs()[0];
|
||||
|
||||
if (origLibPathStr == null || origLibPathStr.isEmpty()) {
|
||||
logger.info("Native library path specified as null or empty string, proceeding normally");
|
||||
joinPoint.proceed();
|
||||
return;
|
||||
}
|
||||
|
||||
Path origLibPath = Paths.get(origLibPathStr);
|
||||
|
||||
if (!Files.exists(origLibPath)) {
|
||||
logger.info("Native library does not exist, proceeding normally");
|
||||
joinPoint.proceed();
|
||||
return;
|
||||
}
|
||||
|
||||
String libFileName = origLibPath.getFileName().toString();
|
||||
|
||||
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
String prefix = contextClassLoader.getClass().getName() + "@" + contextClassLoader.hashCode() + "_";
|
||||
String suffix = "_" + libFileName;
|
||||
|
||||
Path tempLibPath = Files.createTempFile(prefix, suffix);
|
||||
Files.copy(origLibPath, tempLibPath, REPLACE_EXISTING);
|
||||
|
||||
logger.info("Loading native library via absolute path (original lib: {}, copied lib: {}", origLibPath, tempLibPath);
|
||||
joinPoint.proceed(new Object[]{tempLibPath.toString()});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0"?>
|
||||
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
|
||||
license agreements. See the NOTICE file distributed with this work for additional
|
||||
information regarding copyright ownership. The ASF licenses this file to
|
||||
You under the Apache License, Version 2.0 (the "License"); you may not use
|
||||
this file except in compliance with the License. You may obtain a copy of
|
||||
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
|
||||
by applicable law or agreed to in writing, software distributed under the
|
||||
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
|
||||
OF ANY KIND, either express or implied. See the License for the specific
|
||||
language governing permissions and limitations under the License. -->
|
||||
<aspectj>
|
||||
<aspects>
|
||||
<aspect name="org.apache.nifi.nar.LoadNativeLibAspect"/>
|
||||
<weaver options="-verbose -showWeaveInfo -Xlint:ignore"/>
|
||||
</aspects>
|
||||
</aspectj>
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.nifi.nar;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class LoadNativeLibAspectTest {
|
||||
|
||||
private static final Path TEMP_DIR = Paths.get(System.getProperty("java.io.tmpdir"));
|
||||
|
||||
private LoadNativeLibAspect aspect;
|
||||
|
||||
private ProceedingJoinPoint joinPoint;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
aspect = new LoadNativeLibAspect();
|
||||
joinPoint = mock(ProceedingJoinPoint.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhenArgumentIsNullThenProceedNormally() throws Throwable {
|
||||
// GIVEN
|
||||
when(joinPoint.getArgs()).thenReturn(new Object[]{null});
|
||||
|
||||
// WHEN
|
||||
aspect.around(joinPoint);
|
||||
|
||||
// THEN
|
||||
verify(joinPoint).proceed();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhenArgumentIsEmptyStringThenProceedNormally() throws Throwable {
|
||||
// GIVEN
|
||||
when(joinPoint.getArgs()).thenReturn(new Object[]{""});
|
||||
|
||||
// WHEN
|
||||
aspect.around(joinPoint);
|
||||
|
||||
// THEN
|
||||
verify(joinPoint).proceed();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhenNativeLibraryFileNotExistsThenProceedNormally() throws Throwable {
|
||||
// GIVEN
|
||||
String libFileName = "mylib_dummy.so";
|
||||
Path libFilePath = Paths.get("target", libFileName).toAbsolutePath();
|
||||
|
||||
when(joinPoint.getArgs()).thenReturn(new Object[]{libFilePath.toString()});
|
||||
|
||||
// WHEN
|
||||
aspect.around(joinPoint);
|
||||
|
||||
// THEN
|
||||
verify(joinPoint).proceed();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhenNativeLibraryFileExistsThenCreateATempCopyAndProceedWithThat() throws Throwable {
|
||||
// GIVEN
|
||||
String libFileName = "mylib.so";
|
||||
byte[] libFileContent = "code".getBytes();
|
||||
Path libFilePath = Paths.get("target", libFileName).toAbsolutePath();
|
||||
Files.write(libFilePath, libFileContent);
|
||||
|
||||
when(joinPoint.getArgs()).thenReturn(new Object[]{libFilePath.toString()});
|
||||
|
||||
// WHEN
|
||||
aspect.around(joinPoint);
|
||||
|
||||
// THEN
|
||||
ArgumentCaptor<Object[]> captor = ArgumentCaptor.forClass(Object[].class);
|
||||
verify(joinPoint).proceed(captor.capture());
|
||||
|
||||
Object[] args = captor.getValue();
|
||||
assertNotNull(args);
|
||||
assertEquals(1, args.length);
|
||||
assertNotNull(args[0]);
|
||||
assertTrue(args[0] instanceof String);
|
||||
|
||||
String tempLibFilePathStr = (String) args[0];
|
||||
Path tempLibFilePath = Paths.get(tempLibFilePathStr);
|
||||
assertEquals(TEMP_DIR, tempLibFilePath.getParent());
|
||||
assertTrue(tempLibFilePathStr.endsWith(libFileName));
|
||||
assertTrue(Files.exists(tempLibFilePath));
|
||||
assertArrayEquals(libFileContent, Files.readAllBytes(tempLibFilePath));
|
||||
|
||||
Files.delete(libFilePath);
|
||||
Files.delete(tempLibFilePath);
|
||||
}
|
||||
|
||||
}
|
|
@ -66,6 +66,15 @@ java.arg.16=-Djavax.security.auth.useSubjectCredsOnly=true
|
|||
# Please see https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_adminserver_config for configuration options.
|
||||
java.arg.17=-Dzookeeper.admin.enableServer=false
|
||||
|
||||
# The following options configure a Java Agent to handle native library loading.
|
||||
# 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/aspectj/aspectjweaver-${aspectj.version}.jar
|
||||
#java.arg.19=-Daj.weaving.loadersToSkip=sun.misc.Launcher$AppClassLoader,jdk.internal.loader.ClassLoaders$AppClassLoader,org.eclipse.jetty.webapp.WebAppClassLoader,\
|
||||
# org.apache.jasper.servlet.JasperLoader,org.jvnet.hk2.internal.DelegatingClassLoader,org.apache.nifi.nar.NarClassLoader
|
||||
# End of Java Agent config for native library loading.
|
||||
|
||||
###
|
||||
# Notification Services for notifying interested parties when NiFi is stopped, started, dies
|
||||
###
|
||||
|
|
|
@ -115,7 +115,7 @@
|
|||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<scope>provided</scope>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
|
|
|
@ -554,11 +554,6 @@
|
|||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.8.14</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
|
|
@ -305,12 +305,6 @@ The following binary components are provided under the Apache Software License v
|
|||
Apache Maven
|
||||
Copyright 2003-2017 The Apache Software Foundation.
|
||||
|
||||
************************
|
||||
Eclipse Public License 1.0
|
||||
************************
|
||||
|
||||
(EPL 1.0) AspectJ Runtime (org.aspectj:aspectjrt:jar:1.8.0 - http://www.eclipse.org/aspectj/)
|
||||
|
||||
*****************
|
||||
Public Domain
|
||||
*****************
|
||||
|
|
11
pom.xml
11
pom.xml
|
@ -101,6 +101,7 @@
|
|||
<hadoop.version>3.2.1</hadoop.version>
|
||||
<ozone.version>1.0.0</ozone.version>
|
||||
<gcs.version>2.1.5</gcs.version>
|
||||
<aspectj.version>1.9.6</aspectj.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
|
@ -355,6 +356,16 @@
|
|||
<version>${jetty.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>${aspectj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${aspectj.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
Loading…
Reference in New Issue