Skip to content

Commit c553c3c

Browse files
sjuddConvex, Inc.
authored andcommitted
Generalize logging in compactor (#26398)
GitOrigin-RevId: 734bbdf093bf243ba9efaad842ea612e384dac83
1 parent 8678c49 commit c553c3c

File tree

3 files changed

+119
-93
lines changed

3 files changed

+119
-93
lines changed

crates/database/src/metrics.rs

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ fn log_worker_status(is_working: bool, name: &'static str) {
673673

674674
register_convex_histogram!(
675675
SEARCH_AND_VECTOR_BOOTSTRAP_SECONDS,
676-
"Time taken to bootstrap search and vector indexes",
676+
"Time taken to bootstrap text and vector indexes",
677677
&STATUS_LABEL
678678
);
679679
pub fn bootstrap_timer() -> StatusTimer {
@@ -682,18 +682,18 @@ pub fn bootstrap_timer() -> StatusTimer {
682682

683683
register_convex_histogram!(
684684
SEARCH_AND_VECTOR_BOOTSTRAP_COMMITTER_UPDATE_SECONDS,
685-
"Time to update search and vector index bootstrap in the committer"
685+
"Time to update text and vector index bootstrap in the committer"
686686
);
687687
pub fn bootstrap_update_timer() -> Timer<VMHistogram> {
688688
Timer::new(&SEARCH_AND_VECTOR_BOOTSTRAP_COMMITTER_UPDATE_SECONDS)
689689
}
690690
register_convex_counter!(
691691
SEARCH_AND_VECTOR_BOOTSTRAP_COMMITTER_UPDATE_REVISIONS_TOTAL,
692-
"Number of revisions loaded during search and vector bootstrap updates in the committer"
692+
"Number of revisions loaded during text and vector bootstrap updates in the committer"
693693
);
694694
register_convex_counter!(
695695
SEARCH_AND_VECTOR_BOOTSTRAP_COMMITTER_UPDATE_REVISIONS_BYTES,
696-
"Total size of revisions loaded during search and vector bootstrap updates in the committer"
696+
"Total size of revisions loaded during text and vector bootstrap updates in the committer"
697697
);
698698

699699
pub fn finish_bootstrap_update(num_revisions: usize, bytes: usize) {
@@ -709,11 +709,11 @@ pub fn finish_bootstrap_update(num_revisions: usize, bytes: usize) {
709709

710710
register_convex_counter!(
711711
SEARCH_AND_VECTOR_BOOTSTRAP_REVISIONS_TOTAL,
712-
"Number of revisions loaded during search and vector bootstrap"
712+
"Number of revisions loaded during text and vector bootstrap"
713713
);
714714
register_convex_counter!(
715715
SEARCH_AND_VECTOR_BOOTSTRAP_REVISIONS_BYTES,
716-
"Total size of revisions loaded during search and vector bootstrap"
716+
"Total size of revisions loaded during text and vector bootstrap"
717717
);
718718
pub fn finish_bootstrap(num_revisions: usize, bytes: usize, timer: StatusTimer) {
719719
log_counter(
@@ -877,6 +877,76 @@ pub fn log_non_deleted_documents_per_search_index(count: u64, search_type: Searc
877877
);
878878
}
879879

880+
const COMPACTION_REASON_LABEL: &str = "compaction_reason";
881+
882+
pub enum CompactionReason {
883+
Unknown,
884+
SmallSegments,
885+
LargeSegments,
886+
Deletes,
887+
}
888+
889+
impl CompactionReason {
890+
fn metric_label(&self) -> StaticMetricLabel {
891+
let label = match self {
892+
CompactionReason::Unknown => "unknown",
893+
CompactionReason::SmallSegments => "small",
894+
CompactionReason::LargeSegments => "large",
895+
CompactionReason::Deletes => "deletes",
896+
};
897+
StaticMetricLabel::new(COMPACTION_REASON_LABEL, label)
898+
}
899+
}
900+
901+
register_convex_histogram!(
902+
COMPACTION_BUILD_ONE_SECONDS,
903+
"Time to run a single vector/text index compaction",
904+
&[STATUS_LABEL[0], COMPACTION_REASON_LABEL, SEARCH_TYPE_LABEL],
905+
);
906+
pub fn compaction_build_one_timer(search_type: SearchType) -> StatusTimer {
907+
let mut timer = StatusTimer::new(&COMPACTION_BUILD_ONE_SECONDS);
908+
timer.add_label(CompactionReason::Unknown.metric_label());
909+
timer.add_label(search_type.tag());
910+
timer
911+
}
912+
913+
pub fn finish_compaction_timer(mut timer: StatusTimer, reason: CompactionReason) {
914+
timer.replace_label(
915+
CompactionReason::Unknown.metric_label(),
916+
reason.metric_label(),
917+
);
918+
timer.finish();
919+
}
920+
921+
register_convex_histogram!(
922+
COMPACTION_COMPACTED_SEGMENTS_TOTAL,
923+
"Total number of compacted segments",
924+
&[SEARCH_TYPE_LABEL],
925+
);
926+
pub fn log_compaction_total_segments(total_segments: usize, search_type: SearchType) {
927+
log_distribution_with_labels(
928+
&COMPACTION_COMPACTED_SEGMENTS_TOTAL,
929+
total_segments as f64,
930+
vec![search_type.tag()],
931+
);
932+
}
933+
934+
register_convex_histogram!(
935+
COMPACTION_COMPACTED_SEGMENT_NUM_DOCUMENTS_TOTAL,
936+
"The number of documents in the newly generated compacted segment",
937+
&[SEARCH_TYPE_LABEL],
938+
);
939+
pub fn log_compaction_compacted_segment_num_documents_total(
940+
total_vectors: u64,
941+
search_type: SearchType,
942+
) {
943+
log_distribution_with_labels(
944+
&COMPACTION_COMPACTED_SEGMENT_NUM_DOCUMENTS_TOTAL,
945+
total_vectors as f64,
946+
vec![search_type.tag()],
947+
);
948+
}
949+
880950
pub mod search {
881951

882952
use metrics::{
@@ -941,7 +1011,7 @@ pub mod search {
9411011

9421012
register_convex_histogram!(
9431013
DATABASE_VECTOR_AND_SEARCH_BOOTSTRAP_SECONDS,
944-
"Time to bootstrap vector and search indexes",
1014+
"Time to bootstrap vector and text indexes",
9451015
&STATUS_LABEL
9461016
);
9471017
pub fn search_and_vector_bootstrap_timer() -> StatusTimer {
@@ -950,18 +1020,16 @@ pub fn search_and_vector_bootstrap_timer() -> StatusTimer {
9501020

9511021
register_convex_counter!(
9521022
SEARCH_AND_VECTOR_BOOTSTRAP_DOCUMENTS_SKIPPED_TOTAL,
953-
"Number of documents skipped during vector and search index bootstrap",
1023+
"Number of documents skipped during vector and text index bootstrap",
9541024
);
9551025
pub fn log_document_skipped() {
9561026
log_counter(&SEARCH_AND_VECTOR_BOOTSTRAP_DOCUMENTS_SKIPPED_TOTAL, 1);
9571027
}
9581028

9591029
pub mod vector {
9601030
use metrics::{
961-
log_distribution,
9621031
register_convex_histogram,
9631032
CancelableTimer,
964-
StaticMetricLabel,
9651033
StatusTimer,
9661034
STATUS_LABEL,
9671035
};
@@ -992,66 +1060,4 @@ pub mod vector {
9921060
pub fn vector_search_with_retries_timer() -> CancelableTimer {
9931061
CancelableTimer::new(&DATABASE_VECTOR_SEARCH_WITH_RETRIES_QUERY_SECONDS)
9941062
}
995-
996-
const COMPACTION_REASON_LABEL: &str = "compaction_reason";
997-
998-
pub enum CompactionReason {
999-
Unknown,
1000-
SmallSegments,
1001-
LargeSegments,
1002-
Deletes,
1003-
}
1004-
1005-
impl CompactionReason {
1006-
fn metric_label(&self) -> StaticMetricLabel {
1007-
let label = match self {
1008-
CompactionReason::Unknown => "unknown",
1009-
CompactionReason::SmallSegments => "small",
1010-
CompactionReason::LargeSegments => "large",
1011-
CompactionReason::Deletes => "deletes",
1012-
};
1013-
StaticMetricLabel::new(COMPACTION_REASON_LABEL, label)
1014-
}
1015-
}
1016-
1017-
register_convex_histogram!(
1018-
VECTOR_COMPACTION_BUILD_ONE_SECONDS,
1019-
"Time to run a single vector compaction",
1020-
&[STATUS_LABEL[0], COMPACTION_REASON_LABEL],
1021-
);
1022-
pub fn vector_compaction_build_one_timer() -> StatusTimer {
1023-
let mut timer = StatusTimer::new(&VECTOR_COMPACTION_BUILD_ONE_SECONDS);
1024-
timer.add_label(CompactionReason::Unknown.metric_label());
1025-
timer
1026-
}
1027-
1028-
pub fn finish_compaction_timer(mut timer: StatusTimer, reason: CompactionReason) {
1029-
timer.replace_label(
1030-
CompactionReason::Unknown.metric_label(),
1031-
reason.metric_label(),
1032-
);
1033-
timer.finish();
1034-
}
1035-
1036-
register_convex_histogram!(
1037-
VECTOR_COMPACTION_COMPACTED_SEGMENTS_TOTAL,
1038-
"Total number of compacted segments",
1039-
);
1040-
pub fn log_vector_compaction_total_segments(total_segments: usize) {
1041-
log_distribution(
1042-
&VECTOR_COMPACTION_COMPACTED_SEGMENTS_TOTAL,
1043-
total_segments as f64,
1044-
);
1045-
}
1046-
1047-
register_convex_histogram!(
1048-
VECTOR_COMPACTION_COMPACTED_SEGMENT_NUM_VECTORS_TOTAL,
1049-
"Size of the newly generated compacted segment",
1050-
);
1051-
pub fn log_vector_compaction_compacted_segment_num_vectors_total(total_vectors: u64) {
1052-
log_distribution(
1053-
&VECTOR_COMPACTION_COMPACTED_SEGMENT_NUM_VECTORS_TOTAL,
1054-
total_vectors as f64,
1055-
);
1056-
}
10571063
}

crates/database/src/vector_index_worker/compactor.rs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use common::{
1717
};
1818
use itertools::Itertools;
1919
use keybroker::Identity;
20-
use search::searcher::Searcher;
20+
use search::{
21+
metrics::SearchType,
22+
searcher::Searcher,
23+
};
2124
use storage::Storage;
2225
use value::ResolvedDocumentId;
2326

@@ -35,11 +38,11 @@ use crate::{
3538
},
3639
writer::SearchIndexMetadataWriter,
3740
},
38-
metrics::vector::{
41+
metrics::{
42+
compaction_build_one_timer,
3943
finish_compaction_timer,
40-
log_vector_compaction_compacted_segment_num_vectors_total,
41-
log_vector_compaction_total_segments,
42-
vector_compaction_build_one_timer,
44+
log_compaction_compacted_segment_num_documents_total,
45+
log_compaction_total_segments,
4346
CompactionReason,
4447
},
4548
vector_index_worker::vector_meta::{
@@ -113,13 +116,17 @@ impl<RT: Runtime, T: SearchIndexConfigParser> VectorIndexCompactor<RT, T> {
113116
Ok(())
114117
}
115118

119+
fn search_type() -> SearchType {
120+
<T::IndexType as SearchIndex>::search_type()
121+
}
122+
116123
pub(crate) async fn step(&self) -> anyhow::Result<(BTreeMap<TabletIndexName, u64>, Token)> {
117124
let mut metrics = BTreeMap::new();
118125

119126
let (to_build, token) = self.needs_compaction().await?;
120127
let num_to_build = to_build.len();
121128
if num_to_build > 0 {
122-
tracing::info!("{num_to_build} vector indexes to build");
129+
tracing::info!("{num_to_build} {:?} indexes to build", Self::search_type());
123130
}
124131

125132
for job in to_build {
@@ -129,7 +136,7 @@ impl<RT: Runtime, T: SearchIndexConfigParser> VectorIndexCompactor<RT, T> {
129136
}
130137

131138
if num_to_build > 0 {
132-
tracing::info!("built {num_to_build} vector indexes");
139+
tracing::info!("built {num_to_build} {:?} indexes", Self::search_type());
133140
}
134141

135142
Ok((metrics, token))
@@ -178,7 +185,10 @@ impl<RT: Runtime, T: SearchIndexConfigParser> VectorIndexCompactor<RT, T> {
178185
_ => false,
179186
};
180187
if needs_compaction {
181-
tracing::info!("Queueing vector index for compaction: {name:?}");
188+
tracing::info!(
189+
"Queueing {:?} index for compaction: {name:?}",
190+
Self::search_type()
191+
);
182192
let job = CompactionJob {
183193
index_id,
184194
index_name: name.clone(),
@@ -192,26 +202,36 @@ impl<RT: Runtime, T: SearchIndexConfigParser> VectorIndexCompactor<RT, T> {
192202
}
193203

194204
async fn build_one(&self, job: CompactionJob<T::IndexType>) -> anyhow::Result<u64> {
195-
let timer = vector_compaction_build_one_timer();
205+
let timer = compaction_build_one_timer(Self::search_type());
196206
let (segments, snapshot_ts) = match job.on_disk_state {
197207
SearchOnDiskState::Backfilling(BackfillState {
198208
segments,
199209
backfill_snapshot_ts,
200210
..
201211
}) => {
202-
let ts = backfill_snapshot_ts
203-
.context("Trying to compact backfilling segments with no backfill timestamp")?;
212+
let ts = backfill_snapshot_ts.with_context(|| {
213+
format!(
214+
"Trying to compact backfilling {:?} segments with no backfill timestamp",
215+
Self::search_type()
216+
)
217+
})?;
204218
(segments, ts)
205219
},
206220
SearchOnDiskState::Backfilled(snapshot)
207221
| SearchOnDiskState::SnapshottedAt(snapshot) => {
208222
let segments = match snapshot.data {
209223
SnapshotData::Unknown(_) => {
210-
anyhow::bail!("Trying to compact unknown vector snapshot");
224+
anyhow::bail!(
225+
"Trying to compact unknown {:?} snapshot",
226+
Self::search_type()
227+
);
211228
},
212229
SnapshotData::MultiSegment(segments) => segments,
213230
SnapshotData::SingleSegment(_) => {
214-
anyhow::bail!("Trying to compact a single segment index!");
231+
anyhow::bail!(
232+
"Trying to compact a single segment {:?} index!",
233+
Self::search_type()
234+
);
215235
},
216236
};
217237
(segments, snapshot.ts)
@@ -223,15 +243,15 @@ impl<RT: Runtime, T: SearchIndexConfigParser> VectorIndexCompactor<RT, T> {
223243
anyhow::ensure!(segments_to_compact.len() > 0);
224244

225245
let total_compacted_segments = segments_to_compact.len();
226-
log_vector_compaction_total_segments(total_compacted_segments);
246+
log_compaction_total_segments(total_compacted_segments, Self::search_type());
227247

228248
let new_segment = self
229249
.compact(&job.developer_config, &segments_to_compact)
230250
.await?;
231251
let stats = new_segment.statistics()?;
232252

233-
let total_vectors = stats.num_non_deleted_documents();
234-
log_vector_compaction_compacted_segment_num_vectors_total(total_vectors);
253+
let total_documents = stats.num_documents();
254+
log_compaction_compacted_segment_num_documents_total(total_documents, Self::search_type());
235255

236256
self.writer
237257
.commit_compaction(
@@ -268,11 +288,12 @@ impl<RT: Runtime, T: SearchIndexConfigParser> VectorIndexCompactor<RT, T> {
268288
) -> anyhow::Result<String> {
269289
let stats = segment.statistics()?;
270290
Ok(format!(
271-
"(id: {}, size: {}, vectors: {}, deletes: {})",
291+
"(id: {}, size: {}, documents : {}, deletes: {}, type: {:?})",
272292
segment.id(),
273293
segment.total_size_bytes(developer_config)?,
274294
stats.num_documents(),
275295
stats.num_deleted_documents(),
296+
Self::search_type(),
276297
))
277298
}
278299

@@ -366,7 +387,7 @@ impl<RT: Runtime, T: SearchIndexConfigParser> VectorIndexCompactor<RT, T> {
366387
}
367388

368389
// Finally check to see if any individual segment has a large number of deleted
369-
// vectors and if so compact just that segment.
390+
// documents and if so compact just that segment.
370391
let compact_deletes = large_segments
371392
.into_iter()
372393
.try_find(|(segment, segment_size_bytes)| {
@@ -425,7 +446,7 @@ impl<RT: Runtime, T: SearchIndexConfigParser> VectorIndexCompactor<RT, T> {
425446
.try_fold(0u64, |sum, current| {
426447
current.and_then(|current| {
427448
sum.checked_add(current)
428-
.context("Summing vector sizes overflowed")
449+
.context("Summing document sizes overflowed")
429450
})
430451
})?;
431452
anyhow::ensure!(
@@ -470,7 +491,6 @@ pub(crate) struct CompactionConfig {
470491
// only 2 of those can actually be compacted due to the max size restriction, then we don't
471492
// want to compact yet.
472493
pub(crate) min_compaction_segments: u64,
473-
// 1.1 million vectors at the largest size
474494
pub(crate) max_segment_size_bytes: u64,
475495
}
476496

crates/search/src/metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ pub fn upload_archive_timer(search_file_type: SearchFileType) -> StatusTimer {
445445
timer
446446
}
447447

448-
#[derive(Clone, Copy)]
448+
#[derive(Clone, Copy, Debug)]
449449
pub enum SearchType {
450450
Vector,
451451
Text,

0 commit comments

Comments
 (0)