From 2ff47bff60dce1763a7853b0bbebf9c7cfb4540b Mon Sep 17 00:00:00 2001 From: ColinLee Date: Fri, 20 Nov 2026 15:57:37 +0800 Subject: [PATCH 1/9] feat(cpp,python): read TsFiles from object storage with a block cache Add native s3:// and oss:// read support, plus a caching layer for sources whose per-page reads are far more expensive than memory copies. - CachedRandomAccessReadFile: block-aligned LRU cache with adaptive sequential read-ahead, single-flight loads, a thread-local single-block fast path that serves most reads without the cache lock, and optional background prefetch workers that fetch contiguous block groups in parallel for native object-storage sources. - Native S3/OSS backend over libcurl (ENABLE_S3, on when libcurl is found): SigV4 signing backed by an in-tree SHA-256/HMAC, connection pooling, retries with backoff, anonymous access, s3:// and oss:// URI parsing, and region derivation from the endpoint host. - Python: TsFileReader("s3://bucket/key", s3={...}) with TSFILE_S3_* / AWS_* environment fallbacks, s3_available(), and read_cache_* config keys; opening an object releases the GIL while the HEAD request runs. - Reader: group parallel value-column decode tasks per worker instead of one task per column, count null-bitmap prefixes byte-wise, and add a two-bit control peek plus a single-run fast path to Gorilla batch decode. - Docs, unit and integration tests, and CI dependency updates for libcurl. Verified locally: 1896 C++ tests and 677 Python tests pass; clang-format and Black checks are clean. --- .github/workflows/codeql.yml | 2 +- .github/workflows/cpp-dependency-source.yml | 6 +- .github/workflows/cppcheck.yml | 2 +- .github/workflows/unit-test-cpp.yml | 4 +- .github/workflows/unit-test-python.yml | 4 +- .github/workflows/wheels.yml | 1 + cpp/CMakeLists.txt | 20 + cpp/README-zh.md | 35 + cpp/README.md | 36 + cpp/build.sh | 11 + cpp/cmake/TsFileStaticDependencies.cmake | 4 + cpp/pom.xml | 2 + cpp/src/CMakeLists.txt | 3 +- cpp/src/common/config/config.h | 14 + cpp/src/encoding/gorilla_decoder.h | 21 +- .../file/cached_random_access_read_file.cc | 560 ++++++++++++++++ cpp/src/file/cached_random_access_read_file.h | 161 +++++ cpp/src/file/s3_random_access_read_file.cc | 617 ++++++++++++++++++ cpp/src/file/s3_random_access_read_file.h | 67 ++ cpp/src/file/s3_signing.cc | 265 ++++++++ cpp/src/file/s3_signing.h | 81 +++ cpp/src/reader/aligned_chunk_reader.cc | 69 +- cpp/src/utils/CMakeLists.txt | 8 +- cpp/src/utils/sha256.cc | 221 +++++++ cpp/src/utils/sha256.h | 47 ++ .../cached_random_access_read_file_test.cc | 352 ++++++++++ .../file/s3_random_access_read_file_test.cc | 66 ++ cpp/test/file/s3_signing_test.cc | 142 ++++ cpp/test/utils/sha256_test.cc | 79 +++ python/README.md | 70 ++ python/setup.py | 11 +- python/tests/test_reader_sources.py | 90 ++- python/tests/test_s3_reader.py | 228 +++++++ python/tests/test_write_and_read.py | 35 + python/tsfile/__init__.py | 6 +- .../tsfile/python_random_access_read_file.cc | 14 + python/tsfile/python_s3_read_file.cc | 150 +++++ python/tsfile/python_s3_read_file.h | 49 ++ python/tsfile/s3.py | 158 +++++ python/tsfile/tsfile_cpp.pxd | 4 + python/tsfile/tsfile_py_cpp.pyx | 16 + python/tsfile/tsfile_reader.pyx | 94 ++- skills/tsfile/references/advanced-apis.md | 10 + skills/tsfile/references/python.md | 16 +- 44 files changed, 3805 insertions(+), 46 deletions(-) create mode 100644 cpp/src/file/cached_random_access_read_file.cc create mode 100644 cpp/src/file/cached_random_access_read_file.h create mode 100644 cpp/src/file/s3_random_access_read_file.cc create mode 100644 cpp/src/file/s3_random_access_read_file.h create mode 100644 cpp/src/file/s3_signing.cc create mode 100644 cpp/src/file/s3_signing.h create mode 100644 cpp/src/utils/sha256.cc create mode 100644 cpp/src/utils/sha256.h create mode 100644 cpp/test/file/cached_random_access_read_file_test.cc create mode 100644 cpp/test/file/s3_random_access_read_file_test.cc create mode 100644 cpp/test/file/s3_signing_test.cc create mode 100644 cpp/test/utils/sha256_test.cc create mode 100644 python/tests/test_s3_reader.py create mode 100644 python/tsfile/python_s3_read_file.cc create mode 100644 python/tsfile/python_s3_read_file.h create mode 100644 python/tsfile/s3.py diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index cb96c06583..d48ec3d7a8 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -108,7 +108,7 @@ jobs: - name: Install native dependencies if: matrix.language == 'go' - run: sudo apt-get update && sudo apt-get install -y uuid-dev + run: sudo apt-get update && sudo apt-get install -y uuid-dev libcurl4-openssl-dev # The Go bridge needs headers and libtsfile under cpp/target/build. - name: Build native library for Go diff --git a/.github/workflows/cpp-dependency-source.yml b/.github/workflows/cpp-dependency-source.yml index b3facf1374..2988676a0e 100644 --- a/.github/workflows/cpp-dependency-source.yml +++ b/.github/workflows/cpp-dependency-source.yml @@ -193,7 +193,7 @@ jobs: - name: Install bundled build prerequisites run: | sudo apt-get update - sudo apt-get install -y uuid-dev + sudo apt-get install -y uuid-dev libcurl4-openssl-dev - name: Configure all dependencies from verified archives shell: bash @@ -240,7 +240,7 @@ jobs: - name: Install offline build prerequisites run: | sudo apt-get update - sudo apt-get install -y uuid-dev + sudo apt-get install -y uuid-dev libcurl4-openssl-dev - name: Configure all dependencies offline shell: bash @@ -280,7 +280,7 @@ jobs: - name: Install SDK test prerequisites run: | sudo apt-get update - sudo apt-get install -y uuid-dev liblz4-dev pkg-config ninja-build + sudo apt-get install -y uuid-dev libcurl4-openssl-dev liblz4-dev pkg-config ninja-build - name: Test installed CMake and pkg-config consumers and CLI RPATH env: diff --git a/.github/workflows/cppcheck.yml b/.github/workflows/cppcheck.yml index 53453d6d35..8744f6003e 100644 --- a/.github/workflows/cppcheck.yml +++ b/.github/workflows/cppcheck.yml @@ -107,7 +107,7 @@ jobs: - name: Install dependencies run: | sudo apt-get update - sudo apt-get install -y uuid-dev + sudo apt-get install -y uuid-dev libcurl4-openssl-dev - name: Install Cppcheck shell: bash diff --git a/.github/workflows/unit-test-cpp.yml b/.github/workflows/unit-test-cpp.yml index 3df95cb972..86366a47ad 100644 --- a/.github/workflows/unit-test-cpp.yml +++ b/.github/workflows/unit-test-cpp.yml @@ -137,9 +137,9 @@ jobs: if [[ "$RUNNER_OS" == "Linux" ]]; then if command -v apt-get >/dev/null 2>&1; then sudo apt-get update - sudo apt-get install -y uuid-dev + sudo apt-get install -y uuid-dev libcurl4-openssl-dev elif command -v yum >/dev/null 2>&1; then - sudo yum install -y libuuid-devel + sudo yum install -y libuuid-devel libcurl-devel fi python -m venv "$RUNNER_TEMP/clang-format" "$RUNNER_TEMP/clang-format/bin/python" -m pip install --only-binary=:all: clang-format==17.0.6 diff --git a/.github/workflows/unit-test-python.yml b/.github/workflows/unit-test-python.yml index c7ad7528f6..98e71a4ce5 100644 --- a/.github/workflows/unit-test-python.yml +++ b/.github/workflows/unit-test-python.yml @@ -112,7 +112,7 @@ jobs: "$RUNNER_TEMP/clang-format/bin/clang-format" --version echo "$RUNNER_TEMP/clang-format/bin" >> "$GITHUB_PATH" sudo apt-get update - sudo apt-get install -y uuid-dev + sudo apt-get install -y uuid-dev libcurl4-openssl-dev elif [[ "$RUNNER_OS" == "Windows" ]]; then choco install llvm --version 17.0.6 --force else @@ -176,7 +176,7 @@ jobs: shell: bash run: | sudo apt-get update - sudo apt-get install -y uuid-dev binutils + sudo apt-get install -y uuid-dev libcurl4-openssl-dev binutils - name: Build with ASan/LSan shell: bash diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 124e9cb85e..5830fad187 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -126,6 +126,7 @@ jobs: CIBW_MANYLINUX_X86_64_IMAGE: manylinux_2_28 CIBW_MANYLINUX_AARCH64_IMAGE: manylinux_2_28 CIBW_BEFORE_ALL_LINUX: >- + (dnf install -y libcurl-devel || yum install -y libcurl-devel) && bash packaging/scripts/build_python_sdk.sh ${{ matrix.name }} ${{ steps.version.outputs.python_version }} CIBW_BEFORE_ALL_MACOS: >- diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index d434418d69..85b6e983ac 100755 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -383,6 +383,23 @@ if (ENABLE_SIMD) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/SIMDeDependency.cmake) endif() +# Native S3/OSS object-storage reads need an HTTP client with TLS. libcurl is +# optional: when it is missing the Python and C++ APIs still exist but report +# that native object-storage support was not built. +option(ENABLE_S3 "Enable native S3/OSS reads when libcurl is available" ON) +message("cmake using: ENABLE_S3=${ENABLE_S3}") +if (ENABLE_S3) + find_package(CURL QUIET) + if (CURL_FOUND) + message(STATUS "native S3/OSS reads: enabled (libcurl ${CURL_VERSION_STRING})") + add_definitions(-DTSFILE_ENABLE_S3) + else () + message(STATUS "native S3/OSS reads: disabled (libcurl not found)") + endif () +else () + message(STATUS "native S3/OSS reads: disabled by ENABLE_S3=OFF") +endif () + # All libs will be stored here, including libtsfile, compress-encoding lib. set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) @@ -446,6 +463,9 @@ endif () if (ENABLE_SIMD) list(APPEND _TSFILE_PROJECT_DEPENDENCIES TsFile::SIMDe) endif () +if (ENABLE_S3 AND CURL_FOUND) + list(APPEND _TSFILE_PROJECT_DEPENDENCIES CURL::libcurl) +endif () if (NOT "${_TSFILE_PROJECT_DEPENDENCIES}" STREQUAL "") get_property(_TSFILE_BASE_LINK_LIBRARIES diff --git a/cpp/README-zh.md b/cpp/README-zh.md index 8b127e07bf..cc5a527534 100644 --- a/cpp/README-zh.md +++ b/cpp/README-zh.md @@ -207,6 +207,41 @@ C API 可通过 `tsfile_set_file_read_backend(TSFILE_READ_BACKEND_*)` 设置相 成功后会关闭原始文件描述符,映射本身会保持有效直至 reader 关闭。映射 reader 打开期间不得修改或截断文件。 +### 对象存储读取后端 + +当以 `ENABLE_S3=ON`(默认)构建且 configure 阶段找到 libcurl 时,SDK 可以直接 +从 S3 兼容对象存储(AWS S3、阿里云 OSS、MinIO 等)读取 TsFile,内部通过带 +SigV4 签名的 HTTP Range 请求完成。可用 `s3_support_available()` 判断当前构建 +是否包含该能力。 + +```cpp +#include "file/cached_random_access_read_file.h" +#include "file/s3_random_access_read_file.h" +#include "reader/tsfile_reader.h" + +storage::S3Options options; +options.endpoint = "https://oss-cn-beijing.aliyuncs.com"; // 或 AWS/MinIO +options.bucket = "bucket"; +options.key = "path/example.tsfile"; +options.access_key_id = "..."; // 两者都为空表示匿名访问 +options.secret_access_key = "..."; +options.region = "cn-beijing"; // 为空时从 endpoint 推导 +std::string error; +std::unique_ptr source = + storage::open_s3_read_file(options, &error); + +storage::ReadCacheOptions cache; +cache.prefetch_threads = 4; // 后台按连续块组并发拉取 +source = storage::make_cached_read_file(std::move(source), cache); + +storage::TsFileReader reader; +reader.open(std::move(source)); +``` + +签名和 HTTP 错误通过 `error` 返回,`read()` 会按 `max_retries` 重试瞬时失败。 +块缓存层对远程源强烈建议启用;块大小、缓存容量与预读量见 +`ReadCacheOptions`。 + --- ## 使用 TsFile diff --git a/cpp/README.md b/cpp/README.md index 30ca0461eb..32b95d792b 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -393,6 +393,42 @@ not be modified or truncated while a mapped reader is open. After a mapping is created successfully, the original file descriptor is closed; the mapping itself remains valid until the reader closes. +### Object Storage Read Backend + +With `ENABLE_S3=ON` (the default) and libcurl available at configure time, the +SDK can read a TsFile directly from S3-compatible object storage (AWS S3, +Alibaba Cloud OSS, MinIO, ...) with signed HTTP range requests. Check +`s3_support_available()` when the build environment is unknown. + +```cpp +#include "file/cached_random_access_read_file.h" +#include "file/s3_random_access_read_file.h" +#include "reader/tsfile_reader.h" + +storage::S3Options options; +options.endpoint = "https://oss-cn-beijing.aliyuncs.com"; // or AWS/MinIO +options.bucket = "bucket"; +options.key = "path/example.tsfile"; +options.access_key_id = "..."; // empty pair => anonymous access +options.secret_access_key = "..."; +options.region = "cn-beijing"; // derived from the endpoint when empty +std::string error; +std::unique_ptr source = + storage::open_s3_read_file(options, &error); + +storage::ReadCacheOptions cache; +cache.prefetch_threads = 4; // background grouped range GETs +source = storage::make_cached_read_file(std::move(source), cache); + +storage::TsFileReader reader; +reader.open(std::move(source)); +``` + +Signing failures and HTTP problems surface through `error`, and `read()` +retries transient failures `max_retries` times. The block cache wrapper is +optional but strongly recommended for remote sources; see +`ReadCacheOptions` for block size, capacity and read-ahead. + ## Use TsFile You can find examples on how to read and write data in `demo_read.cpp` and `demo_write.cpp` located under `./examples/cpp_examples`. There are also examples under `./examples/c_examples` on how to use a C-style API to read and write data in a C environment. The examples will be built automatically when you run the main build command. diff --git a/cpp/build.sh b/cpp/build.sh index f57c43c612..90b277c00a 100644 --- a/cpp/build.sh +++ b/cpp/build.sh @@ -36,6 +36,7 @@ enable_lz4=ON enable_lzokay=ON enable_zlib=ON enable_zstd=ON +enable_s3=ON build_jobs="" # Keep the default build compatible with CMake 3.11-era industrial toolchains. # Bundled XZ Utils requires CMake 3.20+, so LZMA2 must be enabled explicitly. @@ -92,6 +93,11 @@ Options: --disable-zlib --enable-zstd= --disable-zstd + --enable-s3= + Enable native S3/OSS reads when libcurl is found + (default: ON; the feature stays off when libcurl is + unavailable). + --disable-s3 --enable-lzma2= Enable LZMA2 explicitly (default: OFF; bundled XZ requires CMake 3.20 or newer). @@ -177,6 +183,8 @@ parse_options() enable_zlib=$(get_key_value "$1");; --enable-zstd=*) enable_zstd=$(get_key_value "$1");; + --enable-s3=*) + enable_s3=$(get_key_value "$1");; --enable-lzma2=*) enable_lzma2=$(get_key_value "$1");; --disable-antlr4) @@ -191,6 +199,8 @@ parse_options() enable_zlib=OFF;; --disable-zstd) enable_zstd=OFF;; + --disable-s3) + enable_s3=OFF;; --disable-lzma2) enable_lzma2=OFF;; -h | --help) @@ -270,6 +280,7 @@ cmake ../../ \ -DENABLE_LZOKAY=$enable_lzokay \ -DENABLE_ZLIB=$enable_zlib \ -DENABLE_ZSTD=$enable_zstd \ + -DENABLE_S3=$enable_s3 \ -DENABLE_LZMA2=$enable_lzma2 VERBOSE=1 make -j"${build_jobs}" diff --git a/cpp/cmake/TsFileStaticDependencies.cmake b/cpp/cmake/TsFileStaticDependencies.cmake index db6acfbb26..735aa5bf1a 100644 --- a/cpp/cmake/TsFileStaticDependencies.cmake +++ b/cpp/cmake/TsFileStaticDependencies.cmake @@ -153,6 +153,10 @@ tsfile_install_static_dependency(LZOKAY "${TSFILE_LZOKAY_SOURCE}" lzokay::lzokay "find_dependency(lzokay ${TSFILE_LZOKAY_MIN_VERSION} CONFIG)") tsfile_install_static_dependency(ZLIB "${TSFILE_ZLIB_SOURCE}" ZLIB::ZLIB "find_dependency(ZLIB ${TSFILE_ZLIB_MIN_VERSION})") +if (ENABLE_S3 AND CURL_FOUND) + tsfile_install_static_dependency(CURL "SYSTEM" CURL::libcurl + "find_dependency(CURL)") +endif () tsfile_install_static_dependency(ZSTD "${TSFILE_ZSTD_SOURCE}" "${TSFILE_ZSTD_SYSTEM_TARGET}" "find_dependency(zstd ${TSFILE_ZSTD_MIN_VERSION} CONFIG)") diff --git a/cpp/pom.xml b/cpp/pom.xml index ab967160cd..bf5bc2ad5e 100644 --- a/cpp/pom.xml +++ b/cpp/pom.xml @@ -59,6 +59,7 @@ ON ON ON + ON