|
18 | 18 | */ |
19 | 19 |
|
20 | 20 | #include <algorithm> |
| 21 | +#include <atomic> |
| 22 | +#include <cstdint> |
21 | 23 | #include <cstdlib> |
| 24 | +#include <exception> |
| 25 | +#include <format> |
| 26 | +#include <functional> |
| 27 | +#include <future> |
22 | 28 | #include <memory> |
23 | 29 | #include <mutex> |
24 | 30 | #include <optional> |
25 | 31 | #include <shared_mutex> |
| 32 | +#include <span> |
26 | 33 | #include <string> |
27 | 34 | #include <string_view> |
| 35 | +#include <thread> |
28 | 36 | #include <unordered_map> |
29 | 37 | #include <utility> |
30 | 38 | #include <vector> |
|
39 | 47 | #include "iceberg/arrow/arrow_status_internal.h" |
40 | 48 | #include "iceberg/arrow/s3/s3_properties.h" |
41 | 49 | #include "iceberg/logging/log_macros.h" |
| 50 | +#include "iceberg/logging/logger.h" |
| 51 | +#include "iceberg/util/executor.h" |
42 | 52 | #include "iceberg/util/macros.h" |
43 | 53 | #include "iceberg/util/property_util.h" |
44 | 54 | #include "iceberg/util/string_util.h" |
45 | 55 |
|
46 | 56 | namespace iceberg::arrow { |
47 | 57 |
|
| 58 | +namespace { |
| 59 | + |
| 60 | +struct DeleteTasks { |
| 61 | + std::vector<std::future<void>> futures; |
| 62 | + |
| 63 | + ~DeleteTasks() { |
| 64 | + for (auto& future : futures) { |
| 65 | + if (future.valid()) { |
| 66 | + future.wait(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +// Submit may throw instead of returning an error; both reject the worker. |
| 73 | +Status SubmitWorker(Executor& executor, ExecutorTask task) { |
| 74 | + try { |
| 75 | + return executor.Submit(std::move(task)); |
| 76 | + } catch (const std::exception& e) { |
| 77 | + return IOError("Submit threw an exception: {}", e.what()); |
| 78 | + } catch (...) { |
| 79 | + return IOError("Submit threw an unknown exception"); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +DeleteExecutorState delete_executor; |
| 84 | + |
| 85 | +} // namespace |
| 86 | + |
| 87 | +Status DeleteExecutorState::Initialize(Executor* executor) { |
| 88 | + std::lock_guard lock(mutex_); |
| 89 | + if (initialized_) { |
| 90 | + if (executor_ != executor) { |
| 91 | + return InvalidArgument("Delete executor is already initialized"); |
| 92 | + } |
| 93 | + return {}; |
| 94 | + } |
| 95 | + executor_ = executor; |
| 96 | + initialized_ = true; |
| 97 | + return {}; |
| 98 | +} |
| 99 | + |
| 100 | +Executor* DeleteExecutorState::Get() { |
| 101 | + std::lock_guard lock(mutex_); |
| 102 | + initialized_ = true; |
| 103 | + return executor_; |
| 104 | +} |
| 105 | + |
| 106 | +Status BulkDeleteFiles(std::span<const std::string> file_locations, Executor* executor, |
| 107 | + size_t parallelism, |
| 108 | + const std::function<Status(const std::string&)>& delete_file) { |
| 109 | + ICEBERG_PRECHECK(parallelism > 0, "Delete parallelism must be positive"); |
| 110 | + auto logger = GetCurrentLogger(); |
| 111 | + std::atomic<size_t> next = 0; |
| 112 | + std::atomic<size_t> failed = 0; |
| 113 | + auto work = [&] { |
| 114 | + ScopedLogger bind(logger); |
| 115 | + while (true) { |
| 116 | + const auto index = next.fetch_add(1, std::memory_order_relaxed); |
| 117 | + if (index >= file_locations.size()) { |
| 118 | + return; |
| 119 | + } |
| 120 | + const auto& location = file_locations[index]; |
| 121 | + Status status; |
| 122 | + try { |
| 123 | + status = delete_file(location); |
| 124 | + } catch (const std::exception& e) { |
| 125 | + status = IOError("Delete threw an exception: {}", e.what()); |
| 126 | + } catch (...) { |
| 127 | + status = IOError("Delete threw an unknown exception"); |
| 128 | + } |
| 129 | + if (!status.has_value()) { |
| 130 | + failed.fetch_add(1, std::memory_order_relaxed); |
| 131 | + ICEBERG_LOG_WARN("Failed to delete {}: {}", location, status.error().message); |
| 132 | + } |
| 133 | + } |
| 134 | + }; |
| 135 | + |
| 136 | + std::optional<Error> submission_error; |
| 137 | + try { |
| 138 | + if (executor == nullptr) { |
| 139 | + work(); |
| 140 | + } else { |
| 141 | + const auto workers = std::min(parallelism, file_locations.size()); |
| 142 | + DeleteTasks tasks; |
| 143 | + tasks.futures.reserve(workers); |
| 144 | + size_t accepted = 0; |
| 145 | + for (; accepted < workers; ++accepted) { |
| 146 | + std::packaged_task<void()> task(work); |
| 147 | + // Submit may accept the task before failing. |
| 148 | + tasks.futures.push_back(task.get_future()); |
| 149 | + ExecutorTask executor_task([task = std::move(task)]() mutable { task(); }); |
| 150 | + if (auto status = SubmitWorker(*executor, std::move(executor_task)); |
| 151 | + !status.has_value()) { |
| 152 | + submission_error = std::move(status.error()); |
| 153 | + break; |
| 154 | + } |
| 155 | + } |
| 156 | + // A rejected worker's future is only waited for, by `tasks`. |
| 157 | + for (size_t i = 0; i < accepted; ++i) { |
| 158 | + tasks.futures[i].get(); |
| 159 | + } |
| 160 | + } |
| 161 | + } catch (const std::exception& e) { |
| 162 | + return IOError("Bulk delete execution failed: {}", e.what()); |
| 163 | + } catch (...) { |
| 164 | + return IOError("Bulk delete execution failed with an unknown exception"); |
| 165 | + } |
| 166 | + if (submission_error.has_value()) { |
| 167 | + if (failed > 0) { |
| 168 | + submission_error->message += std::format("; failed to delete {} of {} files", |
| 169 | + failed.load(), file_locations.size()); |
| 170 | + } |
| 171 | + return Status::unexpected_type(std::move(*submission_error)); |
| 172 | + } |
| 173 | + if (failed > 0) { |
| 174 | + return IOError("Failed to delete {} of {} files", failed.load(), |
| 175 | + file_locations.size()); |
| 176 | + } |
| 177 | + return {}; |
| 178 | +} |
| 179 | + |
| 180 | +Status InitializeS3FileIODeleteExecutor(Executor* executor) { |
| 181 | + return delete_executor.Initialize(executor); |
| 182 | +} |
| 183 | + |
48 | 184 | #if ICEBERG_S3_ENABLED |
49 | 185 |
|
50 | 186 | namespace { |
@@ -181,9 +317,11 @@ std::string CanonicalizeS3Scheme(std::string_view location) { |
181 | 317 | class ArrowS3FileIO final : public FileIO, public SupportsStorageCredentials { |
182 | 318 | public: |
183 | 319 | ArrowS3FileIO(std::shared_ptr<::arrow::fs::FileSystem> arrow_fs, |
184 | | - std::unordered_map<std::string, std::string> default_properties) |
| 320 | + std::unordered_map<std::string, std::string> default_properties, |
| 321 | + size_t delete_parallelism) |
185 | 322 | : default_file_io_(std::make_shared<ArrowFileSystemFileIO>(std::move(arrow_fs))), |
186 | | - default_properties_(std::move(default_properties)) {} |
| 323 | + default_properties_(std::move(default_properties)), |
| 324 | + delete_parallelism_(delete_parallelism) {} |
187 | 325 |
|
188 | 326 | Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location) override; |
189 | 327 |
|
@@ -237,6 +375,7 @@ class ArrowS3FileIO final : public FileIO, public SupportsStorageCredentials { |
237 | 375 |
|
238 | 376 | std::shared_ptr<ArrowFileSystemFileIO> default_file_io_; |
239 | 377 | std::unordered_map<std::string, std::string> default_properties_; |
| 378 | + const size_t delete_parallelism_; |
240 | 379 | // Guards everything below; shared because reads happen per file operation. |
241 | 380 | mutable std::shared_mutex mutex_; |
242 | 381 | std::vector<StorageCredential> storage_credentials_; |
@@ -338,41 +477,35 @@ Status ArrowS3FileIO::DeleteFile(const std::string& file_location) { |
338 | 477 | } |
339 | 478 |
|
340 | 479 | Status ArrowS3FileIO::DeleteFiles(const std::vector<std::string>& file_locations) { |
341 | | - // One snapshot so the whole batch matches the same delegate generation; only |
342 | | - // ever a handful of delegates, so a linear scan beats hashing. |
| 480 | + // Keep one delegate snapshot for the whole batch. |
343 | 481 | std::shared_ptr<ArrowFileSystemFileIO> fallback; |
344 | 482 | DelegatesByPrefix by_prefix; |
345 | 483 | { |
346 | 484 | std::shared_lock lock(mutex_); |
347 | 485 | fallback = default_file_io_; |
348 | 486 | by_prefix = file_io_by_prefix_; |
349 | 487 | } |
350 | | - std::vector<std::pair<std::shared_ptr<ArrowFileSystemFileIO>, std::vector<std::string>>> |
351 | | - locations_by_io; |
352 | | - for (const auto& file_location : file_locations) { |
353 | | - auto file_io = MatchDelegate(fallback, by_prefix, file_location); |
354 | | - auto it = std::ranges::find_if( |
355 | | - locations_by_io, [&](const auto& entry) { return entry.first == file_io; }); |
356 | | - if (it == locations_by_io.end()) { |
357 | | - locations_by_io.emplace_back(std::move(file_io), |
358 | | - std::vector<std::string>{file_location}); |
359 | | - } else { |
360 | | - it->second.push_back(file_location); |
361 | | - } |
362 | | - } |
363 | | - for (auto& [file_io, locations] : locations_by_io) { |
364 | | - ICEBERG_RETURN_UNEXPECTED(file_io->DeleteFiles(locations)); |
365 | | - } |
366 | | - return {}; |
| 488 | + return BulkDeleteFiles( |
| 489 | + file_locations, delete_executor.Get(), delete_parallelism_, |
| 490 | + [&](const std::string& location) { |
| 491 | + return MatchDelegate(fallback, by_prefix, location)->DeleteFile(location); |
| 492 | + }); |
367 | 493 | } |
368 | 494 |
|
369 | 495 | } // namespace |
370 | 496 |
|
371 | 497 | Result<std::unique_ptr<FileIO>> MakeS3FileIO( |
372 | 498 | const std::unordered_map<std::string, std::string>& properties) { |
| 499 | + size_t delete_parallelism = std::max(1u, std::thread::hardware_concurrency()); |
| 500 | + if (const auto* value = FindProperty(properties, S3Properties::kDeleteNumThreads); |
| 501 | + value != nullptr) { |
| 502 | + ICEBERG_ASSIGN_OR_RAISE(auto count, StringUtils::ParseNumber<int32_t>(*value)); |
| 503 | + ICEBERG_PRECHECK(count > 0, "{} must be positive", S3Properties::kDeleteNumThreads); |
| 504 | + delete_parallelism = static_cast<size_t>(count); |
| 505 | + } |
373 | 506 | // Uses default credentials if properties are empty. |
374 | 507 | ICEBERG_ASSIGN_OR_RAISE(auto fs, BuildArrowS3FileSystem(properties)); |
375 | | - return std::make_unique<ArrowS3FileIO>(std::move(fs), properties); |
| 508 | + return std::make_unique<ArrowS3FileIO>(std::move(fs), properties, delete_parallelism); |
376 | 509 | } |
377 | 510 |
|
378 | 511 | Status FinalizeS3() { |
|
0 commit comments