Skip to content

Commit

Permalink
storage: limit segments per term in compaction backlog calculation
Browse files Browse the repository at this point in the history
The previous commit limited the computational complexity
of the per-term calculation.

This commit limits the max allocation size done while building
the list of segments in a term.

As with the previous commit, this makes no difference in typical
compaction use cases, but protects us from pathological cases.
  • Loading branch information
jcsp committed Jun 26, 2023
1 parent 4f0197b commit 730c8b4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/v/storage/disk_log_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,14 @@ int64_t disk_log_impl::compaction_backlog() const {
int64_t backlog = 0;
std::vector<ss::lw_shared_ptr<segment>> segments_this_term;

// Limit how large we will try to allocate the sgements_this_term vector:
// this protects us against corner cases where a term has a really large
// number of segments. Typical compaction use cases will have many fewer
// segments per term than this (because segments are continuously compacted
// away). Corner cases include non-compactible data in a compacted topic,
// or enabling compaction on a previously non-compacted topic.
static constexpr size_t limit_segments_this_term = 1024;

for (auto& s : _segs) {
if (!s->finished_self_compaction()) {
backlog += static_cast<int64_t>(s->size_bytes());
Expand All @@ -1802,7 +1810,10 @@ int64_t disk_log_impl::compaction_backlog() const {
std::move(segments_this_term), cf);
segments_this_term.clear();
}
segments_this_term.push_back(s);

if (segments_this_term.size() < limit_segments_this_term) {
segments_this_term.push_back(s);
}
}

// Consume segments from last term in the log after falling out of loop
Expand Down

0 comments on commit 730c8b4

Please sign in to comment.