Skip to content

Commit 4851928

Browse files
emmaling27Convex, Inc.
authored and
Convex, Inc.
committed
Remove unnecessary find_segments_to_compact and CompactionReason::Unknown (#27057)
This removes the extra call to `find_segments_to_compact` by putting the segments to compact and compaction reason in the `CompactionJob`. Also gets rid of `CompactionReason::Unknown`, instead making the segments to compact optional. GitOrigin-RevId: c14d06bfd9cd2ff601b5f50c7371d14cd7b3daa2
1 parent 8fcc62f commit 4851928

File tree

2 files changed

+37
-64
lines changed

2 files changed

+37
-64
lines changed

crates/database/src/index_workers/search_compactor.rs

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,16 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
111111
};
112112
let name = index_metadata.name;
113113

114-
let needs_compaction = match &config.on_disk_state {
114+
let maybe_segments_to_compact = match &config.on_disk_state {
115115
SearchOnDiskState::Backfilling(BackfillState {
116116
segments,
117117
backfill_snapshot_ts,
118118
..
119119
}) => {
120120
if backfill_snapshot_ts.is_none() {
121-
false
121+
continue;
122122
} else {
123-
Self::segments_need_compaction(
123+
Self::find_segments_to_compact(
124124
segments,
125125
&config.developer_config,
126126
&self.config,
@@ -134,14 +134,14 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
134134
| SearchOnDiskState::Backfilled(SearchSnapshot {
135135
data: SnapshotData::MultiSegment(segments),
136136
..
137-
}) => Self::segments_need_compaction(
137+
}) => Self::find_segments_to_compact(
138138
segments,
139139
&config.developer_config,
140140
&self.config,
141141
)?,
142-
_ => false,
142+
_ => continue,
143143
};
144-
if needs_compaction {
144+
if let Some((segments_to_compact, compaction_reason)) = maybe_segments_to_compact {
145145
tracing::info!(
146146
"Queueing {:?} index for compaction: {name:?}",
147147
Self::search_type()
@@ -151,6 +151,8 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
151151
index_name: name.clone(),
152152
developer_config: config.developer_config.clone(),
153153
on_disk_state: config.on_disk_state,
154+
segments_to_compact,
155+
compaction_reason,
154156
};
155157
to_build.push(job);
156158
}
@@ -160,43 +162,21 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
160162

161163
async fn build_one(&self, job: CompactionJob<T>) -> anyhow::Result<u64> {
162164
let timer = compaction_build_one_timer(Self::search_type());
163-
let (segments, snapshot_ts) = match job.on_disk_state {
165+
let snapshot_ts = match job.on_disk_state {
164166
SearchOnDiskState::Backfilling(BackfillState {
165-
segments,
166167
backfill_snapshot_ts,
167168
..
168-
}) => {
169-
let ts = backfill_snapshot_ts.with_context(|| {
170-
format!(
171-
"Trying to compact backfilling {:?} segments with no backfill timestamp",
172-
Self::search_type()
173-
)
174-
})?;
175-
(segments, ts)
176-
},
169+
}) => backfill_snapshot_ts.with_context(|| {
170+
format!(
171+
"Trying to compact backfilling {:?} segments with no backfill timestamp",
172+
Self::search_type()
173+
)
174+
})?,
177175
SearchOnDiskState::Backfilled(snapshot)
178-
| SearchOnDiskState::SnapshottedAt(snapshot) => {
179-
let segments = match snapshot.data {
180-
SnapshotData::Unknown(_) => {
181-
anyhow::bail!(
182-
"Trying to compact unknown {:?} snapshot",
183-
Self::search_type()
184-
);
185-
},
186-
SnapshotData::MultiSegment(segments) => segments,
187-
SnapshotData::SingleSegment(_) => {
188-
anyhow::bail!(
189-
"Trying to compact a single segment {:?} index!",
190-
Self::search_type()
191-
);
192-
},
193-
};
194-
(segments, snapshot.ts)
195-
},
176+
| SearchOnDiskState::SnapshottedAt(snapshot) => snapshot.ts,
196177
};
197178

198-
let (segments_to_compact, reason) =
199-
Self::find_segments_to_compact(&segments, &job.developer_config, &self.config)?;
179+
let segments_to_compact = job.segments_to_compact;
200180
anyhow::ensure!(segments_to_compact.len() > 0);
201181

202182
let total_compacted_segments = segments_to_compact.len();
@@ -231,7 +211,7 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
231211
Self::format(&new_segment, &job.developer_config)?,
232212
);
233213

234-
finish_compaction_timer(timer, reason);
214+
finish_compaction_timer(timer, job.compaction_reason);
235215
Ok(total_compacted_segments)
236216
}
237217

@@ -290,7 +270,7 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
290270
segments: &'a Vec<T::Segment>,
291271
developer_config: &'a T::DeveloperConfig,
292272
compaction_config: &CompactionConfig,
293-
) -> anyhow::Result<(Vec<T::Segment>, CompactionReason)> {
273+
) -> anyhow::Result<Option<(Vec<T::Segment>, CompactionReason)>> {
294274
fn to_owned<R: Clone>(borrowed: Vec<&R>) -> Vec<R> {
295275
borrowed.into_iter().cloned().collect_vec()
296276
}
@@ -315,7 +295,10 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
315295
let compact_small =
316296
Self::get_compactable_segments(small_segments, developer_config, compaction_config)?;
317297
if let Some(compact_small) = compact_small {
318-
return Ok((to_owned(compact_small), CompactionReason::SmallSegments));
298+
return Ok(Some((
299+
to_owned(compact_small),
300+
CompactionReason::SmallSegments,
301+
)));
319302
}
320303
// Next check to see if we have too many larger segments and if so, compact
321304
// them.
@@ -329,7 +312,10 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
329312
compaction_config,
330313
)?;
331314
if let Some(compact_large) = compact_large {
332-
return Ok((to_owned(compact_large), CompactionReason::LargeSegments));
315+
return Ok(Some((
316+
to_owned(compact_large),
317+
CompactionReason::LargeSegments,
318+
)));
333319
}
334320

335321
// Finally check to see if any individual segment has a large number of deleted
@@ -349,7 +335,7 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
349335
})?
350336
.map(|(segment, _)| vec![segment]);
351337
if let Some(compact_deletes) = compact_deletes {
352-
return Ok((to_owned(compact_deletes), CompactionReason::Deletes));
338+
return Ok(Some((to_owned(compact_deletes), CompactionReason::Deletes)));
353339
}
354340
tracing::trace!(
355341
"Found no segments to compact, segments: {:#?}",
@@ -366,19 +352,7 @@ impl<RT: Runtime, T: SearchIndex> SearchIndexCompactor<RT, T> {
366352
})
367353
.collect_vec()
368354
);
369-
Ok((vec![], CompactionReason::Unknown))
370-
}
371-
372-
fn segments_need_compaction(
373-
segments: &Vec<T::Segment>,
374-
developer_config: &T::DeveloperConfig,
375-
compaction_config: &CompactionConfig,
376-
) -> anyhow::Result<bool> {
377-
Ok(
378-
!Self::find_segments_to_compact(segments, developer_config, compaction_config)?
379-
.0
380-
.is_empty(),
381-
)
355+
Ok(None)
382356
}
383357

384358
async fn compact(
@@ -461,4 +435,6 @@ struct CompactionJob<T: SearchIndex> {
461435
index_name: TabletIndexName,
462436
developer_config: T::DeveloperConfig,
463437
on_disk_state: SearchOnDiskState<T>,
438+
segments_to_compact: Vec<T::Segment>,
439+
compaction_reason: CompactionReason,
464440
}

crates/database/src/metrics.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::LazyLock;
2+
13
use ::search::metrics::{
24
SearchType,
35
SEARCH_TYPE_LABEL,
@@ -880,8 +882,6 @@ pub fn log_non_deleted_documents_per_search_index(count: u64, search_type: Searc
880882
const COMPACTION_REASON_LABEL: &str = "compaction_reason";
881883

882884
pub enum CompactionReason {
883-
// TODO(emma) remove and replace with optional result
884-
Unknown,
885885
SmallSegments,
886886
LargeSegments,
887887
Deletes,
@@ -890,14 +890,15 @@ pub enum CompactionReason {
890890
impl CompactionReason {
891891
fn metric_label(&self) -> StaticMetricLabel {
892892
let label = match self {
893-
CompactionReason::Unknown => "unknown",
894893
CompactionReason::SmallSegments => "small",
895894
CompactionReason::LargeSegments => "large",
896895
CompactionReason::Deletes => "deletes",
897896
};
898897
StaticMetricLabel::new(COMPACTION_REASON_LABEL, label)
899898
}
900899
}
900+
static UNKNOWN_COMPACTION_LABEL: LazyLock<StaticMetricLabel> =
901+
LazyLock::new(|| StaticMetricLabel::new(COMPACTION_REASON_LABEL, "unknown"));
901902

902903
register_convex_histogram!(
903904
COMPACTION_BUILD_ONE_SECONDS,
@@ -906,16 +907,12 @@ register_convex_histogram!(
906907
);
907908
pub fn compaction_build_one_timer(search_type: SearchType) -> StatusTimer {
908909
let mut timer = StatusTimer::new(&COMPACTION_BUILD_ONE_SECONDS);
909-
timer.add_label(CompactionReason::Unknown.metric_label());
910-
timer.add_label(search_type.tag());
910+
timer.replace_label(UNKNOWN_COMPACTION_LABEL.clone(), search_type.tag());
911911
timer
912912
}
913913

914914
pub fn finish_compaction_timer(mut timer: StatusTimer, reason: CompactionReason) {
915-
timer.replace_label(
916-
CompactionReason::Unknown.metric_label(),
917-
reason.metric_label(),
918-
);
915+
timer.add_label(reason.metric_label());
919916
timer.finish();
920917
}
921918

0 commit comments

Comments
 (0)