Skip to content

Commit d2fb021

Browse files
authored
Fix native library loading (kroxylicious#2887)
1 parent ff8656c commit d2fb021

File tree

6 files changed

+152
-7
lines changed

6 files changed

+152
-7
lines changed

compose/compose-proxy-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#
66

77
---
8+
useIoUring: false
89
management:
910
endpoints:
1011
prometheus: {}

compose/kafka-compose.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ services:
6363
image: 'quay.io/kroxylicious/proxy:0.18.0-SNAPSHOT'
6464
container_name: kroxylicious
6565
command: --config /opt/kroxylicious/config/proxy-config.yaml
66+
environment:
67+
JAVA_ENABLE_DEBUG: true
68+
JAVA_DEBUG_SUSPEND: false
6669
networks:
6770
- kroxylicious_network
6871
ports:
6972
- "9292-9296:9292-9296"
73+
- "5005:5005"
7074
configs:
7175
- source: proxy-config
7276
target: /opt/kroxylicious/config/proxy-config.yaml
@@ -78,5 +82,3 @@ networks:
7882
configs:
7983
proxy-config:
8084
file: compose-proxy-config.yaml
81-
82-

kroxylicious-app/pom.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,94 @@
185185
</properties>
186186
<build>
187187
<plugins>
188+
<plugin>
189+
<groupId>org.apache.maven.plugins</groupId>
190+
<artifactId>maven-dependency-plugin</artifactId>
191+
<executions>
192+
<execution>
193+
<id>unpack-netty-native-libs</id>
194+
<phase>prepare-package</phase>
195+
<goals>
196+
<goal>unpack-dependencies</goal>
197+
</goals>
198+
<configuration>
199+
<includeArtifactIds>netty-transport-native-epoll,netty-transport-native-unix-common,netty-transport-native-io_uring,netty-transport-native-kqueue</includeArtifactIds>
200+
<outputDirectory>${project.build.directory}/libs/native/netty</outputDirectory>
201+
<!-- derived form snappy's resource-config.json -->
202+
<includes>**/*.dylib,**/*.jnilib,**/*.so,**/*.a</includes>
203+
<fileMappers>
204+
<org.codehaus.plexus.components.io.filemappers.FlattenFileMapper/>
205+
</fileMappers>
206+
</configuration>
207+
</execution>
208+
<execution>
209+
<id>unpack-snappy-native-libs</id>
210+
<phase>prepare-package</phase>
211+
<goals>
212+
<goal>unpack-dependencies</goal>
213+
</goals>
214+
<configuration>
215+
<includeArtifactIds>snappy-java</includeArtifactIds>
216+
<outputDirectory>${project.build.directory}/libs/native/snappy</outputDirectory>
217+
<!-- derived form snappy's resource-config.json -->
218+
<includes>**/*.dylib,**/*.jnilib,**/*.so,**/*.a</includes>
219+
<!-- when built into the container paths are cases sensitive and the container build variables for arch and platform are lower case-->
220+
<fileMappers>
221+
<org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
222+
<pattern>AIX</pattern>
223+
<replacement>aix</replacement>
224+
</org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
225+
<org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
226+
<pattern>FreeBSD</pattern>
227+
<replacement>freebsd</replacement>
228+
</org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
229+
<org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
230+
<pattern>Linux</pattern>
231+
<replacement>linux</replacement>
232+
</org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
233+
<org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
234+
<pattern>Mac</pattern>
235+
<replacement>darwin</replacement>
236+
</org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
237+
<org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
238+
<pattern>SunOS</pattern>
239+
<replacement>sunos</replacement>
240+
</org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
241+
<org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
242+
<pattern>Windows</pattern>
243+
<replacement>windows</replacement>
244+
</org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
245+
</fileMappers>
246+
</configuration>
247+
</execution>
248+
<execution>
249+
<id>unpack-zstd-native-libs</id>
250+
<phase>prepare-package</phase>
251+
<goals>
252+
<goal>unpack-dependencies</goal>
253+
</goals>
254+
<configuration>
255+
<includeArtifactIds>zstd-jni</includeArtifactIds>
256+
<outputDirectory>${project.build.directory}/libs/native/zstd-jni</outputDirectory>
257+
<!-- derived form snappy's resource-config.json -->
258+
<includes>**/*.dylib,**/*.jnilib,**/*.so,**/*.a</includes>
259+
</configuration>
260+
</execution>
261+
<execution>
262+
<id>unpack-lz4-native-libs</id>
263+
<phase>prepare-package</phase>
264+
<goals>
265+
<goal>unpack-dependencies</goal>
266+
</goals>
267+
<configuration>
268+
<includeArtifactIds>lz4-java</includeArtifactIds>
269+
<outputDirectory>${project.build.directory}/libs/native/lz4-java</outputDirectory>
270+
<!-- derived form snappy's resource-config.json -->
271+
<includes>**/*.dylib,**/*.jnilib,**/*.so,**/*.a</includes>
272+
</configuration>
273+
</execution>
274+
</executions>
275+
</plugin>
188276
<plugin>
189277
<groupId>org.apache.maven.plugins</groupId>
190278
<artifactId>maven-assembly-plugin</artifactId>

kroxylicious-app/src/assembly/binary-distribution.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@
4545
<include>log4j2.yaml</include>
4646
</includes>
4747
</fileSet>
48+
<fileSet>
49+
<directory>${project.build.directory}/libs/native/</directory>
50+
<outputDirectory>libs/native/</outputDirectory>
51+
<!-- derived form snappy's resource-config.json -->
52+
<includes>
53+
<include>**/*.dylib</include>
54+
<include>**/*.jnilib</include>
55+
<include>**/*.so</include>
56+
<include>**/*.a</include>
57+
</includes>
58+
<excludes>
59+
<exclude>**/*.class</exclude>
60+
</excludes>
61+
</fileSet>
4862
</fileSets>
4963
<dependencySets>
5064
<dependencySet>

kroxylicious-app/src/assembly/kroxylicious-start.sh

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ script_dir() {
1313
dir=$(dirname "$0")
1414
local full_dir
1515
full_dir=$(cd "${dir}" && pwd)
16-
echo ${full_dir}
16+
echo "${full_dir}"
1717
}
1818

19+
# Disable warnings about `local` variables
20+
# shellcheck disable=SC3043
1921
classpath() {
2022
local class_path
2123
class_path="$(script_dir)/../libs/*"
@@ -25,10 +27,48 @@ classpath() {
2527
echo "${class_path}"
2628
}
2729

30+
# Disable warnings about `local` variables
31+
# shellcheck disable=SC3043
32+
native_library_path() {
33+
local lib_path="${1}"
34+
arch="${TARGETARCH:-$(uname -m)}"
35+
native_lib="${NATIVE_LIB_BASE_DIR}${lib_path}/${TARGETOS:-linux}"
36+
if [ -r "${native_lib}" ]; then
37+
if [ -r "${native_lib}/${arch}" ]; then
38+
echo "${native_lib}/${arch}"
39+
return
40+
else
41+
# not everyone thinks `uname -m` is the right convention
42+
case ${arch} in
43+
arm64) arch=aarch64 ;;
44+
x86_64) arch=amd64 ;;
45+
esac
46+
if [ -r "${native_lib}/${arch}" ]; then
47+
echo "${native_lib}/${arch}"
48+
return
49+
fi
50+
fi
51+
elif [ -r "${NATIVE_LIB_BASE_DIR}${lib_path}/" ]; then
52+
# The native dependency isn't broken down by OS and arch but still has something loadable. (Looking at you Netty)
53+
echo "${NATIVE_LIB_BASE_DIR}${lib_path}/"
54+
return
55+
fi
56+
}
57+
2858
if [ "${KROXYLICIOUS_LOGGING_OPTIONS+set}" != set ]; then
2959
KROXYLICIOUS_LOGGING_OPTIONS="-Dlog4j2.configurationFile=$(script_dir)/../config/log4j2.yaml -Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"
3060
fi
31-
export JAVA_OPTIONS="${KROXYLICIOUS_LOGGING_OPTIONS} ${JAVA_OPTIONS:-}"
61+
62+
NATIVE_LIB_BASE_DIR=${NATIVE_LIB_BASE_DIR:-$(script_dir)/../libs/native/}
63+
NETTY_NATIVE_LIB=$(native_library_path netty)
64+
LZ4_NATIVE_LIB=$(native_library_path lz4-java/net/jpountz/util)
65+
SNAPPY_NATIVE_LIB=$(native_library_path snappy/org/xerial/snappy/native)
66+
ZSTD_NATIVE_LIB=$(native_library_path zstd-jni)
67+
NATIVE_LIB_PATH="${NATIVE_LIB_PATH:-${NETTY_NATIVE_LIB}:${LZ4_NATIVE_LIB}:${SNAPPY_NATIVE_LIB}:${ZSTD_NATIVE_LIB}:${LD_LIBRARY_PATH:-}}"
68+
echo "setting java.library.path=${NATIVE_LIB_PATH}"
69+
NATIVE_LIB_OPTIONS=${NATIVE_LIB_OPTIONS:-"-Djava.library.path=${NATIVE_LIB_PATH} -Dorg.xerial.snappy.disable.bundled.libs=true"}
70+
71+
export JAVA_OPTIONS="${KROXYLICIOUS_LOGGING_OPTIONS:-} ${NATIVE_LIB_OPTIONS:-} ${JAVA_OPTIONS:-}"
3272
JAVA_CLASSPATH="$(classpath)"
3373
export JAVA_CLASSPATH
3474
export JAVA_MAIN_CLASS=io.kroxylicious.app.Kroxylicious

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,9 +1029,9 @@
10291029
</os>
10301030
</activation>
10311031
<properties>
1032-
<netty.epoll.classifier>linux-aarch_64</netty.epoll.classifier>
1033-
<netty.io_uring.classifier>linux-aarch_64</netty.io_uring.classifier>
1034-
<netty.kqueue.classifier>osx-aarch_64</netty.kqueue.classifier>
1032+
<netty.epoll.architecture>linux-aarch_64</netty.epoll.architecture>
1033+
<netty.io_uring.architecture>linux-aarch_64</netty.io_uring.architecture>
1034+
<netty.kqueue.architecture>osx-aarch_64</netty.kqueue.architecture>
10351035
</properties>
10361036
</profile>
10371037
<profile>

0 commit comments

Comments
 (0)