This is an automated email from the ASF dual-hosted git repository.

LuciferYang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git


The following commit(s) were added to refs/heads/main by this push:
     new 204bf2b3f7 [GLUTEN-13788][VL] Load the core library first when 
spark.gluten.sql.columnar.libpath is set (#13789)
204bf2b3f7 is described below

commit 204bf2b3f7d16b9f74470c49ba020dfdc5d286a8
Author: Kent Yao <[email protected]>
AuthorDate: Thu Nov 26 14:44:51 2026 +0800

    [GLUTEN-13788][VL] Load the core library first when 
spark.gluten.sql.columnar.libpath is set (#13789)
    
    ## What changes are proposed in this pull request?
    
    Fixes #13788.
    
    With `spark.gluten.sql.columnar.libpath` pointing at `libvelox.so`, the 
Velox backend
    passes only that file to `System.load`:
    
    ```scala
    } else {
      // Path based load. Ignore all other loaderes.
      JniLibLoader.loadFromPath(libPath)
    }
    ```
    
    `libvelox.so` links against `libgluten.so`, so the dynamic linker maps it, 
but the JVM
    runs `JNI_OnLoad` only for a library given to `System.load`. Native methods 
still bind
    (symbol lookup searches dependencies), so nothing fails at load time. The 
core JNI
    globals set in `cpp/core/jni/JniWrapper.cc` `JNI_OnLoad` stay null, and the 
first call
    that uses one crashes the JVM:
    
    ```
    #  SIGSEGV (0xb) ...
    V  [libjvm.so+0x9d35dd]  jni_NewObjectV+0xbd
    C  [libgluten.so+...]  JNIEnv_::NewObject(_jclass*, _jmethodID*, ...)
    C  [libgluten.so+...]  
Java_org_apache_gluten_metrics_IteratorMetricsJniWrapper_nativeFetchMetrics
    ```
    
    This PR loads `spark.gluten.sql.columnar.libname` (`libgluten.so` by 
default) from the
    same directory first, then the configured library. That is the same order 
as the
    default bundled-library load. If `libpath` already names the core library, 
it is loaded
    once. If the core library is missing, initialization fails with a message 
that names
    both settings, instead of crashing later. The `libpath` doc text now says 
this.
    
    Alternative not taken: initialize the core JNI state from the Velox 
`JNI_OnLoad` as well.
    That needs an idempotent init shared by two libraries and changes the 
default path too.
    
    ## How was this patch tested?
    
    - `VeloxListenerApiTest`: 4 new cases for the load list (core first, core 
only once,
      relative path, missing core library). `tests="5" errors="0" failures="0"`.
    - End to end, Spark 4.1.1 local, a 2-stage aggregate over parquet with
      `libpath=<build>/releases/libvelox.so`: without this change the JVM exits 
134 with the
      SIGSEGV above; with it the query returns the correct result.
    - `AllGlutenConfiguration` / `AllVeloxConfiguration` golden check passes 
after
      regenerating `docs/Configuration.md`.
    
    ## Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
---
 .../backendsapi/velox/VeloxListenerApi.scala       | 30 ++++++++++++-
 .../gluten/backendsapi/VeloxListenerApiTest.java   | 49 ++++++++++++++++++++++
 docs/Configuration.md                              |  2 +-
 .../org/apache/gluten/config/GlutenConfig.scala    |  5 ++-
 4 files changed, 83 insertions(+), 3 deletions(-)

diff --git 
a/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxListenerApi.scala
 
b/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxListenerApi.scala
index a48ea8fcb6..56273c2c71 100644
--- 
a/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxListenerApi.scala
+++ 
b/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxListenerApi.scala
@@ -20,6 +20,7 @@ import org.apache.gluten.backendsapi.ListenerApi
 import 
org.apache.gluten.backendsapi.arrow.ArrowBatchTypes.{ArrowJavaBatchType, 
ArrowNativeBatchType}
 import org.apache.gluten.config.{GlutenConfig, GlutenCoreConfig, VeloxConfig}
 import org.apache.gluten.config.VeloxConfig._
+import org.apache.gluten.exception.GlutenException
 import org.apache.gluten.execution.VeloxBroadcastBuildSideCache
 import org.apache.gluten.execution.datasource.GlutenFormatFactory
 import org.apache.gluten.expression.UDFMappings
@@ -51,6 +52,7 @@ import org.apache.spark.util.{SparkDirectoryUtil, 
SparkResourceUtil, SparkShutdo
 
 import org.apache.commons.lang3.StringUtils
 
+import java.io.File
 import java.util.UUID
 import java.util.concurrent.atomic.AtomicBoolean
 
@@ -227,7 +229,8 @@ class VeloxListenerApi extends ListenerApi with Logging {
       
loader.load(s"$platformLibDir/${System.mapLibraryName(VeloxBackend.BACKEND_NAME)}")
     } else {
       // Path based load. Ignore all other loaderes.
-      JniLibLoader.loadFromPath(libPath)
+      checkedLibPathsToLoad(libPath, conf.get(GlutenConfig.GLUTEN_LIB_NAME))
+        .foreach(JniLibLoader.loadFromPath)
     }
 
     // Initial native backend with configurations.
@@ -279,6 +282,31 @@ object VeloxListenerApi {
     s"$osName/$arch"
   }
 
+  /**
+   * Libraries to load when `spark.gluten.sql.columnar.libpath` points at the 
backend library. The
+   * backend library links against the core library, but the JVM runs 
`JNI_OnLoad` only for
+   * libraries passed to `System.load`, so the core library in the same 
directory is loaded first.
+   */
+  def libPathsToLoad(libPath: String, baseLibName: String): Seq[String] = {
+    val backendLib = new File(libPath)
+    val coreLib = new File(backendLib.getParentFile, 
System.mapLibraryName(baseLibName))
+    if (coreLib.getName == backendLib.getName) {
+      Seq(libPath)
+    } else {
+      Seq(coreLib.getPath, libPath)
+    }
+  }
+
+  def checkedLibPathsToLoad(libPath: String, baseLibName: String): Seq[String] 
= {
+    val paths = libPathsToLoad(libPath, baseLibName)
+    if (!new File(paths.head).isFile) {
+      throw new GlutenException(
+        s"${GlutenConfig.GLUTEN_LIB_PATH.key} is $libPath, so the core library 
${paths.head} " +
+          s"(from ${GlutenConfig.GLUTEN_LIB_NAME.key}) must be in the same 
directory")
+    }
+    paths
+  }
+
   private def inLocalMode(conf: SparkConf): Boolean = {
     SparkResourceUtil.isLocalMaster(conf)
   }
diff --git 
a/backends-velox/src/test/java/org/apache/gluten/backendsapi/VeloxListenerApiTest.java
 
b/backends-velox/src/test/java/org/apache/gluten/backendsapi/VeloxListenerApiTest.java
index 8cb66b55f8..fb4e790baa 100644
--- 
a/backends-velox/src/test/java/org/apache/gluten/backendsapi/VeloxListenerApiTest.java
+++ 
b/backends-velox/src/test/java/org/apache/gluten/backendsapi/VeloxListenerApiTest.java
@@ -17,13 +17,22 @@
 package org.apache.gluten.backendsapi;
 
 import org.apache.gluten.backendsapi.velox.VeloxListenerApi;
+import org.apache.gluten.exception.GlutenException;
 
 import org.apache.spark.SparkConf;
 import org.junit.Test;
 
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Arrays;
+
+import scala.collection.JavaConverters;
 import scala.collection.immutable.Map;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
 
 public class VeloxListenerApiTest {
 
@@ -38,4 +47,44 @@ public class VeloxListenerApiTest {
         "52428800",
         
parsed.get("spark.gluten.sql.columnar.backend.velox.filePreloadThreshold").get());
   }
+
+  // Library file names are platform specific (.so on Linux, .dylib on macOS).
+  private static final String CORE = System.mapLibraryName("gluten");
+  private static final String BACKEND = System.mapLibraryName("velox");
+
+  private static String inDir(String name) {
+    return new File("/opt/gluten", name).getPath();
+  }
+
+  @Test
+  public void testLibPathLoadsCoreLibraryFirst() {
+    assertEquals(
+        Arrays.asList(inDir(CORE), inDir(BACKEND)),
+        
JavaConverters.seqAsJavaList(VeloxListenerApi.libPathsToLoad(inDir(BACKEND), 
"gluten")));
+  }
+
+  @Test
+  public void testLibPathPointingAtCoreLibraryLoadsItOnce() {
+    assertEquals(
+        Arrays.asList(inDir(CORE)),
+        
JavaConverters.seqAsJavaList(VeloxListenerApi.libPathsToLoad(inDir(CORE), 
"gluten")));
+  }
+
+  @Test
+  public void testRelativeLibPathLoadsCoreLibraryFromSameDirectory() {
+    assertEquals(
+        Arrays.asList(CORE, BACKEND),
+        JavaConverters.seqAsJavaList(VeloxListenerApi.libPathsToLoad(BACKEND, 
"gluten")));
+  }
+
+  @Test
+  public void testMissingCoreLibraryNamesLibnameConf() throws Exception {
+    Path dir = Files.createTempDirectory("gluten-libpath");
+    File backend = Files.createFile(dir.resolve(BACKEND)).toFile();
+    GlutenException e =
+        assertThrows(
+            GlutenException.class,
+            () -> VeloxListenerApi.checkedLibPathsToLoad(backend.getPath(), 
"gluten"));
+    assertTrue(e.getMessage(), 
e.getMessage().contains("spark.gluten.sql.columnar.libname"));
+  }
 }
diff --git a/docs/Configuration.md b/docs/Configuration.md
index 17372334ff..60aa45d1b7 100644
--- a/docs/Configuration.md
+++ b/docs/Configuration.md
@@ -75,7 +75,7 @@ Type: ⚓ Static - can only be set before the Spark application 
starts; 🔄 Dyn
 | spark.gluten.sql.columnar.hashagg                                   | 🔄   | 
true              | Enable or disable columnar hashagg.                         
                                                                                
                                                                                
                                                                                
                                                                                
               [...]
 | spark.gluten.sql.columnar.hivetablescan                             | 🔄   | 
true              | Enable or disable columnar hivetablescan.                   
                                                                                
                                                                                
                                                                                
                                                                                
               [...]
 | spark.gluten.sql.columnar.libname                                   | 🔄   | 
gluten            | The gluten library name.                                    
                                                                                
                                                                                
                                                                                
                                                                                
               [...]
-| spark.gluten.sql.columnar.libpath                                   | 🔄      
                || The gluten library path.                                     
                                                                                
                                                                                
                                                                                
                                                                                
              [...]
+| spark.gluten.sql.columnar.libpath                                   | 🔄      
                || The gluten library path. For the Velox backend, point it at 
the backend library; the core library named by 
spark.gluten.sql.columnar.libname is loaded first from the same directory.      
                                                                                
                                                                                
                                                [...]
 | spark.gluten.sql.columnar.limit                                     | 🔄   | 
true              |
 | spark.gluten.sql.columnar.localTableScan                            | 🔄   | 
false             | Enable or disable native columnar execution of 
LocalTableScanExec. When true, Gluten attempts to replace LocalTableScanExec (a 
driver-side local collection) with a backend transformer that converts the rows 
into columnar batches natively.                                                 
                                                                                
                            [...]
 | spark.gluten.sql.columnar.maxBatchSize                              | 🔄   | 
4096              |
diff --git 
a/gluten-substrait/src/main/scala/org/apache/gluten/config/GlutenConfig.scala 
b/gluten-substrait/src/main/scala/org/apache/gluten/config/GlutenConfig.scala
index 8a010762e1..8958081ac8 100644
--- 
a/gluten-substrait/src/main/scala/org/apache/gluten/config/GlutenConfig.scala
+++ 
b/gluten-substrait/src/main/scala/org/apache/gluten/config/GlutenConfig.scala
@@ -887,7 +887,10 @@ object GlutenConfig extends ConfigRegistry {
 
   val GLUTEN_LIB_PATH =
     buildConf("spark.gluten.sql.columnar.libpath")
-      .doc("The gluten library path.")
+      .doc(
+        "The gluten library path. For the Velox backend, point it at the 
backend library; the " +
+          "core library named by spark.gluten.sql.columnar.libname is loaded 
first from the " +
+          "same directory.")
       .stringConf
       .createWithDefault("")
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to