iceberg-cpp
Loading...
Searching...
No Matches
task_group.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
24
25#include <concepts>
26#include <optional>
27#include <type_traits>
28#include <utility>
29#include <vector>
30
32#include "iceberg/result.h"
36
37namespace iceberg {
38
39namespace internal {
40
41template <typename F>
43
44template <typename T>
46 std::is_invocable_r_v<Status, const T&> ||
47 (std::copy_constructible<T> && std::is_invocable_r_v<Status, T&>);
48
49template <typename F>
50concept RetryableStatusTask = std::constructible_from<std::remove_cvref_t<F>, F> &&
52
53ICEBERG_EXPORT Status RunTasksSingleThreaded(std::vector<FnOnce<Status()>> tasks);
54
55ICEBERG_EXPORT Status RunTasksParallel(Executor& executor,
56 std::vector<FnOnce<Status()>> tasks);
57
58} // namespace internal
59
60template <retry::Policy RetryPolicy = retry::NoRetry>
61class ICEBERG_TEMPLATE_CLASS_EXPORT TaskGroup {
62 private:
63 static constexpr bool kRetryEnabled = !std::same_as<RetryPolicy, retry::NoRetry>;
64
65 struct Empty {};
66
67 using RetryConfigStorage = std::conditional_t<kRetryEnabled, RetryConfig, Empty>;
68
69 public:
70 TaskGroup() = default;
71
72 explicit TaskGroup(RetryConfig retry_config)
73 requires(kRetryEnabled)
74 : retry_config_(std::move(retry_config)) {}
75
76 auto&& SetExecutor(this auto&& self, OptionalExecutor executor) {
77 self.executor_ = std::move(executor);
78 return std::forward<decltype(self)>(self);
79 }
80
81 template <typename F>
82 requires((!kRetryEnabled && internal::OnceStatusTask<F>) ||
83 (kRetryEnabled && internal::RetryableStatusTask<F>))
84 auto&& Submit(this auto&& self, F&& task) {
85 self.tasks_.emplace_back([&] {
86 if constexpr (!kRetryEnabled) {
87 return std::forward<F>(task);
88 } else {
89 return [retry_config = self.retry_config_,
90 task = std::forward<F>(task)]() mutable -> Status {
91 return RetryRunner<RetryPolicy>(retry_config).Run(task);
92 };
93 }
94 }());
95 return std::forward<decltype(self)>(self);
96 }
97
98 Status Run() && {
99 if (!executor_.has_value()) {
100 return internal::RunTasksSingleThreaded(std::move(tasks_));
101 }
102 return internal::RunTasksParallel(executor_->get(), std::move(tasks_));
103 }
104
105 private:
106 std::vector<FnOnce<Status()>> tasks_;
107 OptionalExecutor executor_;
108 [[no_unique_address]] RetryConfigStorage retry_config_;
109};
110
111} // namespace iceberg
Definition functional.h:48
Utility class for running tasks with retry logic.
Definition retry_util.h:138
auto Run(F &&task, int32_t *attempt_counter=nullptr) -> std::remove_cvref_t< std::invoke_result_t< F & > >
Run a task that returns a Result<T>
Definition retry_util.h:150
Definition task_group.h:61
Definition task_group.h:42
Definition task_group.h:45
Definition task_group.h:50
Definition functional.h:41
Define task executor interfaces.
Provide move-only function wrappers.
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
Define Result, Status, and error helpers.
Provide retry policy and runner utilities.
Configuration for retry behavior.
Definition retry_util.h:46