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 TaskGroup& SetExecutor(OptionalExecutor executor) & {
77 executor_ = std::move(executor);
78 return *this;
79 }
80
81 TaskGroup&& SetExecutor(OptionalExecutor executor) && {
82 executor_ = std::move(executor);
83 return std::move(*this);
84 }
85
86 template <typename F>
87 requires((!kRetryEnabled && internal::OnceStatusTask<F>) ||
88 (kRetryEnabled && internal::RetryableStatusTask<F>))
89 TaskGroup& Submit(F&& task) & {
90 AddTask(std::forward<F>(task));
91 return *this;
92 }
93
94 template <typename F>
95 requires((!kRetryEnabled && internal::OnceStatusTask<F>) ||
96 (kRetryEnabled && internal::RetryableStatusTask<F>))
97 TaskGroup&& Submit(F&& task) && {
98 AddTask(std::forward<F>(task));
99 return std::move(*this);
100 }
101
102 Status Run() && {
103 if (!executor_.has_value()) {
104 return internal::RunTasksSingleThreaded(std::move(tasks_));
105 }
106 return internal::RunTasksParallel(executor_->get(), std::move(tasks_));
107 }
108
109 private:
110 // Shared by both Submit overloads.
111 template <typename F>
112 void AddTask(F&& task) {
113 tasks_.emplace_back([&] {
114 if constexpr (!kRetryEnabled) {
115 return std::forward<F>(task);
116 } else {
117 return [retry_config = retry_config_,
118 task = std::forward<F>(task)]() mutable -> Status {
119 return RetryRunner<RetryPolicy>(retry_config).Run(task);
120 };
121 }
122 }());
123 }
124
125 std::vector<FnOnce<Status()>> tasks_;
126 OptionalExecutor executor_;
127 [[no_unique_address]] RetryConfigStorage retry_config_;
128};
129
130} // namespace iceberg
Definition functional.h:48
Utility class for running tasks with retry logic.
Definition retry_util.h:139
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:151
Definition task_group.h:61
Definition expected.h:1199
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:34
Define Result, Status, and error helpers.
Provide retry policy and runner utilities.
Configuration for retry behavior.
Definition retry_util.h:46