iceberg-cpp
Loading...
Searching...
No Matches
expire_snapshots.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
22#include <cstdint>
23#include <functional>
24#include <memory>
25#include <optional>
26#include <string>
27#include <unordered_map>
28#include <unordered_set>
29#include <vector>
30
31#include "iceberg/iceberg_export.h"
32#include "iceberg/result.h"
33#include "iceberg/type_fwd.h"
35#include "iceberg/util/executor.h"
36#include "iceberg/util/timepoint.h"
37
40
41namespace iceberg {
42
44enum class CleanupLevel : uint8_t {
46 kNone,
51 kAll
52};
53
68class ICEBERG_EXPORT ExpireSnapshots : public PendingUpdate {
69 public:
70 static Result<std::shared_ptr<ExpireSnapshots>> Make(
71 std::shared_ptr<TransactionContext> ctx);
72
73 ~ExpireSnapshots() override;
74
75 struct ApplyResult {
76 std::vector<std::string> refs_to_remove;
77 std::vector<int64_t> snapshot_ids_to_remove;
78 std::vector<int32_t> partition_spec_ids_to_remove;
79 std::unordered_set<int32_t> schema_ids_to_remove;
80 std::shared_ptr<const TableMetadata> metadata_before_expiration;
81 };
82
87 ExpireSnapshots& ExpireSnapshotId(int64_t snapshot_id);
88
93 ExpireSnapshots& ExpireOlderThan(int64_t timestamp_millis);
94
108 ExpireSnapshots& RetainLast(int num_snapshots);
109
123 ExpireSnapshots& DeleteWith(std::function<void(const std::string&)> delete_func);
124
129 ExpireSnapshots& PlanWith(Executor& executor);
130
145
150 ExpireSnapshots& CleanExpiredMetadata(bool clean);
151
156 ExpireSnapshots& ExecuteDeleteWith(Executor& executor);
157
158 Kind kind() const final { return Kind::kExpireSnapshots; }
159 bool IsRetryable() const override { return true; }
160
163 Result<ApplyResult> Apply();
164
174 Status Finalize(Result<const TableMetadata*> commit_result) override;
175
176 private:
177 explicit ExpireSnapshots(std::shared_ptr<TransactionContext> ctx);
178
179 using SnapshotToRef = std::unordered_map<std::string, std::shared_ptr<SnapshotRef>>;
180
181 Result<SnapshotToRef> ComputeRetainedRefs(const SnapshotToRef& refs) const;
182
183 Result<std::unordered_set<int64_t>> ComputeBranchSnapshotsToRetain(
184 int64_t snapshot_id, TimePointMs expire_snapshot_older_than,
185 int32_t min_snapshots_to_keep) const;
186
187 Result<std::unordered_set<int64_t>> ComputeAllBranchSnapshotIdsToRetain(
188 const SnapshotToRef& refs) const;
189
190 Result<std::unordered_set<int64_t>> UnreferencedSnapshotIdsToRetain(
191 const SnapshotToRef& refs) const;
192
193 const TimePointMs current_time_ms_;
194 const int64_t default_max_ref_age_ms_;
195 int32_t default_min_num_snapshots_;
196 TimePointMs default_expire_older_than_;
197 std::function<void(const std::string&)> delete_func_;
198 std::vector<int64_t> snapshot_ids_to_expire_;
199 enum CleanupLevel cleanup_level_ { CleanupLevel::kAll };
200 OptionalExecutor plan_executor_;
201 bool clean_expired_metadata_{false};
202 bool specified_snapshot_id_{false};
203 OptionalExecutor delete_executor_;
204
206 std::optional<ApplyResult> apply_result_;
207};
208
209} // namespace iceberg
Schedules iceberg-cpp internal planning tasks.
Definition executor.h:43
API for removing old snapshots from a table.
Definition expire_snapshots.h:68
bool IsRetryable() const override
Whether this update can be retried after a commit conflict.
Definition expire_snapshots.h:159
Kind kind() const final
Return the kind of this pending update.
Definition expire_snapshots.h:158
Base class for all kinds of table metadata updates.
Definition pending_update.h:41
CleanupLevel
An enum representing possible clean up levels used in snapshot expiration.
Definition expire_snapshots.h:44
Definition expire_snapshots.h:75