Skip to content

Commit 0485599

Browse files
authored
Merge pull request #35 from mathworks/gbornste/snapshots-lite
Snapshots Lite, No Manual Deletions
2 parents 6cd0346 + 45b1665 commit 0485599

17 files changed

Lines changed: 716 additions & 114 deletions

‎conanfile.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def requirements(self):
9797
self.requires("yaml-cpp/[>=0.9.0 <1]")
9898
self.requires("zlib/1.3.1", **OVERRIDE)
9999

100+
# boost/1.88.0 and ninja/1.13.2 depend (exactly) on libbacktrace/cci.20210118
101+
#
102+
self.requires("libbacktrace/[>=cci.20240730]", **OVERRIDE)
103+
100104
if platform.system() == "Linux":
101105
if self.options.with_keyvcr:
102106
self.requires("keyvcr/[>=0.2.2 <1]", **VISIBLE)

‎src/turtle_kv/checkpoint.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,32 @@ namespace turtle_kv {
6060
};
6161
}
6262

63+
//==#==========+==+=+=++=+++++++++++-+-+--+----- --- -- - - - -
64+
//
65+
/*static*/ StatusOr<Checkpoint> Checkpoint::recover(
66+
llfs::PageCache& page_cache,
67+
const PackedCheckpoint& packed_checkpoint,
68+
CheckpointLock&& checkpoint_lock) noexcept
69+
{
70+
BATT_CHECK_GT(packed_checkpoint.edit_offset_upper_bound, 0)
71+
<< "Invalid PackedCheckpoint: batch_upper_bound==0 indicates no checkpoint.";
72+
73+
const llfs::PageId tree_root_id = packed_checkpoint.new_tree_root.as_page_id();
74+
75+
Subtree tree = Subtree::from_page_id(tree_root_id);
76+
77+
BATT_ASSIGN_OK_RESULT(
78+
i32 height, tree.get_height(page_cache, llfs::PageCacheOvercommit::not_allowed()));
79+
80+
return Checkpoint{
81+
tree_root_id,
82+
std::make_shared<Subtree>(std::move(tree)),
83+
height,
84+
EditOffset{packed_checkpoint.edit_offset_upper_bound.value()},
85+
std::move(checkpoint_lock),
86+
};
87+
}
88+
6389
//==#==========+==+=+=++=+++++++++++-+-+--+----- --- -- - - - -
6490
//
6591
/*static*/ Checkpoint Checkpoint::make_empty() noexcept

‎src/turtle_kv/checkpoint.hpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class Checkpoint
4747
llfs::SlotParse& slot,
4848
const PackedCheckpoint& checkpoint) noexcept;
4949

50+
/** \brief Returns a valid, in-memory Checkpoint from the given packed checkpoint data and an
51+
* existing CheckpointLock (e.g., cloned from another Checkpoint).
52+
*/
53+
static StatusOr<Checkpoint> recover(llfs::PageCache& page_cache,
54+
const PackedCheckpoint& checkpoint,
55+
CheckpointLock&& checkpoint_lock) noexcept;
56+
5057
/** \brief Returns the canonical, empty initial Checkpoint with invalid llfs::PageId (indicating
5158
* there is no root page) and EditOffset upper bound of 0.
5259
*/

‎src/turtle_kv/checkpoint_generator.cpp‎

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace turtle_kv {
1717
llfs::PageCache& cache,
1818
boost::intrusive_ptr<FilterPageWriteState>&& filter_page_write_state,
1919
Checkpoint&& base_checkpoint,
20-
llfs::Volume& checkpoint_volume) noexcept
20+
llfs::Volume& checkpoint_volume,
21+
const PackedActiveCheckpoints& recovered_active_checkpoints) noexcept
2122
//----- --- -- - - - -
2223
: worker_pool_{worker_pool}
2324
, tree_options_{tree_options}
@@ -26,12 +27,14 @@ namespace turtle_kv {
2627
, base_checkpoint_{std::move(base_checkpoint)}
2728
, stop_requested_{false}
2829
, checkpoint_volume_{checkpoint_volume}
30+
, active_checkpoints_{recovered_active_checkpoints}
2931
{
30-
Optional<llfs::SlotRange> prev_slot_range = base_checkpoint.slot_range();
32+
Optional<llfs::SlotRange> prev_slot_range = this->base_checkpoint_.slot_range();
3133
if (prev_slot_range) {
3234
this->slot_sequencer_.set_current(*prev_slot_range);
3335
this->slot_sequencer_ = this->slot_sequencer_.get_next();
3436
}
37+
3538
this->initialize_job();
3639
}
3740

@@ -102,14 +105,6 @@ StatusOr<usize> CheckpointGenerator::apply_batch(std::unique_ptr<DeltaBatch>&& b
102105

103106
VLOG(2) << "checkpoint task: flushing batch to create new checkpoint tree";
104107

105-
{
106-
Optional<llfs::PageId> root_id = this->base_checkpoint_.maybe_root_id();
107-
if (root_id) {
108-
this->job_->new_root(*root_id);
109-
this->roots_to_remove_.emplace_back(*root_id);
110-
}
111-
}
112-
113108
batt::CancelToken cancel_token;
114109

115110
*this->cancel_token_.lock() = cancel_token;
@@ -132,24 +127,6 @@ StatusOr<usize> CheckpointGenerator::apply_batch(std::unique_ptr<DeltaBatch>&& b
132127

133128
this->current_batch_count_ += 1;
134129

135-
// Periodically serialize to unpin some pages, controlling total memory usage.
136-
//
137-
static const usize serialize_limit =
138-
batt::getenv_as<usize>("TURTLE_KV_SERIALIZE_EVERY_N_BATCHES").value_or(0);
139-
140-
if (serialize_limit != 0 && (this->current_batch_count_ % serialize_limit) == 0) {
141-
BATT_REQUIRE_OK(this->serialize_checkpoint(overcommit));
142-
143-
const llfs::PageId root_id = batt::get_or_panic(this->base_checkpoint_.maybe_root_id());
144-
this->job_->new_root(root_id);
145-
this->clear_old_roots();
146-
147-
BATT_REQUIRE_OK(this->job_->prune(/*callers=*/0));
148-
149-
this->job_->delete_root(root_id);
150-
this->job_->unpin_all();
151-
}
152-
153130
return {1u};
154131
}
155132

@@ -185,16 +162,6 @@ Status CheckpointGenerator::serialize_checkpoint(llfs::PageCacheOvercommit& over
185162
return OkStatus();
186163
}
187164

188-
//==#==========+==+=+=++=+++++++++++-+-+--+----- --- -- - - - -
189-
//
190-
void CheckpointGenerator::clear_old_roots() noexcept
191-
{
192-
for (const llfs::PageId root_id : this->roots_to_remove_) {
193-
this->job_->delete_root(root_id);
194-
}
195-
this->roots_to_remove_.clear();
196-
}
197-
198165
//==#==========+==+=+=++=+++++++++++-+-+--+----- --- -- - - - -
199166
//
200167
StatusOr<batt::Grant> CheckpointGenerator::reserve_slot_grant_for_checkpoints(usize slot_grant_size)
@@ -227,8 +194,6 @@ StatusOr<std::unique_ptr<CheckpointJob>> CheckpointGenerator::finalize_checkpoin
227194
this->prev_batch_id_ = None;
228195
this->current_batch_count_ = 0;
229196

230-
this->clear_old_roots();
231-
232197
//+++++++++++-+-+--+----- --- -- - - - -
233198

234199
auto checkpoint_job = std::make_unique<CheckpointJob>();
@@ -240,19 +205,21 @@ StatusOr<std::unique_ptr<CheckpointJob>> CheckpointGenerator::finalize_checkpoin
240205
checkpoint_job->edit_offset_upper_bound = edit_offset_upper_bound;
241206
checkpoint_job->batch_count = batch_count;
242207

243-
checkpoint_job->packed_checkpoint.emplace(
244-
llfs::PackAsVariant<CheckpointLogEvent, PackedCheckpoint>{
245-
PackedCheckpoint{
246-
.edit_offset_upper_bound = this->base_checkpoint_.edit_offset_upper_bound().value(),
247-
.new_tree_root = llfs::PackedPageId::from(this->base_checkpoint_.root_id()),
248-
},
208+
this->active_checkpoints_.push_back(PackedCheckpoint{
209+
.edit_offset_upper_bound = this->base_checkpoint_.edit_offset_upper_bound().value(),
210+
.new_tree_root = llfs::PackedPageId::from(this->base_checkpoint_.root_id()),
211+
});
212+
213+
checkpoint_job->active_checkpoints.emplace(
214+
llfs::PackAsVariant<CheckpointLogEvent, PackedActiveCheckpoints>{
215+
this->active_checkpoints_,
249216
});
250217

251-
// Package the job up with a PackedCheckpoint event record so we can append it to the Volume.
218+
// Package the job up with a PackedActiveCheckpoints event record so we can append it to the Volume.
252219
//
253220
StatusOr<llfs::AppendableJob> appendable_job =
254221
llfs::make_appendable_job(std::move(this->job_),
255-
llfs::PackableRef{*checkpoint_job->packed_checkpoint});
222+
llfs::PackableRef{*checkpoint_job->active_checkpoints});
256223

257224
BATT_REQUIRE_OK(appendable_job);
258225

‎src/turtle_kv/checkpoint_generator.hpp‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class CheckpointGenerator
5656
llfs::PageCache& cache,
5757
boost::intrusive_ptr<FilterPageWriteState>&& filter_page_write_state,
5858
Checkpoint&& base_checkpoint,
59-
llfs::Volume& checkpoint_volume) noexcept;
59+
llfs::Volume& checkpoint_volume,
60+
const PackedActiveCheckpoints& recovered_active_checkpoints) noexcept;
6061

6162
CheckpointGenerator(const CheckpointGenerator&) = delete;
6263
CheckpointGenerator& operator=(const CheckpointGenerator&) = delete;
@@ -124,11 +125,6 @@ class CheckpointGenerator
124125
*/
125126
Status serialize_checkpoint(llfs::PageCacheOvercommit& overcommit) noexcept;
126127

127-
/** \brief Clears `this->roots_to_remove_` by deleting obsolete root pages from the current job's
128-
* root set.
129-
*/
130-
void clear_old_roots() noexcept;
131-
132128
/** \brief This function first uses a non-blocking call to Volume::reserve on the checkpoint
133129
* volume. If non-blocking reserve fails, it tracks some metrics and then goes into a blocking
134130
* reserve call.
@@ -190,10 +186,6 @@ class CheckpointGenerator
190186
//
191187
llfs::SlotSequencer slot_sequencer_;
192188

193-
// Roots to remove from `job_` when finalizing.
194-
//
195-
batt::SmallVec<llfs::PageId, 8> roots_to_remove_;
196-
197189
// Set to `true` when halt is invoked.
198190
//
199191
batt::Watch<bool> stop_requested_;
@@ -202,6 +194,11 @@ class CheckpointGenerator
202194
//
203195
llfs::Volume& checkpoint_volume_;
204196

197+
// The set of currently active (retained) checkpoints, in sorted order by
198+
// edit_offset_upper_bound.
199+
//
200+
PackedActiveCheckpoints active_checkpoints_{};
201+
205202
// Used to cancel pending checkpoint updates on halt().
206203
//
207204
batt::Mutex<batt::CancelToken> cancel_token_{batt::None};

‎src/turtle_kv/checkpoint_job.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct CheckpointJob {
4747

4848
Optional<Checkpoint> checkpoint;
4949

50-
Optional<llfs::PackAsVariant<CheckpointLogEvent, PackedCheckpoint>> packed_checkpoint;
50+
Optional<llfs::PackAsVariant<CheckpointLogEvent, PackedActiveCheckpoints>> active_checkpoints;
5151

5252
//----- --- -- - - - -
5353

‎src/turtle_kv/checkpoint_log_events.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
namespace turtle_kv {
88

9-
using CheckpointLogEvent = llfs::PackedVariant<PackedCheckpoint>;
9+
using CheckpointLogEvent = llfs::PackedVariant<PackedActiveCheckpoints>;
1010

1111
}

0 commit comments

Comments
 (0)