stepstone-tech / hnswlib-jna

Native-Like Performance for Nearest Neighbor Search in Java Applications using Hnswlib and Java Native Access
Apache License 2.0
32 stars 8 forks source link

Getting the `UnexpectedNativeException` despite adding the .so file to JNA path #14

Open ch4174nya opened 1 year ago

ch4174nya commented 1 year ago

Hello, I have a Spring-Boot module (specifically, it trains a KNN model) where we leverage this library. When I tried deploying the jar file on a Linux system, I started getting the UnexpectedNativeException. I then followed the steps in your README, compiled the .so file, and attempted loading it as

java -Djna.debug_load=true -Djna.library.path=/root/hnswlib-jna/lib -jar -Dspring.profiles.active=
default my-application.jar "2022-01-01" "2022-01-10"

However, the exception was still thrown:

...
Sep 22, 2023 8:18:03 AM com.sun.jna.Native extractFromResourcePath
INFO: Looking in classpath from org.springframework.boot.loader.LaunchedURLClassLoader@46f7f36a for /com/sun/jna/linux-x86-64/libjnidispatch.so
Sep 22, 2023 8:18:03 AM com.sun.jna.Native extractFromResourcePath
INFO: Found library resource at jar:file:/root/vessel-movement-training/vessel-movement-training-1.0.0.jar!/BOOT-INF/lib/jna-5.5.0.jar!/com/sun/jna/linux-x86-64/libjnidispatch.so
Sep 22, 2023 8:18:03 AM com.sun.jna.Native extractFromResourcePath
INFO: Extracting library to /root/.cache/JNA/temp/jna5135192293771266151.tmp
Sep 22, 2023 8:18:03 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Looking for library 'hnswlib-jna-x86-64'
Sep 22, 2023 8:18:03 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Adding paths from jna.library.path: /root/hnswlib-jna/lib
Sep 22, 2023 8:18:03 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Trying /root/hnswlib-jna/lib/libhnswlib-jna-x86-64.so
Sep 22, 2023 8:18:03 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Found library 'hnswlib-jna-x86-64' at /root/hnswlib-jna/lib/libhnswlib-jna-x86-64.so
2023-09-22 08:18:05.973  INFO 20810 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-09-22 08:18:05.990 ERROR 20810 --- [           main] o.s.b.SpringApplication                  : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:772) [spring-boot-2.6.3.jar!/:2.6.3]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:753) [spring-boot-2.6.3.jar!/:2.6.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:309) [spring-boot-2.6.3.jar!/:2.6.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar!/:2.6.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar!/:2.6.3]
        at com.mpa.vesselmovement.main.Application.main(Application.java:32) [classes!/:1.0.0]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) [vessel-movement-training-1.0.0.jar:1.0.0]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) [vessel-movement-training-1.0.0.jar:1.0.0]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) [vessel-movement-training-1.0.0.jar:1.0.0]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) [vessel-movement-training-1.0.0.jar:1.0.0]
Caused by: com.stepstone.search.hnswlib.jna.exception.UnexpectedNativeException
        at com.stepstone.search.hnswlib.jna.Index.checkResultCode(Index.java:294) ~[hnswlib-jsn-1.4.0.jar!/:?]
        at com.stepstone.search.hnswlib.jna.Index.initialize(Index.java:94) ~[hnswlib-jsn-1.4.0.jar!/:?]
...

I have tried compiling the *.so file using clang-12, 14, and 16 and the host is running kernel 4.18 (if it matters). Could you please help me narrow down this issue as this is blocking some key production deployments?

ch4174nya commented 1 year ago

I have managed to get past this, and in my experience, the cause for this error can vary from there not being enough memory to support the Max Elements or there being an issue with the file that the index is being loaded from. I imagine there could be more scenarios leading to this exception, but these are the two I've encountered so far.

Is there any way for the exception to indicate more details on the cause?