Skip to content

Commit 19018a9

Browse files
emmaling27Convex, Inc.
authored and
Convex, Inc.
committed
Use TextFlusher2 in tests (#27337)
This PR converts tests to use `TextFlusher2` instead of `TextFlusher`, which enables us to remove the single-segment build path. GitOrigin-RevId: be6a9788d62a3f289affa904bed5c3c1d1fcb8e2
1 parent d36d8c3 commit 19018a9

File tree

12 files changed

+89
-62
lines changed

12 files changed

+89
-62
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/database/src/index_workers/writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use crate::{
7676
/// conflicting writes that may have happened due to concurrent modifications in
7777
/// the flusher and compactor.
7878
#[derive(Clone)]
79-
pub(crate) struct SearchIndexMetadataWriter<RT: Runtime, T: SearchIndex> {
79+
pub struct SearchIndexMetadataWriter<RT: Runtime, T: SearchIndex> {
8080
inner: Arc<Mutex<Inner<RT, T>>>,
8181
}
8282

crates/database/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mod table_iteration;
5151
pub mod test_helpers;
5252
#[cfg(test)]
5353
pub mod tests;
54-
mod text_index_worker;
54+
pub mod text_index_worker;
5555

5656
pub use execution_size::FunctionExecutionSize;
5757
pub use index_worker::IndexWorker;

crates/database/src/search_and_vector_bootstrap.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,6 @@ mod tests {
580580
types::{
581581
IndexId,
582582
IndexName,
583-
TabletIndexName,
584583
WriteTimestamp,
585584
},
586585
};
@@ -614,13 +613,13 @@ mod tests {
614613
DbFixtures,
615614
DbFixturesArgs,
616615
},
616+
text_index_worker::flusher2::new_text_flusher_for_tests,
617617
vector_index_worker::flusher::backfill_vector_indexes,
618618
Database,
619619
IndexModel,
620620
SystemMetadataModel,
621621
TableModel,
622622
TestFacingModel,
623-
TextIndexFlusher,
624623
Transaction,
625624
UserFacingModel,
626625
};
@@ -961,8 +960,7 @@ mod tests {
961960
async fn test_load_snapshot_without_fast_forward(rt: TestRuntime) -> anyhow::Result<()> {
962961
let db_fixtures = DbFixtures::new(&rt).await?;
963962
let db = &db_fixtures.db;
964-
let (index_id, _) =
965-
create_new_search_index(&rt, db, db_fixtures.search_storage.clone()).await?;
963+
let (index_id, _) = create_new_search_index(&rt, &db_fixtures).await?;
966964

967965
let mut tx = db.begin_system().await.unwrap();
968966
add_document(
@@ -991,8 +989,7 @@ mod tests {
991989
async fn test_load_snapshot_with_fast_forward(rt: TestRuntime) -> anyhow::Result<()> {
992990
let db_fixtures = DbFixtures::new(&rt).await?;
993991
let db = &db_fixtures.db;
994-
let (index_id, _) =
995-
create_new_search_index(&rt, db, db_fixtures.search_storage.clone()).await?;
992+
let (index_id, _) = create_new_search_index(&rt, &db_fixtures).await?;
996993

997994
rt.advance_time(Duration::from_secs(10)).await;
998995

@@ -1041,8 +1038,7 @@ mod tests {
10411038
) -> anyhow::Result<()> {
10421039
let db_fixtures = DbFixtures::new(&rt).await?;
10431040
let db = &db_fixtures.db;
1044-
let (index_id, index_doc) =
1045-
create_new_search_index(&rt, db, db_fixtures.search_storage.clone()).await?;
1041+
let (index_id, index_doc) = create_new_search_index(&rt, &db_fixtures).await?;
10461042

10471043
// We shouldn't ever fast forward across an update in real life, but doing so
10481044
// and verifying we don't read the document is a simple way to verify we
@@ -1083,14 +1079,10 @@ mod tests {
10831079

10841080
#[convex_macro::test_runtime]
10851081
async fn test_load_fast_forward_ts(rt: TestRuntime) -> anyhow::Result<()> {
1086-
let DbFixtures {
1087-
tp,
1088-
db,
1089-
search_storage,
1090-
..
1091-
} = DbFixtures::new(&rt).await?;
1092-
let (index_id, index_doc) =
1093-
create_new_search_index(&rt, &db, search_storage.clone()).await?;
1082+
let db_fixtures = DbFixtures::new(&rt).await?;
1083+
let (index_id, index_doc) = create_new_search_index(&rt, &db_fixtures).await?;
1084+
let db = db_fixtures.db;
1085+
let tp = db_fixtures.tp;
10941086
let mut tx = db.begin_system().await?;
10951087
let mut model = IndexWorkerMetadataModel::new(&mut tx);
10961088
let (metadata_id, mut metadata) = model
@@ -1128,7 +1120,7 @@ mod tests {
11281120
let db = &db_fixtures.db;
11291121
let search_storage = db_fixtures.search_storage.clone();
11301122
// Add a search index at t0 to make bootstrapping start at t0
1131-
create_new_search_index(&rt, db, search_storage.clone()).await?;
1123+
create_new_search_index(&rt, &db_fixtures).await?;
11321124
// Add a vector index to a table with a vector already in it
11331125
add_vector_by_table(db, table(), [1f32, 2f32]).await?;
11341126
add_and_enable_vector_index(&rt, db, db_fixtures.tp.reader(), search_storage).await?;
@@ -1154,7 +1146,7 @@ mod tests {
11541146
)
11551147
.await?;
11561148
db.commit(tx).await?;
1157-
create_new_search_index(&rt, db, search_storage).await?;
1149+
create_new_search_index(&rt, &db_fixtures).await?;
11581150
// Bootstrap
11591151
reopen_db(&rt, &db_fixtures).await?;
11601152
Ok(())
@@ -1173,9 +1165,15 @@ mod tests {
11731165

11741166
async fn create_new_search_index<RT: Runtime>(
11751167
rt: &RT,
1176-
db: &Database<RT>,
1177-
search_storage: Arc<dyn Storage>,
1168+
db_fixtures: &DbFixtures<RT>,
11781169
) -> anyhow::Result<(IndexId, ParsedDocument<TabletIndexMetadata>)> {
1170+
let DbFixtures {
1171+
tp,
1172+
db,
1173+
search_storage,
1174+
build_index_args,
1175+
..
1176+
} = db_fixtures;
11791177
let table_name: TableName = "test".parse()?;
11801178
let mut tx = db.begin_system().await?;
11811179
TableModel::new(&mut tx)
@@ -1191,21 +1189,14 @@ mod tests {
11911189
.await?;
11921190
db.commit(tx).await?;
11931191

1194-
let snapshot = db.latest_snapshot()?;
1195-
let table_id = snapshot
1196-
.table_mapping()
1197-
.namespace(TableNamespace::test_user())
1198-
.id(&"test".parse()?)?
1199-
.tablet_id;
1200-
let index_name = TabletIndexName::new(table_id, "by_text".parse()?)?;
1201-
TextIndexFlusher::build_index_in_test(
1202-
index_name.clone(),
1203-
"test".parse()?,
1192+
let mut flusher = new_text_flusher_for_tests(
12041193
rt.clone(),
12051194
db.clone(),
1195+
tp.reader(),
12061196
search_storage.clone(),
1207-
)
1208-
.await?;
1197+
build_index_args.segment_term_metadata_fetcher.clone(),
1198+
);
1199+
flusher.step().await?;
12091200

12101201
let index_name = IndexName::new(table_name, "by_text".parse()?)?;
12111202
let mut tx = db.begin_system().await?;

crates/database/src/test_helpers/db_fixtures.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use common::{
1111
};
1212
use events::usage::NoOpUsageEventLogger;
1313
use search::{
14-
searcher::SearcherStub,
14+
searcher::{
15+
InProcessSearcher,
16+
SearcherStub,
17+
},
1518
Searcher,
1619
};
1720
use storage::{
@@ -20,6 +23,7 @@ use storage::{
2023
};
2124

2225
use crate::{
26+
text_index_worker::BuildTextIndexArgs,
2327
Database,
2428
ShutdownSignal,
2529
Transaction,
@@ -31,6 +35,7 @@ pub struct DbFixtures<RT: Runtime> {
3135
pub db: Database<RT>,
3236
pub searcher: Arc<dyn Searcher>,
3337
pub search_storage: Arc<dyn Storage>,
38+
pub build_index_args: BuildTextIndexArgs,
3439
}
3540

3641
pub struct DbFixturesArgs {
@@ -89,11 +94,16 @@ impl<RT: Runtime> DbFixtures<RT> {
8994
.into_join_future()
9095
.await?;
9196
}
97+
let build_index_args = BuildTextIndexArgs {
98+
search_storage: search_storage.clone(),
99+
segment_term_metadata_fetcher: Arc::new(InProcessSearcher::new(rt.clone()).await?),
100+
};
92101
Ok(Self {
93102
tp,
94103
db,
95104
searcher,
96105
search_storage,
106+
build_index_args,
97107
})
98108
}
99109
}

crates/database/src/tests/randomized_search_tests.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl Scenario {
157157
search_storage,
158158
searcher,
159159
tp,
160+
build_index_args,
160161
} = DbFixtures::new_with_args(
161162
&rt,
162163
DbFixturesArgs {
@@ -182,11 +183,6 @@ impl Scenario {
182183
.await?;
183184
database.commit(tx).await?;
184185

185-
let build_index_args = BuildTextIndexArgs {
186-
search_storage: search_storage.clone(),
187-
segment_term_metadata_fetcher: Arc::new(InProcessSearcher::new(rt.clone()).await?),
188-
};
189-
190186
let mut self_ = Self {
191187
rt,
192188
database,
@@ -210,6 +206,7 @@ impl Scenario {
210206
searcher,
211207
search_storage,
212208
tp,
209+
build_index_args,
213210
} = DbFixtures::new_with_args(
214211
&self.rt,
215212
DbFixturesArgs {
@@ -226,6 +223,7 @@ impl Scenario {
226223
self.searcher = searcher;
227224
self.search_storage = search_storage;
228225
self.tp = tp;
226+
self.build_index_args = build_index_args;
229227
Ok(())
230228
}
231229

crates/database/src/text_index_worker/flusher.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,18 +354,6 @@ impl<RT: Runtime> TextIndexFlusher<RT> {
354354
worker.build_one(job).await?;
355355
Ok(())
356356
}
357-
358-
/// Backfills all search indexes that are in a "backfilling" state.
359-
#[cfg(any(test, feature = "testing"))]
360-
pub async fn backfill_all_in_test(
361-
runtime: RT,
362-
database: Database<RT>,
363-
storage: Arc<dyn Storage>,
364-
) -> anyhow::Result<()> {
365-
let mut worker = TextIndexFlusher::new(runtime, database, storage);
366-
worker.step().await?;
367-
Ok(())
368-
}
369357
}
370358

371359
#[derive(Clone)]

crates/database/src/text_index_worker/flusher2.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,36 @@ impl<RT: Runtime> FlusherBuilder<RT> {
137137

138138
pub type TextIndexFlusher2<RT> = SearchFlusher<RT, TextSearchIndex>;
139139

140+
#[allow(unused)]
141+
#[cfg(any(test, feature = "testing"))]
142+
pub fn new_text_flusher_for_tests<RT: Runtime>(
143+
runtime: RT,
144+
database: Database<RT>,
145+
reader: Arc<dyn PersistenceReader>,
146+
storage: Arc<dyn Storage>,
147+
segment_metadata_fetcher: Arc<dyn SegmentTermMetadataFetcher>,
148+
) -> TextIndexFlusher2<RT> {
149+
let writer = SearchIndexMetadataWriter::new(
150+
runtime.clone(),
151+
database.clone(),
152+
reader.clone(),
153+
storage.clone(),
154+
BuildTextIndexArgs {
155+
search_storage: storage.clone(),
156+
segment_term_metadata_fetcher: segment_metadata_fetcher.clone(),
157+
},
158+
);
159+
FlusherBuilder::new(
160+
runtime,
161+
database,
162+
reader,
163+
storage,
164+
segment_metadata_fetcher,
165+
writer,
166+
)
167+
.build()
168+
}
169+
140170
pub(crate) fn new_text_flusher<RT: Runtime>(
141171
runtime: RT,
142172
database: Database<RT>,

crates/isolate/src/test_helpers.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ use database::{
5656
DbFixtures,
5757
DbFixturesArgs,
5858
},
59+
text_index_worker::flusher2::backfill_text_indexes,
5960
vector_index_worker::flusher::backfill_vector_indexes,
6061
BootstrapComponentsModel,
6162
Database,
6263
FollowerRetentionManager,
6364
IndexModel,
6465
IndexWorker,
65-
TextIndexFlusher,
6666
Transaction,
6767
};
6868
use file_storage::TransactionalFileStorage;
@@ -808,11 +808,15 @@ impl<RT: Runtime, P: Persistence + Clone> UdfTest<RT, P> {
808808
Ok(())
809809
}
810810

811-
pub async fn backfill_search_indexes(&self) -> anyhow::Result<()> {
812-
TextIndexFlusher::backfill_all_in_test(
811+
pub async fn backfill_text_indexes(&self) -> anyhow::Result<()> {
812+
let segment_term_metadata_fetcher =
813+
Arc::new(InProcessSearcher::new(self.rt.clone()).await?);
814+
backfill_text_indexes(
813815
self.rt.clone(),
814816
self.database.clone(),
817+
self.persistence.reader(),
815818
self.search_storage.clone(),
819+
segment_term_metadata_fetcher,
816820
)
817821
.await?;
818822
self.enable_backfilled_indexes().await

crates/isolate/src/tests/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async fn add_and_backfill_search_index(
4646
t: &UdfTest<TestRuntime, TestPersistence>,
4747
) -> anyhow::Result<()> {
4848
add_search_index(t).await?;
49-
t.backfill_search_indexes().await?;
49+
t.backfill_text_indexes().await?;
5050

5151
Ok(())
5252
}

crates/model/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ proptest-derive = { workspace = true, optional = true }
3535
rand = { workspace = true }
3636
runtime = { path = "../runtime" }
3737
saffron = { workspace = true }
38+
search = { path = "../search" }
3839
semver = { workspace = true }
3940
serde = { workspace = true }
4041
serde_bytes = { workspace = true }
@@ -57,6 +58,7 @@ metrics = { path = "../metrics", features = ["testing"] }
5758
proptest = { workspace = true }
5859
proptest-derive = { workspace = true }
5960
runtime = { path = "../runtime", features = ["testing"] }
61+
search = { path = "../search", features = ["testing"] }
6062
storage = { path = "../storage", features = ["testing"] }
6163
sync_types = { package = "convex_sync_types", path = "../convex/sync_types", features = [
6264
"testing",
@@ -73,6 +75,7 @@ testing = [
7375
"proptest",
7476
"proptest-derive",
7577
"runtime/testing",
78+
"search/testing",
7679
"storage/testing",
7780
"value/testing",
7881
]

0 commit comments

Comments
 (0)