refactor(diskann): decouple from libaio via dlopen (#532)

Co-authored-by: Zefeng Yin <yinzefeng.yzf@alibaba-inc.com>
This commit is contained in:
rayx
2026-07-16 17:58:25 +08:00
committed by GitHub
co-authored by Zefeng Yin
parent 23538ab876
commit ec8a78ee08
49 changed files with 1108 additions and 809 deletions
+27 -2
View File
@@ -29,6 +29,8 @@
#include <string>
#include <unordered_map>
#include <vector>
#include <ailego/io/io_backend_def.h>
#include <zvec/ailego/io/io_backend.h>
#include <zvec/db/collection.h>
#include <zvec/db/config.h>
#include <zvec/db/doc.h>
@@ -759,6 +761,25 @@ const char *zvec_get_default_jieba_dict_dir(void) {
return cached.c_str();
}
// =============================================================================
// I/O Backend Introspection
// =============================================================================
zvec_io_backend_type_t zvec_get_io_backend_type(void) {
auto type = zvec::ailego::current_io_backend_type();
return static_cast<zvec_io_backend_type_t>(static_cast<uint32_t>(type));
}
const char *zvec_get_io_backend_type_name(zvec_io_backend_type_t type) {
auto cpp_type = static_cast<zvec::ailego::IOBackendType>(type);
return zvec::ailego::IOBackendTypeName(cpp_type);
}
const char *zvec_get_io_backend_description(void) {
auto type = zvec::ailego::current_io_backend_type();
return zvec::ailego::IOBackendDescription(type);
}
// =============================================================================
// Error handling interface implementation
// =============================================================================
@@ -2800,12 +2821,16 @@ const char *zvec_index_type_to_string(zvec_index_type_t index_type) {
return "IVF";
case ZVEC_INDEX_TYPE_FLAT:
return "FLAT";
case ZVEC_INDEX_TYPE_HNSW_RABITQ:
return "HNSW_RABITQ";
case ZVEC_INDEX_TYPE_DISKANN:
return "DISKANN";
case ZVEC_INDEX_TYPE_VAMANA:
return "VAMANA";
case ZVEC_INDEX_TYPE_INVERT:
return "INVERT";
case ZVEC_INDEX_TYPE_FTS:
return "FTS";
case ZVEC_INDEX_TYPE_DISKANN:
return "DiskANN";
default:
return "UNKNOWN_INDEX_TYPE";
}
+8 -33
View File
@@ -19,21 +19,6 @@ set(SRC_LISTS
pybind11_add_module(_zvec ${SRC_LISTS})
# pybind11_add_module() defaults to CXX_VISIBILITY_PRESET=hidden +
# VISIBILITY_INLINES_HIDDEN=ON, which hides the compiler-generated helper
# symbols attached to inline functions (guard variables for static locals,
# vtables, typeinfo ...). The DiskAnn runtime plugin has its own copy of
# Factory<IndexStreamer>::Instance()'s guard variable; if _zvec.so's copy
# is hidden, the two guards are separate and the factory constructor runs
# twice during plugin load, wiping out the registrations that happened
# during _zvec.so import. We switch to default visibility here and rely on
# the version script (exports.map / exports.mac) to keep the dynamic
# symbol table small by exporting only zvec::* and PyInit_*.
set_target_properties(_zvec PROPERTIES
CXX_VISIBILITY_PRESET default
C_VISIBILITY_PRESET default
VISIBILITY_INLINES_HIDDEN OFF)
# Ensure any change to the linker version script (exports.map) triggers a
# re-link of _zvec.so. target_link_options() alone is a command-line flag
# and does not register the script file as a build dependency, so stale
@@ -57,11 +42,11 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
$<TARGET_FILE:core_knn_ivf_static>
$<TARGET_FILE:core_knn_vamana_static>
$<TARGET_FILE:core_knn_cluster_static>
$<TARGET_FILE:core_knn_diskann_static>
$<TARGET_FILE:core_mix_reducer_static>
$<TARGET_FILE:core_metric_static>
$<TARGET_FILE:core_utility_static>
$<TARGET_FILE:core_quantizer_static>
$<TARGET_FILE:core_plugin>
-Wl,--no-whole-archive
zvec
${CMAKE_DL_LIBS}
@@ -69,21 +54,11 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_options(_zvec PRIVATE
"LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exports.map"
)
# DiskAnn is x86-only (it depends on libaio, unavailable on ARM64), so the
# runtime plugin (libzvec_diskann_plugin.so) is only produced on non-ARM
# builds. It is shipped as a runtime-loaded shared module and brought up
# implicitly the first time a DiskAnn index is created — users never need to
# call any load function. If libaio is missing at runtime the auto-load
# fails cleanly and the error is surfaced only when DiskAnn is actually
# used; other index types (HNSW/IVF/Flat/Vamana) remain fully functional.
# The .so must therefore be discoverable next to the extension module,
# hence the $ORIGIN rpath below.
if (NOT CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|arm")
set_target_properties(_zvec PROPERTIES
BUILD_RPATH "$ORIGIN"
INSTALL_RPATH "$ORIGIN"
)
endif()
# DiskAnn is now statically linked into _zvec.so via --whole-archive, so
# no separate runtime .so is shipped in the wheel. libaio is still loaded
# at runtime via dlopen() (see libaio_loader.h); if it is missing, DiskAnn
# fails cleanly with an actionable error while other index types
# (HNSW/IVF/Flat/Vamana) remain fully functional.
elseif (APPLE)
target_link_libraries(_zvec PRIVATE
-Wl,-force_load,$<TARGET_FILE:core_knn_flat_static>
@@ -94,11 +69,11 @@ elseif (APPLE)
-Wl,-force_load,$<TARGET_FILE:core_knn_ivf_static>
-Wl,-force_load,$<TARGET_FILE:core_knn_vamana_static>
-Wl,-force_load,$<TARGET_FILE:core_knn_cluster_static>
-Wl,-force_load,$<TARGET_FILE:core_knn_diskann_static>
-Wl,-force_load,$<TARGET_FILE:core_mix_reducer_static>
-Wl,-force_load,$<TARGET_FILE:core_metric_static>
-Wl,-force_load,$<TARGET_FILE:core_utility_static>
-Wl,-force_load,$<TARGET_FILE:core_quantizer_static>
-Wl,-force_load,$<TARGET_FILE:core_plugin>
zvec
)
target_link_libraries(_zvec PRIVATE
@@ -113,11 +88,11 @@ elseif (MSVC)
core_knn_ivf_static
core_knn_vamana_static
core_knn_cluster_static
core_knn_diskann_static
core_mix_reducer_static
core_metric_static
core_utility_static
core_quantizer_static
core_plugin
)
target_link_libraries(_zvec PRIVATE
${_zvec_whole_archive_libs}
-35
View File
@@ -13,7 +13,6 @@
// limitations under the License.
#include <pybind11/pybind11.h>
#include <zvec/plugin/diskann_plugin.h>
#include "python_collection.h"
#include "python_config.h"
#include "python_doc.h"
@@ -24,39 +23,6 @@
namespace zvec {
namespace {
// Expose DiskAnn plugin management to Python. The DiskAnn runtime normally
// auto-loads on first use, but tests (and diagnostic tooling) need a way to
// force a load up-front and get actionable errors when libaio is missing or
// the plugin shared object cannot be located.
void InitializeDiskAnnPluginBindings(pybind11::module_ &m) {
m.def(
"load_diskann_plugin",
[](const std::string &path) { return ::zvec::LoadDiskAnnPlugin(path); },
pybind11::arg("path") = std::string(),
"Load the DiskAnn runtime plugin. Returns 0 on success or a negative "
"DiskAnnPluginStatus code on failure (unsupported platform, libaio "
"missing, or dlopen failure).");
m.def("is_diskann_plugin_loaded", &::zvec::IsDiskAnnPluginLoaded,
"Return True if the DiskAnn runtime plugin is currently loaded.");
m.def("is_libaio_available", &::zvec::IsLibAioAvailable,
"Return True if libaio is resolvable on this host (required by the "
"DiskAnn runtime).");
// Status constants so callers can compare against well-known codes without
// hard-coding integers.
m.attr("DISKANN_PLUGIN_OK") = static_cast<int>(::zvec::kDiskAnnPluginOk);
m.attr("DISKANN_PLUGIN_UNSUPPORTED_PLATFORM") =
static_cast<int>(::zvec::kDiskAnnPluginUnsupportedPlatform);
m.attr("DISKANN_PLUGIN_LIBAIO_MISSING") =
static_cast<int>(::zvec::kDiskAnnPluginLibAioMissing);
m.attr("DISKANN_PLUGIN_DLOPEN_FAILED") =
static_cast<int>(::zvec::kDiskAnnPluginDlopenFailed);
}
} // namespace
PYBIND11_MODULE(_zvec, m) {
m.doc() = "Zvec core module";
@@ -67,6 +33,5 @@ PYBIND11_MODULE(_zvec, m) {
ZVecPyConfig::Initialize(m);
ZVecPyDoc::Initialize(m);
ZVecPyCollection::Initialize(m);
InitializeDiskAnnPluginBindings(m);
}
} // namespace zvec
+1 -29
View File
@@ -1,34 +1,6 @@
{
global:
# Python module entry point(s).
PyInit_*;
# Expose the full zvec C++ namespace so the DiskAnn runtime plugin
# (libzvec_diskann_plugin.so), which is dlopen()ed with
# RTLD_NOW | RTLD_GLOBAL after the interpreter has loaded _zvec.so,
# can resolve its undefined references against this module. Without
# this, the plugin fails to load with errors like
# undefined symbol: _ZN4zvec6ailego6Logger10LEVEL_INFOE
# because the default version script hides every internal symbol.
extern "C++" {
"zvec::*";
zvec::*;
# Also export the compiler-generated helper symbols that live
# alongside symbols in the zvec namespace (guard variables for
# static locals, vtables, typeinfo, VTT, construction vtables,
# thunks). Without these, the DiskAnn plugin and _zvec.so each
# get their own copy of e.g. the guard for Factory<T>::Instance's
# static local, which causes the factory constructor to run
# twice - wiping out registrations done during _zvec.so load.
"guard variable for zvec::*";
"vtable for zvec::*";
"VTT for zvec::*";
"typeinfo for zvec::*";
"typeinfo name for zvec::*";
"construction vtable for zvec::*";
"non-virtual thunk to zvec::*";
"virtual thunk to zvec::*";
};
PyInit_*; # export only python related functions
local:
*;
};
+5 -1
View File
@@ -10,9 +10,12 @@
// 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.#pragma once
// limitations under the License.
#pragma once
#include <pybind11/pybind11.h>
#include <zvec/ailego/io/io_backend.h>
#include <zvec/db/status.h>
#include <zvec/db/type.h>
@@ -32,6 +35,7 @@ class ZVecPyTyping {
static void bind_index_types(py::module_ &m);
static void bind_metric_types(py::module_ &m);
static void bind_quantize_types(py::module_ &m);
static void bind_io_backend_types(py::module_ &m);
static void bind_status(py::module_ &m);
};
@@ -14,6 +14,7 @@
#include "python_config.h"
#include <pybind11/stl.h>
#include <zvec/ailego/io/io_backend.h>
namespace zvec {
@@ -217,6 +218,27 @@ void ZVecPyConfig::Initialize(pybind11::module_ &m) {
"get_default_jieba_dict_dir",
[]() -> std::string { return GlobalConfig::Instance().jieba_dict_dir(); },
"Read the currently registered default jieba dict directory.");
// Returns the current I/O backend type for DiskAnn async disk reads.
// Pure introspection \u2014 no side effects, no install hints.
m.def(
"io_backend_type",
[]() -> ailego::IOBackendType {
return ailego::current_io_backend_type();
},
"Returns the current I/O backend type for DiskAnn async disk reads "
"as an IOBackendType enum (zvec.typing.IOBackendType). "
"IOBackendType.LIBAIO if libaio is available, "
"IOBackendType.PREAD otherwise.");
// Returns a human-readable description of the I/O backend, including
// installation guidance for libaio when only pread is available.
m.def(
"io_backend_description",
[]() -> std::string { return ailego::current_io_backend_description(); },
"Returns a human-readable description of the current I/O backend. "
"When only pread is available, includes instructions for installing "
"libaio to enable async I/O.");
}
+21 -2
View File
@@ -24,6 +24,7 @@ void ZVecPyTyping::Initialize(pybind11::module_ &parent) {
bind_index_types(m);
bind_metric_types(m);
bind_quantize_types(m);
bind_io_backend_types(m);
bind_status(m);
}
@@ -96,11 +97,13 @@ Examples:
)pbdoc")
.value("UNDEFINED", IndexType::UNDEFINED)
.value("HNSW", IndexType::HNSW)
.value("HNSW_RABITQ", IndexType::HNSW_RABITQ)
.value("IVF", IndexType::IVF)
.value("FLAT", IndexType::FLAT)
.value("HNSW_RABITQ", IndexType::HNSW_RABITQ)
.value("DISKANN", IndexType::DISKANN)
.value("VAMANA", IndexType::VAMANA)
.value("INVERT", IndexType::INVERT);
.value("INVERT", IndexType::INVERT)
.value("FTS", IndexType::FTS);
}
void ZVecPyTyping::bind_metric_types(pybind11::module_ &m) {
@@ -137,6 +140,22 @@ Examples:
.value("RABITQ", QuantizeType::RABITQ);
}
void ZVecPyTyping::bind_io_backend_types(py::module_ &m) {
py::enum_<ailego::IOBackendType>(m, "IOBackendType", R"pbdoc(
Enumeration of supported I/O backend types for DiskAnn async disk reads.
- PREAD: Synchronous pread() \u2014 no async I/O.
- LIBAIO: libaio loaded at runtime via dlopen().
Examples:
>>> from zvec.typing import IOBackendType
>>> print(IOBackendType.LIBAIO)
IOBackendType.LIBAIO
)pbdoc")
.value("PREAD", ailego::IOBackendType::kPread)
.value("LIBAIO", ailego::IOBackendType::kLibAio);
}
void ZVecPyTyping::bind_status(py::module_ &m) {
// bind status code
py::enum_<StatusCode>(m, "StatusCode", R"pbdoc(