wgtmac commented on code in PR #1167: URL: https://github.com/apache/iceberg-cpp/pull/1167#discussion_r4715279457
########## src/iceberg/data/parallel_scan_reader.cc: ########## @@ -0,0 +1,592 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software 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. + */ + +#include "iceberg/data/parallel_scan_reader.h" + +#include <algorithm> +#include <chrono> +#include <condition_variable> +#include <cstdint> +#include <deque> +#include <exception> +#include <format> +#include <memory> +#include <mutex> +#include <optional> +#include <string> +#include <string_view> +#include <thread> +#include <utility> +#include <vector> + +#include <nanoarrow/nanoarrow.h> + +#include "iceberg/arrow_c_data_guard_internal.h" +#include "iceberg/arrow_c_data_util_internal.h" +#include "iceberg/data/arrow_batch_sizer_internal.h" +#include "iceberg/data/parallel_scan_reader_internal.h" +#include "iceberg/file_reader.h" +#include "iceberg/manifest/manifest_entry.h" +#include "iceberg/nanoarrow_status_internal.h" +#include "iceberg/schema.h" +#include "iceberg/schema_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace internal { + +ParallelScanMetricsSnapshot ParallelScanMetrics::Snapshot() const { + std::lock_guard lock(mutex_); + return snapshot_; +} + +namespace { + +// Names a task in error messages, e.g. "data file s3://b/f.parquet [0, 1024)". +std::string DescribeTask(const FileScanTask& task) { + const auto& data_file = task.data_file(); + if (data_file == nullptr) { + return "task without a data file"; + } + if (!task.is_split()) { + return std::format("data file {}", data_file->file_path); + } + return std::format("data file {} [{}, {})", data_file->file_path, task.start(), + task.start() + task.length()); +} + +Error WithContext(const Error& error, std::string_view context) { + return Error{.kind = error.kind, + .message = std::format("{}: {}", context, error.message)}; +} + +std::string_view NullSafe(const char* value) { + return value == nullptr ? std::string_view{} : std::string_view{value}; +} + +// Compares the physical layout of two Arrow schemas. Field names, formats, +// nullability, children, and dictionaries must match; metadata is ignored and the +// root name is not compared. +Status CheckSameLayout(const ArrowSchema& expected, const ArrowSchema& actual, + const std::string& path, bool compare_name) { + if (NullSafe(expected.format) != NullSafe(actual.format)) { + return InvalidSchema("Field {} has Arrow format '{}', expected '{}'", path, + NullSafe(actual.format), NullSafe(expected.format)); + } + if (compare_name && NullSafe(expected.name) != NullSafe(actual.name)) { + return InvalidSchema("Field {} is named '{}', expected '{}'", path, + NullSafe(actual.name), NullSafe(expected.name)); + } + if ((expected.flags & ARROW_FLAG_NULLABLE) != (actual.flags & ARROW_FLAG_NULLABLE)) { + return InvalidSchema("Field {} nullability differs from the projected schema", path); + } + if (expected.n_children != actual.n_children) { + return InvalidSchema("Field {} has {} children, expected {}", path, actual.n_children, + expected.n_children); + } + for (int64_t index = 0; index < expected.n_children; ++index) { + const ArrowSchema* expected_child = expected.children[index]; + const ArrowSchema* actual_child = actual.children[index]; + if (actual_child == nullptr || expected_child == nullptr) { + return InvalidSchema("Field {} has a null child", path); + } + std::string child_path = std::format("{}.{}", path, NullSafe(expected_child->name)); + ICEBERG_RETURN_UNEXPECTED( + CheckSameLayout(*expected_child, *actual_child, child_path, true)); + } + if ((expected.dictionary == nullptr) != (actual.dictionary == nullptr)) { + return InvalidSchema("Field {} dictionary encoding differs from the projected schema", + path); + } + if (expected.dictionary != nullptr) { + ICEBERG_RETURN_UNEXPECTED(CheckSameLayout(*expected.dictionary, *actual.dictionary, + path + ".dictionary", false)); + } + return {}; +} + +// Releases an ArrowArrayStream on scope exit. +class StreamGuard { + public: + explicit StreamGuard(ArrowArrayStream* stream) : stream_(stream) {} + ~StreamGuard() { + if (stream_->release != nullptr) { + stream_->release(stream_); + } + } + StreamGuard(const StreamGuard&) = delete; + StreamGuard& operator=(const StreamGuard&) = delete; + + private: + ArrowArrayStream* stream_; +}; + +void ReleaseArray(ArrowArray* array) { + if (array->release != nullptr) { + array->release(array); + } +} + +struct QueuedBatch { + ArrowArray array; + int64_t charge; +}; + +void ReleaseAll(std::deque<QueuedBatch>& batches) { + for (auto& batch : batches) { + ReleaseArray(&batch.array); + } + batches.clear(); +} + +// Shared state of one parallel read. All fields below `mutex_` are guarded by it. +// No planning or reader callback, Arrow release callback, thread join, or group +// stream destructor runs while `mutex_` is held. +class ParallelScan { + public: + ParallelScan(FileScanTaskGroupStreamPtr groups, ArrowSchema output_schema, + int64_t max_queued_batches, int64_t max_queued_bytes, + std::shared_ptr<ParallelScanMetrics> metrics) + : groups_(std::move(groups)), + output_schema_(output_schema), + max_queued_batches_(max_queued_batches), + max_queued_bytes_(max_queued_bytes), + metrics_(std::move(metrics)) {} + + ~ParallelScan() { + Shutdown(); + if (output_schema_.release != nullptr) { + output_schema_.release(&output_schema_); + } + } + + ParallelScan(const ParallelScan&) = delete; + ParallelScan& operator=(const ParallelScan&) = delete; + + Status Start(const FileScanTaskReader::Options& reader_options, int32_t worker_count) { + std::vector<std::unique_ptr<FileScanTaskReader>> readers; + std::vector<std::unique_ptr<ArrowBatchSizer>> sizers; + readers.reserve(static_cast<size_t>(worker_count)); + sizers.reserve(static_cast<size_t>(worker_count)); + for (int32_t index = 0; index < worker_count; ++index) { + ICEBERG_ASSIGN_OR_RAISE(auto reader, FileScanTaskReader::Make(reader_options)); Review Comment: These readers share one `NameMapping`. If two workers open files without field IDs, both can build the same name index at once. Can we make that first lookup thread-safe? ########## src/iceberg/data/parallel_scan_reader.cc: ########## @@ -0,0 +1,592 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software 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. + */ + +#include "iceberg/data/parallel_scan_reader.h" + +#include <algorithm> +#include <chrono> +#include <condition_variable> +#include <cstdint> +#include <deque> +#include <exception> +#include <format> +#include <memory> +#include <mutex> +#include <optional> +#include <string> +#include <string_view> +#include <thread> +#include <utility> +#include <vector> + +#include <nanoarrow/nanoarrow.h> + +#include "iceberg/arrow_c_data_guard_internal.h" +#include "iceberg/arrow_c_data_util_internal.h" +#include "iceberg/data/arrow_batch_sizer_internal.h" +#include "iceberg/data/parallel_scan_reader_internal.h" +#include "iceberg/file_reader.h" +#include "iceberg/manifest/manifest_entry.h" +#include "iceberg/nanoarrow_status_internal.h" +#include "iceberg/schema.h" +#include "iceberg/schema_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace internal { + +ParallelScanMetricsSnapshot ParallelScanMetrics::Snapshot() const { + std::lock_guard lock(mutex_); + return snapshot_; +} + +namespace { + +// Names a task in error messages, e.g. "data file s3://b/f.parquet [0, 1024)". +std::string DescribeTask(const FileScanTask& task) { + const auto& data_file = task.data_file(); + if (data_file == nullptr) { + return "task without a data file"; + } + if (!task.is_split()) { + return std::format("data file {}", data_file->file_path); + } + return std::format("data file {} [{}, {})", data_file->file_path, task.start(), + task.start() + task.length()); +} + +Error WithContext(const Error& error, std::string_view context) { + return Error{.kind = error.kind, + .message = std::format("{}: {}", context, error.message)}; +} + +std::string_view NullSafe(const char* value) { + return value == nullptr ? std::string_view{} : std::string_view{value}; +} + +// Compares the physical layout of two Arrow schemas. Field names, formats, +// nullability, children, and dictionaries must match; metadata is ignored and the +// root name is not compared. +Status CheckSameLayout(const ArrowSchema& expected, const ArrowSchema& actual, + const std::string& path, bool compare_name) { + if (NullSafe(expected.format) != NullSafe(actual.format)) { + return InvalidSchema("Field {} has Arrow format '{}', expected '{}'", path, + NullSafe(actual.format), NullSafe(expected.format)); + } + if (compare_name && NullSafe(expected.name) != NullSafe(actual.name)) { + return InvalidSchema("Field {} is named '{}', expected '{}'", path, + NullSafe(actual.name), NullSafe(expected.name)); + } + if ((expected.flags & ARROW_FLAG_NULLABLE) != (actual.flags & ARROW_FLAG_NULLABLE)) { + return InvalidSchema("Field {} nullability differs from the projected schema", path); + } + if (expected.n_children != actual.n_children) { + return InvalidSchema("Field {} has {} children, expected {}", path, actual.n_children, + expected.n_children); + } + for (int64_t index = 0; index < expected.n_children; ++index) { + const ArrowSchema* expected_child = expected.children[index]; + const ArrowSchema* actual_child = actual.children[index]; + if (actual_child == nullptr || expected_child == nullptr) { + return InvalidSchema("Field {} has a null child", path); + } + std::string child_path = std::format("{}.{}", path, NullSafe(expected_child->name)); + ICEBERG_RETURN_UNEXPECTED( + CheckSameLayout(*expected_child, *actual_child, child_path, true)); + } + if ((expected.dictionary == nullptr) != (actual.dictionary == nullptr)) { + return InvalidSchema("Field {} dictionary encoding differs from the projected schema", + path); + } + if (expected.dictionary != nullptr) { + ICEBERG_RETURN_UNEXPECTED(CheckSameLayout(*expected.dictionary, *actual.dictionary, + path + ".dictionary", false)); + } + return {}; +} + +// Releases an ArrowArrayStream on scope exit. +class StreamGuard { + public: + explicit StreamGuard(ArrowArrayStream* stream) : stream_(stream) {} + ~StreamGuard() { + if (stream_->release != nullptr) { + stream_->release(stream_); + } + } + StreamGuard(const StreamGuard&) = delete; + StreamGuard& operator=(const StreamGuard&) = delete; + + private: + ArrowArrayStream* stream_; +}; + +void ReleaseArray(ArrowArray* array) { + if (array->release != nullptr) { + array->release(array); + } +} + +struct QueuedBatch { + ArrowArray array; + int64_t charge; +}; + +void ReleaseAll(std::deque<QueuedBatch>& batches) { + for (auto& batch : batches) { + ReleaseArray(&batch.array); + } + batches.clear(); +} + +// Shared state of one parallel read. All fields below `mutex_` are guarded by it. +// No planning or reader callback, Arrow release callback, thread join, or group +// stream destructor runs while `mutex_` is held. +class ParallelScan { + public: + ParallelScan(FileScanTaskGroupStreamPtr groups, ArrowSchema output_schema, + int64_t max_queued_batches, int64_t max_queued_bytes, + std::shared_ptr<ParallelScanMetrics> metrics) + : groups_(std::move(groups)), + output_schema_(output_schema), + max_queued_batches_(max_queued_batches), + max_queued_bytes_(max_queued_bytes), + metrics_(std::move(metrics)) {} + + ~ParallelScan() { + Shutdown(); + if (output_schema_.release != nullptr) { + output_schema_.release(&output_schema_); + } + } + + ParallelScan(const ParallelScan&) = delete; + ParallelScan& operator=(const ParallelScan&) = delete; + + Status Start(const FileScanTaskReader::Options& reader_options, int32_t worker_count) { + std::vector<std::unique_ptr<FileScanTaskReader>> readers; + std::vector<std::unique_ptr<ArrowBatchSizer>> sizers; + readers.reserve(static_cast<size_t>(worker_count)); + sizers.reserve(static_cast<size_t>(worker_count)); + for (int32_t index = 0; index < worker_count; ++index) { + ICEBERG_ASSIGN_OR_RAISE(auto reader, FileScanTaskReader::Make(reader_options)); + ICEBERG_ASSIGN_OR_RAISE(auto sizer, ArrowBatchSizer::Make(output_schema_)); + readers.push_back(std::move(reader)); + sizers.push_back(std::move(sizer)); + } + + { + std::lock_guard lock(mutex_); + active_workers_ = worker_count; + } + workers_.reserve(static_cast<size_t>(worker_count)); + for (int32_t index = 0; index < worker_count; ++index) { + try { + workers_.emplace_back([this, reader = std::move(readers[index]), + sizer = std::move(sizers[index])]() mutable { + WorkerMain(*reader, *sizer); + }); + } catch (const std::exception& e) { + // Threads that never started will not decrement the active count. + { + std::lock_guard lock(mutex_); + active_workers_ -= worker_count - index; + } + Shutdown(); + return UnknownError("Failed to start parallel scan worker: {}", e.what()); + } + } + return {}; + } + + // Consumer side of get_next. Not called concurrently with itself or Shutdown(). + Result<std::optional<ArrowArray>> Next() { + std::unique_lock lock(mutex_); + consumer_cv_.wait(lock, [this] { + return error_.has_value() || !queue_.empty() || active_workers_ == 0; + }); + if (error_.has_value()) { + return unexpected(*error_); + } + if (queue_.empty()) { + return std::nullopt; + } + QueuedBatch batch = queue_.front(); + queue_.pop_front(); + queued_bytes_ -= batch.charge; + lock.unlock(); + // Producers are admitted in ticket order, and a condition variable cannot wake + // one specific waiter, so wake them all and let the head ticket re-check. At + // most worker_count threads wait here. + producer_cv_.notify_all(); + return batch.array; + } + + Result<ArrowSchema> Schema() const { + ArrowSchema schema{}; + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowSchemaDeepCopy(&output_schema_, &schema)); + return schema; + } + + // Stops new work, joins workers, and releases every batch that was not returned. + // Idempotent. + void Shutdown() { + std::deque<QueuedBatch> drained; + { + std::lock_guard lock(mutex_); + stopped_ = true; + drained.swap(queue_); + queued_bytes_ = 0; + } + planning_cv_.notify_all(); + producer_cv_.notify_all(); + consumer_cv_.notify_all(); + ReleaseAll(drained); + + for (auto& worker : workers_) { + if (worker.joinable()) { + worker.join(); + } + } + workers_.clear(); + + // Workers have exited, so nothing else can enqueue or plan. + { + std::lock_guard lock(mutex_); + drained.swap(queue_); + } + ReleaseAll(drained); + groups_.reset(); + } + + private: + void WorkerMain(FileScanTaskReader& reader, ArrowBatchSizer& sizer) { + Status status; + try { + status = RunWorker(reader, sizer); + } catch (const std::exception& e) { + status = UnknownError("Parallel scan worker failed: {}", e.what()); + } catch (...) { + status = UnknownError("Parallel scan worker failed with an unknown exception"); + } + if (!status.has_value()) { + Fail(status.error()); + } + { + std::lock_guard lock(mutex_); + --active_workers_; + } + consumer_cv_.notify_all(); + } + + Status RunWorker(FileScanTaskReader& reader, ArrowBatchSizer& sizer) { + while (true) { + ICEBERG_ASSIGN_OR_RAISE(auto group, ClaimGroup()); + if (group == nullptr) { + return {}; + } + for (const auto& task : group->tasks()) { + if (IsStopped()) { + return {}; + } + if (task == nullptr) { + return InvalidArgument("Task group contains a null task"); + } + ICEBERG_RETURN_UNEXPECTED(ReadTask(reader, sizer, *task)); + } + } + } + + // Returns the next group, or nullptr when planning is exhausted or the read stopped. + // Only one worker calls the group stream at a time, outside all locks. + Result<std::shared_ptr<FileScanTaskGroup>> ClaimGroup() { + { + std::unique_lock lock(mutex_); + planning_cv_.wait(lock, [this] { return stopped_ || !planning_in_progress_; }); + if (stopped_ || planning_done_) { + return nullptr; + } + planning_in_progress_ = true; + } + + Result<std::optional<std::shared_ptr<FileScanTaskGroup>>> next = std::nullopt; + try { + next = groups_->Next(); + } catch (const std::exception& e) { + next = UnknownError("Planning task groups failed: {}", e.what()); + } catch (...) { + next = UnknownError("Planning task groups failed with an unknown exception"); + } + + const bool finished = !next.has_value() || !next.value().has_value(); + if (finished) { + // This worker still holds the planning token, so no other worker uses the + // stream. Destroy it now, outside locks, so metrics reporting runs at EOF. + groups_.reset(); + } + + { + std::lock_guard lock(mutex_); + planning_in_progress_ = false; + if (finished) { + planning_done_ = true; + } + } + planning_cv_.notify_all(); + + if (!next.has_value()) { + return unexpected(WithContext(next.error(), "Failed to plan task groups")); + } + if (!next.value().has_value()) { + return nullptr; + } + auto group = std::move(next.value().value()); + if (group == nullptr) { + return InvalidArgument("Task group stream returned a null group"); + } + if (metrics_ != nullptr) { + metrics_->Update([](auto& snapshot) { ++snapshot.groups_claimed; }); + } + return group; + } + + Status ReadTask(FileScanTaskReader& reader, ArrowBatchSizer& sizer, + const FileScanTask& task) { + const std::string context = DescribeTask(task); + auto opened = reader.Open(task); + if (!opened.has_value()) { + return unexpected(WithContext(opened.error(), "Failed to open " + context)); + } + ArrowArrayStream stream = std::move(opened.value()); + StreamGuard stream_guard(&stream); + if (metrics_ != nullptr) { + metrics_->Update([](auto& snapshot) { ++snapshot.tasks_opened; }); + } + + auto last_error = [&stream]() { + const char* message = stream.get_last_error(&stream); + return message == nullptr ? std::string("unknown error") : std::string(message); + }; + + ArrowSchema task_schema{}; + if (int code = stream.get_schema(&stream, &task_schema); code != 0) { + return IOError("Failed to read the schema of {} (code {}): {}", context, code, + last_error()); + } + internal::ArrowSchemaGuard task_schema_guard(&task_schema); + if (auto status = CheckSameLayout(output_schema_, task_schema, "root", false); + !status.has_value()) { + return unexpected(WithContext( + status.error(), + "Task stream schema does not match the projected schema for " + context)); + } + + // Enqueue() reports a stop, so no separate check is needed per batch. + while (true) { + ArrowArray batch{}; + if (int code = stream.get_next(&stream, &batch); code != 0) { + return IOError("Failed to read {} (code {}): {}", context, code, last_error()); + } + if (batch.release == nullptr) { + return {}; + } + auto charge = sizer.Measure(batch); Review Comment: Can we use `ArrowArrayGuard` here and release the guard once the batch is in the queue? If sizing or `push_back` throws, nothing releases the batch. ########## src/iceberg/data/parallel_scan_reader.cc: ########## @@ -0,0 +1,592 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software 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. + */ + +#include "iceberg/data/parallel_scan_reader.h" + +#include <algorithm> +#include <chrono> +#include <condition_variable> +#include <cstdint> +#include <deque> +#include <exception> +#include <format> +#include <memory> +#include <mutex> +#include <optional> +#include <string> +#include <string_view> +#include <thread> +#include <utility> +#include <vector> + +#include <nanoarrow/nanoarrow.h> + +#include "iceberg/arrow_c_data_guard_internal.h" +#include "iceberg/arrow_c_data_util_internal.h" +#include "iceberg/data/arrow_batch_sizer_internal.h" +#include "iceberg/data/parallel_scan_reader_internal.h" +#include "iceberg/file_reader.h" +#include "iceberg/manifest/manifest_entry.h" +#include "iceberg/nanoarrow_status_internal.h" +#include "iceberg/schema.h" +#include "iceberg/schema_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace internal { + +ParallelScanMetricsSnapshot ParallelScanMetrics::Snapshot() const { + std::lock_guard lock(mutex_); + return snapshot_; +} + +namespace { + +// Names a task in error messages, e.g. "data file s3://b/f.parquet [0, 1024)". +std::string DescribeTask(const FileScanTask& task) { + const auto& data_file = task.data_file(); + if (data_file == nullptr) { + return "task without a data file"; + } + if (!task.is_split()) { + return std::format("data file {}", data_file->file_path); + } + return std::format("data file {} [{}, {})", data_file->file_path, task.start(), + task.start() + task.length()); +} + +Error WithContext(const Error& error, std::string_view context) { + return Error{.kind = error.kind, + .message = std::format("{}: {}", context, error.message)}; +} + +std::string_view NullSafe(const char* value) { + return value == nullptr ? std::string_view{} : std::string_view{value}; +} + +// Compares the physical layout of two Arrow schemas. Field names, formats, +// nullability, children, and dictionaries must match; metadata is ignored and the +// root name is not compared. +Status CheckSameLayout(const ArrowSchema& expected, const ArrowSchema& actual, + const std::string& path, bool compare_name) { + if (NullSafe(expected.format) != NullSafe(actual.format)) { + return InvalidSchema("Field {} has Arrow format '{}', expected '{}'", path, + NullSafe(actual.format), NullSafe(expected.format)); + } + if (compare_name && NullSafe(expected.name) != NullSafe(actual.name)) { + return InvalidSchema("Field {} is named '{}', expected '{}'", path, + NullSafe(actual.name), NullSafe(expected.name)); + } + if ((expected.flags & ARROW_FLAG_NULLABLE) != (actual.flags & ARROW_FLAG_NULLABLE)) { + return InvalidSchema("Field {} nullability differs from the projected schema", path); + } + if (expected.n_children != actual.n_children) { + return InvalidSchema("Field {} has {} children, expected {}", path, actual.n_children, + expected.n_children); + } + for (int64_t index = 0; index < expected.n_children; ++index) { + const ArrowSchema* expected_child = expected.children[index]; + const ArrowSchema* actual_child = actual.children[index]; + if (actual_child == nullptr || expected_child == nullptr) { + return InvalidSchema("Field {} has a null child", path); + } + std::string child_path = std::format("{}.{}", path, NullSafe(expected_child->name)); + ICEBERG_RETURN_UNEXPECTED( + CheckSameLayout(*expected_child, *actual_child, child_path, true)); + } + if ((expected.dictionary == nullptr) != (actual.dictionary == nullptr)) { + return InvalidSchema("Field {} dictionary encoding differs from the projected schema", + path); + } + if (expected.dictionary != nullptr) { + ICEBERG_RETURN_UNEXPECTED(CheckSameLayout(*expected.dictionary, *actual.dictionary, + path + ".dictionary", false)); + } + return {}; +} + +// Releases an ArrowArrayStream on scope exit. +class StreamGuard { + public: + explicit StreamGuard(ArrowArrayStream* stream) : stream_(stream) {} + ~StreamGuard() { + if (stream_->release != nullptr) { + stream_->release(stream_); + } + } + StreamGuard(const StreamGuard&) = delete; + StreamGuard& operator=(const StreamGuard&) = delete; + + private: + ArrowArrayStream* stream_; +}; + +void ReleaseArray(ArrowArray* array) { + if (array->release != nullptr) { + array->release(array); + } +} + +struct QueuedBatch { + ArrowArray array; + int64_t charge; +}; + +void ReleaseAll(std::deque<QueuedBatch>& batches) { + for (auto& batch : batches) { + ReleaseArray(&batch.array); + } + batches.clear(); +} + +// Shared state of one parallel read. All fields below `mutex_` are guarded by it. +// No planning or reader callback, Arrow release callback, thread join, or group +// stream destructor runs while `mutex_` is held. +class ParallelScan { + public: + ParallelScan(FileScanTaskGroupStreamPtr groups, ArrowSchema output_schema, + int64_t max_queued_batches, int64_t max_queued_bytes, + std::shared_ptr<ParallelScanMetrics> metrics) + : groups_(std::move(groups)), + output_schema_(output_schema), + max_queued_batches_(max_queued_batches), + max_queued_bytes_(max_queued_bytes), + metrics_(std::move(metrics)) {} + + ~ParallelScan() { + Shutdown(); + if (output_schema_.release != nullptr) { + output_schema_.release(&output_schema_); + } + } + + ParallelScan(const ParallelScan&) = delete; + ParallelScan& operator=(const ParallelScan&) = delete; + + Status Start(const FileScanTaskReader::Options& reader_options, int32_t worker_count) { + std::vector<std::unique_ptr<FileScanTaskReader>> readers; + std::vector<std::unique_ptr<ArrowBatchSizer>> sizers; + readers.reserve(static_cast<size_t>(worker_count)); + sizers.reserve(static_cast<size_t>(worker_count)); + for (int32_t index = 0; index < worker_count; ++index) { + ICEBERG_ASSIGN_OR_RAISE(auto reader, FileScanTaskReader::Make(reader_options)); + ICEBERG_ASSIGN_OR_RAISE(auto sizer, ArrowBatchSizer::Make(output_schema_)); + readers.push_back(std::move(reader)); + sizers.push_back(std::move(sizer)); + } + + { + std::lock_guard lock(mutex_); + active_workers_ = worker_count; + } + workers_.reserve(static_cast<size_t>(worker_count)); + for (int32_t index = 0; index < worker_count; ++index) { + try { + workers_.emplace_back([this, reader = std::move(readers[index]), + sizer = std::move(sizers[index])]() mutable { + WorkerMain(*reader, *sizer); + }); + } catch (const std::exception& e) { + // Threads that never started will not decrement the active count. + { + std::lock_guard lock(mutex_); + active_workers_ -= worker_count - index; + } + Shutdown(); + return UnknownError("Failed to start parallel scan worker: {}", e.what()); + } + } + return {}; + } + + // Consumer side of get_next. Not called concurrently with itself or Shutdown(). + Result<std::optional<ArrowArray>> Next() { + std::unique_lock lock(mutex_); + consumer_cv_.wait(lock, [this] { + return error_.has_value() || !queue_.empty() || active_workers_ == 0; + }); + if (error_.has_value()) { + return unexpected(*error_); + } + if (queue_.empty()) { + return std::nullopt; + } + QueuedBatch batch = queue_.front(); + queue_.pop_front(); + queued_bytes_ -= batch.charge; + lock.unlock(); + // Producers are admitted in ticket order, and a condition variable cannot wake + // one specific waiter, so wake them all and let the head ticket re-check. At + // most worker_count threads wait here. + producer_cv_.notify_all(); + return batch.array; + } + + Result<ArrowSchema> Schema() const { + ArrowSchema schema{}; + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowSchemaDeepCopy(&output_schema_, &schema)); + return schema; + } + + // Stops new work, joins workers, and releases every batch that was not returned. + // Idempotent. + void Shutdown() { + std::deque<QueuedBatch> drained; + { + std::lock_guard lock(mutex_); + stopped_ = true; + drained.swap(queue_); + queued_bytes_ = 0; + } + planning_cv_.notify_all(); + producer_cv_.notify_all(); + consumer_cv_.notify_all(); + ReleaseAll(drained); + + for (auto& worker : workers_) { + if (worker.joinable()) { + worker.join(); + } + } + workers_.clear(); + + // Workers have exited, so nothing else can enqueue or plan. + { + std::lock_guard lock(mutex_); + drained.swap(queue_); + } + ReleaseAll(drained); + groups_.reset(); + } + + private: + void WorkerMain(FileScanTaskReader& reader, ArrowBatchSizer& sizer) { + Status status; + try { + status = RunWorker(reader, sizer); + } catch (const std::exception& e) { + status = UnknownError("Parallel scan worker failed: {}", e.what()); + } catch (...) { + status = UnknownError("Parallel scan worker failed with an unknown exception"); + } + if (!status.has_value()) { + Fail(status.error()); + } + { + std::lock_guard lock(mutex_); + --active_workers_; + } + consumer_cv_.notify_all(); + } + + Status RunWorker(FileScanTaskReader& reader, ArrowBatchSizer& sizer) { + while (true) { + ICEBERG_ASSIGN_OR_RAISE(auto group, ClaimGroup()); + if (group == nullptr) { + return {}; + } + for (const auto& task : group->tasks()) { + if (IsStopped()) { + return {}; + } + if (task == nullptr) { + return InvalidArgument("Task group contains a null task"); + } + ICEBERG_RETURN_UNEXPECTED(ReadTask(reader, sizer, *task)); + } + } + } + + // Returns the next group, or nullptr when planning is exhausted or the read stopped. + // Only one worker calls the group stream at a time, outside all locks. + Result<std::shared_ptr<FileScanTaskGroup>> ClaimGroup() { + { + std::unique_lock lock(mutex_); + planning_cv_.wait(lock, [this] { return stopped_ || !planning_in_progress_; }); + if (stopped_ || planning_done_) { + return nullptr; + } + planning_in_progress_ = true; + } + + Result<std::optional<std::shared_ptr<FileScanTaskGroup>>> next = std::nullopt; + try { + next = groups_->Next(); + } catch (const std::exception& e) { + next = UnknownError("Planning task groups failed: {}", e.what()); + } catch (...) { + next = UnknownError("Planning task groups failed with an unknown exception"); + } + + const bool finished = !next.has_value() || !next.value().has_value(); + if (finished) { + // This worker still holds the planning token, so no other worker uses the + // stream. Destroy it now, outside locks, so metrics reporting runs at EOF. + groups_.reset(); + } + + { + std::lock_guard lock(mutex_); + planning_in_progress_ = false; + if (finished) { + planning_done_ = true; + } + } + planning_cv_.notify_all(); + + if (!next.has_value()) { + return unexpected(WithContext(next.error(), "Failed to plan task groups")); + } + if (!next.value().has_value()) { + return nullptr; + } + auto group = std::move(next.value().value()); + if (group == nullptr) { + return InvalidArgument("Task group stream returned a null group"); + } + if (metrics_ != nullptr) { + metrics_->Update([](auto& snapshot) { ++snapshot.groups_claimed; }); + } + return group; + } + + Status ReadTask(FileScanTaskReader& reader, ArrowBatchSizer& sizer, + const FileScanTask& task) { + const std::string context = DescribeTask(task); + auto opened = reader.Open(task); + if (!opened.has_value()) { + return unexpected(WithContext(opened.error(), "Failed to open " + context)); + } + ArrowArrayStream stream = std::move(opened.value()); + StreamGuard stream_guard(&stream); + if (metrics_ != nullptr) { + metrics_->Update([](auto& snapshot) { ++snapshot.tasks_opened; }); + } + + auto last_error = [&stream]() { + const char* message = stream.get_last_error(&stream); + return message == nullptr ? std::string("unknown error") : std::string(message); + }; + + ArrowSchema task_schema{}; + if (int code = stream.get_schema(&stream, &task_schema); code != 0) { + return IOError("Failed to read the schema of {} (code {}): {}", context, code, + last_error()); + } + internal::ArrowSchemaGuard task_schema_guard(&task_schema); + if (auto status = CheckSameLayout(output_schema_, task_schema, "root", false); + !status.has_value()) { + return unexpected(WithContext( + status.error(), + "Task stream schema does not match the projected schema for " + context)); + } + + // Enqueue() reports a stop, so no separate check is needed per batch. + while (true) { + ArrowArray batch{}; + if (int code = stream.get_next(&stream, &batch); code != 0) { + return IOError("Failed to read {} (code {}): {}", context, code, last_error()); + } + if (batch.release == nullptr) { + return {}; + } + auto charge = sizer.Measure(batch); + if (!charge.has_value()) { + ReleaseArray(&batch); + return unexpected( + WithContext(charge.error(), "Failed to size a batch of " + context)); + } + if (!Enqueue(batch, charge.value())) { + // Stopped while waiting; the array was not admitted. + ReleaseArray(&batch); + return {}; + } + } + } + + // Blocks until the batch is admitted in FIFO order or the read stops. Returns + // false when stopped, in which case the caller still owns `batch`. + bool Enqueue(const ArrowArray& batch, int64_t charge) { + const auto wait_start = std::chrono::steady_clock::now(); + std::unique_lock lock(mutex_); + const uint64_t ticket = next_ticket_++; + held_bytes_ += charge; + RecordMetrics([this](auto& snapshot) { + snapshot.peak_held_bytes = std::max(snapshot.peak_held_bytes, held_bytes_); + }); + + producer_cv_.wait(lock, [&] { + return stopped_ || (ticket == serving_ticket_ && CanAdmit(charge)); + }); + held_bytes_ -= charge; + const auto waited = std::chrono::steady_clock::now() - wait_start; + if (stopped_) { + RecordMetrics([&](auto& snapshot) { snapshot.producer_wait += waited; }); + return false; + } + + const bool oversized = charge > max_queued_bytes_; + queue_.push_back(QueuedBatch{.array = batch, .charge = charge}); + queued_bytes_ += charge; + ++serving_ticket_; + RecordMetrics([&](auto& snapshot) { + snapshot.producer_wait += waited; + ++snapshot.batches_admitted; + snapshot.bytes_admitted += charge; + if (oversized) { + ++snapshot.oversized_admissions; + } + snapshot.peak_queued_batches = + std::max(snapshot.peak_queued_batches, static_cast<int64_t>(queue_.size())); + snapshot.peak_queued_bytes = std::max(snapshot.peak_queued_bytes, queued_bytes_); + }); + lock.unlock(); + producer_cv_.notify_all(); + consumer_cv_.notify_one(); + return true; + } + + // Requires `mutex_`. + bool CanAdmit(int64_t charge) const { + if (std::cmp_greater_equal(queue_.size(), max_queued_batches_)) { + return false; + } + if (charge > max_queued_bytes_) { + // An oversized batch enters only as the sole queued batch. + return queue_.empty(); + } + return queued_bytes_ <= max_queued_bytes_ - charge; + } + + // Requires `mutex_`; the metrics lock is always taken after it. + template <typename Update> + void RecordMetrics(Update&& update) { + if (metrics_ != nullptr) { + metrics_->Update(std::forward<Update>(update)); + } + } + + bool IsStopped() { + std::lock_guard lock(mutex_); + return stopped_; + } + + // Records the first error, stops new work, and releases queued batches. + void Fail(const Error& error) { + std::deque<QueuedBatch> drained; + { + std::lock_guard lock(mutex_); + if (!error_.has_value()) { + error_ = error; + } + stopped_ = true; + drained.swap(queue_); + queued_bytes_ = 0; + } + planning_cv_.notify_all(); + producer_cv_.notify_all(); + consumer_cv_.notify_all(); + ReleaseAll(drained); + } + + // Only touched by the worker holding the planning token, or after all workers + // have joined. + FileScanTaskGroupStreamPtr groups_; + ArrowSchema output_schema_; + const int64_t max_queued_batches_; + const int64_t max_queued_bytes_; + std::shared_ptr<ParallelScanMetrics> metrics_; + std::vector<std::thread> workers_; + + std::mutex mutex_; + std::condition_variable planning_cv_; + std::condition_variable producer_cv_; + std::condition_variable consumer_cv_; + bool stopped_ = false; + bool planning_in_progress_ = false; + bool planning_done_ = false; + int32_t active_workers_ = 0; + std::optional<Error> error_; + std::deque<QueuedBatch> queue_; + int64_t queued_bytes_ = 0; + int64_t held_bytes_ = 0; + uint64_t next_ticket_ = 0; + uint64_t serving_ticket_ = 0; +}; + +// Adapts ParallelScan to the ArrowArrayStream provider interface. +class ParallelScanSource { + public: + explicit ParallelScanSource(std::unique_ptr<ParallelScan> scan) + : scan_(std::move(scan)) {} + + Status Close() { + scan_->Shutdown(); + return {}; + } + + Result<std::optional<ArrowArray>> Next() { return scan_->Next(); } + + Result<ArrowSchema> Schema() { return scan_->Schema(); } + + private: + std::unique_ptr<ParallelScan> scan_; +}; + +} // namespace + +Result<ArrowArrayStream> OpenParallelScan(FileScanTaskGroupStreamPtr groups, + ParallelScanReader::Options options, + std::shared_ptr<ParallelScanMetrics> metrics) { + ICEBERG_PRECHECK(groups != nullptr, "Task group stream must not be null"); + ICEBERG_PRECHECK(options.worker_count > 0, "Worker count must be positive: {}", + options.worker_count); + ICEBERG_PRECHECK(options.max_queued_batches > 0, + "Maximum queued batches must be positive: {}", + options.max_queued_batches); + ICEBERG_PRECHECK(options.max_queued_bytes > 0, + "Maximum queued bytes must be positive: {}", options.max_queued_bytes); + ICEBERG_PRECHECK(options.reader_options.projected_schema != nullptr, + "Projected schema must not be null"); + + options.reader_options.apply_residuals = true; Review Comment: On an unpartitioned table, try selecting only `name` with `id IS NOT NULL` on a required `id`. The scan drops `id` from the projection, but the task still has the filter. Binding it here fails. Can we cover this with a planned scan? ########## src/iceberg/data/parallel_scan_reader.cc: ########## @@ -0,0 +1,592 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software 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. + */ + +#include "iceberg/data/parallel_scan_reader.h" + +#include <algorithm> +#include <chrono> +#include <condition_variable> +#include <cstdint> +#include <deque> +#include <exception> +#include <format> +#include <memory> +#include <mutex> +#include <optional> +#include <string> +#include <string_view> +#include <thread> +#include <utility> +#include <vector> + +#include <nanoarrow/nanoarrow.h> + +#include "iceberg/arrow_c_data_guard_internal.h" +#include "iceberg/arrow_c_data_util_internal.h" +#include "iceberg/data/arrow_batch_sizer_internal.h" +#include "iceberg/data/parallel_scan_reader_internal.h" +#include "iceberg/file_reader.h" +#include "iceberg/manifest/manifest_entry.h" +#include "iceberg/nanoarrow_status_internal.h" +#include "iceberg/schema.h" +#include "iceberg/schema_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace internal { + +ParallelScanMetricsSnapshot ParallelScanMetrics::Snapshot() const { + std::lock_guard lock(mutex_); + return snapshot_; +} + +namespace { + +// Names a task in error messages, e.g. "data file s3://b/f.parquet [0, 1024)". +std::string DescribeTask(const FileScanTask& task) { + const auto& data_file = task.data_file(); + if (data_file == nullptr) { + return "task without a data file"; + } + if (!task.is_split()) { + return std::format("data file {}", data_file->file_path); + } + return std::format("data file {} [{}, {})", data_file->file_path, task.start(), + task.start() + task.length()); +} + +Error WithContext(const Error& error, std::string_view context) { + return Error{.kind = error.kind, + .message = std::format("{}: {}", context, error.message)}; +} + +std::string_view NullSafe(const char* value) { + return value == nullptr ? std::string_view{} : std::string_view{value}; +} + +// Compares the physical layout of two Arrow schemas. Field names, formats, +// nullability, children, and dictionaries must match; metadata is ignored and the +// root name is not compared. +Status CheckSameLayout(const ArrowSchema& expected, const ArrowSchema& actual, + const std::string& path, bool compare_name) { + if (NullSafe(expected.format) != NullSafe(actual.format)) { + return InvalidSchema("Field {} has Arrow format '{}', expected '{}'", path, + NullSafe(actual.format), NullSafe(expected.format)); + } + if (compare_name && NullSafe(expected.name) != NullSafe(actual.name)) { + return InvalidSchema("Field {} is named '{}', expected '{}'", path, + NullSafe(actual.name), NullSafe(expected.name)); + } + if ((expected.flags & ARROW_FLAG_NULLABLE) != (actual.flags & ARROW_FLAG_NULLABLE)) { + return InvalidSchema("Field {} nullability differs from the projected schema", path); + } + if (expected.n_children != actual.n_children) { + return InvalidSchema("Field {} has {} children, expected {}", path, actual.n_children, + expected.n_children); + } + for (int64_t index = 0; index < expected.n_children; ++index) { + const ArrowSchema* expected_child = expected.children[index]; + const ArrowSchema* actual_child = actual.children[index]; + if (actual_child == nullptr || expected_child == nullptr) { + return InvalidSchema("Field {} has a null child", path); + } + std::string child_path = std::format("{}.{}", path, NullSafe(expected_child->name)); + ICEBERG_RETURN_UNEXPECTED( + CheckSameLayout(*expected_child, *actual_child, child_path, true)); + } + if ((expected.dictionary == nullptr) != (actual.dictionary == nullptr)) { + return InvalidSchema("Field {} dictionary encoding differs from the projected schema", + path); + } + if (expected.dictionary != nullptr) { + ICEBERG_RETURN_UNEXPECTED(CheckSameLayout(*expected.dictionary, *actual.dictionary, + path + ".dictionary", false)); + } + return {}; +} + +// Releases an ArrowArrayStream on scope exit. +class StreamGuard { + public: + explicit StreamGuard(ArrowArrayStream* stream) : stream_(stream) {} + ~StreamGuard() { + if (stream_->release != nullptr) { + stream_->release(stream_); + } + } + StreamGuard(const StreamGuard&) = delete; + StreamGuard& operator=(const StreamGuard&) = delete; + + private: + ArrowArrayStream* stream_; +}; + +void ReleaseArray(ArrowArray* array) { + if (array->release != nullptr) { + array->release(array); + } +} + +struct QueuedBatch { + ArrowArray array; + int64_t charge; +}; + +void ReleaseAll(std::deque<QueuedBatch>& batches) { + for (auto& batch : batches) { + ReleaseArray(&batch.array); + } + batches.clear(); +} + +// Shared state of one parallel read. All fields below `mutex_` are guarded by it. +// No planning or reader callback, Arrow release callback, thread join, or group +// stream destructor runs while `mutex_` is held. +class ParallelScan { + public: + ParallelScan(FileScanTaskGroupStreamPtr groups, ArrowSchema output_schema, + int64_t max_queued_batches, int64_t max_queued_bytes, + std::shared_ptr<ParallelScanMetrics> metrics) + : groups_(std::move(groups)), + output_schema_(output_schema), + max_queued_batches_(max_queued_batches), + max_queued_bytes_(max_queued_bytes), + metrics_(std::move(metrics)) {} + + ~ParallelScan() { + Shutdown(); + if (output_schema_.release != nullptr) { + output_schema_.release(&output_schema_); + } + } + + ParallelScan(const ParallelScan&) = delete; + ParallelScan& operator=(const ParallelScan&) = delete; + + Status Start(const FileScanTaskReader::Options& reader_options, int32_t worker_count) { + std::vector<std::unique_ptr<FileScanTaskReader>> readers; + std::vector<std::unique_ptr<ArrowBatchSizer>> sizers; + readers.reserve(static_cast<size_t>(worker_count)); + sizers.reserve(static_cast<size_t>(worker_count)); + for (int32_t index = 0; index < worker_count; ++index) { + ICEBERG_ASSIGN_OR_RAISE(auto reader, FileScanTaskReader::Make(reader_options)); + ICEBERG_ASSIGN_OR_RAISE(auto sizer, ArrowBatchSizer::Make(output_schema_)); + readers.push_back(std::move(reader)); + sizers.push_back(std::move(sizer)); + } + + { + std::lock_guard lock(mutex_); + active_workers_ = worker_count; + } + workers_.reserve(static_cast<size_t>(worker_count)); + for (int32_t index = 0; index < worker_count; ++index) { + try { + workers_.emplace_back([this, reader = std::move(readers[index]), + sizer = std::move(sizers[index])]() mutable { + WorkerMain(*reader, *sizer); + }); + } catch (const std::exception& e) { + // Threads that never started will not decrement the active count. + { + std::lock_guard lock(mutex_); + active_workers_ -= worker_count - index; + } + Shutdown(); + return UnknownError("Failed to start parallel scan worker: {}", e.what()); + } + } + return {}; + } + + // Consumer side of get_next. Not called concurrently with itself or Shutdown(). + Result<std::optional<ArrowArray>> Next() { + std::unique_lock lock(mutex_); + consumer_cv_.wait(lock, [this] { + return error_.has_value() || !queue_.empty() || active_workers_ == 0; + }); + if (error_.has_value()) { + return unexpected(*error_); + } + if (queue_.empty()) { + return std::nullopt; + } + QueuedBatch batch = queue_.front(); + queue_.pop_front(); + queued_bytes_ -= batch.charge; + lock.unlock(); + // Producers are admitted in ticket order, and a condition variable cannot wake + // one specific waiter, so wake them all and let the head ticket re-check. At + // most worker_count threads wait here. + producer_cv_.notify_all(); + return batch.array; + } + + Result<ArrowSchema> Schema() const { + ArrowSchema schema{}; + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowSchemaDeepCopy(&output_schema_, &schema)); + return schema; + } + + // Stops new work, joins workers, and releases every batch that was not returned. + // Idempotent. + void Shutdown() { + std::deque<QueuedBatch> drained; + { + std::lock_guard lock(mutex_); + stopped_ = true; + drained.swap(queue_); + queued_bytes_ = 0; + } + planning_cv_.notify_all(); + producer_cv_.notify_all(); + consumer_cv_.notify_all(); + ReleaseAll(drained); + + for (auto& worker : workers_) { + if (worker.joinable()) { + worker.join(); + } + } + workers_.clear(); + + // Workers have exited, so nothing else can enqueue or plan. + { + std::lock_guard lock(mutex_); + drained.swap(queue_); + } + ReleaseAll(drained); + groups_.reset(); + } + + private: + void WorkerMain(FileScanTaskReader& reader, ArrowBatchSizer& sizer) { + Status status; + try { + status = RunWorker(reader, sizer); + } catch (const std::exception& e) { + status = UnknownError("Parallel scan worker failed: {}", e.what()); + } catch (...) { + status = UnknownError("Parallel scan worker failed with an unknown exception"); + } + if (!status.has_value()) { + Fail(status.error()); + } + { + std::lock_guard lock(mutex_); + --active_workers_; + } + consumer_cv_.notify_all(); + } + + Status RunWorker(FileScanTaskReader& reader, ArrowBatchSizer& sizer) { + while (true) { + ICEBERG_ASSIGN_OR_RAISE(auto group, ClaimGroup()); + if (group == nullptr) { + return {}; + } + for (const auto& task : group->tasks()) { + if (IsStopped()) { + return {}; + } + if (task == nullptr) { + return InvalidArgument("Task group contains a null task"); + } + ICEBERG_RETURN_UNEXPECTED(ReadTask(reader, sizer, *task)); + } + } + } + + // Returns the next group, or nullptr when planning is exhausted or the read stopped. + // Only one worker calls the group stream at a time, outside all locks. + Result<std::shared_ptr<FileScanTaskGroup>> ClaimGroup() { + { + std::unique_lock lock(mutex_); + planning_cv_.wait(lock, [this] { return stopped_ || !planning_in_progress_; }); + if (stopped_ || planning_done_) { + return nullptr; + } + planning_in_progress_ = true; + } + + Result<std::optional<std::shared_ptr<FileScanTaskGroup>>> next = std::nullopt; + try { + next = groups_->Next(); + } catch (const std::exception& e) { + next = UnknownError("Planning task groups failed: {}", e.what()); + } catch (...) { + next = UnknownError("Planning task groups failed with an unknown exception"); + } + + const bool finished = !next.has_value() || !next.value().has_value(); + if (finished) { + // This worker still holds the planning token, so no other worker uses the + // stream. Destroy it now, outside locks, so metrics reporting runs at EOF. + groups_.reset(); + } + + { + std::lock_guard lock(mutex_); + planning_in_progress_ = false; + if (finished) { + planning_done_ = true; + } + } + planning_cv_.notify_all(); + + if (!next.has_value()) { + return unexpected(WithContext(next.error(), "Failed to plan task groups")); + } + if (!next.value().has_value()) { + return nullptr; + } + auto group = std::move(next.value().value()); + if (group == nullptr) { + return InvalidArgument("Task group stream returned a null group"); + } + if (metrics_ != nullptr) { + metrics_->Update([](auto& snapshot) { ++snapshot.groups_claimed; }); + } + return group; + } + + Status ReadTask(FileScanTaskReader& reader, ArrowBatchSizer& sizer, + const FileScanTask& task) { + const std::string context = DescribeTask(task); + auto opened = reader.Open(task); + if (!opened.has_value()) { + return unexpected(WithContext(opened.error(), "Failed to open " + context)); + } + ArrowArrayStream stream = std::move(opened.value()); + StreamGuard stream_guard(&stream); + if (metrics_ != nullptr) { + metrics_->Update([](auto& snapshot) { ++snapshot.tasks_opened; }); + } + + auto last_error = [&stream]() { + const char* message = stream.get_last_error(&stream); + return message == nullptr ? std::string("unknown error") : std::string(message); + }; + + ArrowSchema task_schema{}; + if (int code = stream.get_schema(&stream, &task_schema); code != 0) { + return IOError("Failed to read the schema of {} (code {}): {}", context, code, + last_error()); + } + internal::ArrowSchemaGuard task_schema_guard(&task_schema); + if (auto status = CheckSameLayout(output_schema_, task_schema, "root", false); + !status.has_value()) { + return unexpected(WithContext( + status.error(), + "Task stream schema does not match the projected schema for " + context)); + } + + // Enqueue() reports a stop, so no separate check is needed per batch. + while (true) { + ArrowArray batch{}; + if (int code = stream.get_next(&stream, &batch); code != 0) { Review Comment: Can we check the stop flag before calling `get_next`? If the stream was released while `get_schema` was running, this call can still read the rest of the file looking for a match. ########## src/iceberg/data/arrow_batch_sizer.cc: ########## @@ -0,0 +1,364 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software 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. + */ + +#include <cstddef> +#include <cstdint> +#include <limits> +#include <optional> +#include <string> +#include <string_view> +#include <utility> +#include <vector> + +#include <nanoarrow/nanoarrow.h> + +#include "iceberg/data/arrow_batch_sizer_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg::internal { + +namespace { + +constexpr int64_t kInt64Max = std::numeric_limits<int64_t>::max(); + +// The number of leading buffers a binary or string view array always has. +constexpr int64_t kBinaryViewFixedBuffers = 2; + +constexpr int64_t kVariadicSizeBytes = static_cast<int64_t>(sizeof(int64_t)); + +bool IsBinaryView(ArrowType type) { + return type == NANOARROW_TYPE_BINARY_VIEW || type == NANOARROW_TYPE_STRING_VIEW; +} + +std::optional<int64_t> CheckedAdd(int64_t lhs, int64_t rhs) { + if (lhs < 0 || rhs < 0 || lhs > kInt64Max - rhs) { + return std::nullopt; + } + return lhs + rhs; +} + +std::optional<int64_t> CheckedMultiply(int64_t lhs, int64_t rhs) { + if (lhs < 0 || rhs < 0) { + return std::nullopt; + } + if (lhs != 0 && rhs > kInt64Max / lhs) { + return std::nullopt; + } + return lhs * rhs; +} + +// Returns whether `bytes` can also be represented as a size_t, which nanoarrow and +// the allocator use for buffer sizes. +bool FitsSizeT(int64_t bytes) { + if constexpr (sizeof(size_t) >= sizeof(int64_t)) { + return bytes >= 0; + } else { + return bytes >= 0 && + static_cast<uint64_t>(bytes) <= std::numeric_limits<size_t>::max(); + } +} + +// The position of a nested array, e.g. "root.1.dictionary". Only formatted when an +// error is reported, so measuring a valid batch does not allocate strings. +class ArrayPath { + public: + static constexpr int64_t kDictionary = -1; + + // Appends a child index (or kDictionary) until the returned scope ends. + class Scope { + public: + Scope(ArrayPath& path, int64_t step) : path_(path) { path_.steps_.push_back(step); } + ~Scope() { path_.steps_.pop_back(); } + Scope(const Scope&) = delete; + Scope& operator=(const Scope&) = delete; + + private: + ArrayPath& path_; + }; + + std::string ToString() const { + std::string result = "root"; + for (int64_t step : steps_) { + result += step == kDictionary ? ".dictionary" : "." + std::to_string(step); + } + return result; + } + + private: + std::vector<int64_t> steps_; +}; + +// Returns the minimum byte size nanoarrow derives for fixed buffer `index` from the +// array's offset and length alone, or std::nullopt when the product overflows. +// Buffers whose size depends on data (for example string data) report zero here. +std::optional<int64_t> LayoutBufferBytes(const ArrowLayout& layout, int index, + int64_t offset_plus_length) { + const int64_t element_size_bits = layout.element_size_bits[index]; + const int64_t element_size_bytes = element_size_bits / 8; + switch (layout.buffer_type[index]) { + case NANOARROW_BUFFER_TYPE_VALIDITY: + return offset_plus_length / 8 + (offset_plus_length % 8 != 0 ? 1 : 0); + case NANOARROW_BUFFER_TYPE_DATA_OFFSET: { + if (offset_plus_length == 0) { + return 0; + } + auto offset_count = CheckedAdd(offset_plus_length, 1); + if (!offset_count.has_value()) { + return std::nullopt; + } + return CheckedMultiply(*offset_count, element_size_bytes); + } + case NANOARROW_BUFFER_TYPE_SIZE: + case NANOARROW_BUFFER_TYPE_VIEW_OFFSET: + case NANOARROW_BUFFER_TYPE_TYPE_ID: + case NANOARROW_BUFFER_TYPE_UNION_OFFSET: + return CheckedMultiply(offset_plus_length, element_size_bytes); + case NANOARROW_BUFFER_TYPE_DATA: { + auto bits = CheckedMultiply(offset_plus_length, element_size_bits); + if (!bits.has_value()) { + return std::nullopt; + } + auto rounded = CheckedAdd(*bits, 7); + if (!rounded.has_value()) { + return std::nullopt; + } + return *rounded / 8; + } + case NANOARROW_BUFFER_TYPE_NONE: + case NANOARROW_BUFFER_TYPE_VARIADIC_DATA: + case NANOARROW_BUFFER_TYPE_VARIADIC_SIZE: + return 0; + } + return std::nullopt; +} + +int FixedBufferCount(const ArrowArrayView& view) { + if (IsBinaryView(view.storage_type)) { + return static_cast<int>(kBinaryViewFixedBuffers); + } + int count = 0; + while (count < NANOARROW_MAX_FIXED_BUFFERS && + view.layout.buffer_type[count] != NANOARROW_BUFFER_TYPE_NONE) { + ++count; + } + return count; +} + +// Checks everything nanoarrow trusts before it sizes buffers: counts used to index +// array members, and every product that it computes without overflow checks. +Status Preflight(const ArrowArrayView& view, const ArrowArray& array, ArrayPath& path) { + switch (view.storage_type) { + case NANOARROW_TYPE_LIST_VIEW: + case NANOARROW_TYPE_LARGE_LIST_VIEW: + case NANOARROW_TYPE_RUN_END_ENCODED: + return NotSupported("Cannot size Arrow {} array at {}: unsupported layout", + ArrowTypeString(view.storage_type), path.ToString()); + default: + break; + } + + if (array.length < 0 || array.offset < 0) { + return InvalidArrowData("Arrow array at {} has negative offset {} or length {}", + path.ToString(), array.offset, array.length); + } + auto offset_plus_length = CheckedAdd(array.offset, array.length); + if (!offset_plus_length.has_value()) { + return InvalidArrowData("Arrow array at {} offset {} plus length {} overflows", + path.ToString(), array.offset, array.length); + } + + const int fixed_buffers = FixedBufferCount(view); + if (IsBinaryView(view.storage_type)) { + // Validity, views, zero or more variadic data buffers, then the size buffer. + if (array.n_buffers < kBinaryViewFixedBuffers + 1) { + return InvalidArrowData("Arrow {} array at {} has {} buffers; expected at least {}", + ArrowTypeString(view.storage_type), path.ToString(), + array.n_buffers, kBinaryViewFixedBuffers + 1); + } + const int64_t variadic_buffers = array.n_buffers - kBinaryViewFixedBuffers - 1; + if (variadic_buffers > std::numeric_limits<int32_t>::max()) { + return InvalidArrowData("Arrow array at {} has too many variadic buffers: {}", + path.ToString(), variadic_buffers); + } + auto size_buffer_bytes = CheckedMultiply(variadic_buffers, kVariadicSizeBytes); + if (!size_buffer_bytes.has_value() || !FitsSizeT(*size_buffer_bytes)) { + return InvalidArrowData("Arrow array at {} variadic size buffer overflows", + path.ToString()); + } + if (array.buffers == nullptr || + (variadic_buffers > 0 && array.buffers[array.n_buffers - 1] == nullptr)) { + return InvalidArrowData("Arrow array at {} is missing its variadic size buffer", + path.ToString()); + } + } else if (array.n_buffers != fixed_buffers) { + return InvalidArrowData("Arrow {} array at {} has {} buffers; expected {}", + ArrowTypeString(view.storage_type), path.ToString(), + array.n_buffers, fixed_buffers); + } + if (array.n_buffers > 0 && array.buffers == nullptr) { + return InvalidArrowData("Arrow array at {} has a null buffer list", path.ToString()); + } + + for (int index = 0; index < fixed_buffers; ++index) { + auto bytes = LayoutBufferBytes(view.layout, index, *offset_plus_length); + if (!bytes.has_value() || !FitsSizeT(*bytes)) { + return InvalidArrowData( + "Arrow {} array at {} buffer {} size overflows for offset {} and length {}", + ArrowTypeString(view.storage_type), path.ToString(), index, array.offset, + array.length); + } + } + + if (view.storage_type == NANOARROW_TYPE_FIXED_SIZE_LIST) { + auto child_length = + CheckedMultiply(*offset_plus_length, view.layout.child_size_elements); + if (!child_length.has_value()) { + return InvalidArrowData( + "Arrow fixed_size_list array at {} child length overflows for offset {}, " + "length {}, and list size {}", + path.ToString(), array.offset, array.length, view.layout.child_size_elements); + } + } + + if (array.n_children != view.n_children) { + return InvalidArrowData("Arrow array at {} has {} children; expected {}", + path.ToString(), array.n_children, view.n_children); + } + if (array.n_children > 0 && array.children == nullptr) { + return InvalidArrowData("Arrow array at {} has a null child list", path.ToString()); + } + for (int64_t index = 0; index < view.n_children; ++index) { + ArrayPath::Scope child(path, index); + if (array.children[index] == nullptr) { + return InvalidArrowData("Arrow array at {} is null", path.ToString()); + } + ICEBERG_RETURN_UNEXPECTED( + Preflight(*view.children[index], *array.children[index], path)); + } + + if ((view.dictionary == nullptr) != (array.dictionary == nullptr)) { + return InvalidArrowData("Arrow array at {} dictionary does not match its schema", + path.ToString()); + } + if (view.dictionary != nullptr) { + ArrayPath::Scope dictionary(path, ArrayPath::kDictionary); + ICEBERG_RETURN_UNEXPECTED(Preflight(*view.dictionary, *array.dictionary, path)); + } + return {}; +} + +// Sums the spans of the buffers that nanoarrow sized for `view`. +Result<int64_t> SumBufferSpans(const ArrowArrayView& view, ArrayPath& path) { + int64_t total = 0; + auto add = [&](int64_t bytes, std::string_view what) -> Status { + if (bytes < 0) { + return NotSupported("Cannot size Arrow {} array at {}: unknown {} size", + ArrowTypeString(view.storage_type), path.ToString(), what); + } + auto sum = CheckedAdd(total, bytes); + if (!sum.has_value() || !FitsSizeT(*sum)) { + return InvalidArrowData("Arrow array at {} buffer sizes overflow", path.ToString()); + } + total = *sum; + return {}; + }; + + const int fixed_buffers = FixedBufferCount(view); Review Comment: Can we use `ArrowArrayViewGetNumBuffers` and `ArrowArrayViewGetBufferView` here? They cover variadic buffers too. We could keep the overflow checks and drop the separate loop. ########## src/iceberg/data/parallel_scan_reader.h: ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software 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 + +/// \file iceberg/data/parallel_scan_reader.h +/// Reads planned FileScanTaskGroups concurrently into one Arrow stream. + +#include <cstdint> + +#include "iceberg/arrow_c_data.h" +#include "iceberg/data/file_scan_task_reader.h" +#include "iceberg/iceberg_data_export.h" +#include "iceberg/result.h" +#include "iceberg/table_scan.h" + +namespace iceberg { + +/// \brief Reads the groups of a FileScanTaskGroupStream with a fixed set of workers. +/// +/// Each worker owns its own FileScanTaskReader. Workers take groups from the +/// single-pass group stream one at a time, read the tasks within a group in order, +/// and place each completed batch into an output queue bounded by batch count and +/// by charged bytes. The caller receives one Arrow C stream that yields batches as +/// workers admit them. +/// +/// Output order across groups and tasks is unspecified; the batches of one task keep +/// their order relative to each other. Every batch matches the projected schema. +/// +/// Residual filters are always enforced: `apply_residuals` in the reader options is +/// forced on. `reader_options.case_sensitive` must match the planning scan's +/// `TableScan::is_case_sensitive()`, because task residuals are unbound and groups do +/// not carry that setting. +/// +/// Queue memory: `max_queued_bytes` limits admission using the summed spans of the +/// Arrow buffers of queued batches, counting every buffer occurrence. A batch larger +/// than the budget is admitted only when the queue is empty, as its sole batch. +/// Zero-byte batches still count toward `max_queued_batches`. This is not a limit +/// on process memory: format reader buffers, delete indexes, allocator overhead, and +/// arrays already returned to the caller are outside the budget. Each worker can also +/// hold one completed batch while it waits for admission. Per worker, expect roughly +/// one open format reader with its read buffers, one task's delete index, and one +/// completed batch, in addition to the queue budget. +/// +/// Preconditions, which the reader cannot check: +/// - Format reader factories are registered before Open() and are not changed until +/// the returned stream is released. +/// - `reader_options.io` supports concurrent reads from several threads. +/// - Tasks and their shared data and delete file metadata are not mutated while the +/// stream is in use. +/// - Any executor passed to `TableScanBuilder::PlanWith()` outlives the returned +/// stream, which owns the lazy group stream. +/// +/// Stream callbacks follow the Arrow C stream contract: they must not be called +/// concurrently, and `release` must not race with `get_next`, `get_schema`, or +/// `get_last_error`. Releasing the stream between callbacks stops assigning work, +/// wakes waiting workers, releases batches that were not returned, and joins the +/// workers. A planning call or read that is already in progress finishes first. +/// Cancelling a `get_next` that is blocked on another thread is not supported. +/// +/// When planning, reading, schema checking, or batch sizing fails, the reader stops +/// assigning work, releases queued batches, and reports the first error from the +/// next `get_next` call as `EIO`, with a message that names the file and byte range +/// available from `get_last_error`. Arrays already returned to the caller remain +/// valid after the stream is released. +class ICEBERG_DATA_EXPORT ParallelScanReader { + public: + /// \brief Options for one parallel read. + struct Options { + /// Options for the per-worker task readers. `apply_residuals` is forced on. + FileScanTaskReader::Options reader_options; + /// Number of worker threads. Must be positive. + int32_t worker_count = 0; Review Comment: Could we take a caller-provided `Executor` and keep `worker_count` as the limit for one scan? `WriteManifestsWith` already follows that pattern. With this API, ten scans with eight workers each start 80 threads. ########## src/iceberg/data/parallel_scan_reader.cc: ########## @@ -0,0 +1,592 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software 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. + */ + +#include "iceberg/data/parallel_scan_reader.h" + +#include <algorithm> +#include <chrono> +#include <condition_variable> +#include <cstdint> +#include <deque> +#include <exception> +#include <format> +#include <memory> +#include <mutex> +#include <optional> +#include <string> +#include <string_view> +#include <thread> +#include <utility> +#include <vector> + +#include <nanoarrow/nanoarrow.h> + +#include "iceberg/arrow_c_data_guard_internal.h" +#include "iceberg/arrow_c_data_util_internal.h" +#include "iceberg/data/arrow_batch_sizer_internal.h" +#include "iceberg/data/parallel_scan_reader_internal.h" +#include "iceberg/file_reader.h" +#include "iceberg/manifest/manifest_entry.h" +#include "iceberg/nanoarrow_status_internal.h" +#include "iceberg/schema.h" +#include "iceberg/schema_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace internal { + +ParallelScanMetricsSnapshot ParallelScanMetrics::Snapshot() const { + std::lock_guard lock(mutex_); + return snapshot_; +} + +namespace { + +// Names a task in error messages, e.g. "data file s3://b/f.parquet [0, 1024)". +std::string DescribeTask(const FileScanTask& task) { + const auto& data_file = task.data_file(); + if (data_file == nullptr) { + return "task without a data file"; + } + if (!task.is_split()) { + return std::format("data file {}", data_file->file_path); + } + return std::format("data file {} [{}, {})", data_file->file_path, task.start(), + task.start() + task.length()); +} + +Error WithContext(const Error& error, std::string_view context) { + return Error{.kind = error.kind, + .message = std::format("{}: {}", context, error.message)}; +} + +std::string_view NullSafe(const char* value) { + return value == nullptr ? std::string_view{} : std::string_view{value}; +} + +// Compares the physical layout of two Arrow schemas. Field names, formats, +// nullability, children, and dictionaries must match; metadata is ignored and the +// root name is not compared. +Status CheckSameLayout(const ArrowSchema& expected, const ArrowSchema& actual, + const std::string& path, bool compare_name) { + if (NullSafe(expected.format) != NullSafe(actual.format)) { + return InvalidSchema("Field {} has Arrow format '{}', expected '{}'", path, + NullSafe(actual.format), NullSafe(expected.format)); + } + if (compare_name && NullSafe(expected.name) != NullSafe(actual.name)) { + return InvalidSchema("Field {} is named '{}', expected '{}'", path, + NullSafe(actual.name), NullSafe(expected.name)); + } + if ((expected.flags & ARROW_FLAG_NULLABLE) != (actual.flags & ARROW_FLAG_NULLABLE)) { + return InvalidSchema("Field {} nullability differs from the projected schema", path); + } + if (expected.n_children != actual.n_children) { + return InvalidSchema("Field {} has {} children, expected {}", path, actual.n_children, + expected.n_children); + } + for (int64_t index = 0; index < expected.n_children; ++index) { + const ArrowSchema* expected_child = expected.children[index]; + const ArrowSchema* actual_child = actual.children[index]; + if (actual_child == nullptr || expected_child == nullptr) { + return InvalidSchema("Field {} has a null child", path); + } + std::string child_path = std::format("{}.{}", path, NullSafe(expected_child->name)); + ICEBERG_RETURN_UNEXPECTED( + CheckSameLayout(*expected_child, *actual_child, child_path, true)); + } + if ((expected.dictionary == nullptr) != (actual.dictionary == nullptr)) { + return InvalidSchema("Field {} dictionary encoding differs from the projected schema", + path); + } + if (expected.dictionary != nullptr) { + ICEBERG_RETURN_UNEXPECTED(CheckSameLayout(*expected.dictionary, *actual.dictionary, + path + ".dictionary", false)); + } + return {}; +} + +// Releases an ArrowArrayStream on scope exit. +class StreamGuard { + public: + explicit StreamGuard(ArrowArrayStream* stream) : stream_(stream) {} + ~StreamGuard() { + if (stream_->release != nullptr) { + stream_->release(stream_); + } + } + StreamGuard(const StreamGuard&) = delete; + StreamGuard& operator=(const StreamGuard&) = delete; + + private: + ArrowArrayStream* stream_; +}; + +void ReleaseArray(ArrowArray* array) { + if (array->release != nullptr) { + array->release(array); + } +} + +struct QueuedBatch { + ArrowArray array; + int64_t charge; +}; + +void ReleaseAll(std::deque<QueuedBatch>& batches) { + for (auto& batch : batches) { + ReleaseArray(&batch.array); + } + batches.clear(); +} + +// Shared state of one parallel read. All fields below `mutex_` are guarded by it. +// No planning or reader callback, Arrow release callback, thread join, or group +// stream destructor runs while `mutex_` is held. +class ParallelScan { + public: + ParallelScan(FileScanTaskGroupStreamPtr groups, ArrowSchema output_schema, + int64_t max_queued_batches, int64_t max_queued_bytes, + std::shared_ptr<ParallelScanMetrics> metrics) + : groups_(std::move(groups)), + output_schema_(output_schema), + max_queued_batches_(max_queued_batches), + max_queued_bytes_(max_queued_bytes), + metrics_(std::move(metrics)) {} + + ~ParallelScan() { + Shutdown(); + if (output_schema_.release != nullptr) { + output_schema_.release(&output_schema_); + } + } + + ParallelScan(const ParallelScan&) = delete; + ParallelScan& operator=(const ParallelScan&) = delete; + + Status Start(const FileScanTaskReader::Options& reader_options, int32_t worker_count) { + std::vector<std::unique_ptr<FileScanTaskReader>> readers; + std::vector<std::unique_ptr<ArrowBatchSizer>> sizers; + readers.reserve(static_cast<size_t>(worker_count)); + sizers.reserve(static_cast<size_t>(worker_count)); + for (int32_t index = 0; index < worker_count; ++index) { + ICEBERG_ASSIGN_OR_RAISE(auto reader, FileScanTaskReader::Make(reader_options)); + ICEBERG_ASSIGN_OR_RAISE(auto sizer, ArrowBatchSizer::Make(output_schema_)); + readers.push_back(std::move(reader)); + sizers.push_back(std::move(sizer)); + } + + { + std::lock_guard lock(mutex_); + active_workers_ = worker_count; + } + workers_.reserve(static_cast<size_t>(worker_count)); + for (int32_t index = 0; index < worker_count; ++index) { + try { + workers_.emplace_back([this, reader = std::move(readers[index]), + sizer = std::move(sizers[index])]() mutable { + WorkerMain(*reader, *sizer); + }); + } catch (const std::exception& e) { + // Threads that never started will not decrement the active count. + { + std::lock_guard lock(mutex_); + active_workers_ -= worker_count - index; + } + Shutdown(); + return UnknownError("Failed to start parallel scan worker: {}", e.what()); + } + } + return {}; + } + + // Consumer side of get_next. Not called concurrently with itself or Shutdown(). + Result<std::optional<ArrowArray>> Next() { + std::unique_lock lock(mutex_); + consumer_cv_.wait(lock, [this] { + return error_.has_value() || !queue_.empty() || active_workers_ == 0; + }); + if (error_.has_value()) { + return unexpected(*error_); + } + if (queue_.empty()) { + return std::nullopt; + } + QueuedBatch batch = queue_.front(); + queue_.pop_front(); + queued_bytes_ -= batch.charge; + lock.unlock(); + // Producers are admitted in ticket order, and a condition variable cannot wake + // one specific waiter, so wake them all and let the head ticket re-check. At + // most worker_count threads wait here. + producer_cv_.notify_all(); + return batch.array; + } + + Result<ArrowSchema> Schema() const { + ArrowSchema schema{}; + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowSchemaDeepCopy(&output_schema_, &schema)); + return schema; + } + + // Stops new work, joins workers, and releases every batch that was not returned. + // Idempotent. + void Shutdown() { + std::deque<QueuedBatch> drained; + { + std::lock_guard lock(mutex_); + stopped_ = true; + drained.swap(queue_); + queued_bytes_ = 0; + } + planning_cv_.notify_all(); + producer_cv_.notify_all(); + consumer_cv_.notify_all(); + ReleaseAll(drained); + + for (auto& worker : workers_) { + if (worker.joinable()) { + worker.join(); + } + } + workers_.clear(); + + // Workers have exited, so nothing else can enqueue or plan. + { + std::lock_guard lock(mutex_); + drained.swap(queue_); + } + ReleaseAll(drained); + groups_.reset(); + } + + private: + void WorkerMain(FileScanTaskReader& reader, ArrowBatchSizer& sizer) { + Status status; + try { + status = RunWorker(reader, sizer); + } catch (const std::exception& e) { + status = UnknownError("Parallel scan worker failed: {}", e.what()); + } catch (...) { + status = UnknownError("Parallel scan worker failed with an unknown exception"); + } + if (!status.has_value()) { + Fail(status.error()); + } + { + std::lock_guard lock(mutex_); + --active_workers_; + } + consumer_cv_.notify_all(); + } + + Status RunWorker(FileScanTaskReader& reader, ArrowBatchSizer& sizer) { + while (true) { + ICEBERG_ASSIGN_OR_RAISE(auto group, ClaimGroup()); + if (group == nullptr) { + return {}; + } + for (const auto& task : group->tasks()) { + if (IsStopped()) { + return {}; + } + if (task == nullptr) { + return InvalidArgument("Task group contains a null task"); + } + ICEBERG_RETURN_UNEXPECTED(ReadTask(reader, sizer, *task)); + } + } + } + + // Returns the next group, or nullptr when planning is exhausted or the read stopped. + // Only one worker calls the group stream at a time, outside all locks. + Result<std::shared_ptr<FileScanTaskGroup>> ClaimGroup() { + { + std::unique_lock lock(mutex_); + planning_cv_.wait(lock, [this] { return stopped_ || !planning_in_progress_; }); + if (stopped_ || planning_done_) { + return nullptr; + } + planning_in_progress_ = true; + } + + Result<std::optional<std::shared_ptr<FileScanTaskGroup>>> next = std::nullopt; + try { + next = groups_->Next(); + } catch (const std::exception& e) { + next = UnknownError("Planning task groups failed: {}", e.what()); + } catch (...) { + next = UnknownError("Planning task groups failed with an unknown exception"); + } + + const bool finished = !next.has_value() || !next.value().has_value(); + if (finished) { + // This worker still holds the planning token, so no other worker uses the + // stream. Destroy it now, outside locks, so metrics reporting runs at EOF. + groups_.reset(); + } + + { + std::lock_guard lock(mutex_); + planning_in_progress_ = false; + if (finished) { + planning_done_ = true; + } + } + planning_cv_.notify_all(); + + if (!next.has_value()) { + return unexpected(WithContext(next.error(), "Failed to plan task groups")); + } + if (!next.value().has_value()) { + return nullptr; + } + auto group = std::move(next.value().value()); + if (group == nullptr) { + return InvalidArgument("Task group stream returned a null group"); + } + if (metrics_ != nullptr) { + metrics_->Update([](auto& snapshot) { ++snapshot.groups_claimed; }); + } + return group; + } + + Status ReadTask(FileScanTaskReader& reader, ArrowBatchSizer& sizer, + const FileScanTask& task) { + const std::string context = DescribeTask(task); + auto opened = reader.Open(task); + if (!opened.has_value()) { + return unexpected(WithContext(opened.error(), "Failed to open " + context)); + } + ArrowArrayStream stream = std::move(opened.value()); + StreamGuard stream_guard(&stream); + if (metrics_ != nullptr) { + metrics_->Update([](auto& snapshot) { ++snapshot.tasks_opened; }); + } + + auto last_error = [&stream]() { + const char* message = stream.get_last_error(&stream); + return message == nullptr ? std::string("unknown error") : std::string(message); + }; + + ArrowSchema task_schema{}; + if (int code = stream.get_schema(&stream, &task_schema); code != 0) { + return IOError("Failed to read the schema of {} (code {}): {}", context, code, + last_error()); + } + internal::ArrowSchemaGuard task_schema_guard(&task_schema); + if (auto status = CheckSameLayout(output_schema_, task_schema, "root", false); + !status.has_value()) { + return unexpected(WithContext( + status.error(), + "Task stream schema does not match the projected schema for " + context)); + } + + // Enqueue() reports a stop, so no separate check is needed per batch. + while (true) { + ArrowArray batch{}; + if (int code = stream.get_next(&stream, &batch); code != 0) { + return IOError("Failed to read {} (code {}): {}", context, code, last_error()); + } + if (batch.release == nullptr) { + return {}; + } + auto charge = sizer.Measure(batch); + if (!charge.has_value()) { + ReleaseArray(&batch); + return unexpected( + WithContext(charge.error(), "Failed to size a batch of " + context)); + } + if (!Enqueue(batch, charge.value())) { + // Stopped while waiting; the array was not admitted. + ReleaseArray(&batch); + return {}; + } + } + } + + // Blocks until the batch is admitted in FIFO order or the read stops. Returns + // false when stopped, in which case the caller still owns `batch`. + bool Enqueue(const ArrowArray& batch, int64_t charge) { + const auto wait_start = std::chrono::steady_clock::now(); + std::unique_lock lock(mutex_); + const uint64_t ticket = next_ticket_++; + held_bytes_ += charge; + RecordMetrics([this](auto& snapshot) { + snapshot.peak_held_bytes = std::max(snapshot.peak_held_bytes, held_bytes_); + }); + + producer_cv_.wait(lock, [&] { Review Comment: If we use a shared executor, can the reader yield when the queue fills? If all the pool threads are waiting here, none are available for manifest planning. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
