diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index cb96c0658..d48ec3d7a 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 b3facf137..2988676a0 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 53453d6d3..8744f6003 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 3df95cb97..86366a47a 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 c7ad7528f..98e71a4ce 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/.gitignore b/.gitignore index 3c5fe07ea..3147e98df 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ python/tsfile/*.h python/tsfile/*.cpp python/tsfile/**/*.cpp python/tsfile/**/*so* +!python/tsfile/python_reader_source.h python/data python/venv/* python/tests/__pycache__/* diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index d434418d6..dae05ae38 100755 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -383,6 +383,30 @@ 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. find_package(CURL) only +# defines the CURL::libcurl target on CMake 3.12 and newer, so the CMake 3.11 +# baseline keeps the feature disabled rather than relying on a target that the +# link step cannot resolve. +option(ENABLE_S3 "Enable native S3/OSS reads when libcurl is available" ON) +message("cmake using: ENABLE_S3=${ENABLE_S3}") +set(TSFILE_S3_ENABLED OFF) +if (ENABLE_S3) + find_package(CURL 7.42 QUIET) + if (CURL_FOUND AND TARGET CURL::libcurl) + message(STATUS "native S3/OSS reads: enabled (libcurl ${CURL_VERSION_STRING})") + add_definitions(-DTSFILE_ENABLE_S3) + set(TSFILE_S3_ENABLED ON) + elseif (CURL_FOUND) + message(STATUS "native S3/OSS reads: disabled (CMake does not define CURL::libcurl)") + 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 +470,9 @@ endif () if (ENABLE_SIMD) list(APPEND _TSFILE_PROJECT_DEPENDENCIES TsFile::SIMDe) endif () +if (TSFILE_S3_ENABLED) + list(APPEND _TSFILE_PROJECT_DEPENDENCIES TsFile::CURL) +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 8b127e07b..83e615ec0 100644 --- a/cpp/README-zh.md +++ b/cpp/README-zh.md @@ -207,6 +207,48 @@ C API 可通过 `tsfile_set_file_read_backend(TSFILE_READ_BACKEND_*)` 设置相 成功后会关闭原始文件描述符,映射本身会保持有效直至 reader 关闭。映射 reader 打开期间不得修改或截断文件。 +### 对象存储读取后端 + +当以 `ENABLE_S3=ON`(默认)构建且 configure 阶段找到 libcurl 7.42 或更新版本时,SDK 可以直接 +从 S3 兼容对象存储(AWS S3、阿里云 OSS、MinIO 等)读取 TsFile,内部通过带 +SigV4 签名的 HTTP Range 请求完成。可用 `s3_support_available()` 判断当前构建 +是否包含该能力。打开对象时需要强 ETag,每次 Range GET 都会携带对应的 +`If-Match`。对象被覆盖后,请重新打开 reader 以读取新版本。缓存容量同时计入驻留块、正在传输的数据和活跃读取持有的块。预算用尽时,新的缓存未命中会等待空间。 + +```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://s3.oss-cn-beijing.aliyuncs.com"; // 或 AWS/MinIO +options.path_style = false; // 阿里云 OSS 使用虚拟主机寻址 +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)); +``` + +每次范围读取都使用打开时取得的强 ETag。对于 HTTP 206,读取器会核对起止偏移和对象总大小。 +若服务返回 HTTP 200,读取器跳过目标区间之前的字节,取得所需数据后即可结束传输。 +使用 `ENABLE_THREADS=OFF` 构建时,预读同步执行。顺序预读按配置增长(最多额外 64 块),并受缓存容量和单次传输上限约束。 +使用自定义端点或 S3 传输加速端点进行签名时,请显式指定存储桶所在区域。HTTP 408 和 429 可重试,其他 4xx 响应会立即结束该请求。 + +打开阶段的签名和 HTTP 错误通过 `error` 返回;`read()` 会按 `max_retries` +重试瞬时失败,重试耗尽后返回读取错误。块缓存层对远程源强烈建议启用;块大小、 +缓存容量与预读量见 `ReadCacheOptions`。 + --- ## 使用 TsFile diff --git a/cpp/README.md b/cpp/README.md index 30ca0461e..b5b44b002 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -393,6 +393,53 @@ 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 7.42 or newer 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://s3.oss-cn-beijing.aliyuncs.com"; // or AWS/MinIO +options.path_style = false; // Alibaba OSS uses virtual-hosted addressing +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)); +``` + +Opening requires a strong ETag. Every range GET uses it with `If-Match`. +A 206 response must identify the requested offsets and object size in +`Content-Range`. For a 200 response the reader consumes the prefix through the +requested range and ends the transfer there. Single-thread builds perform +read-ahead synchronously. Reopen the reader to read a replacement object. The cache capacity includes in-flight range buffers and blocks held by active reads. +When the budget is full, a new cache miss waits for space. Sequential read-ahead grows up to the +configured limit (0–64 extra blocks), subject to capacity and transfer-size limits. + +Opening reports signing failures and HTTP problems through `error`, and +`read()` retries transient failures `max_retries` times before returning a +read error. The block cache wrapper is optional but strongly recommended for +remote sources. Set the bucket's signing region explicitly for custom and S3 Transfer Acceleration endpoints. HTTP 408 and 429 are +retryable; other 4xx responses end the request immediately. 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 f57c43c61..90b277c00 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 db6acfbb2..36e92b5aa 100644 --- a/cpp/cmake/TsFileStaticDependencies.cmake +++ b/cpp/cmake/TsFileStaticDependencies.cmake @@ -153,6 +153,16 @@ 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 (TSFILE_S3_ENABLED) + tsfile_install_static_dependency(CURL "SYSTEM" CURL::libcurl + [=[find_dependency(CURL 7.42) +if (NOT TARGET CURL::libcurl) + add_library(CURL::libcurl INTERFACE IMPORTED) + set_target_properties(CURL::libcurl PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}" + INTERFACE_LINK_LIBRARIES "${CURL_LIBRARIES}") +endif ()]=]) +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/cmake/tests/projects/InstalledConsumer/main.cc b/cpp/cmake/tests/projects/InstalledConsumer/main.cc index 2d265d429..819cddb0d 100644 --- a/cpp/cmake/tests/projects/InstalledConsumer/main.cc +++ b/cpp/cmake/tests/projects/InstalledConsumer/main.cc @@ -17,11 +17,17 @@ * under the License. */ #include +#include #include #include #include int main() { if (set_global_time_encoding(TS_ENCODING_TS_2DIFF) != 0) return 1; - return get_global_time_encoding() == TS_ENCODING_TS_2DIFF ? 0 : 1; + if (get_global_time_encoding() != TS_ENCODING_TS_2DIFF) return 1; + // Referencing the object-storage entry point pulls its translation unit + // out of a static archive, so an installed static package must propagate + // the libcurl link dependency (TsFile::Static_CURL). + (void)storage::s3_support_available(); + return 0; } diff --git a/cpp/pom.xml b/cpp/pom.xml index ab967160c..bf5bc2ad5 100644 --- a/cpp/pom.xml +++ b/cpp/pom.xml @@ -59,6 +59,7 @@ ON ON ON + ON